在keras中预测单个图像(维度问题)
predicting a single image in keras (dimension problem)
我想我在使用 Keras 时遇到了一个常见的维度问题。我尝试使用预训练模型 ('model.h5') 以便
预测单个测试图像 ('test.jpg') 的 class。
使用以下代码:
model = load_model('model.h5')
model.summary()
# load dataset
# evaluate the model
score = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], score[1]*100))
我正在获取有关模型的信息:
现在,在 运行、
之后
img = cv2.imread('test.jpg')
model.predict(img)
我收到错误消息:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-43-c2dfe8703a1b> in <module>()
1 img = cv2.imread('test.jpg')
2
----> 3 model.predict(img)
2 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1439
1440 # Case 2: Symbolic tensors or Numpy array-like.
-> 1441 x, _, _ = self._standardize_user_data(x)
1442 if self.stateful:
1443 if x[0].shape[0] > batch_size and x[0].shape[0] % batch_size != 0:
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
577 feed_input_shapes,
578 check_batch_axis=False, # Don't enforce the batch size.
--> 579 exception_prefix='input')
580
581 if y is not None:
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
133 ': expected ' + names[i] + ' to have ' +
134 str(len(shape)) + ' dimensions, but got array '
--> 135 'with shape ' + str(data_shape))
136 if not check_batch_axis:
137 data_shape = data_shape[1:]
ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (194, 259, 3)
我正在尝试类似问题的一些代码,但对我没有任何作用。我在这里错过了什么?非常感谢您的帮助!
当您使用的图像尺寸与用于训练模型的尺寸不匹配时,会出现此错误。
您图像的形状是 (194, 259, 3),
但是模型期望这样的结果:(1, 194, 259, 3),因为您使用的是单个样本。您可以借助 numpy.expand_dims()
获得所需的尺寸。
img = cv2.imread('test.jpg')
img = np.expand_dims(img, axis=0)
model.predict(img)
我想我在使用 Keras 时遇到了一个常见的维度问题。我尝试使用预训练模型 ('model.h5') 以便 预测单个测试图像 ('test.jpg') 的 class。
使用以下代码:
model = load_model('model.h5')
model.summary()
# load dataset
# evaluate the model
score = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], score[1]*100))
我正在获取有关模型的信息:
现在,在 运行、
之后img = cv2.imread('test.jpg')
model.predict(img)
我收到错误消息:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-43-c2dfe8703a1b> in <module>()
1 img = cv2.imread('test.jpg')
2
----> 3 model.predict(img)
2 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1439
1440 # Case 2: Symbolic tensors or Numpy array-like.
-> 1441 x, _, _ = self._standardize_user_data(x)
1442 if self.stateful:
1443 if x[0].shape[0] > batch_size and x[0].shape[0] % batch_size != 0:
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
577 feed_input_shapes,
578 check_batch_axis=False, # Don't enforce the batch size.
--> 579 exception_prefix='input')
580
581 if y is not None:
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
133 ': expected ' + names[i] + ' to have ' +
134 str(len(shape)) + ' dimensions, but got array '
--> 135 'with shape ' + str(data_shape))
136 if not check_batch_axis:
137 data_shape = data_shape[1:]
ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (194, 259, 3)
我正在尝试类似问题的一些代码,但对我没有任何作用。我在这里错过了什么?非常感谢您的帮助!
当您使用的图像尺寸与用于训练模型的尺寸不匹配时,会出现此错误。
您图像的形状是 (194, 259, 3),
但是模型期望这样的结果:(1, 194, 259, 3),因为您使用的是单个样本。您可以借助 numpy.expand_dims()
获得所需的尺寸。
img = cv2.imread('test.jpg')
img = np.expand_dims(img, axis=0)
model.predict(img)