简体   繁体   English

tf.nn.conv2d_transpose 的填充有什么作用?

[英]What does the padding of tf.nn.conv2d_transpose do?

As we know, we can calculate the shape of output tensor by padding mode for conv2d , and the algorithm is clear, but I'm very confused about conv2d_transpose , does it pad the input tensor and then invoke conv2d ?众所周知,我们可以通过填充模式为conv2d计算输出tensor的形状,并且算法很清楚,但我对conv2d_transpose非常困惑,它是否填充输入张量然后调用conv2d And where does it transpose filter or input?它在哪里转置过滤器或输入? How to calculate the shape of output tensor according to the padding mode, SAME or VALID for conv2d_transpose ?如何根据conv2d_transpose的填充模式, SAMEVALID计算输出张量的形状?

What does Padding mean for conv2d_transpose?填充对于 conv2d_transpose 意味着什么?

'SAME' means simply multiply the input shape by the strides. “相同”意味着简单地将输入形状乘以步幅。 For example, if the input shape has a height and width of 7 and the conv2d_transpose has padding=same and strides=3, then the output shape will have a height and width of 7x3 = 21.例如,如果输入形状的高度和宽度为 7,并且 conv2d_transpose 的 padding=same 和 strides=3,那么输出形状的高度和宽度将为 7x3 = 21。

'VALID' is nearly the same. 'VALID'几乎相同。 Start with 'SAME', then check the kernel_size compared to the strides.从“SAME”开始,然后检查与步幅相比的 kernel_size。 If it is larger, then add that amount to the height and width.如果它更大,则将该数量添加到高度和宽度。 Why?为什么? Because as the kernel is moving across the image for convolution (by strikes amount at a time) the last kernel will hang over the image by the difference.因为当内核在图像上移动以进行卷积时(一次通过撞击量),最后一个内核将通过差异悬垂在图像上。 Imagine the above example of a height and width of 7 as the input and this time the padding=valid, strides=3, and kernel=5.想象一下上面的例子,输入高度和宽度为 7,这次 padding=valid,strides=3,kernel=5。 The output height and width will be 7x3 + (5-3).输出高度和宽度将为 7x3 + (5-3)。

In both cases, if the kernel is less than the strides you'll just get a whole lot of zeros in the output.在这两种情况下,如果内核小于步幅,您只会在输出中得到很多零。 Why?为什么? Consider what stride does...考虑 stride 的作用...

For a given value of stride, the input image is increased by many times.对于给定的 stride 值,输入图像会增加很多倍。 A stride of 3 makes the input image 3 times wider and higher.步幅为 3 会使输入图像的宽度和高度增加 3 倍。 The original values occupy every 3rd place and the rest is filled with zeros !原始值占据每 3 位,其余的则用填充! For padding=valid there is that extra we talked about earlier.对于 padding=valid 有我们之前讨论过的额外内容。

The kernel_size is the size of the kernel for convolution that goes over the image, and it is moved over the image by strides. kernel_size 是卷积核在图像上的大小,它在图像上按步幅移动。 So, if the kernel_size is 1 and the strides is 3 then your output is mostly zeros.因此,如果 kernel_size 为 1 且步幅为 3,则您的输出大部分为零。

Example SAME示例相同

>>> conv2d_tr = tf.keras.layers.Conv2DTranspose(5,kernel_size=3,padding='same',strides=2)
>>> conv2d_tr(np.zeros([3,2,2,4],dtype=np.float32)).numpy().shape

(3, 4, 4, 5)

Example VALID (bigger kernel than strides)示例 VALID(比步幅更大的内核)

>>> conv2d_tr = tf.keras.layers.Conv2DTranspose(5,kernel_size=3,padding='valid',strides=2)
>>> conv2d_tr(np.zeros([3,10,10,4],dtype=np.float32)).numpy().shape

(3, 21, 21, 5)

Example VALID (kernel equal or less than stride)示例 VALID(内核等于或小于 stride)

>>> conv2d_tr = tf.keras.layers.Conv2DTranspose(5,kernel_size=2,padding='valid',strides=2)
>>> conv2d_tr(np.zeros([3,2,2,4],dtype=np.float32)).numpy().shape

(3, 20, 20, 5)

Example ZEROS for small kernel to stride小内核步幅的示例零

>>> conv2d_tr = tf.keras.layers.Conv2DTranspose(1,kernel_size=1,padding='same',strides=2)
>>> conv2d_tr(np.ones([1,2,2,3],dtype=np.float32)).numpy().shape
(1, 4, 4, 1)

>>> conv2d_tr(np.ones([1,2,2,3],dtype=np.float32)).numpy()

array([[[[0.702],[0.   ],[0.702],[0.   ]],
        [[0.   ],[0.   ],[0.   ],[0.   ]],
        [[0.702],[0.   ],[0.702],[0.   ]],
        [[0.   ],[0.   ],[0.   ],[0.   ]]]], dtype=float32)

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

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