1
votes

I have trained a basic CNN model for image classification. While training the model I have used ImageDataGenerator from keras api. After the model is being trained i used testdatagenerator and flow_from_directory method for testing. Everything Went well. Then I saved the model for future use. Now i am using the same model and used predict method from keras api with a single image, but the prediction is very different every time I test using different images. Could you please let me know any solution.

training_augmentation = ImageDataGenerator(rescale=1 / 255.0)
validation_testing_augmentation = ImageDataGenerator(rescale=1 / 255.0)

# Initialize training generator
training_generator = training_augmentation.flow_from_directory(
                                                                 JPG_TRAIN_IMAGE_DIR,
                                                                 class_mode="categorical",
                                                                 target_size=(32, 32),
                                                                 color_mode="rgb",
                                                                 shuffle=True,
                                                                 batch_size=batch_size
                                                              )

# initialize the validation generator
validation_generator = validation_testing_augmentation.flow_from_directory(
                                                                             JPG_VAL_IMAGE_DIR,
                                                                             class_mode="categorical",
                                                                             target_size=(32, 32),
                                                                             color_mode="rgb",
                                                                             shuffle=False,
                                                                             batch_size=batch_size
                                                                          )

# initialize the testing generator
testing_generator = validation_testing_augmentation.flow_from_directory(
                                                                           JPG_TEST_IMAGE_DIR,
                                                                           class_mode="categorical",
                                                                           target_size=(32, 32),
                                                                           color_mode="rgb",
                                                                           shuffle=False,
                                                                           batch_size=batch_size
                                                                        )

history = model.fit_generator(
    training_generator,
    steps_per_epoch=total_img_count_dict['train'] // batch_size,
    validation_data=validation_generator,
    validation_steps=total_img_count_dict['val'] // batch_size,
    epochs=epochs,
    callbacks=callbacks)

testing_generator.reset()
prediction_stats = model.predict_generator(testing_generator, steps=(total_img_count_dict['test'] // batch_size) + 1)


### Trying to use predict method
img_file = '/content/drive/My Drive/Traffic_Sign_Recognition/to_display_output/Copy of 00003_00019.jpg'
img = cv2.imread(img_file)
img=cv2.resize(img, (32,32))
img = img/255.0
a=np.reshape(img, (1, 32, 32, 3))

model = load_model('/content/drive/My Drive/Traffic_Sign_Recognition/basic_cnn.h5')

prediction = model.predict(a)

When I am trying to use predict, every time wrong prediction is coming.

Any leads will be appreciated.

1

1 Answers

0
votes

Keras generator uses PIL for image reading which read images from disk as RGB.

You are using opencv for reading which reads images as BGR. You have to convert your image from BGR to RGB.

img = cv2.imread(img_file)

img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)

...