Deep learning Keras:单个图像的模型预测

Deep learning Keras:单个图像的模型预测,deep-learning,keras,Deep Learning,Keras,我想用Keras预测一张图像。我已经训练了我的模型,所以我只是加载重量 from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D from keras.layers import Activation, Dropout, Flatten, Dense from keras imp

我想用Keras预测一张图像。我已经训练了我的模型,所以我只是加载重量

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
import numpy as np
import cv2

# dimensions of our images.
img_width, img_height = 150, 150



def create_model():
  if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
  else:
    input_shape = (img_width, img_height, 3)

  model = Sequential()
  model.add(Conv2D(32, (3, 3), input_shape=input_shape))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(32, (3, 3)))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(64, (3, 3)))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Flatten())
  model.add(Dense(64))
  model.add(Activation('relu'))
  model.add(Dropout(0.5))
  model.add(Dense(1))
  model.add(Activation('sigmoid'))

  return model


img = cv2.imread('./test1/1.jpg')
model = create_model()
model.load_weights('./weight.h5')
model.predict(img)
我正在使用以下方式加载图像:

img = cv2.imread('./test1/1.jpg')
并使用模型的预测功能:

 model.predict(img)
但我得到了一个错误:

ValueError: Error when checking : expected conv2d_1_input to have 4 dimensions, but got array with shape (499, 381, 3)

如何对单个图像进行预测?

由于您在小批量上训练了模型,因此您的输入是形状张量
[批量大小、图像宽度、图像高度、通道数]

预测时,即使只有一张图像,也必须尊重此形状。您的输入应为以下形状:
[1,图像宽度、图像高度、通道数]

你可以在numpy轻松地完成这项工作。假设您有一个5x5x3图像:

    >>> x = np.random.randint(0,10,(5,5,3))
    >>> x.shape
    >>> (5, 5, 3)
    >>> x = np.expand_dims(x, axis=0)
    >>> x.shape
    >>> (1, 5, 5, 3)
现在x是一个4阶张量

试试看:

 model.predict(img[None,...])

即使这并不能解决您的错误,如果您之前已经这样做了,请确保并重新缩放您的图像。例如,我的训练生成器如下所示:

train_datagen = ImageDataGenerator(
   rotation_range=40,
   zoom_range=[0.7, 0.9],
   horizontal_flip=True,
   rescale=1./255
)
所以当我去预测一幅图像时:

from PIL import Image
import numpy as np
from skimage import transform
def load(filename):
   np_image = Image.open(filename)
   np_image = np.array(np_image).astype('float32')/255
   np_image = transform.resize(np_image, (256, 256, 3))
   np_image = np.expand_dims(np_image, axis=0)
   return np_image

 image = load('my_file.jpg')
 model.predict(image)

我还必须将其重新缩放255。

您可以加载具有所需宽度和高度的图像,将其转换为形状为
(图像宽度、图像高度、通道数量)
的numpy数组,然后将数组形状更改为
(1、图像宽度、图像高度、通道数量)
。(批量大小=1)


谢谢你的回答。但是,在您的情况下,包含图像的img变量应该在哪里?假设我的变量
x
是图像。我可能应该写
x=cv2.imread('image.jpg')
。函数cv2.imread()返回一个numpy数组。因此,在您的例子中,
img
是一个numpy数组,我的
x
变量也是一个numpy数组。另一种方法是
x=x.restrape((1,)+x.shape)
。也。可用于转换回秩3张量。还请记住,如果使用ImageDataGenerator加载和训练数据,则可能使用了rescale=1./255。确保添加np.expand_dims(image,axis=0)/255(如果您使用该选项的话)…仅在数组中嵌套也能为我带来好处:
x=np.array([x])
,在这种情况下,这对我来说更直观。数据格式化应该在预处理阶段完成,而不是在模型调用中。这是一个有效的答案。奇怪的是这家伙被否决了。是因为他没有解释吗
model.predict(img[None])
也可以工作。它比
np.expand_dims()
stuff.要短得多。同意,对这个答案投了赞成票@不要气馁。你的回答很有帮助。请提供一行解释,这对其他人会更有帮助。谢谢,这对我有用。为了使用skimage,我将
scikit image==0.15.0
添加到requirement.txt包中
import numpy as np
from keras.preprocessing import image

img_width, img_height = 150, 150
img = image.load_img('image_path/image_name.jpg', target_size = (img_width, img_height))
img = image.img_to_array(img)
img = np.expand_dims(img, axis = 0)

model.predict(img)
single_test = model.predict(np.expand_dims(X_test[i], axis=0))