Proper way to make prediction with Keras model trained with ImageDataGenerator

Viewed 1456

I have trained a model applying some image augmentations by using ImageDataGenerator in Keras as follows:

train_datagen = ImageDataGenerator(
        rotation_range=60, 
        width_shift_range=0.1,
        height_shift_range=0.1, 
        horizontal_flip=True)
train_datagen.fit(x_train)

history = model.fit_generator(
    train_datagen.flow(x_train, y_train, batch_size=7),
    steps_per_epoch=600,
    epochs=epochs,
    callbacks=callbacks_list
)

How should I make predictions with this model? By using model.predict() as shown below?

predictions = model.predict(x_test)

Or should I use model.predict_generator() where an ImageDataGenerator is applied on x_test where x_test is unlabelled?

If I use predict_generator(): How to do that?

What is the difference between two methods?

1 Answers

predict_generator() is a convenience function that makes it easier to load in the images and apply the same preprocessing like you did for your training samples. I recommend using that rather than model.predict.

In your case simply do:

test_gen = ImageDataGenerator()
predictions = model.predict_generator(test_gen.flow(# ... your params here ... #))
Related