【问题标题】:how to input image for class's prediction in keras using python如何使用python在keras中输入图像以进行类预测
【发布时间】:2018-03-26 20:44:33
【问题描述】:

我正在尝试预测单个图像的结果,但它给出了一个不相关的结果。我已经在 cifar 10 数据集上训练了模型 我已经使用 keras 和 tensorflow 来训练这个模型。我想我提供的输入大小不正确。

这里是培训代码的要点:https://github.com/09rohanchopra/cifar10/blob/master/cifar10-simple-cnn.ipynb 单张图片预测代码

#
from keras.preprocessing import image
from keras.models import load_model
from scipy.misc import imread,imresize
import numpy as np
model=load_model('augmented_best_model.h5')
im=imread('1.jpg')
im=im/255
im=im.resize(im,(32,32))
pr=model.predict(im.reshape(-1,3,32,32))

【问题讨论】:

  • 为什么要先将图像重新塑造成通道?在您的训练代码中,图像输入形状为 input_shape=(IMAGE_SIZE,IMAGE_SIZE,CHANNELS)
  • 另外,如果 im 的 dtype 为整数,im/255 可能会在 Python 2 中将其归零

标签: python-3.x machine-learning keras computer-vision


【解决方案1】:

imread 现在已弃用。更好的选择是使用 keras 函数。

x = image.load_img('test/2.jpg', target_size=(32,32))
x = image.img_to_array(x)
x = x.reshape((1,) + x.shape)
x = x/255.

【讨论】:

    【解决方案2】:

    我得到了答案

    x=imread('test/2.jpg',mode='RGB')
    x=imresize(x,(32,32))
    x=np.invert(x)
    x=x.reshape(-1,32,32,3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 2018-09-29
      • 1970-01-01
      • 2020-10-30
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多