プリンキピア

第14章コンピュータビジョン

14.10.2. Padding, Strides, and Multiple Channels14.10.2 パディング、ストライド、複数のチャネル

Different from in the regular convolution where padding is applied to input, it is applied to output in the transposed convolution. For example, when specifying the padding number on either side of the height and width as 1, the first and last rows and columns will be removed from the transposed convolution output.
英語のヒント
パディングを入力に当てはめる通常の畳み込みとは違って、転置畳み込みではパディングは出力に当てはめられる。たとえば、高さと幅の両側のパディング数を1と指定すると、転置畳み込みの出力から最初と最後の行と列が取り除かれる。
tconv = nn.ConvTranspose2d(1, 1, kernel_size=2, padding=1, bias=False)
tconv.weight.data = K
tconv(X)
tensor([[[[4.]]]], grad_fn=<ConvolutionBackward0>)
In the transposed convolution, strides are specified for intermediate results (thus output), not for input. Using the same input and kernel tensors from Fig. 14.10.1, changing the stride from 1 to 2 increases both the height and weight of intermediate tensors, hence the output tensor in Fig. 14.10.2.
英語のヒント
転置畳み込みでは、ストライドは入力ではなく途中結果(したがって出力)に対して指定される。図14.10.1と同じ入力とカーネルのテンソルを使い、ストライドを1から2に変えると、途中のテンソルの高さと幅がどちらも増え、したがって図14.10.2の出力テンソルになる。
Fig. 14.10.2図14.10.2
Transposed convolution with a 2×2 kernel with stride of 2. The shaded portions are a portion of an intermediate tensor as well as the input and kernel tensor elements used for the computation.
英語のヒント
ストライド2の2×2のカーネルによる転置畳み込み。網かけの部分は、途中のテンソルの一部と、計算に使う入力とカーネルのテンソルの要素である。
The following code snippet can validate the transposed convolution output for stride of 2 in Fig. 14.10.2.
英語のヒント
次のコード片で、図14.10.2のストライド2の転置畳み込みの出力を確かめられる。
tconv = nn.ConvTranspose2d(1, 1, kernel_size=2, stride=2, bias=False)
tconv.weight.data = K
tconv(X)
tensor([[[[0., 0., 0., 1.],
          [0., 0., 2., 3.],
          [0., 2., 0., 3.],
          [4., 6., 6., 9.]]]], grad_fn=<ConvolutionBackward0>)
For multiple input and output channels, the transposed convolution works in the same way as the regular convolution. Suppose that the input has ci channels, and that the transposed convolution assigns a kh×kw kernel tensor to each input channel. When multiple output channels are specified, we will have a ci×kh×kw kernel for each output channel.
英語のヒント
入力と出力のチャネルが複数ある場合、転置畳み込みは通常の畳み込みと同じように働く。入力にci個のチャネルがあり、転置畳み込みが各入力チャネルにkh×kwのカーネルテンソルを割り当てるとする。出力チャネルを複数指定すると、各出力チャネルについてci×kh×kwのカーネルをもつことになる。
As in all, if we feed 𝖷 into a convolutional layer f to output 𝖸=f(𝖷) and create a transposed convolutional layer g with the same hyperparameters as f except for the number of output channels being the number of channels in 𝖷, then g(Y) will have the same shape as 𝖷. This can be illustrated in the following example.
英語のヒント
まとめると、𝖷を畳み込み層fに入れて𝖸=f(𝖷)を出力し、出力チャネルの数を𝖷のチャネル数にする点を除いてfと同じハイパーパラメータをもつ転置畳み込み層gを作ると、g(Y)は𝖷と同じ形状になる。これは次の例で示せる。
X = torch.rand(size=(1, 10, 16, 16))
conv = nn.Conv2d(10, 20, kernel_size=5, padding=2, stride=3)
tconv = nn.ConvTranspose2d(20, 10, kernel_size=5, padding=2, stride=3)
tconv(conv(X)).shape == X.shape
True