【问题标题】:Why in created dataset in tensorflow Labels showed with shape and datatype information?为什么在 tensorflow 标签中创建的数据集中显示形状和数据类型信息?
【发布时间】:2021-02-27 15:18:11
【问题描述】:

我是 Tensorflow 的新手: 我想创建自己的数据集:

labels = ['N', 'N', 'N', 'A', 'A']

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir , labels = labels,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
  for i in range(2):
    ax = plt.subplot(2, 2, i + 1)
    plt.imshow(images[i].numpy().astype('uint8'))
    plt.title(labels[i])
    plt.axis('off')

为什么当我绘制图像时,标签以这种方式打印,而不仅仅是标签名称?

【问题讨论】:

  • 为什么当我绘制图像时标签以这种方式打印,而不仅仅是标签名称? - 什么?

标签: python tensorflow deep-learning dataset label


【解决方案1】:

当你迭代一个数据集时,你会得到 tf.Tensor 对象,当你将它直接传递给 plt.title 时,它会被转换为产生该结果的字符串。如果你想要张量的内容,你可以使用.numpy(),就像处理图像一样:

plt.title(labels[i].numpy())

编辑:b 前缀是因为 TensorFlow 字符串实际上对应于 Python 字节数组(当表示为字符串时,获取前缀)。如果你想得到一个实际的str,所以你看不到,你可以使用.decode()

plt.title(labels[i].numpy().decode())

【讨论】:

  • 几乎可以工作但仍然打印“b'N'”,我怎样才能删除那个“b”
  • @MaryamKia 对,忘记了,我编辑了答案。
猜你喜欢
  • 1970-01-01
  • 2021-06-08
  • 2017-11-08
  • 2017-07-22
  • 2021-12-11
  • 1970-01-01
  • 2018-05-02
  • 1970-01-01
  • 2019-04-20
相关资源
最近更新 更多