3

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?

tardis
  • 1,280
  • 3
  • 23
  • 48

3 Answers3

6
validation_generator.class_indices  # ==> return: {'ants': 0, 'bees': 1}
Heecheol Cho
  • 61
  • 1
  • 3
0

The class_indices attribute is a dictionary that contains the mapping from class names to class indices, so this is indeed the same for each data example that the generator yields. You could access the label for image_x from y as follows:

label_x = y[i]
sdcbr
  • 7,021
  • 3
  • 27
  • 44
  • I also thought that and tried it but its output is a very big numpy array of arrays (?) like [[[1.] [1.] [1.] ... [1.] [1.] [1.]] – tardis Aug 09 '18 at 13:50
  • What is the shape of this array? – sdcbr Aug 09 '18 at 13:51
  • It is 128x128 px as my image. In fact both `x,y = validation_generator.next()` are exactly the same image. – tardis Aug 10 '18 at 06:06
  • So your generator is not actually yielding (image, label) pairs but rather (image, image) pairs? – sdcbr Aug 10 '18 at 06:07
  • It seems so, you can see the generator in my first posting. – tardis Aug 10 '18 at 06:13
  • Aha, your are training an autoencoder. I missed that point. In that case, I guess you will have to stick with using validation_generator.filenames[i] – sdcbr Aug 10 '18 at 06:16
0

There does not seem to be an elegant way for an autoencoder so I post my quirky way:

import matplotlib.pyplot as plt
import os
n = 7
x,y = validation_generator.next()
for i in range(0,n):
    plt.figure(figsize=(2, 2))
    image_x = x[i,:,:,0]
    image_label = os.path.dirname(validation_generator.filenames[i]) # only OK if shuffle=false
    print(image_label)
    plt.imshow(image_x)
    plt.show()
tardis
  • 1,280
  • 3
  • 23
  • 48