首页 文章

预期zero_padding2d_1_input具有形状(无,3,875,375)但是具有形状的数组(1,375,875,3)

提问于
浏览
1

我正在使用keras加载我的CNN中的model.h5(权重文件) . 我使用的是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 ..........

这是我尝试过的第一个:我查看了这些帖子:Error when checking target: expected dense_20 to have shape (None, 3) but got array with shape (1200, 1)

Keras Error when checking : expected embedding_1_input to have shape (None, 100) but got array with shape (1, 3)

Error when checking model target: expected dense_24 to have shape...but got array with shape... in Keras

我正在尝试的代码:

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数组如何甚至具有None的维度?我究竟做错了什么?如何修改代码以便仅使用单个图像进行预测 .

任何帮助将不胜感激!

1 回答

  • 1

    我相信你需要使用

    model.add(ZeroPadding2D((1,1),input_shape=(3,875,375),data_format='channels_first'))
    

    这是因为根据docs,默认值为 'channels_last' .

    第一个参数 None 只是批量大小的表示,而不是模型体系结构中预先确定的内容,因此您无需担心这一点 . 在该错误消息中看到 None 是预期的

相关问题