第8章現代の畳み込みニューラルネットワーク
8.7.2. Dense Blocks8.7.2 密なブロック
DenseNet uses the modified “batch normalization, activation, and convolution” structure of ResNet (see the exercise in Section 8.6). First, we implement this convolution block structure.
英語のヒント
DenseNetは、ResNetの「バッチ正規化、活性化、畳み込み」という改良版の構造を使う(8.6節の練習問題を参照)。まず、この畳み込みブロックの構造を実装する。
def conv_block(num_channels):
return nn.Sequential(
nn.LazyBatchNorm2d(), nn.ReLU(),
nn.LazyConv2d(num_channels, kernel_size=3, padding=1))A dense block consists of multiple convolution blocks, each using the same number of output channels. In the forward propagation, however, we concatenate the input and output of each convolution block on the channel dimension. Lazy evaluation allows us to adjust the dimensionality automatically.
英語のヒント
密なブロックは複数の畳み込みブロックからなり、それぞれが同じ数の出力チャネルを使う。しかし、順伝播では、各畳み込みブロックの入力と出力をチャネルの次元で連結する。遅延評価のおかげで、次元を自動で調整できる。
class DenseBlock(nn.Module):
def __init__(self, num_convs, num_channels):
super(DenseBlock, self).__init__()
layer = []
for i in range(num_convs):
layer.append(conv_block(num_channels))
self.net = nn.Sequential(*layer)
def forward(self, X):
for blk in self.net:
Y = blk(X)
# Concatenate input and output of each block along the channels
X = torch.cat((X, Y), dim=1)
return XIn the following example, we define a
DenseBlock instance with two convolution blocks of 10 output channels. When using an input with three channels, we will get an output with channels. The number of convolution block channels controls the growth in the number of output channels relative to the number of input channels. This is also referred to as the growth rate.英語のヒント
次の例では、出力チャネルが10の畳み込みブロックを二つもつ
DenseBlockのインスタンスを定義する。3チャネルの入力を使うと、チャネルの出力が得られる。畳み込みブロックのチャネル数は、入力チャネルの数に対する出力チャネルの数の増え方を制御する。これは成長率とも呼ばれる。blk = DenseBlock(2, 10)
X = torch.randn(4, 3, 8, 8)
Y = blk(X)
Y.shapetorch.Size([4, 23, 8, 8])