首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Keras方法'predict‘和'predict_generator’结果不同

Keras方法'predict‘和'predict_generator’结果不同
EN

Stack Overflow用户
提问于 2019-10-24 17:19:08
回答 1查看 662关注 0票数 1

我已经训练了一个用于图像分类的基本CNN模型。在训练模型时,我使用了keras api中的ImageDataGenerator。在对模型进行训练后,我使用了测试数据生成器和flow_from_directory方法进行测试。一切都很顺利。然后我保存了模型以备将来使用。现在我正在使用相同的模型,并使用keras api中的预测方法对单个图像进行预测,但每次我使用不同的图像进行测试时,预测结果都非常不同。你能告诉我有什么解决办法吗?

代码语言:javascript
复制
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)

当我尝试使用预测时,每次都会出现错误的预测。

任何线索都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2019-10-24 17:31:29

Keras generator使用PIL进行图像读取,它以RGB的形式从磁盘读取图像。

您正在使用opencv进行读取,它将图像读取为BGR。您必须将您的图像从BGR转换为RGB

代码语言:javascript
复制
img = cv2.imread(img_file)

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

...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58538135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档