【问题标题】:expected zero_padding2d_1_input to have shape (None, 3, 875, 375) but got array with shape (1, 375, 875, 3)预期 zero_padding2d_1_input 的形状为 (None, 3, 875, 375) 但得到的数组形状为 (1, 375, 875, 3)
【发布时间】:2018-01-20 14:30:41
【问题描述】:

我正在使用 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 的顶部 sn-p

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 的形状为 (None, 3, 875, 375) 但得到的数组的形状为 (1, 375, 875, 3)

一个 numpy 数组怎么可能有一个无维度? 我究竟做错了什么?如何修改我的代码以便仅使用单个图像进行预测。

任何帮助将不胜感激谢谢!

【问题讨论】:

    标签: python numpy tensorflow keras


    【解决方案1】:

    相信你需要使用

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

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

    同样,None 的第一个参数只是批量大小的表示,而不是模型架构中预先确定的东西,因此您无需担心。在该错误消息中看到 None 是预期的

    【讨论】:

      猜你喜欢
      • 2019-03-11
      • 1970-01-01
      • 2017-10-22
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 2020-05-14
      • 2019-05-09
      相关资源
      最近更新 更多