Add ssgr1 recurrence

This commit is contained in:
James Betker 2020-10-12 17:18:19 -06:00
parent c1a00f31b7
commit 597b6e92d6
2 changed files with 14 additions and 3 deletions

View File

@ -153,11 +153,18 @@ class SwitchWithReference(nn.Module):
class SSGr1(SwitchModelBase): 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) super(SSGr1, self).__init__(init_temperature, 10000)
n_upscale = int(math.log(upscale, 2)) n_upscale = int(math.log(upscale, 2))
self.nf = nf 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 # processing the input embedding
self.reference_embedding = ReferenceImageBranch(nf) 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.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] 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 <x>. Save that here. # The attention_maps debugger outputs <x>. Save that here.
self.lr = x.detach().cpu() 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) 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) 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) x1, a1 = checkpoint(self.sw1, x, ref_embedding)
x_grad = self.grad_conv(x_grad) x_grad = self.grad_conv(x_grad)

View File

@ -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, 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) init_temperature=opt_net['temperature'] if 'temperature' in opt_net.keys() else 10)
elif which_model == "ssgr1": 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 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'], 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': elif which_model == 'stacked_switches':
xforms = opt_net['num_transforms'] if 'num_transforms' in opt_net.keys() else 8 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 in_nc = opt_net['in_nc'] if 'in_nc' in opt_net.keys() else 3