プリンキピア

第8章現代の畳み込みニューラルネットワーク

8.4.2. GoogLeNet Model8.4.2 GoogLeNetモデル

As shown in Fig. 8.4.2, GoogLeNet uses a stack of a total of 9 inception blocks, arranged into three groups with max-pooling in between, and global average pooling in its head to generate its estimates. Max-pooling between inception blocks reduces the dimensionality. At its stem, the first module is similar to AlexNet and LeNet.
英語のヒント
図8.4.2に示すように、GoogLeNetは合計9個のInceptionブロックを積み重ねて使う。ブロックはあいだに最大プーリングを挟んで三つのグループにまとめられ、ヘッドでは大域平均プーリングを使って推定値を生成する。Inceptionブロックのあいだの最大プーリングは次元を下げる。ステムでは、最初のモジュールはAlexNetやLeNetと似ている。
Fig. 8.4.2図8.4.2
The GoogLeNet architecture.
英語のヒント
GoogLeNetの構造。
We can now implement GoogLeNet piece by piece. Let’s begin with the stem. The first module uses a 64-channel 7×7 convolutional layer.
英語のヒント
これでGoogLeNetを一つずつ実装できる。ステムから始めよう。最初のモジュールは、64チャネルの7×7の畳み込み層を使う。
class GoogleNet(d2l.Classifier):
    def b1(self):
        return nn.Sequential(
            nn.LazyConv2d(64, kernel_size=7, stride=2, padding=3),
            nn.ReLU(), nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
The second module uses two convolutional layers: first, a 64-channel 1×1 convolutional layer, followed by a 3×3 convolutional layer that triples the number of channels. This corresponds to the second branch in the Inception block and concludes the design of the body. At this point we have 192 channels.
英語のヒント
2番目のモジュールは二つの畳み込み層を使う。まず64チャネルの1×1の畳み込み層があり、その後に、チャネルの数を3倍にする3×3の畳み込み層が続く。これはInceptionブロックの2番目の枝に対応し、ボディの設計を締めくくる。この時点で、チャネルは192になる。
@d2l.add_to_class(GoogleNet)
def b2(self):
    return nn.Sequential(
        nn.LazyConv2d(64, kernel_size=1), nn.ReLU(),
        nn.LazyConv2d(192, kernel_size=3, padding=1), nn.ReLU(),
        nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
The third module connects two complete Inception blocks in series. The number of output channels of the first Inception block is 64+128+32+32=256. This amounts to a ratio of the number of output channels among the four branches of 2:4:1:1. To achieve this, we first reduce the input dimensions by 12 and by 112 in the second and third branch respectively to arrive at 96=192/2 and 16=192/12 channels respectively.
英語のヒント
3番目のモジュールは、二つの完全なInceptionブロックを直列につなぐ。最初のInceptionブロックの出力チャネルの数は64+128+32+32=256である。これは、四つの枝のあいだの出力チャネルの数の比が2:4:1:1になることにあたる。これを実現するため、まず2番目と3番目の枝で、入力の次元をそれぞれ12と112に減らし、それぞれ96=192/2と16=192/12チャネルにする。
The number of output channels of the second Inception block is increased to 128+192+96+64=480, yielding a ratio of 128:192:96:64=4:6:3:2. As before, we need to reduce the number of intermediate dimensions in the second and third channel. A scale of 12 and 18 respectively suffices, yielding 128 and 32 channels respectively. This is captured by the arguments of the following Inception block constructors.
英語のヒント
2番目のInceptionブロックの出力チャネルの数は128+192+96+64=480に増やされ、比は128:192:96:64=4:6:3:2になる。前と同じく、2番目と3番目のチャネルで中間の次元の数を減らす必要がある。それぞれ12と18の縮尺で十分であり、それぞれ128と32チャネルになる。これは、以下のInceptionブロックのコンストラクタの引数に反映されている。
@d2l.add_to_class(GoogleNet)
def b3(self):
    return nn.Sequential(Inception(64, (96, 128), (16, 32), 32),
                         Inception(128, (128, 192), (32, 96), 64),
                         nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
The fourth module is more complicated. It connects five Inception blocks in series, and they have 192+208+48+64=512, 160+224+64+64=512, 128+256+64+64=512, 112+288+64+64=528, and 256+320+128+128=832 output channels, respectively. The number of channels assigned to these branches is similar to that in the third module: the second branch with the 3×3 convolutional layer outputs the largest number of channels, followed by the first branch with only the 1×1 convolutional layer, the third branch with the 5×5 convolutional layer, and the fourth branch with the 3×3 max-pooling layer. The second and third branches will first reduce the number of channels according to the ratio. These ratios are slightly different in different Inception blocks.
英語のヒント
4番目のモジュールはもっと複雑である。五つのInceptionブロックを直列につなぎ、それらの出力チャネルはそれぞれ192+208+48+64=512、160+224+64+64=512、128+256+64+64=512、112+288+64+64=528、256+320+128+128=832である。これらの枝に割り当てるチャネルの数は、3番目のモジュールのものと似ている。3×3の畳み込み層をもつ2番目の枝が最も多くのチャネルを出力し、1×1の畳み込み層だけをもつ1番目の枝、5×5の畳み込み層をもつ3番目の枝、3×3の最大プーリング層をもつ4番目の枝が続く。2番目と3番目の枝は、まず比に従ってチャネルの数を減らす。これらの比は、Inceptionブロックごとに少しずつ異なる。
@d2l.add_to_class(GoogleNet)
def b4(self):
    return nn.Sequential(Inception(192, (96, 208), (16, 48), 64),
                         Inception(160, (112, 224), (24, 64), 64),
                         Inception(128, (128, 256), (24, 64), 64),
                         Inception(112, (144, 288), (32, 64), 64),
                         Inception(256, (160, 320), (32, 128), 128),
                         nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
The fifth module has two Inception blocks with 256+320+128+128=832 and 384+384+128+128=1024 output channels. The number of channels assigned to each branch is the same as that in the third and fourth modules, but differs in specific values. It should be noted that the fifth block is followed by the output layer. This block uses the global average pooling layer to change the height and width of each channel to 1, just as in NiN. Finally, we turn the output into a two-dimensional array followed by a fully connected layer whose number of outputs is the number of label classes.
英語のヒント
5番目のモジュールには、出力チャネルが256+320+128+128=832と384+384+128+128=1024の二つのInceptionブロックがある。各枝へのチャネルの割り当て方は3番目と4番目のモジュールと同じだが、具体的な値が異なる。5番目のブロックの後には出力層が続くことに注意すべきである。このブロックは、NiNと同じように、大域平均プーリング層を使って各チャネルの高さと幅を1にする。最後に、出力を2次元の配列に変え、その後に、出力の数がラベルのクラスの数である全結合層を続ける。
@d2l.add_to_class(GoogleNet)
def b5(self):
    return nn.Sequential(Inception(256, (160, 320), (32, 128), 128),
                         Inception(384, (192, 384), (48, 128), 128),
                         nn.AdaptiveAvgPool2d((1,1)), nn.Flatten())
Now that we defined all blocks b1 through b5, it is just a matter of assembling them all into a full network.
英語のヒント
ブロックb1からb5をすべて定義したので、あとはそれらをすべて組み立てて一つのネットワークにするだけである。
@d2l.add_to_class(GoogleNet)
def __init__(self, lr=0.1, num_classes=10):
    super(GoogleNet, self).__init__()
    self.save_hyperparameters()
    self.net = nn.Sequential(self.b1(), self.b2(), self.b3(), self.b4(),
                             self.b5(), nn.LazyLinear(num_classes))
    self.net.apply(d2l.init_cnn)
The GoogLeNet model is computationally complex. Note the large number of relatively arbitrary hyperparameters in terms of the number of channels chosen, the number of blocks prior to dimensionality reduction, the relative partitioning of capacity across channels, etc. Much of it is due to the fact that at the time when GoogLeNet was introduced, automatic tools for network definition or design exploration were not yet available. For instance, by now we take it for granted that a competent deep learning framework is capable of inferring dimensionalities of input tensors automatically. At the time, many such configurations had to be specified explicitly by the experimenter, thus often slowing down active experimentation. Moreover, the tools needed for automatic exploration were still in flux and initial experiments largely amounted to costly brute-force exploration, genetic algorithms, and similar strategies.
英語のヒント
GoogLeNetのモデルは、計算の面で複雑である。選んだチャネルの数、次元を下げる前のブロックの数、チャネルのあいだでの容量の相対的な配分などの点で、比較的恣意的なハイパーパラメータが多いことに注意してほしい。その多くは、GoogLeNetが紹介された当時、ネットワークの定義や設計の探索のための自動の道具がまだなかったことによる。たとえば、今では、まともな深層学習のフレームワークなら入力テンソルの次元を自動で推論できるのは当然のことだと思っている。当時は、そうした設定の多くを実験者がはっきり指定しなければならず、そのせいで活発な実験がしばしば遅くなった。さらに、自動の探索に必要な道具はまだ流動的で、初期の実験はおおむね、費用のかかる力ずくの探索、遺伝的アルゴリズム、そしてそれに似た戦略にとどまっていた。
For now the only modification we will carry out is to reduce the input height and width from 224 to 96 to have a reasonable training time on Fashion-MNIST. This simplifies the computation. Let’s have a look at the changes in the shape of the output between the various modules.
英語のヒント
ここで行う変更は、Fashion-MNISTでの訓練時間をほどよい長さにするため、入力の高さと幅を224から96に減らすことだけである。これで計算が単純になる。さまざまなモジュールのあいだで出力の形状がどう変わるかを見てみよう。
model = GoogleNet().layer_summary((1, 1, 96, 96))
Sequential output shape:     torch.Size([1, 64, 24, 24])
Sequential output shape:     torch.Size([1, 192, 12, 12])
Sequential output shape:     torch.Size([1, 480, 6, 6])
Sequential output shape:     torch.Size([1, 832, 3, 3])
Sequential output shape:     torch.Size([1, 1024])
Linear output shape:         torch.Size([1, 10])