Keras: predict_generator cannot maintain image order

Created on 9 Oct 2017  路  4Comments  路  Source: rstudio/keras

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:

image_generator

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.

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:

  1. Image names need to be 01, 02, 03, ..., 99 or 001, 002, 003, ..., 999 or 0001, 0002, 0003, ..., 9999 and so on.
  2. And batch size in flow_images_from_directory needs to correspond to the image number of the test images.

Easy batch renaming via console: https://stackoverflow.com/questions/3211595/renaming-files-in-a-folder-to-sequential-numbers

All 4 comments

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:

  1. Image names need to be 01, 02, 03, ..., 99 or 001, 002, 003, ..., 999 or 0001, 0002, 0003, ..., 9999 and so on.
  2. And batch size in flow_images_from_directory needs to correspond to the image number of the 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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aliarsalankazmi picture aliarsalankazmi  路  4Comments

masterlord99 picture masterlord99  路  6Comments

vincenhe picture vincenhe  路  7Comments

gokceneraslan picture gokceneraslan  路  6Comments

LarsHill picture LarsHill  路  6Comments