简体   繁体   English

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

[英]Finding the amount of zeros to pad the input of a convolutional layer

I am using these these sources to build a convolutional autoencoder in tensorflow. 我使用这些这些 资源打造tensorflow卷积的自动编码。 I understand that I need to pad my input image with zeros, in order to get an output from the decoder equal to the original input. 我知道我需要用零填充我的输入图像,以使解码器的输出等于原始输入。 The author is giving an example for the simple case of a square kernel and equal values for the strides (vertical and horrizontal). 作者给出了一个简单的例子,说明了正方形核的简单情况,并且步幅的值相等(垂直和水平)。 I need to generalize this padding function for my input, however I fail to get the correct shape of my tensor. 我需要针对我的输入概括此填充函数,但是我无法获得张量的正确形状。 My function so far is: 到目前为止,我的功能是:

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]])

This gives me 这给我

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)

for 对于

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

Any idea on what I'm doing wrong? 关于我在做什么错的任何想法吗?

The function you use does not take into account strides. 您使用的功能未考虑跨步。 Actually it just decrements by 1 your initial input. 实际上,它只是将初始输入值减少1。 For 1D case, knowing the input size i , kernel size k , stride s and padding p you can calculate the output size of the convolution as: 对于一维情况,知道输入大小i ,内核大小k ,步幅s和填充p ,则可以计算出卷积的输出大小为:

在此处输入图片说明

Here || 在这里|| operator means ceiling operation. 操作员表示天花板操作。 Knowing the math for a 1-dim case, n-dim case is easy once you see that each dim is independent. 一旦知道了每个暗角是独立的,就知道了1暗角情况的数学原理,n暗角情况很容易。 So you just slide each dimension separately. 因此,您只需分别滑动每个尺寸。


Looking at the formula, and knowing that your o should be equal to i , you can calculate the appropriate padding. 查看公式,知道您的o应该等于i ,您可以计算适当的填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM