プリンキピア

第20章敵対的生成ネットワーク

20.2.2. The Generator20.2.2 生成器

The generator needs to map the noise variable 𝐳∈ℝd, a length-d vector, to a RGB image with width and height to be 64×64 . In Section 14.11 we introduced the fully convolutional network that uses transposed convolution layer (refer to Section 14.10) to enlarge input size. The basic block of the generator contains a transposed convolution layer followed by the batch normalization and ReLU activation.
英語のヒント
生成器は、長さdのベクトルであるノイズ変数𝐳∈ℝdを、幅と高さが64×64のRGB画像に写像する必要がある。14.11節では、入力の大きさを拡大するために転置畳み込み層(14.10節を参照)を使う全畳み込みネットワークを紹介した。生成器の基本ブロックは、転置畳み込み層の後にバッチ正規化とReLU活性化が続くものからなる。
class G_block(nn.Module):
    def __init__(self, out_channels, in_channels=3, kernel_size=4, strides=2,
                 padding=1, **kwargs):
        super(G_block, self).__init__(**kwargs)
        self.conv2d_trans = nn.ConvTranspose2d(in_channels, out_channels,
                                kernel_size, strides, padding, bias=False)
        self.batch_norm = nn.BatchNorm2d(out_channels)
        self.activation = nn.ReLU()

    def forward(self, X):
        return self.activation(self.batch_norm(self.conv2d_trans(X)))
In default, the transposed convolution layer uses a kh=kw=4 kernel, a sh=sw=2 strides, and a ph=pw=1 padding. With a input shape of nh′×nw′=16×16, the generator block will double input’s width and height.
英語のヒント
既定では、転置畳み込み層はkh=kw=4のカーネル、sh=sw=2のストライド、ph=pw=1のパディングを使う。入力の形状がnh′×nw′=16×16なら、生成器のブロックは入力の幅と高さを2倍にする。
nh′×nw′=[(nhkh−(nh−1)(kh−sh)−2ph]×[(nwkw−(nw−1)(kw−sw)−2pw]=[(kh+sh(nh−1)−2ph]×[(kw+sw(nw−1)−2pw]=[(4+2×(16−1)−2×1]×[(4+2×(16−1)−2×1]=32×32.(20.2.1)
x = torch.zeros((2, 3, 16, 16))
g_blk = G_block(20)
g_blk(x).shape
torch.Size([2, 20, 32, 32])
If changing the transposed convolution layer to a 4×4 kernel, 1×1 strides and zero padding. With a input size of 1×1, the output will have its width and height increased by 3 respectively.
英語のヒント
転置畳み込み層を、4×4のカーネル、1×1のストライド、パディングなしに変えたとする。入力の大きさが1×1なら、出力の幅と高さはそれぞれ3だけ増える。
x = torch.zeros((2, 3, 1, 1))
g_blk = G_block(20, strides=1, padding=0)
g_blk(x).shape
torch.Size([2, 20, 4, 4])
The generator consists of four basic blocks that increase input’s both width and height from 1 to 32. At the same time, it first projects the latent variable into 64×8 channels, and then halve the channels each time. At last, a transposed convolution layer is used to generate the output. It further doubles the width and height to match the desired 64×64 shape, and reduces the channel size to 3. The tanh activation function is applied to project output values into the (−1,1) range.
英語のヒント
生成器は、入力の幅と高さの両方を1から32に増やす四つの基本ブロックからなる。同時に、まず潜在変数を64×8チャネルに射影し、それから毎回チャネルを半分にする。最後に、転置畳み込み層を使って出力を生成する。これはさらに幅と高さを2倍にして目的の64×64の形状に合わせ、チャネルの大きさを3に減らす。出力の値を(−1,1)の範囲に射影するために、tanh活性化関数を当てはめる。
n_G = 64
net_G = nn.Sequential(
    G_block(in_channels=100, out_channels=n_G*8,
            strides=1, padding=0),                  # Output: (64 * 8, 4, 4)
    G_block(in_channels=n_G*8, out_channels=n_G*4), # Output: (64 * 4, 8, 8)
    G_block(in_channels=n_G*4, out_channels=n_G*2), # Output: (64 * 2, 16, 16)
    G_block(in_channels=n_G*2, out_channels=n_G),   # Output: (64, 32, 32)
    nn.ConvTranspose2d(in_channels=n_G, out_channels=3,
                       kernel_size=4, stride=2, padding=1, bias=False),
    nn.Tanh())  # Output: (3, 64, 64)
Generate a 100 dimensional latent variable to verify the generator’s output shape.
英語のヒント
100次元の潜在変数を生成して、生成器の出力の形状を確かめる。
x = torch.zeros((1, 100, 1, 1))
net_G(x).shape
torch.Size([1, 3, 64, 64])