プリンキピア

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

8.6.5. ResNeXt8.6.5 ResNeXt

One of the challenges one encounters in the design of ResNet is the trade-off between nonlinearity and dimensionality within a given block. That is, we could add more nonlinearity by increasing the number of layers, or by increasing the width of the convolutions. An alternative strategy is to increase the number of channels that can carry information between blocks. Unfortunately, the latter comes with a quadratic penalty since the computational cost of ingesting ci channels and emitting co channels is proportional to 𝒪(ci⋅co) (see our discussion in Section 7.4).
英語のヒント
ResNetの設計で直面する難題の一つは、あるブロックの中での非線形性と次元とのトレードオフである。つまり、層の数を増やすか、畳み込みの幅を広げることで、非線形性を増やせる。もう一つの戦略は、ブロックのあいだで情報を運べるチャネルの数を増やすことである。残念ながら、後者には2乗の代償が伴う。ciチャネルを取り込んでcoチャネルを出す計算の費用は𝒪(ci⋅co)に比例するからである(7.4節での議論を参照)。
We can take some inspiration from the Inception block of Fig. 8.4.1 which has information flowing through the block in separate groups. Applying the idea of multiple independent groups to the ResNet block of Fig. 8.6.3 led to the design of ResNeXt (Xie et al., 2017). Different from the smorgasbord of transformations in Inception, ResNeXt adopts the same transformation in all branches, thus minimizing the need for manual tuning of each branch.
英語のヒント
図8.4.1のInceptionブロックから着想を得られる。そこでは、情報はブロックの中を別々のグループに分かれて流れる。複数の独立したグループという考えを図8.6.3のResNetブロックに当てはめたことが、ResNeXt(Xie et al., 2017)の設計につながった。Inceptionのさまざまな変換の寄せ集めとは違って、ResNeXtはすべての枝で同じ変換を採用し、それによって各枝を手作業で調整する必要を最小にする。
Fig. 8.6.5図8.6.5
The ResNeXt block. The use of grouped convolution with 𝑔 groups is 𝑔 times faster than a dense convolution. It is a bottleneck residual block when the number of intermediate channels 𝑏 is less than 𝑐.
英語のヒント
ResNeXtブロック。𝑔個のグループによるグループ化畳み込みを使うと、密な畳み込みより𝑔倍速い。中間のチャネルの数𝑏が𝑐より少ないとき、これはボトルネック残差ブロックになる。
Breaking up a convolution from ci to co channels into one of g groups of size ci/g generating g outputs of size co/g is called, quite fittingly, a grouped convolution. The computational cost (proportionally) is reduced from 𝒪(ci⋅co) to 𝒪(g⋅(ci/g)⋅(co/g))=𝒪(ci⋅co/g), i.e., it is g times faster. Even better, the number of parameters needed to generate the output is also reduced from a ci×co matrix to g smaller matrices of size (ci/g)×(co/g), again a g times reduction. In what follows we assume that both ci and co are divisible by g.
英語のヒント
ciチャネルからcoチャネルへの畳み込みを、大きさci/gのg個のグループに分け、大きさco/gの出力をg個生成するようにすることを、まさにふさわしい名前だが、グループ化畳み込みと呼ぶ。計算の費用は(比例的に)𝒪(ci⋅co)から𝒪(g⋅(ci/g)⋅(co/g))=𝒪(ci⋅co/g)に減る。つまりg倍速くなる。さらによいことに、出力を生成するのに必要なパラメータの数も、ci×coの行列一つから、大きさ(ci/g)×(co/g)の小さな行列g個へと減り、やはりg分の1になる。以下では、ciとcoがどちらもgで割り切れると仮定する。
The only challenge in this design is that no information is exchanged between the g groups. The ResNeXt block of Fig. 8.6.5 amends this in two ways: the grouped convolution with a 3×3 kernel is sandwiched in between two 1×1 convolutions. The second one serves double duty in changing the number of channels back. The benefit is that we only pay the 𝒪(c⋅b) cost for 1×1 kernels and can make do with an 𝒪(b2/g) cost for 3×3 kernels. Similar to the residual block implementation in Section 8.6.2, the residual connection is replaced (thus generalized) by a 1×1 convolution.
英語のヒント
この設計の唯一の難点は、g個のグループのあいだで情報がまったくやりとりされないことである。図8.6.5のResNeXtブロックは、これを二つの方法で補う。3×3のカーネルによるグループ化畳み込みを、二つの1×1の畳み込みのあいだに挟むのである。2番目の畳み込みは、チャネルの数を元に戻すという二役を兼ねる。利点は、1×1のカーネルについては𝒪(c⋅b)の費用を払うだけでよく、3×3のカーネルについては𝒪(b2/g)の費用で済ませられることである。8.6.2節の残差ブロックの実装と同じように、残差接続は1×1の畳み込みで置き換えられる(したがって一般化される)。
The right-hand figure in Fig. 8.6.5 provides a much more concise summary of the resulting network block. It will also play a major role in the design of generic modern CNNs in Section 8.8. Note that the idea of grouped convolutions dates back to the implementation of AlexNet (Krizhevsky et al., 2012). When distributing the network across two GPUs with limited memory, the implementation treated each GPU as its own channel with no ill effects.
英語のヒント
図8.6.5の右側の図は、こうしてできるネットワークのブロックをずっと簡潔にまとめている。これは、8.8節での汎用的な現代のCNNの設計でも大きな役割を果たす。グループ化畳み込みの考えは、AlexNetの実装(Krizhevsky et al., 2012)にさかのぼることに注意してほしい。メモリの限られた二つのGPUにネットワークを分けたとき、その実装は各GPUをそれ自体のチャネルとして扱ったが、悪影響はなかった。
The following implementation of the ResNeXtBlock class takes as argument groups (g), with bot_channels (b) intermediate (bottleneck) channels. Lastly, when we need to reduce the height and width of the representation, we add a stride of 2 by setting use_1x1conv=True, strides=2.
英語のヒント
以下のResNeXtBlockクラスの実装は、引数としてグループ数groups(g)と、中間の(ボトルネックの)チャネル数bot_channels(b)をとる。最後に、表現の高さと幅を減らす必要があるときは、use_1x1conv=True, strides=2と設定してストライド2を加える。
class ResNeXtBlock(nn.Module):  #@save
    """The ResNeXt block."""
    def __init__(self, num_channels, groups, bot_mul, use_1x1conv=False,
                 strides=1):
        super().__init__()
        bot_channels = int(round(num_channels * bot_mul))
        self.conv1 = nn.LazyConv2d(bot_channels, kernel_size=1, stride=1)
        self.conv2 = nn.LazyConv2d(bot_channels, kernel_size=3,
                                   stride=strides, padding=1,
                                   groups=bot_channels//groups)
        self.conv3 = nn.LazyConv2d(num_channels, kernel_size=1, stride=1)
        self.bn1 = nn.LazyBatchNorm2d()
        self.bn2 = nn.LazyBatchNorm2d()
        self.bn3 = nn.LazyBatchNorm2d()
        if use_1x1conv:
            self.conv4 = nn.LazyConv2d(num_channels, kernel_size=1,
                                       stride=strides)
            self.bn4 = nn.LazyBatchNorm2d()
        else:
            self.conv4 = None

    def forward(self, X):
        Y = F.relu(self.bn1(self.conv1(X)))
        Y = F.relu(self.bn2(self.conv2(Y)))
        Y = self.bn3(self.conv3(Y))
        if self.conv4:
            X = self.bn4(self.conv4(X))
        return F.relu(Y + X)
Its use is entirely analogous to that of the ResNetBlock discussed previously. For instance, when using (use_1x1conv=False, strides=1), the input and output are of the same shape. Alternatively, setting use_1x1conv=True, strides=2 halves the output height and width.
英語のヒント
その使い方は、前に論じたResNetBlockとまったく同じである。たとえば、use_1x1conv=False, strides=1を使うと、入力と出力は同じ形状になる。あるいは、use_1x1conv=True, strides=2と設定すると、出力の高さと幅が半分になる。
blk = ResNeXtBlock(32, 16, 1)
X = torch.randn(4, 32, 96, 96)
blk(X).shape
torch.Size([4, 32, 96, 96])