gpt4 book ai didi

python - 预期 Zero_padding2d_1_input 具有形状 (None, 3, 875, 375),但得到的数组具有形状 (1, 375, 875, 3)

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:01 32 4
gpt4 key购买 nike

我正在使用 keras 加载 CNN 的 model.h5(权重文件)。我正在使用 VGG-16 架构。我的训练数据由大小为(2590、3(RGB)、875(宽度像素)、375(高度像素))的 numpy 数组组成。我已经完成了数据训练,现在我正在使用 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 具有形状 (None, 3, 875, 375),但得到的数组具有形状 (1, 375, 875, 3)

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

如有任何帮助,我们将不胜感激,谢谢!

最佳答案

我相信你需要使用

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

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

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

关于python - 预期 Zero_padding2d_1_input 具有形状 (None, 3, 875, 375),但得到的数组具有形状 (1, 375, 875, 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45645928/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com