From 6a0d5f48137fb357c590743e8b4a6392f70724db Mon Sep 17 00:00:00 2001 From: James Betker Date: Sun, 18 Oct 2020 09:57:47 -0600 Subject: [PATCH] Add a checkpointable discriminator --- codes/models/archs/discriminator_vgg_arch.py | 65 ++++++++++++++++++-- codes/models/networks.py | 2 + codes/models/steps/progressive_zoom.py | 2 +- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/codes/models/archs/discriminator_vgg_arch.py b/codes/models/archs/discriminator_vgg_arch.py index d6c81f18..7d56e889 100644 --- a/codes/models/archs/discriminator_vgg_arch.py +++ b/codes/models/archs/discriminator_vgg_arch.py @@ -77,7 +77,6 @@ class Discriminator_VGG_128(nn.Module): out = self.linear2(fea) return out - class Discriminator_VGG_128_GN(nn.Module): # input_img_factor = multiplier to support images over 128x128. Only certain factors are supported. def __init__(self, in_nc, nf, input_img_factor=1): @@ -108,12 +107,12 @@ class Discriminator_VGG_128_GN(nn.Module): self.bn4_1 = nn.GroupNorm(8, nf * 8, affine=True) final_nf = nf * 8 - self.linear1 = nn.Linear(int(final_nf * 4 * input_img_factor * 4 * input_img_factor), 100) - self.linear2 = nn.Linear(100, 1) - # activation function self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True) + self.linear1 = nn.Linear(int(final_nf * 4 * input_img_factor * 4 * input_img_factor), 100) + self.linear2 = nn.Linear(100, 1) + def forward(self, x): fea = self.lrelu(self.conv0_0(x)) fea = self.lrelu(self.bn0_1(self.conv0_1(fea))) @@ -131,7 +130,63 @@ class Discriminator_VGG_128_GN(nn.Module): fea = self.lrelu(self.bn4_0(self.conv4_0(fea))) fea = self.lrelu(self.bn4_1(self.conv4_1(fea))) + + fea = fea.contiguous().view(fea.size(0), -1) + fea = self.lrelu(self.linear1(fea)) + out = self.linear2(fea) + return out + +from utils.util import checkpoint +class Discriminator_VGG_128_GN_Checkpointed(nn.Module): + # input_img_factor = multiplier to support images over 128x128. Only certain factors are supported. + def __init__(self, in_nc, nf, input_img_factor=1): + super(Discriminator_VGG_128_GN_Checkpointed, self).__init__() + # [64, 128, 128] + self.conv0_0 = nn.Conv2d(in_nc, nf, 3, 1, 1, bias=True) + self.conv0_1 = nn.Conv2d(nf, nf, 4, 2, 1, bias=False) + self.bn0_1 = nn.GroupNorm(8, nf, affine=True) + # [64, 64, 64] + self.conv1_0 = nn.Conv2d(nf, nf * 2, 3, 1, 1, bias=False) + self.bn1_0 = nn.GroupNorm(8, nf * 2, affine=True) + self.conv1_1 = nn.Conv2d(nf * 2, nf * 2, 4, 2, 1, bias=False) + self.bn1_1 = nn.GroupNorm(8, nf * 2, affine=True) + # [128, 32, 32] + self.conv2_0 = nn.Conv2d(nf * 2, nf * 4, 3, 1, 1, bias=False) + self.bn2_0 = nn.GroupNorm(8, nf * 4, affine=True) + self.conv2_1 = nn.Conv2d(nf * 4, nf * 4, 4, 2, 1, bias=False) + self.bn2_1 = nn.GroupNorm(8, nf * 4, affine=True) + # [256, 16, 16] + self.conv3_0 = nn.Conv2d(nf * 4, nf * 8, 3, 1, 1, bias=False) + self.bn3_0 = nn.GroupNorm(8, nf * 8, affine=True) + self.conv3_1 = nn.Conv2d(nf * 8, nf * 8, 4, 2, 1, bias=False) + self.bn3_1 = nn.GroupNorm(8, nf * 8, affine=True) + # [512, 8, 8] + self.conv4_0 = nn.Conv2d(nf * 8, nf * 8, 3, 1, 1, bias=False) + self.bn4_0 = nn.GroupNorm(8, nf * 8, affine=True) + self.conv4_1 = nn.Conv2d(nf * 8, nf * 8, 4, 2, 1, bias=False) + self.bn4_1 = nn.GroupNorm(8, nf * 8, affine=True) + final_nf = nf * 8 + + # activation function + self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True) + + self.body = nn.Sequential(self.conv0_0, self.lrelu, + self.conv0_1, self.bn0_1, self.lrelu, + self.conv1_0, self.bn1_0, self.lrelu, + self.conv1_1, self.bn1_1, self.lrelu, + self.conv2_0, self.bn2_0, self.lrelu, + self.conv2_1, self.bn2_1, self.lrelu, + self.conv3_0, self.bn3_0, self.lrelu, + self.conv3_1, self.bn3_1, self.lrelu, + self.conv4_0, self.bn4_0, self.lrelu, + self.conv4_1, self.bn4_1, self.lrelu) + + self.linear1 = nn.Linear(int(final_nf * 4 * input_img_factor * 4 * input_img_factor), 100) + self.linear2 = nn.Linear(100, 1) + + def forward(self, x): + fea = checkpoint(self.body, x) fea = fea.contiguous().view(fea.size(0), -1) fea = self.lrelu(self.linear1(fea)) out = self.linear2(fea) @@ -504,4 +559,4 @@ class RefDiscriminatorVgg128(nn.Module): fea = self.lrelu(self.linear1(fea)) out = self.output_linears(torch.cat([fea, ref_vector], dim=1)) - return out \ No newline at end of file + return out diff --git a/codes/models/networks.py b/codes/models/networks.py index c12f20c5..531380f1 100644 --- a/codes/models/networks.py +++ b/codes/models/networks.py @@ -171,6 +171,8 @@ def define_D_net(opt_net, img_sz=None, wrap=False): netD = SRGAN_arch.Discriminator_VGG_128_GN(in_nc=opt_net['in_nc'], nf=opt_net['nf'], input_img_factor=img_sz / 128) if wrap: netD = GradDiscWrapper(netD) + elif which_model == 'discriminator_vgg_128_gn_checkpointed': + netD = SRGAN_arch.Discriminator_VGG_128_GN_Checkpointed(in_nc=opt_net['in_nc'], nf=opt_net['nf'], input_img_factor=img_sz / 128) elif which_model == 'discriminator_resnet': netD = DiscriminatorResnet_arch.fixup_resnet34(num_filters=opt_net['nf'], num_classes=1, input_img_size=img_sz) elif which_model == 'discriminator_resnet_50': diff --git a/codes/models/steps/progressive_zoom.py b/codes/models/steps/progressive_zoom.py index 1c3b9d43..98d7af7f 100644 --- a/codes/models/steps/progressive_zoom.py +++ b/codes/models/steps/progressive_zoom.py @@ -95,7 +95,7 @@ class ProgressiveGeneratorInjector(Injector): chain_output.append(recurrent_hq) results_hq.append(hq_inputs[:, link.index]) - if self.env['step'] % 1 == 0: + if self.env['step'] % 50 == 0: self.produce_progressive_visual_debugs(chain_input, chain_output, debug_index) debug_index += 1 results[self.hq_output_key] = results_hq