MCPcopy Create free account
hub / github.com/KeepTryingTo/Pytorch-GAN / Discriminator

Class Discriminator

ProGAN/model.py:154–238  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

152判别器通道数从:[4 -> 8 ->] 16 -> 32 -> 64 -> 128 -> 256 -> 512 -> 512 -> 512 -> 512
153"""
154class 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"""
195 # alpha should be scalar within [0, 1], and upscale.shape == generated.shape
196 return alpha * out + (1 - alpha) * downscaled
197
198 def minibatch_std(self, x):
199 batch_statistics = (
200 torch.std(x, dim=0).mean().repeat(x.shape[0], 1, x.shape[2], x.shape[3])
201 )
202 # we take the std for each example (across all channels, and pixels) then we repeat it
203 # for a single channel and concatenate it with the image. In this way the discriminator
204 # will get information about the variation in the batch/image
205 return torch.cat([x, batch_statistics], dim=1)
206
207 def forward(self, x, alpha, steps):
208 # where we should start in the list of prog_blocks, maybe a bit confusing but
209 # the last is for the 4x4. So example let's say steps=1, then we should start
210 # at the second to last because input_size will be 8x8. If steps==0 we just
211 # use the final block

Callers 3

mainFunction · 0.90
model.pyFile · 0.70
mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected