Keras:单个图像的model.predict

时间:2017-03-25 13:35:59

标签: 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 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)

我应该如何对单张图像进行预测?

6 个答案:

答案 0 :(得分:24)

由于您使用迷你批次训练模型,因此输入的形状是张量[batch_size, image_width, image_height, number_of_channels]

预测时,即使只有一张图像,也必须尊重这种形状。您的输入应该是正确的:[1, image_width, image_height, number_of_channels]

你可以轻松地在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张量!

答案 1 :(得分:2)

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

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。

答案 2 :(得分:2)

您可以加载具有所需宽度和高度的图像,将其转换为形状为(image_width, image_height, number_of_channels)的numpy数组,然后将该数组的形状更改为(1, image_width, image_height, number_of_channels)。 (batch_size = 1)

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)

答案 3 :(得分:0)

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

答案 4 :(得分:-1)

如果你解决了你的问题,我不会。 但是,您是否尝试添加以下行? :

from keras import backend as K
K.set_image_dim_ordering('th')

注意:如果您使用的是Tensorflow,我认为您需要将 'tf' 改为 'th'

哈拉

答案 5 :(得分:-1)

尝试:

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