gpt4 book ai didi

python - 预期 conv2d_7 具有形状 (4, 268, 1),但得到的数组具有形状 (1, 270, 480)

转载 作者:行者123 更新时间:2023-11-30 09:04:48 24 4
gpt4 key购买 nike

我在使用 Keras 构建的自动编码器时遇到问题。输入的形状取决于屏幕尺寸,输出将是对下一个屏幕尺寸的预测...但是,似乎有一个我无法弄清楚的错误...请原谅我在该网站上糟糕的格式...

代码:

def model_build():
input_img = InputLayer(shape=(1, env_size()[1], env_size()[0]))
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
model = Model(input_img, decoded)
return model
if __name__ == '__main__':
model = model_build()
model.compile('adam', 'mean_squared_error')
y = np.array([env()])
print(y.shape)
print(y.ndim)
debug = model.fit(np.array([[env()]]), np.array([[env()]]))

错误:

Traceback (most recent call last): File "/home/ai/Desktop/algernon-test/rewarders.py", line 46, in debug = model.fit(np.array([[env()]]), np.array([[env()]])) File "/home/ai/.local/lib/python3.6/site-packages/keras/engine/training.py", line 952, in fit batch_size=batch_size) File "/home/ai/.local/lib/python3.6/site-packages/keras/engine/training.py", line 789, in _standardize_user_data exception_prefix='target') File "/home/ai/.local/lib/python3.6/site-packages/keras/engine/training_utils.py", line 138, in standardize_input_data str(data_shape)) ValueError: Error when checking target: expected conv2d_7 to have shape (4, 268, 1) but got array with shape (1, 270, 480)

编辑:

作为 env() 导入的 get_screen 代码:

def get_screen():
img = screen.grab()
img = img.resize(screen_size())
img = img.convert('L')
img = np.array(img)
return img

最佳答案

您有三个 2x 下采样步骤和三个 x2 上采样步骤。这些步骤不知道原始图像的大小,因此它们会将大小四舍五入到最接近的 8 = 2^3 的倍数。

cropX = 7 - ((size[0]+7) % 8)
cropY = 7 - ((size[1]+7) % 8)

cropX = 7 - ((npix+7) % 8)
cropY = 7 - ((nlin+7) % 8)

如果你添加一个新的最后一层,它应该可以工作......

decoded = layers.Cropping2D(((0,cropY),(0,cropX)))(x)

关于python - 预期 conv2d_7 具有形状 (4, 268, 1),但得到的数组具有形状 (1, 270, 480),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55249386/

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