I tested image classification on two images (01.jpg and 02.jpg) via predict_generator. The predicition is correct and very good. However, the image order cannot be maintained and do not correspond to the order generated via the image_data_generator.
Example:
Therefore, the prediction of a large image batch is not possible since I do not know which prediction refers to the individual image. In addition, the order changes in each repetition of the predict_generator step.
A similar bug is reported here, but no solution is given: https://github.com/fchollet/keras/issues/5048)
Is there any solution? Otherwise the results given via predict_generator are more or less useless for a large batch of images to be classified.
I don't see a way we can force this to behave differently from the R side (although if there is a way to do it from the Python side we could certainly facilitate users getting it right from the R side).
I think for the time being you might just need to use straight predict.
Many thanks for your answer. I guess I found the mistake I made:
for flow_images_from_directory I need to set shuffle to FALSE. Then the order of predicition via predict_generator corresponds to the image order. At least for my test case using the two images 01.jpg and 02.jpg. I checked it via a loop of 1000 repetitions for predict_generator and the order is fix and fits image order.
Important when using a larger number of test images:
Easy batch renaming via console: https://stackoverflow.com/questions/3211595/renaming-files-in-a-folder-to-sequential-numbers
Excellent! So happy you got to the bottom of it.
In case you are still curious, the way to manually pull from a generator is to call reticulate::iter_next(test_generator)
setting shuffle=false to evaluate_generator and predict_generator fixed the issue for me
Most helpful comment
Many thanks for your answer. I guess I found the mistake I made:
for flow_images_from_directory I need to set shuffle to FALSE. Then the order of predicition via predict_generator corresponds to the image order. At least for my test case using the two images 01.jpg and 02.jpg. I checked it via a loop of 1000 repetitions for predict_generator and the order is fix and fits image order.
Important when using a larger number of test images:
Easy batch renaming via console: https://stackoverflow.com/questions/3211595/renaming-files-in-a-folder-to-sequential-numbers