PyTorch卷积

1. 2D 卷积

1
class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
  • 输入尺度:(N,Cin,Hin,Win) (N,C_{in},H_{in},W_{in})
  • 输出尺度: (N,Cout,Hout,Wout) (N,C_{out},H_{out},W_{out})

Hout=floor((Hin+2×padding[0]dilation[0]×(kernerl_size[0]1)1)/stride[0]+1)\begin{array}{c} H_{out} = floor((H_{in} + 2 \times padding[0] - dilation[0] \times (kernerl\_size[0]-1)-1) / stride[0] + 1) \end{array}

Wout=floor((Win+2×padding[1]dilation[1]×(kernerl_size[1]1)1)/stride[1]+1)\begin{array}{c} W_{out} = floor((W_{in} + 2 \times padding[1] - dilation[1] \times (kernerl\_size[1] - 1) - 1) / stride[1] + 1) \end{array}

2. 2D 反卷积

1
class torch.nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True)
  • 输入尺度:(N,Cin,Hin,Win) (N,C_{in},H_{in},W_{in})
  • 输出尺度: (N,Cout,Hout,Wout) (N,C_{out},H_{out},W_{out})

Hout=(Hin1)×stride[0]2×padding[0]+kernel_size[0]+output_padding[0]\begin{array}{c} H_{out} = (H_{in} - 1) \times stride[0] - 2 \times padding[0] + kernel\_size[0] + output\_padding[0] \end{array}

Wout=(Win1)×stride[1]2×padding[1]+kernel_size[1]+output_padding[1]\begin{array}{c} W_{out} = (W_{in} - 1) \times stride[1] - 2 \times padding[1] + kernel\_size[1] + output\_padding[1] \end{array}

【注】Conv2d 函数中的 stridestride 为卷积核的移动步长,paddingpadding 为输入张量的填充长度;而 ConvTranspose2d 函数中的 stridestride 为输入张量元素的间隔,paddingpadding 为输入张量的填充长度减小值。

在 ConvTranspose2d 中,stridestride 后的张量步长为:

o=(i1)×stride+1\begin{array}{c} o = (i - 1) \times stride + 1 \end{array}

此外,ConvTranspose2d 默认对输入张量做全填充,即 kernel_size/2 kernel\_size / 2 函数中的 paddingpadding 则是用来指定填充步长的减小值,即最终填充步长为

kernel_size/2padding\begin{array}{c} kernel\_size / 2 - padding \end{array}

output_paddingoutput\_padding 是在转置卷积后,给输出张量增加的 0 填充步长。