I'm trying to classify images into 10 classes. To get probabilities for images, I'm using model.predict_generator() function in keras. This returns only prediction values and not the corresponding sample ID(In this case image file name).
test_datagen = ImageDataGenerator(rescale=1./255)
validation_generator = test_datagen.flow_from_directory(
'/path/',
target_size=(256, 256),
batch_size=32,`
class_mode='categorical')
predictions = model.predict_generator(validation_generator, val_samples=10000)
In what order does the '.flow_from_directory' read the samples?
(OR)
How do I find the corresponding image name/id of the predictions?
Click here for the code.
flow_from_directory Method This method will identify classes automatically from the folder name. For this method, arguments to be used are: directory value : The path to parent directory containing sub-directories(class/label) with images. classes value : Name of the class/classes for which images should be loaded.
flow_from_directory(directory) , Description:Takes the path to a directory, and generates batches of augmented/normalized data. Yields batches indefinitely, in an infinite loop. With shuffle = False , it takes the same batch indefinitely.
The target_size is the size of your input images, every image will be resized to this size. color_mode: if the image is either black and white or grayscale set “grayscale” or if the image has three color channels, set “rgb”. batch_size: No. of images to be yielded from the generator per batch.
Keras ImageDataGenerator is used for getting the input of the original data and further, it makes the transformation of this data on a random basis and gives the output resultant containing only the data that is newly transformed. It does not add the data.
If you set shuffle=False
it looks like images are returned in whatever order os.listdir
returns them in.
I just tested this by using my generator to write a few sample images to a file with shuffle=False
test_datagen = ImageDataGenerator(samplewise_center=False)
test_generator = test_datagen.flow_from_directory(
training_config['test_data_dir'],
target_size=(256, 256),
batch_size=5,
class_mode=None,
shuffle=False,
save_to_dir=os.path.join('samples'))
you can also print the generator's filenames.. which ( I looked at the code) just performs os.listdir
print test_generator.filenames
So, your predict_generator
result at index 0 should be for the image at os.listdir
index 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With