Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

predict_generator cannot maintain image order #149

Closed
saanasum opened this issue Oct 9, 2017 · 4 comments
Closed

predict_generator cannot maintain image order #149

saanasum opened this issue Oct 9, 2017 · 4 comments

Comments

@saanasum
Copy link

saanasum commented Oct 9, 2017

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: keras-team/keras#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.

@jjallaire
Copy link
Member

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.

@saanasum
Copy link
Author

saanasum commented Oct 10, 2017

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

@jjallaire
Copy link
Member

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)

@mizia42
Copy link

mizia42 commented Mar 23, 2020

setting shuffle=false to evaluate_generator and predict_generator fixed the issue for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants