プリンキピア

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

14.11.2. Initializing Transposed Convolutional Layers14.11.2 転置畳み込み層の初期化

We already know that transposed convolutional layers can increase the height and width of feature maps. In image processing, we may need to scale up an image, i.e., upsampling. Bilinear interpolation is one of the commonly used upsampling techniques. It is also often used for initializing transposed convolutional layers.
英語のヒント
転置畳み込み層が特徴マップの高さと幅を増やせることは、すでにわかっている。画像処理では、画像を拡大する、つまりアップサンプリングする必要があることがある。双線形補間は、よく使われるアップサンプリングの技法の一つである。それは、転置畳み込み層の初期化にもよく使われる。
To explain bilinear interpolation, say that given an input image we want to calculate each pixel of the upsampled output image. In order to calculate the pixel of the output image at coordinate (x,y), first map (x,y) to coordinate (x′,y′) on the input image, for example, according to the ratio of the input size to the output size. Note that the mapped x′ and y′ are real numbers. Then, find the four pixels closest to coordinate (x′,y′) on the input image. Finally, the pixel of the output image at coordinate (x,y) is calculated based on these four closest pixels on the input image and their relative distance from (x′,y′).
英語のヒント
双線形補間を説明するため、入力画像が与えられたとき、アップサンプリングした出力画像の各画素を計算したいとしよう。出力画像の座標(x,y)の画素を計算するには、まず、たとえば入力の大きさと出力の大きさの比に従って、(x,y)を入力画像上の座標(x′,y′)に対応させる。対応させたx′とy′は実数であることに注意してほしい。次に、入力画像上で座標(x′,y′)に最も近い四つの画素を見つける。最後に、出力画像の座標(x,y)の画素は、入力画像上のこれら四つの最も近い画素と、それらの(x′,y′)からの相対的な距離にもとづいて計算される。
Upsampling of bilinear interpolation can be implemented by the transposed convolutional layer with the kernel constructed by the following bilinear_kernel function. Due to space limitations, we only provide the implementation of the bilinear_kernel function below without discussions on its algorithm design.
英語のヒント
双線形補間のアップサンプリングは、次のbilinear_kernel関数で作ったカーネルをもつ転置畳み込み層で実装できる。紙幅の都合で、下ではbilinear_kernel関数の実装だけを示し、そのアルゴリズムの設計は論じない。
def bilinear_kernel(in_channels, out_channels, kernel_size):
    factor = (kernel_size + 1) // 2
    if kernel_size % 2 == 1:
        center = factor - 1
    else:
        center = factor - 0.5
    og = (torch.arange(kernel_size).reshape(-1, 1),
          torch.arange(kernel_size).reshape(1, -1))
    filt = (1 - torch.abs(og[0] - center) / factor) * \
           (1 - torch.abs(og[1] - center) / factor)
    weight = torch.zeros((in_channels, out_channels,
                          kernel_size, kernel_size))
    weight[range(in_channels), range(out_channels), :, :] = filt
    return weight
Let’s experiment with upsampling of bilinear interpolation that is implemented by a transposed convolutional layer. We construct a transposed convolutional layer that doubles the height and weight, and initialize its kernel with the bilinear_kernel function.
英語のヒント
転置畳み込み層で実装した双線形補間のアップサンプリングを試してみよう。高さと幅を2倍にする転置畳み込み層を作り、そのカーネルをbilinear_kernel関数で初期化する。
conv_trans = nn.ConvTranspose2d(3, 3, kernel_size=4, padding=1, stride=2,
                                bias=False)
conv_trans.weight.data.copy_(bilinear_kernel(3, 3, 4));
Read the image X and assign the upsampling output to Y. In order to print the image, we need to adjust the position of the channel dimension.
英語のヒント
画像Xを読み、アップサンプリングの出力をYに代入する。画像を出力するため、チャネルの次元の位置を調整する必要がある。
img = torchvision.transforms.ToTensor()(d2l.Image.open('../img/catdog.jpg'))
X = img.unsqueeze(0)
Y = conv_trans(X)
out_img = Y[0].permute(1, 2, 0).detach()
As we can see, the transposed convolutional layer increases both the height and width of the image by a factor of two. Except for the different scales in coordinates, the image scaled up by bilinear interpolation and the original image printed in Section 14.3 look the same.
英語のヒント
見てのとおり、転置畳み込み層は画像の高さと幅をどちらも2倍にする。座標の縮尺が違うことを除けば、双線形補間で拡大した画像と、14.3節で出力した元の画像は同じに見える。
d2l.set_figsize()
print('input image shape:', img.permute(1, 2, 0).shape)
d2l.plt.imshow(img.permute(1, 2, 0));
print('output image shape:', out_img.shape)
d2l.plt.imshow(out_img);
input image shape: torch.Size([561, 728, 3])
output image shape: torch.Size([1122, 1456, 3])
In a fully convolutional network, we initialize the transposed convolutional layer with upsampling of bilinear interpolation. For the 1×1 convolutional layer, we use Xavier initialization.
英語のヒント
全畳み込みネットワークでは、転置畳み込み層を双線形補間のアップサンプリングで初期化する。1×1の畳み込み層には、Xavierの初期化を使う。
W = bilinear_kernel(num_classes, num_classes, 64)
net.transpose_conv.weight.data.copy_(W);