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