我正在使用keras在我的model.h5(一个权重文件)为我的CNN加载。我使用的是VGG-16架构。我的训练数据包括numpy大小数组(2590,3(用于RGB),875(宽度中的像素),375(高度))。我已经完成了数据的训练,现在我正在使用model.h5(带有权重)来进行预测。我遇到了以下错误。
expected zero_padding2d_1_input to have shape (None, 3, 875, 375) but got array with shape (1, 375, 875, 3)
这是我的VGG-16 CNN的顶级片段。
def VGG_16(weights_path=None):
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,875,375)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
.......... Continued ..........
下面是我首先尝试过的:我查看了这些帖子:20有形状(无,3),但得到与形状(1200,1)数组
我正在尝试的代码:
model = VGG_16('/path/to/weights/file....../model.h5')
print("Created model")
img = np.array([np.array(Image.open(path_to_image_i_want_to_convert))])
img.reshape(1, 3, 875,375)
try:
prediction = model.predict(img)
print(prediction)
print("I finished your prediction")
except Exception as e:
print(str(e))
但是,这总是会引发错误。
期望zero_padding2d_1_input具有形状(无,3,875,375),但具有形状的数组(1,375,875,3)
一个numpy数组怎么可能没有一个维度呢?我做错了什么?如何修改我的代码,以便只使用一个图像进行预测。
任何帮助都将不胜感激!
发布于 2017-08-11 21:27:20
我相信你需要用
model.add(ZeroPadding2D((1,1),input_shape=(3,875,375),data_format='channels_first'))
这是因为默认的是'channels_last'
,按照文档。
另外,作为None
的第一个参数只是批量大小的表示,而不是模型体系结构中预先确定的内容,因此您不需要担心这个问题。在该错误消息中,期望看到None
https://stackoverflow.com/questions/45645928
复制