keras - 为什么在最大池化中使用相同的填充?

标签 keras padding

在经过时the autoencoder tutorial in Keras blog ,看到作者在Convolutional Autoencoder部分的max pooling layer使用了相同的padding,如下图。

x = MaxPooling2D((2, 2), padding='same')(x)

有人可以解释这背后的原因吗?使用最大池化,我们想减少高度和宽度,但为什么这里使用相同的填充,保持高度和宽度相同?

此外,此代码的结果将维度减半,因此相同的填充似乎不起作用。

最佳答案

来自 https://keras.io/layers/convolutional/

"same" results in padding the input such that the output has the same length as the original input.



来自 https://keras.io/layers/pooling/

pool_size: integer or tuple of 2 integers, factors by which to downscale (vertical, horizontal). (2, 2) will halve the input in both spatial dimension. If only one integer is specified, the same window length will be used for both dimensions.



那么,首先让我们先问一下为什么要使用填充?在卷积内核上下文中,这很重要,因为我们不想错过位于内核“中心”的每个像素。内核正在寻找的图像边缘/角落可能存在重要行为。所以我们在 Conv2D 的边缘周围填充,结果它返回与输入相同大小的输出。

然而,在 MaxPooling2D 层的情况下,我们出于类似的原因进行填充,但步幅大小受您选择的池化大小的影响。由于您的池化大小为 2,因此每次通过池化层时,您的图像都会减半。
input_img = Input(shape=(28, 28, 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

因此,就您的教程示例而言;您的图像尺寸将从 28->14->7->4 开始,每个箭头代表池化层。

关于keras - 为什么在最大池化中使用相同的填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316717/

相关文章:

python - 如何修复 "ModuleNotFoundError: No module named ' keras.layers.advanced_activations'“

python - 管理多个 session 和图表的合理方法

c++ - 关于结构填充

css - 如何以给定 div 相对于其父 div 的百分比计算 padding-top

html - 如何通过单击 CSS 中的另一个元素来更改元素属性

machine-learning - Keras、顺序和时间序列 : should we flatten or not?

python - Keras 预测()值错误 : input doesn't have the correct dimension

machine-learning - Keras 中样本权重的大小

html - 车身顶部间隙;我应该使用边距还是填充?

css - 试图让div向右滑动