Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to make prediction with Keras model trained with ImageDataGenerator

Tags:

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?

like image 791
Sreeram TP Avatar asked Sep 23 '17 09:09

Sreeram TP


People also ask

What method is used to fit a model on batches from an ImageDataGenerator?

You can do this by calling the fit() function on the data generator and passing it to your training dataset. The data generator itself is, in fact, an iterator, returning batches of image samples when requested.


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 ... #))
like image 195
petezurich Avatar answered Oct 11 '22 12:10

petezurich