gpt4 book ai didi

python - 上采样不匹配时的 keras 形状

转载 作者:行者123 更新时间:2023-12-04 13:44:55 27 4
gpt4 key购买 nike

我正在尝试运行此 convolutional auto encoder示例但使用我自己的数据,所以我根据我的图像修改了它的 InputLayer。但是,在输出层上存在维度问题。我确定问题出在 UpSampling 上,但我不确定为什么会发生这种情况:代码如下。

N, H, W = X_train.shape
input_img = Input(shape=(H,W,1)) # adapt this if using `channels_first` image data format

x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (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)

# at this point the representation is (4, 4, 8) i.e. 128-dimensional

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder.summary()

model summary for my images of 150x81
然后,当我运行 fit 时,抛出此错误:
i+=1
autoencoder.fit(x_train, x_train,
epochs=50,
batch_size=128,
shuffle=True,
validation_data=(x_test, x_test),
callbacks= [TensorBoard(log_dir='/tmp/autoencoder/{}'.format(i))])

ValueError: Error when checking target: expected conv2d_23 to have shape (148, 84, 1) but got array with shape (150, 81, 1)

我回到教程代码,并尝试查看其模型的摘要,它显示以下内容:

model summary for images of 28x28
我确定在解码器上重建输出时存在问题,但我不确定为什么会这样,为什么它适用于 128x28 图像而不适用于 150x81 的地雷

我想我可以通过稍微改变我的图像尺寸来解决这个问题,但我想了解正在发生的事情以及如何避免它

最佳答案

您可以使用 ZeroPadding2D 填充输入图像到 32X32,然后使用 Cropping2D 裁剪解码图像。

from keras.layers import ZeroPadding2D, Cropping2D


input_img = Input(shape=(28,28,1)) # adapt this if using `channels_first` image data format
input_img_padding = ZeroPadding2D((2,2))(input_img) #zero padding image to shape 32X32
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img_padding)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (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)

# at this point the representation is (4, 4, 8) i.e. 128-dimensional

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
decoded_cropping = Cropping2D((2,2))(decoded)

autoencoder = Model(input_img, decoded_cropping) #cropping image from 32X32 to 28X28
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder.summary()

summary

关于python - 上采样不匹配时的 keras 形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50515409/

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