Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use predict_generator with ImageDataGenerator?

I'm very new to Keras. I trained a model and would like to predict some images stored in subfolders (like for training). For testing, I want to predict 2 images from 7 classes (subfolders). The test_generator below sees 14 images, but I get 196 predictions. Where is the mistake? Thanks a lot!

test_datagen = ImageDataGenerator(rescale=1./255)

test_generator = test_datagen.flow_from_directory(
        test_dir,
        target_size=(200, 200),
        color_mode="rgb",
        shuffle = "false",
        class_mode='categorical')

filenames = test_generator.filenames
nb_samples = len(filenames)

predict = model.predict_generator(test_generator,nb_samples)
like image 530
Mario Kreutzfeldt Avatar asked Aug 21 '17 23:08

Mario Kreutzfeldt


People also ask

What is Predict_generator?

predict_generator: (Deprecated) Generates predictions for the input samples from a data generator.

What does Predict_generator return?

predict_generator returns a list of predictions which is a list of float values between 0 and 1.

What is ImageDataGenerator?

Introduction to Keras ImageDataGenerator. Keras ImageDataGenerator is used for getting the input of the original data and further, it makes the transformation of this data on a random basis and gives the output resultant containing only the data that is newly transformed. It does not add the data.

What does Evaluate_generator return?

evaluate_generator. Evaluates the model on a data generator. The generator should return the same kind of data as accepted by test_on_batch . steps: Total number of steps (batches of samples) to yield from generator before stopping.


4 Answers

You can change the value of batch_size in flow_from_directory from default value (which is batch_size=32 ) to batch_size=1. Then set the steps of predict_generator to the total number of your test images. Something like this:

test_datagen = ImageDataGenerator(rescale=1./255)

test_generator = test_datagen.flow_from_directory(
        test_dir,
        target_size=(200, 200),
        color_mode="rgb",
        shuffle = False,
        class_mode='categorical',
        batch_size=1)

filenames = test_generator.filenames
nb_samples = len(filenames)

predict = model.predict_generator(test_generator,steps = nb_samples)
like image 55
Matin H Avatar answered Oct 18 '22 23:10

Matin H


Default batch_size in generator is 32. If you want to make 1 prediction for every sample of total nb_samples you should devide your nb_samples with the batch_size. Thus with a batch_size of 7 you only need 14/7=2 steps for your 14 images

desired_batch_size=7

test_datagen = ImageDataGenerator(rescale=1./255)

test_generator = test_datagen.flow_from_directory(
        test_dir,
        target_size=(200, 200),
        color_mode="rgb",
        shuffle = False,
        class_mode='categorical',
        batch_size=desired_batch_size)

filenames = test_generator.filenames
nb_samples = len(filenames)

predict = model.predict_generator(test_generator,steps = 
                                   np.ceil(nb_samples/desired_batch_size))
like image 38
Ioannis Nasios Avatar answered Oct 19 '22 00:10

Ioannis Nasios


The problem is the inclusion of nb_samples in the predict_generator which is creating 14 batches of 14 images

14*14 = 196
like image 5
DJK Avatar answered Oct 19 '22 00:10

DJK


Use fit and predict, TensorFlow now supports both the methods with generators.

like image 2
K. Mitra Avatar answered Oct 19 '22 01:10

K. Mitra