Tensorflow 1.x 和 Pytorch 中 Conv2d Padding的区别

Tensorflow 和 Pytorch 中 Conv2d Padding的区别

Pytorch中Conv2d的Padding

Tensorflow 1.x 和 Pytorch 中 Conv2d Padding的区别

它可以有三种形式:整数、二进制和字符串。

[En]

It can be in three forms: integer, binary, and string.

  1. 整数(int)。如果输入的padding为整数则代表在 上,下,左,右 四个方向都充填一样数量的0或者由 padding_mode 确定的padding类型。
  2. 二元组(tuple)。如果输入的padding 为二元组(padding[0],padding[1]),则padding[0]代表上下两个方向的padding大小,padding[1]则代表左右两个方向的padding大小。
  3. 字符串(str)。字符串模式可选参数为 validsamevalid模式表示不充填, same模式表示输入与输出的形状大小保持一致(但是仅仅适用于stride=1的情况!!!)

可以看出pytorch都是对称的进行padding,要么是四个方向都是一样的padding,要么上下或左右做一样的padding。

最终的输出形状计算公式如下:

[En]

The final output shape calculation formula is as follows:

H o u t = ⌊ H i n + 2 × padding [ 0 ] − dilation [ 0 ] × ( kernel_size [ 0 ] − 1 ) − 1 stride [ 0 ] + 1 ⌋ H_{out} = \left\lfloor\frac{H_{in} + 2 \times \text{padding}[0] – \text{dilation}[0] \times (\text{kernel_size}[0] – 1) – 1}{\text{stride}[0]} + 1\right\rfloor H o u t ​=⌊stride [0 ]H i n ​+2 ×padding [0 ]−dilation [0 ]×(kernel_size [0 ]−1 )−1 ​+1 ⌋

W o u t = ⌊ W i n + 2 × padding [ 1 ] − dilation [ 1 ] × ( kernel_size [ 1 ] − 1 ) − 1 stride [ 1 ] + 1 ⌋ W_{out} = \left\lfloor\frac{W_{in} + 2 \times \text{padding}[1] – \text{dilation}[1] \times (\text{kernel_size}[1] – 1) – 1}{\text{stride}[1]} + 1\right\rfloor W o u t ​=⌊stride [1 ]W i n ​+2 ×padding [1 ]−dilation [1 ]×(kernel_size [1 ]−1 )−1 ​+1 ⌋

; Tensorflow 中 Conv2d的padding

Tensorflow 1.x 和 Pytorch 中 Conv2d Padding的区别

可以是 字符串和列表两种形式:

  1. 字符串。 字符串是可选 SAMEVALIDSAME模式下,根据以下公式计算各方向的padding:

P a d H = m a x ( f i l t e r H − ( I n H % s t r i d e H ) , 0 ) Pad_{H} = max(filter_H – (In_H \space \% \space stride_H),0)P a d H ​=m a x (f i l t e r H ​−(I n H ​%s t r i d e H ​),0 )

P a d W = m a x ( f i l t e r W − ( I n W % s t r i d e W ) , 0 ) Pad_{W} = max(filter_W – (In_W \space \% \space stride_W),0)P a d W ​=m a x (f i l t e r W ​−(I n W ​%s t r i d e W ​),0 )

P a d t o p = ⌊ P a d H 2 ⌋ Pad_{top} =\left\lfloor \frac{Pad_{H}}{2} \right\rfloor P a d t o p ​=⌊2 P a d H ​​⌋

P a d b o t t o m = P a d H − P a d t o p Pad_{bottom} = Pad_{H} – Pad_{top}P a d b o t t o m ​=P a d H ​−P a d t o p ​

P a d l e f t = ⌊ P a d W 2 ⌋ Pad_{left} = \left\lfloor \frac{Pad_{W}}{2} \right\rfloor P a d l e f t ​=⌊2 P a d W ​​⌋

P a d r i g h t = P a d W − P a d l e f t Pad_{right} = Pad_{W} – Pad_{left}P a d r i g h t ​=P a d W ​−P a d l e f t ​

不同于pytorch,tensorflow的 SAME模式适用于任何的Stride时的padding,输出的形状为:
O u t H = I n H s t r i d e Out_{H} = \frac{In_{H}}{stride}O u t H ​=s t r i d e I n H ​​

O u t W = I n W s t r i d e Out_{W} = \frac{In_{W}}{stride}O u t W ​=s t r i d e I n W ​​

可以看出,tensorflow的padding不要求是对称的,上下左右的padding大小都可以不一样,而且优先进行右边和下边的padding(因为 SAME模式设计的初衷就是为了应对输出形状无法整除卷积核大小,而导致的最右边或最下边无法卷积的情况),当你不关心padding的方向和大小,只关心输出的形状是不是原来形状的整数倍时,就可以无脑使用 SAMEpadding 模式,tensorflow会自动帮你计算好要padding的大小。

VALID模式下,与pytorch一样,直接不padding 。当输入形状不能整除卷积核大小,滑动窗口滑到最右边发现无法满足卷积条件时,多出来的那部分直接会被舍弃掉,输出的形状为:
O u t H = I n H − f i l t e r H + 1 s t r i d e H Out_{H} = \frac{In_H – filter_H + 1}{stride_{H}}O u t H ​=s t r i d e H ​I n H ​−f i l t e r H ​+1 ​

O u t W = I n W − f i l t e r W + 1 s t r i d e W Out_{W} = \frac{In_W – filter_W + 1}{stride_{W}}O u t W ​=s t r i d e W ​I n W ​−f i l t e r W ​+1 ​

  1. 列表(list)。Tensor格式为默认的“NHWC” 时, padding list的格式为 [[0,0],[pad_top,pad_bottom],[pad_left,pad_right],[0,0]];采用的“NCHW”格式存储的Tensor时,padding list 的格式为 [[0,0],[0,0],[pad_top,pad_bottom],[pad_left,pad_right]]。输出的形状为:

O u t H = I n H + pad t o p + pad b o t t o m − dilation H × ( filter H − 1 ) − 1 stride H Out_{H} = \frac{In_{H} + \text{pad}{top}+\text{pad}{bottom} – \text{dilation}{H} \times (\text{filter}{H}- 1) – 1}{\text{stride}_{H}}O u t H ​=stride H ​I n H ​+pad t o p ​+pad b o t t o m ​−dilation H ​×(filter H ​−1 )−1 ​

O u t W = I n W + pad r i g h t + pad l e f t − dilation W × ( filter W − 1 ) − 1 stride W Out_{W} = \frac{In_{W} + \text{pad}{right}+\text{pad}{left} – \text{dilation}{W} \times (\text{filter}{W}- 1) – 1}{\text{stride}_{W}}O u t W ​=stride W ​I n W ​+pad r i g h t ​+pad l e f t ​−dilation W ​×(filter W ​−1 )−1 ​

注意: Tensorflow 和 Pytorch 中Conv2D 对输出的形状大小的小数部分处理不一样。Pytorch中都是做 截断处理 ,或者说 向下取整 ,例如 输出若为 117.5 则直接取 117;而Tensorflow中对小数的处理采用的则是 向上取整 ,输出若为117.5,则取128

参考

https://www.tensorflow.org/api_docs/python/tf/nn#notes_on_padding_2

https://www.tensorflow.org/api_docs/python/tf/nn/conv2d

https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html

Original: https://blog.csdn.net/daimashiren/article/details/123708688
Author: daimashiren
Title: Tensorflow 1.x 和 Pytorch 中 Conv2d Padding的区别

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/508987/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球