非常简单地说,当我在 Keras 代码中使用时,我的问题涉及到图像大小与 maxpool 层之后的输入图像大小不同padding = 'same'
。我正在浏览 Keras 博客:在 Keras 中构建自动编码器。我正在构建卷积自动编码器。自编码器代码如下:
input_layer = Input(shape=(28, 28, 1))
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_layer)
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_layer, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
根据autoencoder.summary()
,第一Conv2D(16, (3, 3), activation='relu', padding='same')(input_layer)
层之后的图像输出为 28 X 28 X 16,即与输入图像大小相同。这是因为填充是'same'
.
在 [49] 中:autoencoder.summary() (层数由我给出,不在输出中产生) _________________________________________________________________ 层(类型)输出形状参数# ==================================================== ================ 1.input_1 (InputLayer) (无, 28, 28, 1) 0 _________________________________________________________________ 2.conv2d_1 (Conv2D) (无, 28, 28, 16) 160 _________________________________________________________________ 3.max_pooling2d_1 (MaxPooling2 (无, 14, 14, 16) 0 _________________________________________________________________ 4.conv2d_2 (Conv2D) (无, 14, 14, 8) 1160 _________________________________________________________________ 5.max_pooling2d_2 (MaxPooling2 (无, 7, 7, 8) 0 _________________________________________________________________ 6.conv2d_3 (Conv2D) (无, 7, 7, 8) 584 _________________________________________________________________ 7.max_pooling2d_3 (MaxPooling2 (无, 4, 4, 8) 0 _________________________________________________________________ 8.conv2d_4 (Conv2D) (无, 4, 4, 8) 584 _________________________________________________________________ 9.up_sampling2d_1 (UpSampling2 (None, 8, 8, 8) 0 _________________________________________________________________ 10.conv2d_5 (Conv2D) (无, 8, 8, 8) 584 _________________________________________________________________ 11.up_sampling2d_2 (UpSampling2 (无, 16, 16, 8) 0 _________________________________________________________________ 12.conv2d_6 (Conv2D) (无, 14, 14, 16) 1168 _________________________________________________________________ 13.up_sampling2d_3 (UpSampling2 (无, 28, 28, 16) 0 _________________________________________________________________ 14.conv2d_7 (Conv2D) (无, 28, 28, 1) 145 ==================================================== ================
下一层(第 3 层)是,MaxPooling2D((2, 2), padding='same')(x)
。summary() 显示该层的输出图像大小为 14 X 14 X 16。但该层中的填充也是'same'
. 那么为什么输出图像大小不保持为 28 X 28 X 16 并填充零?
此外,当来自其早期层上方的输入形状为 (16 X 16 X 8) 时,在第 12 层之后输出形状如何变为 (14 X 14 X 16) 尚不清楚。
`