第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.
英語のヒント
Fig. 14.10.2図14.10.2
Transposed convolution with a 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ののカーネルによる転置畳み込み。網かけの部分は、途中のテンソルの一部と、計算に使う入力とカーネルのテンソルの要素である。
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 channels, and that the transposed convolution assigns a kernel tensor to each input channel. When multiple output channels are specified, we will have a kernel for each output channel.
英語のヒント
入力と出力のチャネルが複数ある場合、転置畳み込みは通常の畳み込みと同じように働く。入力に個のチャネルがあり、転置畳み込みが各入力チャネルにのカーネルテンソルを割り当てるとする。出力チャネルを複数指定すると、各出力チャネルについてのカーネルをもつことになる。
As in all, if we feed into a convolutional layer to output and create a transposed convolutional layer with the same hyperparameters as except for the number of output channels being the number of channels in , then will have the same shape as . This can be illustrated in the following example.
英語のヒント
まとめると、を畳み込み層に入れてを出力し、出力チャネルの数をのチャネル数にする点を除いてと同じハイパーパラメータをもつ転置畳み込み層を作ると、はと同じ形状になる。これは次の例で示せる。
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.shapeTrue