找到填充卷积层输入的零的数量

Finding the amount of zeros to pad the input of a convolutional layer

我正在使用这些 these sources 在 tensorflow 中构建一个卷积自动编码器。我知道我需要用零填充我的输入图像,以便从解码器获得等于原始输入的输出。 作者给出了一个简单情况下的方形核和步幅(垂直和水平)相等值的示例。我需要为我的输入泛化这个填充函数,但是我没能得到我的张量的正确形状。到目前为止我的功能是:

def _pad(self, input_x, filter_height, filter_width):
    """
    pads input_x with the right amount of zeros.
    Args:
        input_x: 4-D tensor, [batch_side, widht, height, depth]
        filter_side: used to dynamically determine the padding amount
    Returns:
        input_x padded
    """
    # calculate the padding amount for each side
    top_bottom_padding = filter_height - 1
    left_right_padding = filter_width - 1

    # pad the input on top, bottom, left, right, with amount zeros
    return tf.pad(input_x,
                  [[0, 0], [top_bottom_padding, top_bottom_padding], [left_right_padding, left_right_padding], [0, 0]])

这给了我

Shape of input:  (10, 161, 1800, 1)
Shape of padded input: (10, 187, 1826, 1)
Shape of encoder output:  (10, 187, 913, 15)
Shape of decoder output:  (10, 187, 457, 15)

对于

num_outputs=15, kernel_size=14, stride=[1,2]

知道我做错了什么吗?

您使用的函数没有考虑步幅。实际上它只是将您的初始输入减 1。对于一维情况,知道输入大小 i、内核大小 k、步幅 s 和填充 p 你可以计算卷积的输出大小为:

这里|| operator 表示天花板操作。了解 1-dim 情况下的数学,一旦您看到每个 dim 都是独立的,n-dim 情况就很容易了。所以你只需分别滑动每个维度。


看公式,知道你的o应该等于i,你可以计算出合适的padding。