我已经训练了一个用于图像分类的基本CNN模型。在训练模型时,我使用了keras api中的ImageDataGenerator。在对模型进行训练后,我使用了测试数据生成器和flow_from_directory方法进行测试。一切都很顺利。然后我保存了模型以备将来使用。现在我正在使用相同的模型,并使用keras api中的预测方法对单个图像进行预测,但每次我使用不同的图像进行测试时,预测结果都非常不同。你能告诉我有什么解决办法吗?
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)当我尝试使用预测时,每次都会出现错误的预测。
任何线索都将不胜感激。
发布于 2019-10-24 17:31:29
Keras generator使用PIL进行图像读取,它以RGB的形式从磁盘读取图像。
您正在使用opencv进行读取,它将图像读取为BGR。您必须将您的图像从BGR转换为RGB。
img = cv2.imread(img_file)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
...https://stackoverflow.com/questions/58538135
复制相似问题