Keras/Tensorflow预测:数组形状错误

6
我正在按照Keras CIFAR10教程进行操作。 我所做的唯一更改是: 在教程文件末尾添加了[a]。
model.save_weights('./weights.h5', overwrite=True)
修改了~./keras/keras.json
{"floatx": "float32", "backend": "tensorflow", "epsilon": 1e-07}

我可以成功运行这个模型。

然后我想对训练好的模型测试一张单独的图片。我的代码:

[... similar to tutorial file with model being created and compiled...]
...
model = Sequential()
...
model.compile()

model.load_weights('./ddx_weights.h5')

img = cv2.imread('car.jpeg', -1) # this is is a 32x32 RGB image
img = np.array(img)
y_pred = model.predict_classes(img, 1)
print(y_pred)

我遇到了这个错误:

ValueError: Cannot feed value of shape (1, 32, 3) for Tensor 'convolution2d_input_1:0', which has shape '(?, 3, 32, 32)'

如何正确地调整输入数据的形状,以便测试单个图像?

我在./keras/keras.json未添加"image_dim_ordering": "tf"


1
介意解释一下为什么给了个踩的评价吗?这会对以后的发帖有所帮助。 - pepe
2个回答

6

你需要将输入图像重新塑形为[?, 3, 32, 32],其中?是批量大小。在你的情况下,由于你只有一个图像,因此批量大小为1,所以可以这样做:

img = np.array(img)
img = img.reshape((1, 3, 32, 32))

0

我现在正在处理cifar10数据,发现简单的reshape无法奏效,应该使用numpy.transpose。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接