第8章現代の畳み込みニューラルネットワーク
8.6.3. ResNet Model8.6.3 ResNetモデル
The first two layers of ResNet are the same as those of the GoogLeNet we described before: the convolutional layer with 64 output channels and a stride of 2 is followed by the max-pooling layer with a stride of 2. The difference is the batch normalization layer added after each convolutional layer in ResNet.
英語のヒント
ResNetの最初の二つの層は、前に説明したGoogLeNetのものと同じである。出力チャネルが64でストライドが2のの畳み込み層の後に、ストライドが2のの最大プーリング層が続く。違いは、ResNetでは各畳み込み層の後にバッチ正規化層が加えられていることである。
class ResNet(d2l.Classifier):
def b1(self):
return nn.Sequential(
nn.LazyConv2d(64, kernel_size=7, stride=2, padding=3),
nn.LazyBatchNorm2d(), nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1))GoogLeNet uses four modules made up of Inception blocks. However, ResNet uses four modules made up of residual blocks, each of which uses several residual blocks with the same number of output channels. The number of channels in the first module is the same as the number of input channels. Since a max-pooling layer with a stride of 2 has already been used, it is not necessary to reduce the height and width. In the first residual block for each of the subsequent modules, the number of channels is doubled compared with that of the previous module, and the height and width are halved.
英語のヒント
GoogLeNetは、Inceptionブロックからなる四つのモジュールを使う。一方、ResNetは、残差ブロックからなる四つのモジュールを使い、各モジュールは出力チャネルの数が同じ残差ブロックをいくつか使う。最初のモジュールのチャネルの数は、入力チャネルの数と同じである。ストライド2の最大プーリング層がすでに使われているので、高さと幅を減らす必要はない。続く各モジュールの最初の残差ブロックでは、チャネルの数を前のモジュールの2倍にし、高さと幅を半分にする。
@d2l.add_to_class(ResNet)
def block(self, num_residuals, num_channels, first_block=False):
blk = []
for i in range(num_residuals):
if i == 0 and not first_block:
blk.append(Residual(num_channels, use_1x1conv=True, strides=2))
else:
blk.append(Residual(num_channels))
return nn.Sequential(*blk)Then, we add all the modules to ResNet. Here, two residual blocks are used for each module. Lastly, just like GoogLeNet, we add a global average pooling layer, followed by the fully connected layer output.
英語のヒント
次に、すべてのモジュールをResNetに加える。ここでは、各モジュールに二つの残差ブロックを使う。最後に、GoogLeNetと同じように、大域平均プーリング層を加え、その後に全結合層の出力を続ける。
@d2l.add_to_class(ResNet)
def __init__(self, arch, lr=0.1, num_classes=10):
super(ResNet, self).__init__()
self.save_hyperparameters()
self.net = nn.Sequential(self.b1())
for i, b in enumerate(arch):
self.net.add_module(f'b{i+2}', self.block(*b, first_block=(i==0)))
self.net.add_module('last', nn.Sequential(
nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten(),
nn.LazyLinear(num_classes)))
self.net.apply(d2l.init_cnn)There are four convolutional layers in each module (excluding the convolutional layer). Together with the first convolutional layer and the final fully connected layer, there are 18 layers in total. Therefore, this model is commonly known as ResNet-18. By configuring different numbers of channels and residual blocks in the module, we can create different ResNet models, such as the deeper 152-layer ResNet-152. Although the main architecture of ResNet is similar to that of GoogLeNet, ResNet’s structure is simpler and easier to modify. All these factors have resulted in the rapid and widespread use of ResNet. Fig. 8.6.4 depicts the full ResNet-18.
英語のヒント
各モジュールには(の畳み込み層を除いて)四つの畳み込み層がある。最初のの畳み込み層と最後の全結合層をあわせると、全部で18層になる。そのため、このモデルはふつうResNet-18として知られている。モジュール内のチャネルと残差ブロックの数を変えて構成すれば、もっと深い152層のResNet-152のような、さまざまなResNetのモデルを作れる。ResNetの主な構造はGoogLeNetのものと似ているが、ResNetの構造のほうが単純で、変更しやすい。こうした要因がすべて、ResNetの急速で広範な普及につながった。図8.6.4は、ResNet-18の全体を描いている。
Fig. 8.6.4図8.6.4
The ResNet-18 architecture.
英語のヒント
ResNet-18の構造。
Before training ResNet, let’s observe how the input shape changes across different modules in ResNet. As in all the previous architectures, the resolution decreases while the number of channels increases up until the point where a global average pooling layer aggregates all features.
英語のヒント
ResNetを訓練する前に、ResNetのさまざまなモジュールのあいだで入力の形状がどう変わるかを観察しよう。前のすべての構造と同じように、大域平均プーリング層がすべての特徴量を集約するところまで、解像度は下がり、チャネルの数は増える。
class ResNet18(ResNet):
def __init__(self, lr=0.1, num_classes=10):
super().__init__(((2, 64), (2, 128), (2, 256), (2, 512)),
lr, num_classes)
ResNet18().layer_summary((1, 1, 96, 96))Sequential output shape: torch.Size([1, 64, 24, 24])
Sequential output shape: torch.Size([1, 64, 24, 24])
Sequential output shape: torch.Size([1, 128, 12, 12])
Sequential output shape: torch.Size([1, 256, 6, 6])
Sequential output shape: torch.Size([1, 512, 3, 3])
Sequential output shape: torch.Size([1, 10])