Keras: how to predict classes in order?

I am trying to predict image classes in keras (binary classification). The exact accuracy of the model, but it seems that ImageDataGenerator mixing input images, so I could not match the predicted class with the original images.

 datagen = ImageDataGenerator(rescale=1./255) generator = datagen.flow_from_directory( pred_data_dir, target_size=(img_width, img_height), batch_size=32, class_mode=None, shuffle=False, save_to_dir='images/aug'.format(feature)) print model.predict_generator(generator, nb_input) 

For example, if I have a1.jpg , a2.jpg , ..., a9.jpg in pred_data_dir , I expect to get an array like

 [class for a1.jpg, class for a2.jpg, ... class for a9.jpg] 

from model.predict_generator() , but actually I got something like

 [class for a3.jpg, class for a8.jpg, ... class for a2.jpg] 

How can i solve the problem?

+6
source share
2 answers

See the source code for flow_from_directory . In my case, I had to rename all the images. They were named 1.jpg .. 1000.jpg, but to be in order, they had to be named 0001.jpg .. 1000.jpg. Sorting is important here.

flow_from_directory uses sorted(os.listdir(directory)) , so sorting is not always intuitive.

+5
source

The flow_from_directory() method returns a DirectoryIterator object with member filenames , which lists all the files. Since this member is used for the subsequent generation and iteration of the batch, you should be able to use it to match your file names with forecasts.

In your example, generator.filenames should provide you with a parallel list, for example ['a3.jpg', 'a8.jpg', ..., 'a2.jpg'] .

0
source

All Articles