如何获取 Keras 中 ImageDataGenerator 的 .flow_from_directory 函数扫描的类的名称?

Nee*_*mar 3 python machine-learning computer-vision keras

我想制作一个用户友好的 GUI 图像分类器,用户只需要指向数据集的目录即可训练模型,然后他们可以向程序提供任何图像,它将显示概率和标签图像中的对象。但是如何在 Keras 中获取 ImageDataGenerator 的 .flow_from_directory 函数扫描的类的名称?

Man*_*han 5

从文档中,“可以通过属性获得包含从类名到类索引的映射的字典class_indices

https://keras.io/preprocessing/image/#flow_from_directory

在下面的示例中,train_data_dir包含两个子文件夹,catdog

train_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    color_mode='grayscale',
    shuffle = True,
    batch_size=batch_size,
    class_mode='binary')

print(train_generator.class_indices)
{'cat': 0, 'dog': 1}
Run Code Online (Sandbox Code Playgroud)

`