keras model.predict randomises the output order?

Viewed 1310

I am new to keras and Tensorflow. I have trained a model in Keras using Tensorflow as backend, for reducing a specific noise of images. When I use this model to predict a set of test images(noisy, shape=[batchsize_1, width_1, height_1]), I have got correct predicted images(noise removed, shape=[batchsize_1, width_1, height_1]), but the order of the predicted images are randomised. As the order of the predicted images are important to my next step, how could I keep the order of predicted image identical to test images?

Here are my code for loading the model and predict the test data:

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("model.h5")
adam=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
loaded_model.compile(loss=losses.mean_absolute_error, optimizer=adam)
predict_images= loaded_model.predict(test_images, batch_size=16)

I am sure that the test_images are in order in the dimension of batchsize, but the batchsize order of predict_images is randomised. Does the order of predict_image also depend on how I trained the model? How could I remain the order of predict_image unchanged?

Any comments and help would be much appreciated. Thanks a lot!

1 Answers

If your dataset is a generator, you need to pass shuffle=False (by default it is True), or your predictions will be shuffled. Example

imageDataGenerator.flow_from_dataframe(
dataframe, shuffle=False)
Related