diff --git a/codes/models/archs/StructuredSwitchedGenerator.py b/codes/models/archs/StructuredSwitchedGenerator.py index ce9bcc82..058f6c61 100644 --- a/codes/models/archs/StructuredSwitchedGenerator.py +++ b/codes/models/archs/StructuredSwitchedGenerator.py @@ -153,11 +153,18 @@ class SwitchWithReference(nn.Module): class SSGr1(SwitchModelBase): - def __init__(self, in_nc, out_nc, nf, xforms=8, upscale=4, init_temperature=10): + def __init__(self, in_nc, out_nc, nf, xforms=8, upscale=4, init_temperature=10, recurrent=False): super(SSGr1, self).__init__(init_temperature, 10000) n_upscale = int(math.log(upscale, 2)) self.nf = nf + if recurrent: + self.recurrent = True + self.recurrent_process = ConvGnLelu(in_nc, nf, kernel_size=3, stride=2, norm=False, bias=True, activation=False) + self.recurrent_join = ReferenceJoinBlock(nf, residual_weight_init_factor=.01, final_norm=False, kernel_size=1, depth=3, join=False) + else: + self.recurrent = False + # processing the input embedding self.reference_embedding = ReferenceImageBranch(nf) @@ -181,7 +188,7 @@ class SSGr1(SwitchModelBase): self.final_hr_conv2 = ConvGnLelu(nf // 2, out_nc, kernel_size=3, norm=False, activation=False, bias=False) self.switches = [self.sw1.switch, self.sw_grad.switch, self.conjoin_sw.switch] - def forward(self, x, ref, ref_center, save_attentions=True): + def forward(self, x, ref, ref_center, save_attentions=True, recurrent=None): # The attention_maps debugger outputs . Save that here. self.lr = x.detach().cpu() @@ -195,6 +202,9 @@ class SSGr1(SwitchModelBase): 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) + if self.recurrent: + rec = self.recurrent_process(recurrent) + x = self.recurrent_join(x, rec) x1, a1 = checkpoint(self.sw1, x, ref_embedding) x_grad = self.grad_conv(x_grad) diff --git a/codes/models/networks.py b/codes/models/networks.py index d3e58fdc..c3737c09 100644 --- a/codes/models/networks.py +++ b/codes/models/networks.py @@ -89,9 +89,10 @@ def define_G(opt, net_key='network_G', scale=None): multiplexer_reductions=opt_net['multiplexer_reductions'] if 'multiplexer_reductions' in opt_net.keys() else 3, init_temperature=opt_net['temperature'] if 'temperature' in opt_net.keys() else 10) elif which_model == "ssgr1": + recurrent = opt_net['recurrent'] if 'recurrent' in opt_net.keys() else False xforms = opt_net['num_transforms'] if 'num_transforms' in opt_net.keys() else 8 netG = ssg.SSGr1(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) + init_temperature=opt_net['temperature'] if 'temperature' in opt_net.keys() else 10, recurrent=recurrent) elif which_model == 'stacked_switches': xforms = opt_net['num_transforms'] if 'num_transforms' in opt_net.keys() else 8 in_nc = opt_net['in_nc'] if 'in_nc' in opt_net.keys() else 3