【问题标题】:How to get the class names in a tensorflow dataset?如何获取张量流数据集中的类名?
【发布时间】:2021-12-10 23:17:12
【问题描述】:

我正在尝试了解如何使用 tensorflow 数据集 tfds。

数据集就是这种目录

-dataset
  -train
    -class_name1
      -files...
    -class_name2
      -files...
   -val
    -class_name1
      -files...
    -class_name2
      -files...
  -test
    -class_name1
      -files...
    -class_name2
      -files...

这里有一些代码:

import tensorflow_datasets as tfds
builder = tfds.ImageFolder('/content/dataset')
train_ds, val_ds, test_ds = builder.as_dataset(split=['train', 'val', 'test'], shuffle_files=True, as_supervised=True)
print(builder.info)
Output:
tfds.core.DatasetInfo(
    name='image_folder',
    version=1.0.0,
    description='Generic image classification dataset.',
    homepage='https://www.tensorflow.org/datasets/catalog/image_folder',
    features=FeaturesDict({
        'image': Image(shape=(None, None, 3), dtype=tf.uint8),
        'image/filename': Text(shape=(), dtype=tf.string),
        'label': ClassLabel(shape=(), dtype=tf.int64, num_classes=243),
    }),
    total_num_examples=73090,
    splits={
        'test': 5849,
        'train': 58469,
        'val': 8772,
    },
    supervised_keys=('image', 'label'),
    citation="""""",
    redistribution_info=,
)

当我在绘图、做分类报告、混淆矩阵等时 我希望能够使用 class_names 而不是整数标签。

是否有一些简单的命令可以让我访问 class_names? (有 243 个类,不是 2 个)

【问题讨论】:

    标签: python tensorflow machine-learning deep-learning tensorflow-datasets


    【解决方案1】:

    您可以访问train_ds, val_ds, test_ds,他们拥有class_names,您可以使用标签作为索引来访问它,如fpierrem 所述

    这是一个情节的例子:

    import matplotlib.pyplot as plt
    
    plt.figure(figsize=(10, 10))
    for images, labels in train_ds.take(1):
        for i in range(9):
            ax = plt.subplot(3, 3, i + 1)
            plt.imshow(images[i].numpy().astype("uint8"))
            plt.title(train_ds.class_names[labels[i]])
            plt.axis("off")
        
    plt.show()
    

    看。 https://www.tensorflow.org/tutorials/keras/classification#import_the_fashion_mnist_datasethttps://www.tensorflow.org/tutorials/load_data/images#visualize_the_data

    【讨论】:

      【解决方案2】:

      您可以使用 train_ds.class_names。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-02
        • 2018-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-04
        相关资源
        最近更新 更多