I am implementing image classification project .I have generated model and saved it . It was successfully trained . When i use predict_generator in keras to classify the test images then for each image i am getting multiple rows for each image in prediction numpy array.
Prediction Code :
from keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import img_to_array, load_img
import numpy as np
# dimensions of our images.
img_width, img_height = 150, 150
batch_size = 16
test_model = load_model('first_try1.h5')
img = load_img('data/train/dogs/dog.2.jpg',False,target_size=(img_width,img_height))
validation_data_dir="test1"
test_datagen = ImageDataGenerator(rescale=1. / 255)
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
print(len(validation_generator.filenames))
predictions=test_model.predict_generator(validation_generator,len(validation_generator.filenames));
#print(predictions)
Output :
Found 5 images belonging to 1 classes.
5
[[ 0.0626688 ]
[ 0.07796276]
[ 0.46529126]
[ 0.28495458]
[ 0.07343803]
[ 0.07343803]
[ 0.0626688 ]
[ 0.46529126]
[ 0.28495458]
[ 0.07796276]
[ 0.0626688 ]
[ 0.28495458]
[ 0.07796276]
[ 0.46529126]
[ 0.07343803]
[ 0.07796276]
[ 0.46529126]
[ 0.0626688 ]
[ 0.07343803]
[ 0.28495458]
[ 0.0626688 ]
[ 0.07796276]
[ 0.46529126]
[ 0.07343803]
[ 0.28495458]]
So according to documentation you are calling your generator len(validation_generator.filenames)
number of times - where at each call the sample of size batch_size
is provided. Due to how ImageGenerator
is implemented - if there is not enough files to complete batch (in your case you have batch_size=16
but only 5 images in your folder) - the maximum possible number of images is returned - in your case - 5. So you are getting len(validation_generator.filenames) * 5 = 25
images to evaluate - and that's why you have such results (if you look closely - you have the same values in each 5 numbers). In order to get this samples in order of filenames
you need to create a new generator with shuffle
option set to False
and with batch_size=5
and call it once (or e.g. with batch_size=1
and call it 5 times).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With