プリンキピア

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

14.11.1. The Model14.11.1 モデル

Here we describe the basic design of the fully convolutional network model. As shown in Fig. 14.11.1, this model first uses a CNN to extract image features, then transforms the number of channels into the number of classes via a 1×1 convolutional layer, and finally transforms the height and width of the feature maps to those of the input image via the transposed convolution introduced in Section 14.10. As a result, the model output has the same height and width as the input image, where the output channel contains the predicted classes for the input pixel at the same spatial position.
英語のヒント
ここでは、全畳み込みネットワークのモデルの基本的な設計を説明する。図14.11.1に示すように、このモデルは、まずCNNを使って画像の特徴を抽出し、次に1×1の畳み込み層でチャネル数をクラス数に変換し、最後に14.10節で紹介した転置畳み込みで特徴マップの高さと幅を入力画像の高さと幅に変換する。その結果、モデルの出力は入力画像と同じ高さと幅をもち、出力チャネルには、同じ空間の位置にある入力画素について予測したクラスが入る。
Fig. 14.11.1図14.11.1
Fully convolutional network.
英語のヒント
全畳み込みネットワーク。
Below, we use a ResNet-18 model pretrained on the ImageNet dataset to extract image features and denote the model instance as pretrained_net. The last few layers of this model include a global average pooling layer and a fully connected layer: they are not needed in the fully convolutional network.
英語のヒント
下では、ImageNetのデータセットで事前学習したResNet-18モデルを使って画像の特徴を抽出し、そのモデルのインスタンスをpretrained_netで表す。このモデルの最後のいくつかの層には、大域平均プーリング層と全結合層が含まれる。それらは全畳み込みネットワークでは要らない。
pretrained_net = torchvision.models.resnet18(pretrained=True)
list(pretrained_net.children())[-3:]
Downloading: "https://download.pytorch.org/models/resnet18-f37072fd.pth" to /home/ci/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth
100%|██████████| 44.7M/44.7M [00:00<00:00, 56.3MB/s]
[Sequential(
   (0): BasicBlock(
     (conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
     (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
     (relu): ReLU(inplace=True)
     (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
     (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
     (downsample): Sequential(
       (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
       (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
     )
   )
   (1): BasicBlock(
     (conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
     (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
     (relu): ReLU(inplace=True)
     (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
     (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
   )
 ),
 AdaptiveAvgPool2d(output_size=(1, 1)),
 Linear(in_features=512, out_features=1000, bias=True)]
Next, we create the fully convolutional network instance net. It copies all the pretrained layers in the ResNet-18 except for the final global average pooling layer and the fully connected layer that are closest to the output.
英語のヒント
次に、全畳み込みネットワークのインスタンスnetを作る。それは、出力に最も近い最後の大域平均プーリング層と全結合層を除いて、ResNet-18の事前学習したすべての層をコピーする。
net = nn.Sequential(*list(pretrained_net.children())[:-2])
Given an input with height and width of 320 and 480 respectively, the forward propagation of net reduces the input height and width to 1/32 of the original, namely 10 and 15.
英語のヒント
高さと幅がそれぞれ320と480の入力が与えられると、netの順伝播は入力の高さと幅を元の1/32、つまり10と15に減らす。
X = torch.rand(size=(1, 3, 320, 480))
net(X).shape
torch.Size([1, 512, 10, 15])
Next, we use a 1×1 convolutional layer to transform the number of output channels into the number of classes (21) of the Pascal VOC2012 dataset. Finally, we need to increase the height and width of the feature maps by 32 times to change them back to the height and width of the input image. Recall how to calculate the output shape of a convolutional layer in Section 7.3. Since (320−64+16×2+32)/32=10 and (480−64+16×2+32)/32=15, we construct a transposed convolutional layer with stride of 32, setting the height and width of the kernel to 64, the padding to 16. In general, we can see that for stride s, padding s/2 (assuming s/2 is an integer), and the height and width of the kernel 2s, the transposed convolution will increase the height and width of the input by s times.
英語のヒント
次に、1×1の畳み込み層を使って、出力チャネルの数をPascal VOC2012のデータセットのクラス数(21)に変換する。最後に、特徴マップの高さと幅を32倍に増やして、入力画像の高さと幅に戻す必要がある。7.3節で、畳み込み層の出力の形状をどう計算したかを思い出そう。(320−64+16×2+32)/32=10かつ(480−64+16×2+32)/32=15なので、ストライド32の転置畳み込み層を作り、カーネルの高さと幅を64、パディングを16に設定する。一般に、ストライドs、パディングs/2(s/2は整数とする)、カーネルの高さと幅2sについて、転置畳み込みは入力の高さと幅をs倍に増やすことがわかる。
num_classes = 21
net.add_module('final_conv', nn.Conv2d(512, num_classes, kernel_size=1))
net.add_module('transpose_conv', nn.ConvTranspose2d(num_classes, num_classes,
                                    kernel_size=64, padding=16, stride=32))