diff --git a/codes/models/archs/StructuredSwitchedGenerator.py b/codes/models/archs/StructuredSwitchedGenerator.py index ea6f12fc..b1fde3ae 100644 --- a/codes/models/archs/StructuredSwitchedGenerator.py +++ b/codes/models/archs/StructuredSwitchedGenerator.py @@ -372,3 +372,56 @@ class StackedSwitchGenerator5Layer(SwitchModelBase): self.attentions = [a1, a3, a3, a4, a5] return x_out, + +class StackedSwitchGenerator2xTeco(SwitchModelBase): + def __init__(self, nf, xforms=8, init_temperature=10): + super(StackedSwitchGenerator2xTeco, self).__init__(init_temperature, 10000) + self.nf = nf + + # processing the input embedding + self.reference_embedding = ReferenceImageBranch(nf) + + # Feature branch + self.model_fea_conv = ConvGnLelu(3, nf, kernel_size=7, norm=False, activation=False, bias=True) + self.model_recurrent_conv = ConvGnLelu(3, nf, kernel_size=3, stride=2, norm=False, activation=False, bias=True) + self.model_fea_recurrent_combine = ConvGnLelu(nf*2, nf, 1, activation=False, norm=False, bias=False) + self.sw1 = SwitchWithReference(nf, xforms, init_temperature, has_ref=False) + self.sw2 = SwitchWithReference(nf, xforms // 2, init_temperature, has_ref=False) + self.sw3 = SwitchWithReference(nf, xforms // 2, init_temperature, has_ref=False) + self.sw4 = SwitchWithReference(nf, xforms // 2, init_temperature, has_ref=False) + self.sw5 = SwitchWithReference(nf, xforms, init_temperature, has_ref=False) + self.switches = [self.sw1.switch, self.sw2.switch, self.sw3.switch, self.sw4.switch, self.sw5.switch] + + self.final_lr_conv = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=True) + self.upsample = UpconvBlock(nf, nf // 2, block=ConvGnLelu, norm=False, activation=True, bias=True) + self.final_hr_conv1 = ConvGnLelu(nf // 2, nf // 2, kernel_size=3, norm=False, activation=False, bias=True) + self.final_hr_conv2 = ConvGnLelu(nf // 2, 3, kernel_size=3, norm=False, activation=False, bias=False) + + def forward(self, x, recurrent, ref, ref_center, save_attentions=True): + # The attention_maps debugger outputs . Save that here. + self.lr = x.detach().cpu() + + # If we're not saving attention, we also shouldn't be updating the attention norm. This is because the attention + # norm should only be getting updates with new data, not recurrent generator sampling. + for sw in self.switches: + sw.set_update_attention_norm(save_attentions) + + ref_code = checkpoint(self.reference_embedding, ref, ref_center) + ref_embedding = ref_code.view(-1, ref_code.shape[1], 1, 1).repeat(1, 1, x.shape[2] // 8, x.shape[3] // 8) + + x = self.model_fea_conv(x) + rec = self.model_recurrent_conv(recurrent) + x = self.model_fea_recurrent_combine(torch.cat([x, rec], dim=1)) + x1, a1 = checkpoint(self.sw1, x, ref_embedding) + x2, a2 = checkpoint(self.sw2, x1, ref_embedding) + x3, a3 = checkpoint(self.sw3, x2, ref_embedding) + x4, a4 = checkpoint(self.sw4, x3, ref_embedding) + x5, a5 = checkpoint(self.sw5, x4, ref_embedding) + x_out = checkpoint(self.final_lr_conv, x5) + x_out = checkpoint(self.upsample, x_out) + x_out = checkpoint(self.final_hr_conv2, x_out) + + if save_attentions: + self.attentions = [a1, a3, a3, a4, a5] + return x_out, + diff --git a/codes/models/networks.py b/codes/models/networks.py index 3f5ee5e1..f354f582 100644 --- a/codes/models/networks.py +++ b/codes/models/networks.py @@ -98,6 +98,8 @@ def define_G(opt, net_key='network_G', scale=None): xforms = opt_net['num_transforms'] if 'num_transforms' in opt_net.keys() else 8 netG = ssg.SSGDeep(in_nc=3, out_nc=3, nf=opt_net['nf'], xforms=xforms, upscale=opt_net['scale'], init_temperature=opt_net['temperature'] if 'temperature' in opt_net.keys() else 10) + elif which_model == 'ssg_teco': + netG = ssg.StackedSwitchGenerator2xTeco(nf=opt_net['nf'], xforms=opt_net['num_transforms'], init_temperature=opt_net['temperature'] if 'temperature' in opt_net.keys() else 10) elif which_model == "flownet2": from models.flownet2.models import FlowNet2 ld = torch.load(opt_net['load_path'])