Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get class info of Keras' ImageDataGenerator images besides parsing filename

I load hundres of images with ImageGenerator and its flow_from_dirctory-function from lets say two directories (two classes) in the validation directory (and test directory) with names "cats" and "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)

After using some Keras model generation and fitting I want to debug sample images: I want to take images from the validation_generator and run the model on it. But I have to know in which directory the image was in the first place or with the class it was assigned to.

For plotting I use:

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()

I could only find the possibility to parse validation_generator.filenames[i] and take the directory of it. Is there any other, more elegant, way for that?

like image 205
tardis Avatar asked Sep 02 '25 04:09

tardis


1 Answers

validation_generator.class_indices  # ==> return: {'ants': 0, 'bees': 1}
like image 107
Heecheol Cho Avatar answered Sep 04 '25 16:09

Heecheol Cho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!