第8章現代の畳み込みニューラルネットワーク
8.8.1. The AnyNet Design Space8.8.1 AnyNetの設計空間
The description below closely follows the reasoning in Radosavovic et al. (2020) with some abbreviations to make it fit in the scope of the book. To begin, we need a template for the family of networks to explore. One of the commonalities of the designs in this chapter is that the networks consist of a stem, a body and a head. The stem performs initial image processing, often through convolutions with a larger window size. The body consists of multiple blocks, carrying out the bulk of the transformations needed to go from raw images to object representations. Lastly, the head converts this into the desired outputs, such as via a softmax regressor for multiclass classification. The body, in turn, consists of multiple stages, operating on the image at decreasing resolutions. In fact, both the stem and each subsequent stage quarter the spatial resolution. Lastly, each stage consists of one or more blocks. This pattern is common to all networks, from VGG to ResNeXt. Indeed, for the design of generic AnyNet networks, Radosavovic et al. (2020) used the ResNeXt block of Fig. 8.6.5.
英語のヒント
以下の説明は、この本の範囲に収まるようにいくらか省略しつつ、Radosavovic et al. (2020)の論理に忠実に従う。まず、探索するネットワークの一族のためのひな形が必要である。この章の設計に共通する点の一つは、ネットワークがステム、ボディ、ヘッドからなることである。ステムは、しばしば大きめの窓の畳み込みを通じて、最初の画像処理を行う。ボディは複数のブロックからなり、生の画像から物体の表現へと至るのに必要な変換の大部分を担う。最後に、ヘッドはこれを、たとえば多クラス分類のためのソフトマックス回帰器を通じて、望みの出力に変換する。ボディはさらに複数の段からなり、段が進むにつれて解像度の下がる画像に働く。実際、ステムと続く各段はどちらも空間の解像度を4分の1にする。最後に、各段は一つ以上のブロックからなる。このパターンは、VGGからResNeXtまで、すべてのネットワークに共通している。実際、汎用的なAnyNetのネットワークの設計に、Radosavovic et al. (2020)は図8.6.5のResNeXtブロックを使った。
Fig. 8.8.1図8.8.1
The AnyNet design space. The numbers along each arrow indicate the number of channels and the resolution of the images at that point. From left to right: generic network structure composed of stem, body, and head; body composed of four stages; detailed structure of a stage; two alternative structures for blocks, one without downsampling and one that halves the resolution in each dimension. Design choices include depth , the number of output channels , the number of groups , and bottleneck ratio for any stage .
英語のヒント
AnyNetの設計空間。各矢印に沿った数は、その時点での画像のチャネル数と解像度を表す。左から右へ:ステム、ボディ、ヘッドからなる汎用的なネットワークの構造。四つの段からなるボディ。段の詳しい構造。ブロックの二つの別の構造で、一つはダウンサンプリングなし、もう一つは各次元で解像度を半分にするもの。設計上の選択には、各段についての深さ、出力チャネルの数、グループの数、ボトルネック比が含まれる。
Let’s review the structure outlined in Fig. 8.8.1 in detail. As mentioned, an AnyNet consists of a stem, body, and head. The stem takes as its input RGB images (3 channels), using a convolution with a stride of , followed by a batch norm, to halve the resolution from to . Moreover, it generates channels that serve as input to the body.
英語のヒント
図8.8.1に示した構造を詳しく見ていこう。前に述べたように、AnyNetはステム、ボディ、ヘッドからなる。ステムは入力としてRGB画像(3チャネル)をとり、ストライドのの畳み込みを使い、その後にバッチ正規化を続けて、解像度をからに半分にする。さらに、ボディへの入力となるチャネルを生成する。
Since the network is designed to work well with ImageNet images of shape , the body serves to reduce this to through 4 stages (recall that ), each with an eventual stride of . Lastly, the head employs an entirely standard design via global average pooling, similar to NiN (Section 8.3), followed by a fully connected layer to emit an -dimensional vector for -class classification.
英語のヒント
このネットワークは形状のImageNetの画像でうまく働くように設計されているので、ボディは四つの段を通じてこれをに縮める(を思い出そう)。各段は最終的にストライドをもつ。最後に、ヘッドは、NiN(8.3節)と同じように大域平均プーリングを使い、その後に全結合層を続けてクラス分類のための次元のベクトルを出す、まったく標準的な設計を使う。
Most of the relevant design decisions are inherent to the body of the network. It proceeds in stages, where each stage is composed of the same type of ResNeXt blocks as we discussed in Section 8.6.5. The design there is again entirely generic: we begin with a block that halves the resolution by using a stride of (the rightmost in Fig. 8.8.1). To match this, the residual branch of the ResNeXt block needs to pass through a convolution. This block is followed by a variable number of additional ResNeXt blocks that leave both resolution and the number of channels unchanged. Note that a common design practice is to add a slight bottleneck in the design of convolutional blocks. As such, with bottleneck ratio we afford some number of channels, , within each block for stage (as the experiments show, this is not really effective and should be skipped). Lastly, since we are dealing with ResNeXt blocks, we also need to pick the number of groups for grouped convolutions at stage .
英語のヒント
関係する設計判断のほとんどは、ネットワークのボディに本来備わっている。ボディは段を追って進み、各段は8.6.5節で論じたのと同じ種類のResNeXtブロックからなる。そこでの設計も、やはりまったく汎用的である。まずストライドを使って解像度を半分にするブロックから始める(図8.8.1の一番右)。これに合わせるため、ResNeXtブロックの残差の枝はの畳み込みを通る必要がある。このブロックの後には、解像度とチャネルの数をどちらも変えない追加のResNeXtブロックが、可変の数だけ続く。畳み込みブロックの設計では、わずかなボトルネックを加えるのがよくある設計のやり方であることに注意してほしい。そのため、ボトルネック比で、段の各ブロックの中にいくらかのチャネル数を割り当てる(実験が示すように、これは実はあまり効果的でなく、省くべきである)。最後に、ResNeXtブロックを扱っているので、段でのグループ化畳み込みのためのグループの数も選ぶ必要がある。
This seemingly generic design space provides us nonetheless with many parameters: we can set the block width (number of channels) , the depth (number of blocks) per stage , the bottleneck ratios , and the group widths (numbers of groups) . In total this adds up to 17 parameters, resulting in an unreasonably large number of configurations that would warrant exploring. We need some tools to reduce this huge design space effectively. This is where the conceptual beauty of design spaces comes in. Before we do so, let’s implement the generic design first.
英語のヒント
この一見汎用的な設計空間でも、パラメータは多い。ブロックの幅(チャネルの数)、段ごとの深さ(ブロックの数)、ボトルネック比、グループの幅(グループの数)を設定できる。全部で17個のパラメータになり、探索に値する構成の数は不当に多くなる。この巨大な設計空間を効果的に縮める道具が必要である。ここで設計空間という概念の美しさが生きてくる。その前に、まず汎用的な設計を実装しよう。
class AnyNet(d2l.Classifier):
def stem(self, num_channels):
return nn.Sequential(
nn.LazyConv2d(num_channels, kernel_size=3, stride=2, padding=1),
nn.LazyBatchNorm2d(), nn.ReLU())Each stage consists of
depth ResNeXt blocks, where num_channels specifies the block width. Note that the first block halves the height and width of input images.英語のヒント
各段は
depth個のResNeXtブロックからなり、num_channelsがブロックの幅を指定する。最初のブロックは入力画像の高さと幅を半分にすることに注意してほしい。@d2l.add_to_class(AnyNet)
def stage(self, depth, num_channels, groups, bot_mul):
blk = []
for i in range(depth):
if i == 0:
blk.append(d2l.ResNeXtBlock(num_channels, groups, bot_mul,
use_1x1conv=True, strides=2))
else:
blk.append(d2l.ResNeXtBlock(num_channels, groups, bot_mul))
return nn.Sequential(*blk)Putting the network stem, body, and head together, we complete the implementation of AnyNet.
英語のヒント
ネットワークのステム、ボディ、ヘッドを組み合わせて、AnyNetの実装を完成させる。
@d2l.add_to_class(AnyNet)
def __init__(self, arch, stem_channels, lr=0.1, num_classes=10):
super(AnyNet, self).__init__()
self.save_hyperparameters()
self.net = nn.Sequential(self.stem(stem_channels))
for i, s in enumerate(arch):
self.net.add_module(f'stage{i+1}', self.stage(*s))
self.net.add_module('head', nn.Sequential(
nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten(),
nn.LazyLinear(num_classes)))
self.net.apply(d2l.init_cnn)