第8章現代の畳み込みニューラルネットワーク
8.7.4. DenseNet Model8.7.4 DenseNetモデル
Next, we will construct a DenseNet model. DenseNet first uses the same single convolutional layer and max-pooling layer as in ResNet.
英語のヒント
次に、DenseNetのモデルを組み立てる。DenseNetはまず、ResNetと同じ一つの畳み込み層と最大プーリング層を使う。
class DenseNet(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))Then, similar to the four modules made up of residual blocks that ResNet uses, DenseNet uses four dense blocks. As with ResNet, we can set the number of convolutional layers used in each dense block. Here, we set it to 4, consistent with the ResNet-18 model in Section 8.6. Furthermore, we set the number of channels (i.e., growth rate) for the convolutional layers in the dense block to 32, so 128 channels will be added to each dense block.
英語のヒント
次に、ResNetが使う残差ブロックからなる四つのモジュールと同じように、DenseNetは四つの密なブロックを使う。ResNetと同じく、各密なブロックで使う畳み込み層の数を設定できる。ここでは、8.6節のResNet-18のモデルにそろえて、4に設定する。さらに、密なブロックの中の畳み込み層のチャネルの数(つまり成長率)を32に設定するので、各密なブロックで128チャネルが加わる。
In ResNet, the height and width are reduced between each module by a residual block with a stride of 2. Here, we use the transition layer to halve the height and width and halve the number of channels. Similar to ResNet, a global pooling layer and a fully connected layer are connected at the end to produce the output.
英語のヒント
ResNetでは、各モジュールのあいだで、ストライド2の残差ブロックによって高さと幅を減らす。ここでは、遷移層を使って高さと幅を半分にし、チャネルの数も半分にする。ResNetと同じように、最後に大域プーリング層と全結合層をつないで出力を生成する。
@d2l.add_to_class(DenseNet)
def __init__(self, num_channels=64, growth_rate=32, arch=(4, 4, 4, 4),
lr=0.1, num_classes=10):
super(DenseNet, self).__init__()
self.save_hyperparameters()
self.net = nn.Sequential(self.b1())
for i, num_convs in enumerate(arch):
self.net.add_module(f'dense_blk{i+1}', DenseBlock(num_convs,
growth_rate))
# The number of output channels in the previous dense block
num_channels += num_convs * growth_rate
# A transition layer that halves the number of channels is added
# between the dense blocks
if i != len(arch) - 1:
num_channels //= 2
self.net.add_module(f'tran_blk{i+1}', transition_block(
num_channels))
self.net.add_module('last', nn.Sequential(
nn.LazyBatchNorm2d(), nn.ReLU(),
nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten(),
nn.LazyLinear(num_classes)))
self.net.apply(d2l.init_cnn)