如何在多课程培训中获得Keras的标签ID?

Fáb*_*rez 13 machine-learning deep-learning conv-neural-network keras

我正在使用flow_from_directory具有以下结构的文件夹来获取训练集:

train
  class1
  class2
  class3
  ...
Run Code Online (Sandbox Code Playgroud)

生成器按如下方式调用:

train_generator = train_datagen.flow_from_directory( 
        train_data_dir,                              
        target_size=(img_height, img_width),         
        batch_size=32,                               
        class_mode='categorical')  
Run Code Online (Sandbox Code Playgroud)

我没有设置参数classes,但我希望按字母顺序获取标签.

classes:类子目录的可选列表(例如['dogs', 'cats']).默认值:无.如果未提供,将自动推断类列表(并且将映射到标签索引的类的顺序将是字母数字).

但是,当我对训练图像进行分类(用于检查返回的标签)时,我没有得到任何特定的顺序.训练进展顺利(准确度约为85%),并且在对来自同一班级的图像进行分类时,输出标签保持一致.

如何推断生成的标签数量flow_from_directory并将它们映射到类?

ema*_*ele 17

您可以看到哪个类对应于查看变量的整数 ImageDataGenerator.class_indices

以下是如何使用它的示例

    def build(source=None):
        datagen = ImageDataGenerator(rescale=1. / 255)
        data_generator = datagen.flow_from_directory(
        source,  # this is the target directory
        target_size=(150, 150),  # all images will be resized to 150x150
        batch_size=11,
        class_mode='sparse')
        class_dictionary = data_generator.class_indices
    return data_generator, class_dictionary
Run Code Online (Sandbox Code Playgroud)

  • 我收到错误消息:“ ImageDataGenerator”对象没有属性“ class_indices” (2认同)