TransWikia.com

Why is predict_generator is returning an empty array?

Data Science Asked on January 11, 2021

I am trying to print the predicted labels of my test data but the predict_generator() function is returning an empty array.

My Model:

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Flatten



train_directory = 'D:D_dataRock_Paper_ScissorsTrain'
training_datgagen = ImageDataGenerator(rescale = 1./255)
training_generator = training_datgagen.flow_from_directory(
    train_directory,
    target_size = (28,28),
    class_mode = 'categorical')

validation_directory = 'D:D_dataRock_Paper_ScissorsTest'
validation_datagen = ImageDataGenerator(rescale= 1./255)
validation_generator = validation_datagen.flow_from_directory(
    validation_directory,
    target_size = (28,28),
    class_mode = 'categorical'
    )

filenames = validation_generator.filenames
nb_samples = len(filenames)


model = Sequential()
model.add(Flatten(input_shape = (28,28,3)))
model.add(Dense(128,activation = 'relu'))
model.add(Dense(64, activation = 'relu'))
model.add(Dense(16, activation = 'relu'))
model.add(Dense(3, activation = 'softmax'))

model.compile(optimizer = 'adam', loss = 'categorical_crossentropy',metrics = ['accuracy'],)

model.fit_generator(training_generator,epochs=20,validation_data = validation_generator)
predict = model.predict_generator(validation_generator,steps = nb_samples)

print(predict)

The output :

Found 1748 images belonging to 3 classes.
Found 0 images belonging to 0 classes.
Epoch 1/20
55/55 [==============================] - 4s 64ms/step - loss: 0.9951 - accuracy: 0.4908
Epoch 2/20
55/55 [==============================] - 4s 73ms/step - loss: 0.7280 - accuracy: 0.7248
Epoch 3/20
55/55 [==============================] - 6s 109ms/step - loss: 0.5783 - accuracy: 0.7895
Epoch 4/20
55/55 [==============================] - 5s 85ms/step - loss: 0.4649 - accuracy: 0.8387
Epoch 5/20
55/55 [==============================] - 4s 71ms/step - loss: 0.3955 - accuracy: 0.8707
Epoch 6/20
55/55 [==============================] - 4s 74ms/step - loss: 0.3595 - accuracy: 0.8804
Epoch 7/20
55/55 [==============================] - 4s 70ms/step - loss: 0.3247 - accuracy: 0.8919
Epoch 8/20
55/55 [==============================] - 4s 66ms/step - loss: 0.3435 - accuracy: 0.8827
Epoch 9/20
55/55 [==============================] - 4s 65ms/step - loss: 0.2740 - accuracy: 0.9102
Epoch 10/20
55/55 [==============================] - 3s 60ms/step - loss: 0.2141 - accuracy: 0.9336
Epoch 11/20
55/55 [==============================] - 3s 61ms/step - loss: 0.1836 - accuracy: 0.9462
Epoch 12/20
55/55 [==============================] - 3s 63ms/step - loss: 0.1722 - accuracy: 0.9416
Epoch 13/20
55/55 [==============================] - 3s 62ms/step - loss: 0.1788 - accuracy: 0.9394
Epoch 14/20
55/55 [==============================] - 3s 63ms/step - loss: 0.1331 - accuracy: 0.9571
Epoch 15/20
55/55 [==============================] - 4s 68ms/step - loss: 0.1343 - accuracy: 0.9537
Epoch 16/20
55/55 [==============================] - 4s 65ms/step - loss: 0.1033 - accuracy: 0.9680
Epoch 17/20
55/55 [==============================] - 3s 62ms/step - loss: 0.1001 - accuracy: 0.9651
Epoch 18/20
55/55 [==============================] - 3s 62ms/step - loss: 0.1209 - accuracy: 0.9565
Epoch 19/20
55/55 [==============================] - 3s 61ms/step - loss: 0.1187 - accuracy: 0.9559
Epoch 20/20
55/55 [==============================] - 3s 63ms/step - loss: 0.0834 - accuracy: 0.9737
[]

One Answer

The code looks OK to me... I can only think that you have somehow exhausted the ImageDataGenerator object, because you use the same on during training and then for the prediction. (I do realise this should never happen).

To rule this out, you could create a separate generator for testing.

This would also have the advantage of using some of your data for a hold-out test set, so your final predictions are out-of-sample, giving you a better idea of performance :)

For example, you might change your code to be like this:

validation_directory = 'D:D_dataRock_Paper_ScissorsTest'
holdout_directory =  'D:D_dataRock_Paper_ScissorsHoldout'   # make this new directory

Move e.g. 10 files into the new holdout directory

validation_datagen = ImageDataGenerator(rescale= 1./255)
validation_generator = validation_datagen.flow_from_directory(
    validation_directory,
    target_size = (28,28),
    class_mode = 'categorical'
    )

# new generator for predictions
holdout_generator = validation_datagen.flow_from_directory(
    holdout_directory,
    target_size = (28,28),
    class_mode = 'categorical'
    )


model.fit_generator(training_generator, epochs=20, validation_data=validation_generator)

Set the number N accordingly, e.g. to the 10 files in the new directory

predict = model.predict_generator(holdout_generator, batch_size=N)

print(predict.shape)    # this should return the shape of the numpy array: (N, H, W, C)
print(predict()        # the actual rpedictions

Answered by n1k31t4 on January 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP