| 153 | """ |
| 154 | class Discriminator(nn.Module): |
| 155 | def __init__(self, z_dim, in_channels, img_channels=3): |
| 156 | super(Discriminator, self).__init__() |
| 157 | self.prog_blocks, self.rgb_layers = nn.ModuleList([]), nn.ModuleList([]) |
| 158 | self.leaky = nn.LeakyReLU(0.2) |
| 159 | |
| 160 | # here we work back ways from factors because the discriminator |
| 161 | # should be mirrored from the generator. So the first prog_block and |
| 162 | # rgb layer we append will work for input size 1024x1024, then 512->256-> etc |
| 163 | for i in range(len(factors) - 1, 0, -1): |
| 164 | conv_in = int(in_channels * factors[i]) |
| 165 | conv_out = int(in_channels * factors[i - 1]) |
| 166 | self.prog_blocks.append(ConvBlock(conv_in, conv_out, use_pixelnorm=False)) |
| 167 | self.rgb_layers.append( |
| 168 | WSConv2d(img_channels, conv_in, kernel_size=1, stride=1, padding=0) |
| 169 | ) |
| 170 | |
| 171 | # perhaps confusing name "initial_rgb" this is just the RGB layer for 4x4 input size |
| 172 | # did this to "mirror" the generator initial_rgb |
| 173 | self.initial_rgb = WSConv2d( |
| 174 | img_channels, in_channels, kernel_size=1, stride=1, padding=0 |
| 175 | ) |
| 176 | self.rgb_layers.append(self.initial_rgb) |
| 177 | self.avg_pool = nn.AvgPool2d( |
| 178 | kernel_size=2, stride=2 |
| 179 | ) # down sampling using avg pool |
| 180 | |
| 181 | # this is the block for 4x4 input size |
| 182 | self.final_block = nn.Sequential( |
| 183 | # +1 to in_channels because we concatenate from MiniBatch std |
| 184 | WSConv2d(in_channels + 1, in_channels, kernel_size=3, padding=1), |
| 185 | nn.LeakyReLU(0.2), |
| 186 | WSConv2d(in_channels, in_channels, kernel_size=4, padding=0, stride=1), |
| 187 | nn.LeakyReLU(0.2), |
| 188 | WSConv2d( |
| 189 | in_channels, 1, kernel_size=1, padding=0, stride=1 |
| 190 | ), # we use this instead of linear layer |
| 191 | ) |
| 192 | |
| 193 | def fade_in(self, alpha, downscaled, out): |
| 194 | """Used to fade in downscaled using avg pooling and output from CNN""" |