除了解析文件名之外,还获取 Keras 的 ImageDataGenerator 图像的类信息

tar*_*dis 3 python python-3.x keras keras-2

我加载了数百个图像ImageGenerator及其flow_from_dirctory-function来自验证目录(和测试目录)中的两个目录(两个类),名称为“cats”和“dogs”:

validation_generator = test_datagen.flow_from_directory(
        root_dir + '/validate',
        target_size=(img_x, img_y),
        batch_size=batch_size,
        color_mode='grayscale',
        class_mode='input',  # necessarry for autoencoder
        shuffle=False, # must be false otherwise filenames are wrong
        seed = seed)
Run Code Online (Sandbox Code Playgroud)

使用一些 Keras 模型生成和拟合后,我想调试示例图像:我想从中获取图像validation_generator并在其上运行模型。但我必须知道图像首先位于哪个目录或它被分配到的类。

对于绘图我使用:

import matplotlib.pyplot as plt
n = 7
x,y = validation_generator.next()
for i in range(0,n):
    image_x = x[i,:,:,0]
    #print(validation_generator.class_indices) # always shows the same
    print(validation_generator.filenames[i]) # only OK if shuffle=false
    plt.imshow(image_x)
    plt.show()
Run Code Online (Sandbox Code Playgroud)

validation_generator.filenames[i]我只能找到解析并获取它的目录的可能性。还有其他更优雅的方法吗?

小智 6

validation_generator.class_indices  # ==> return: {'ants': 0, 'bees': 1}
Run Code Online (Sandbox Code Playgroud)