Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what order does 'flow_from_directory' function in keras takes the samples?

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.

like image 371
vivek Avatar asked Jul 23 '16 06:07

vivek


People also ask

How does flow_from_directory work?

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.

What is flow_from_directory in Python?

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.

What is Batch_size in flow_from_directory?

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.

How does Keras ImageDataGenerator work?

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.


1 Answers

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.

like image 196
Austin Avatar answered Sep 29 '22 20:09

Austin