diff --git a/codes/models/archs/SPSR_arch.py b/codes/models/archs/SPSR_arch.py index dec5d1ea..af067fbb 100644 --- a/codes/models/archs/SPSR_arch.py +++ b/codes/models/archs/SPSR_arch.py @@ -4,7 +4,7 @@ import torch.nn as nn import torch.nn.functional as F from models.archs import SPSR_util as B from .RRDBNet_arch import RRDB -from models.archs.arch_util import ConvGnLelu, UpconvBlock +from models.archs.arch_util import ConvGnLelu, UpconvBlock, ConjoinBlock2 from models.archs.SwitchedResidualGenerator_arch import MultiConvBlock, ConvBasisMultiplexer, ConfigurableSwitchComputer from switched_conv_util import save_attention_to_image_rgb from switched_conv import compute_attention_specificity @@ -231,452 +231,11 @@ class SPSRNet(nn.Module): return x_out_branch, x_out, x_grad -class SPSRNetSimplified(nn.Module): - def __init__(self, in_nc, out_nc, nf, nb, upscale=4): - super(SPSRNetSimplified, self).__init__() - n_upscale = int(math.log(upscale, 2)) - - # Feature branch - self.model_fea_conv = ConvGnLelu(in_nc, nf, kernel_size=3, norm=False, activation=False) - self.model_shortcut_blk = nn.Sequential(*[RRDB(nf, gc=32) for _ in range(nb)]) - self.feature_lr_conv = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False) - self.model_upsampler = nn.Sequential(*[UpconvBlock(nf, nf, block=ConvGnLelu, norm=False, activation=False, bias=False) for _ in range(n_upscale)]) - self.feature_hr_conv1 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - self.feature_hr_conv2 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False, bias=False) - - # Grad branch - self.get_g_nopadding = ImageGradientNoPadding() - self.b_fea_conv = ConvGnLelu(in_nc, nf, kernel_size=3, norm=False, activation=False, bias=False) - self.b_concat_decimate_1 = ConvGnLelu(2 * nf, nf, kernel_size=1, norm=False, activation=False, bias=False) - self.b_proc_block_1 = RRDB(nf, gc=32) - self.b_concat_decimate_2 = ConvGnLelu(2 * nf, nf, kernel_size=1, norm=False, activation=False, bias=False) - self.b_proc_block_2 = RRDB(nf, gc=32) - self.b_concat_decimate_3 = ConvGnLelu(2 * nf, nf, kernel_size=1, norm=False, activation=False, bias=False) - self.b_proc_block_3 = RRDB(nf, gc=32) - self.b_concat_decimate_4 = ConvGnLelu(2 * nf, nf, kernel_size=1, norm=False, activation=False, bias=False) - self.b_proc_block_4 = RRDB(nf, gc=32) - - # Upsampling - self.grad_lr_conv = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - b_upsampler = nn.Sequential(*[UpconvBlock(nf, nf, block=ConvGnLelu, norm=False, activation=False, bias=False) for _ in range(n_upscale)]) - grad_hr_conv1 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - grad_hr_conv2 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False, bias=False) - self.branch_upsample = B.sequential(*b_upsampler, grad_hr_conv1, grad_hr_conv2) - # Conv used to output grad branch shortcut. - self.grad_branch_output_conv = ConvGnLelu(nf, out_nc, kernel_size=1, norm=False, activation=False, bias=False) - - # Conjoin branch. - # Note: "_branch_pretrain" is a special tag used to denote parameters that get pretrained before the rest. - self._branch_pretrain_concat = ConvGnLelu(nf * 2, nf, kernel_size=1, norm=False, activation=False, bias=False) - self._branch_pretrain_block = RRDB(nf * 2, gc=32) - self._branch_pretrain_HR_conv0 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - self._branch_pretrain_HR_conv1 = ConvGnLelu(nf, out_nc, kernel_size=3, norm=False, activation=False, bias=False) - - def forward(self, x): - - x_grad = self.get_g_nopadding(x) - x = self.model_fea_conv(x) - - x_ori = x - for i in range(5): - x = self.model_shortcut_blk[i](x) - x_fea1 = x - - for i in range(5): - x = self.model_shortcut_blk[i + 5](x) - x_fea2 = x - - for i in range(5): - x = self.model_shortcut_blk[i + 10](x) - x_fea3 = x - - for i in range(5): - x = self.model_shortcut_blk[i + 15](x) - x_fea4 = x - - x = self.model_shortcut_blk[20:](x) - x = self.feature_lr_conv(x) - - # short cut - x = x_ori + x - x = self.model_upsampler(x) - x = self.feature_hr_conv1(x) - x = self.feature_hr_conv2(x) - - x_b_fea = self.b_fea_conv(x_grad) - x_cat_1 = torch.cat([x_b_fea, x_fea1], dim=1) - - x_cat_1 = self.b_concat_decimate_1(x_cat_1) - x_cat_1 = self.b_proc_block_1(x_cat_1) - - x_cat_2 = torch.cat([x_cat_1, x_fea2], dim=1) - - x_cat_2 = self.b_concat_decimate_2(x_cat_2) - x_cat_2 = self.b_proc_block_2(x_cat_2) - - x_cat_3 = torch.cat([x_cat_2, x_fea3], dim=1) - - x_cat_3 = self.b_concat_decimate_3(x_cat_3) - x_cat_3 = self.b_proc_block_3(x_cat_3) - - x_cat_4 = torch.cat([x_cat_3, x_fea4], dim=1) - - x_cat_4 = self.b_concat_decimate_4(x_cat_4) - x_cat_4 = self.b_proc_block_4(x_cat_4) - - x_cat_4 = self.grad_lr_conv(x_cat_4) - - # short cut - x_cat_4 = x_cat_4 + x_b_fea - x_branch = self.branch_upsample(x_cat_4) - x_out_branch = self.grad_branch_output_conv(x_branch) - - ######## - x_branch_d = x_branch - x__branch_pretrain_cat = torch.cat([x_branch_d, x], dim=1) - x__branch_pretrain_cat = self._branch_pretrain_block(x__branch_pretrain_cat) - x_out = self._branch_pretrain_concat(x__branch_pretrain_cat) - x_out = self._branch_pretrain_HR_conv0(x_out) - x_out = self._branch_pretrain_HR_conv1(x_out) - - ######### - return x_out_branch, x_out, x_grad - - -class SPSRNetSimplifiedNoSkip(nn.Module): - def __init__(self, in_nc, out_nc, nf, nb, upscale=4): - super(SPSRNetSimplifiedNoSkip, self).__init__() - n_upscale = int(math.log(upscale, 2)) - - # Feature branch - self.model_fea_conv = ConvGnLelu(in_nc, nf, kernel_size=3, norm=False, activation=False) - self.model_shortcut_blk = nn.Sequential(*[RRDB(nf, gc=32) for _ in range(nb)]) - self.feature_lr_conv = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False) - self.model_upsampler = nn.Sequential(*[UpconvBlock(nf, nf, block=ConvGnLelu, norm=False, activation=False, bias=False) for _ in range(n_upscale)]) - self.feature_hr_conv1 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - self.feature_hr_conv2 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False, bias=False) - - # Grad branch - self.get_g_nopadding = ImageGradientNoPadding() - self.b_fea_conv = ConvGnLelu(in_nc, nf, kernel_size=3, norm=False, activation=False, bias=False) - self.b_concat_decimate_1 = ConvGnLelu(2 * nf, nf, kernel_size=1, norm=False, activation=False, bias=False) - self.b_proc_block_1 = RRDB(nf, gc=32) - self.b_concat_decimate_2 = ConvGnLelu(2 * nf, nf, kernel_size=1, norm=False, activation=False, bias=False) - self.b_proc_block_2 = RRDB(nf, gc=32) - self.b_concat_decimate_3 = ConvGnLelu(2 * nf, nf, kernel_size=1, norm=False, activation=False, bias=False) - self.b_proc_block_3 = RRDB(nf, gc=32) - self.b_concat_decimate_4 = ConvGnLelu(2 * nf, nf, kernel_size=1, norm=False, activation=False, bias=False) - self.b_proc_block_4 = RRDB(nf, gc=32) - # Upsampling - self.grad_lr_conv = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - b_upsampler = nn.Sequential(*[UpconvBlock(nf, nf, block=ConvGnLelu, norm=False, activation=False, bias=False) for _ in range(n_upscale)]) - grad_hr_conv1 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - grad_hr_conv2 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False, bias=False) - self.branch_upsample = B.sequential(*b_upsampler, grad_hr_conv1, grad_hr_conv2) - # Conv used to output grad branch shortcut. - self.grad_branch_output_conv = ConvGnLelu(nf, out_nc, kernel_size=1, norm=False, activation=False, bias=False) - - # Conjoin branch. - # Note: "_branch_pretrain" is a special tag used to denote parameters that get pretrained before the rest. - self._branch_pretrain_concat = ConvGnLelu(nf * 2, nf, kernel_size=1, norm=False, activation=False, bias=False) - self._branch_pretrain_block = RRDB(nf * 2, gc=32) - self._branch_pretrain_HR_conv0 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - self._branch_pretrain_HR_conv1 = ConvGnLelu(nf, out_nc, kernel_size=3, norm=False, activation=False, bias=False) - - def forward(self, x): - x_grad = self.get_g_nopadding(x) - x = self.model_fea_conv(x) - - x_ori = x - for i in range(5): - x = self.model_shortcut_blk[i](x) - for i in range(5): - x = self.model_shortcut_blk[i + 5](x) - for i in range(5): - x = self.model_shortcut_blk[i + 10](x) - for i in range(5): - x = self.model_shortcut_blk[i + 15](x) - - x = self.model_shortcut_blk[20:](x) - x = self.feature_lr_conv(x) - - # short cut - x = x_ori + x - x = self.model_upsampler(x) - x = self.feature_hr_conv1(x) - x = self.feature_hr_conv2(x) - - x_b_fea = self.b_fea_conv(x_grad) - x_cat_1 = self.b_proc_block_1(x_b_fea) - x_cat_2 = self.b_proc_block_2(x_cat_1) - x_cat_3 = self.b_proc_block_3(x_cat_2) - x_cat_4 = self.b_proc_block_4(x_cat_3) - x_cat_4 = x_cat_4 + x_b_fea - x_cat_4 = self.grad_lr_conv(x_cat_4) - - # short cut - x_branch = self.branch_upsample(x_cat_4) - x_out_branch = self.grad_branch_output_conv(x_branch) - - ######## - x_branch_d = x_branch - x__branch_pretrain_cat = torch.cat([x_branch_d, x], dim=1) - x__branch_pretrain_cat = self._branch_pretrain_block(x__branch_pretrain_cat) - x_out = self._branch_pretrain_concat(x__branch_pretrain_cat) - x_out = self._branch_pretrain_HR_conv0(x_out) - x_out = self._branch_pretrain_HR_conv1(x_out) - ######### - return x_out_branch, x_out, x_grad - - class SwitchedSpsr(nn.Module): def __init__(self, in_nc, out_nc, nf, upscale=4): super(SwitchedSpsr, self).__init__() n_upscale = int(math.log(upscale, 2)) - # switch options - transformation_filters = nf - switch_filters = nf - switch_reductions = 3 - switch_processing_layers = 2 - self.transformation_counts = 8 - multiplx_fn = functools.partial(ConvBasisMultiplexer, transformation_filters, switch_filters, switch_reductions, - switch_processing_layers, self.transformation_counts) - pretransform_fn = functools.partial(ConvGnLelu, transformation_filters, transformation_filters, norm=False, bias=False, weight_init_factor=.1) - transform_fn = functools.partial(MultiConvBlock, transformation_filters, int(transformation_filters * 1.5), - transformation_filters, kernel_size=3, depth=3, - weight_init_factor=.1) - - # Feature branch - self.model_fea_conv = ConvGnLelu(in_nc, nf, kernel_size=3, norm=False, activation=False) - self.sw1 = ConfigurableSwitchComputer(transformation_filters, multiplx_fn, - pre_transform_block=pretransform_fn, transform_block=transform_fn, - attention_norm=True, - transform_count=self.transformation_counts, init_temp=10, - add_scalable_noise_to_transforms=True) - self.sw2 = ConfigurableSwitchComputer(transformation_filters, multiplx_fn, - pre_transform_block=pretransform_fn, transform_block=transform_fn, - attention_norm=True, - transform_count=self.transformation_counts, init_temp=10, - add_scalable_noise_to_transforms=True) - self.feature_lr_conv = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False) - self.model_upsampler = nn.Sequential(*[UpconvBlock(nf, nf, block=ConvGnLelu, norm=False, activation=False, bias=False) for _ in range(n_upscale)]) - self.feature_hr_conv1 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - self.feature_hr_conv2 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False, bias=False) - - # Grad branch - self.get_g_nopadding = ImageGradientNoPadding() - self.b_fea_conv = ConvGnLelu(in_nc, nf, kernel_size=3, norm=False, activation=False, bias=False) - self.sw_grad = ConfigurableSwitchComputer(transformation_filters, multiplx_fn, - pre_transform_block=pretransform_fn, transform_block=transform_fn, - attention_norm=True, - transform_count=self.transformation_counts, init_temp=10, - add_scalable_noise_to_transforms=True) - # Upsampling - self.grad_lr_conv = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - b_upsampler = nn.Sequential(*[UpconvBlock(nf, nf, block=ConvGnLelu, norm=False, activation=False, bias=False) for _ in range(n_upscale)]) - grad_hr_conv1 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - grad_hr_conv2 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False, bias=False) - self.branch_upsample = B.sequential(*b_upsampler, grad_hr_conv1, grad_hr_conv2) - # Conv used to output grad branch shortcut. - self.grad_branch_output_conv = ConvGnLelu(nf, out_nc, kernel_size=1, norm=False, activation=False, bias=False) - - # Conjoin branch. - # Note: "_branch_pretrain" is a special tag used to denote parameters that get pretrained before the rest. - self._branch_pretrain_concat = ConvGnLelu(nf * 2, nf, kernel_size=1, norm=False, activation=False, bias=False) - self._branch_pretrain_sw = ConfigurableSwitchComputer(transformation_filters, multiplx_fn, - pre_transform_block=pretransform_fn, transform_block=transform_fn, - attention_norm=True, - transform_count=self.transformation_counts, init_temp=10, - add_scalable_noise_to_transforms=True) - self._branch_pretrain_HR_conv0 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - self._branch_pretrain_HR_conv1 = ConvGnLelu(nf, out_nc, kernel_size=3, norm=False, activation=False, bias=False) - self.switches = [self.sw1, self.sw2, self.sw_grad, self._branch_pretrain_sw] - self.attentions = None - self.init_temperature = 10 - self.final_temperature_step = 10000 - - def forward(self, x): - x_grad = self.get_g_nopadding(x) - x = self.model_fea_conv(x) - - x1, a1 = self.sw1(x, True) - x2, a2 = self.sw2(x1, True) - x_fea = self.feature_lr_conv(x2) - x_fea = self.model_upsampler(x_fea) - x_fea = self.feature_hr_conv1(x_fea) - x_fea = self.feature_hr_conv2(x_fea) - - x_b_fea = self.b_fea_conv(x_grad) - x_grad, a3 = self.sw_grad(x_b_fea, att_in=x1, output_attention_weights=True) - x_grad = self.grad_lr_conv(x_grad) - x_grad = self.branch_upsample(x_grad) - x_out_branch = self.grad_branch_output_conv(x_grad) - - x__branch_pretrain_cat = torch.cat([x_grad, x_fea], dim=1) - x__branch_pretrain_cat = self._branch_pretrain_concat(x__branch_pretrain_cat) - x__branch_pretrain_cat, a4 = self._branch_pretrain_sw(x__branch_pretrain_cat, True) - x_out = self._branch_pretrain_HR_conv0(x__branch_pretrain_cat) - x_out = self._branch_pretrain_HR_conv1(x_out) - - self.attentions = [a1, a2, a3, a4] - - return x_out_branch, x_out, x_grad - - def set_temperature(self, temp): - [sw.set_temperature(temp) for sw in self.switches] - - def update_for_step(self, step, experiments_path='.'): - if self.attentions: - temp = max(1, 1 + self.init_temperature * - (self.final_temperature_step - step) / self.final_temperature_step) - self.set_temperature(temp) - if step % 200 == 0: - output_path = os.path.join(experiments_path, "attention_maps", "a%i") - prefix = "attention_map_%i_%%i.png" % (step,) - [save_attention_to_image_rgb(output_path % (i,), self.attentions[i], self.transformation_counts, prefix, step) for i in range(len(self.attentions))] - - def get_debug_values(self, step): - temp = self.switches[0].switch.temperature - mean_hists = [compute_attention_specificity(att, 2) for att in self.attentions] - means = [i[0] for i in mean_hists] - hists = [i[1].clone().detach().cpu().flatten() for i in mean_hists] - val = {"switch_temperature": temp} - for i in range(len(means)): - val["switch_%i_specificity" % (i,)] = means[i] - val["switch_%i_histogram" % (i,)] = hists[i] - return val - - -class SwitchedSpsrLr(nn.Module): - def __init__(self, in_nc, out_nc, nf, upscale=4): - super(SwitchedSpsrLr, self).__init__() - n_upscale = int(math.log(upscale, 2)) - - # switch options - transformation_filters = nf - switch_filters = nf - switch_reductions = 3 - switch_processing_layers = 2 - self.transformation_counts = 8 - multiplx_fn = functools.partial(ConvBasisMultiplexer, transformation_filters, switch_filters, switch_reductions, - switch_processing_layers, self.transformation_counts) - pretransform_fn = functools.partial(ConvGnLelu, transformation_filters, transformation_filters, norm=False, bias=False, weight_init_factor=.1) - transform_fn = functools.partial(MultiConvBlock, transformation_filters, int(transformation_filters * 1.5), - transformation_filters, kernel_size=3, depth=3, - weight_init_factor=.1) - - # Feature branch - self.model_fea_conv = ConvGnLelu(in_nc, nf, kernel_size=3, norm=False, activation=False) - self.sw1 = ConfigurableSwitchComputer(transformation_filters, multiplx_fn, - pre_transform_block=pretransform_fn, transform_block=transform_fn, - attention_norm=True, - transform_count=self.transformation_counts, init_temp=10, - add_scalable_noise_to_transforms=True) - self.sw2 = ConfigurableSwitchComputer(transformation_filters, multiplx_fn, - pre_transform_block=pretransform_fn, transform_block=transform_fn, - attention_norm=True, - transform_count=self.transformation_counts, init_temp=10, - add_scalable_noise_to_transforms=True) - self.feature_lr_conv = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False) - self.feature_hr_conv1 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - self.feature_hr_conv2 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False, bias=False) - - # Grad branch - self.get_g_nopadding = ImageGradientNoPadding() - self.b_fea_conv = ConvGnLelu(in_nc, nf, kernel_size=3, norm=False, activation=False, bias=False) - self.sw_grad = ConfigurableSwitchComputer(transformation_filters, multiplx_fn, - pre_transform_block=pretransform_fn, transform_block=transform_fn, - attention_norm=True, - transform_count=self.transformation_counts, init_temp=10, - add_scalable_noise_to_transforms=True) - # Upsampling - self.grad_lr_conv = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - grad_hr_conv1 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - grad_hr_conv2 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False, bias=False) - self.branch_upsample = B.sequential(grad_hr_conv1, grad_hr_conv2) - # Conv used to output grad branch shortcut. - self.grad_branch_output_conv = ConvGnLelu(nf, out_nc, kernel_size=1, norm=False, activation=False, bias=False) - - # Conjoin branch. - # Note: "_branch_pretrain" is a special tag used to denote parameters that get pretrained before the rest. - transform_fn_cat = functools.partial(MultiConvBlock, transformation_filters * 2, int(transformation_filters * 1.5), - transformation_filters, kernel_size=3, depth=4, - weight_init_factor=.1) - pretransform_fn_cat = functools.partial(ConvGnLelu, transformation_filters * 2, transformation_filters * 2, norm=False, bias=False, weight_init_factor=.1) - self._branch_pretrain_sw = ConfigurableSwitchComputer(transformation_filters, multiplx_fn, - pre_transform_block=pretransform_fn_cat, transform_block=transform_fn_cat, - attention_norm=True, - transform_count=self.transformation_counts, init_temp=10, - add_scalable_noise_to_transforms=True) - self.upsample = nn.Sequential(*[UpconvBlock(nf, nf, block=ConvGnLelu, norm=False, activation=False, bias=False) for _ in range(n_upscale)]) - self.upsample_grad = nn.Sequential(*[UpconvBlock(nf, nf, block=ConvGnLelu, norm=False, activation=False, bias=False) for _ in range(n_upscale)]) - self._branch_pretrain_lr_conv = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=False) - self._branch_pretrain_HR_conv0 = ConvGnLelu(nf, nf, kernel_size=3, norm=False, activation=True, bias=False) - self._branch_pretrain_HR_conv1 = ConvGnLelu(nf, out_nc, kernel_size=3, norm=False, activation=False, bias=False) - self.switches = [self.sw1, self.sw2, self.sw_grad, self._branch_pretrain_sw] - self.attentions = None - self.init_temperature = 10 - self.final_temperature_step = 10000 - - def forward(self, x): - x_grad = self.get_g_nopadding(x) - x = self.model_fea_conv(x) - - x1, a1 = self.sw1(x, True) - x2, a2 = self.sw2(x1, True) - x_fea = self.feature_lr_conv(x2) - x_fea = self.feature_hr_conv1(x_fea) - x_fea = self.feature_hr_conv2(x_fea) - - x_b_fea = self.b_fea_conv(x_grad) - x_grad, a3 = self.sw_grad(x_b_fea, att_in=x1, output_attention_weights=True) - x_grad = self.grad_lr_conv(x_grad) - x_grad = self.branch_upsample(x_grad) - x_out_branch = self.upsample_grad(x_grad) - x_out_branch = self.grad_branch_output_conv(x_out_branch) - - x__branch_pretrain_cat = torch.cat([x_grad, x_fea], dim=1) - x__branch_pretrain_cat, a4 = self._branch_pretrain_sw(x__branch_pretrain_cat, att_in=x_fea, identity=x_fea, output_attention_weights=True) - x_out = self._branch_pretrain_lr_conv(x__branch_pretrain_cat) - x_out = self.upsample(x_out) - x_out = self._branch_pretrain_HR_conv0(x_out) - x_out = self._branch_pretrain_HR_conv1(x_out) - - self.attentions = [a1, a2, a3, a4] - - return x_out_branch, x_out, x_grad - - def set_temperature(self, temp): - [sw.set_temperature(temp) for sw in self.switches] - - def update_for_step(self, step, experiments_path='.'): - if self.attentions: - temp = max(1, 1 + self.init_temperature * - (self.final_temperature_step - step) / self.final_temperature_step) - self.set_temperature(temp) - if step % 200 == 0: - output_path = os.path.join(experiments_path, "attention_maps", "a%i") - prefix = "attention_map_%i_%%i.png" % (step,) - [save_attention_to_image_rgb(output_path % (i,), self.attentions[i], self.transformation_counts, prefix, step) for i in range(len(self.attentions))] - - def get_debug_values(self, step): - temp = self.switches[0].switch.temperature - mean_hists = [compute_attention_specificity(att, 2) for att in self.attentions] - means = [i[0] for i in mean_hists] - hists = [i[1].clone().detach().cpu().flatten() for i in mean_hists] - val = {"switch_temperature": temp} - for i in range(len(means)): - val["switch_%i_specificity" % (i,)] = means[i] - val["switch_%i_histogram" % (i,)] = hists[i] - return val - - -class SwitchedSpsrLr2(nn.Module): - def __init__(self, in_nc, out_nc, nf, upscale=4): - super(SwitchedSpsrLr2, self).__init__() - n_upscale = int(math.log(upscale, 2)) - # switch options transformation_filters = nf switch_filters = nf @@ -708,7 +267,8 @@ class SwitchedSpsrLr2(nn.Module): # Grad branch self.get_g_nopadding = ImageGradientNoPadding() self.b_fea_conv = ConvGnLelu(in_nc, nf, kernel_size=3, norm=False, activation=False, bias=False) - mplex_grad = functools.partial(ConvBasisMultiplexer, nf * 2, nf * 2, switch_reductions, + self.sw_grad_mplex_converge = ConjoinBlock2(nf) + mplex_grad = functools.partial(ConvBasisMultiplexer, nf, nf, switch_reductions, switch_processing_layers, self.transformation_counts // 2, use_exp2=True) self.sw_grad = ConfigurableSwitchComputer(transformation_filters, mplex_grad, pre_transform_block=pretransform_fn, transform_block=transform_fn, @@ -752,7 +312,8 @@ class SwitchedSpsrLr2(nn.Module): x_fea = self.feature_hr_conv2(x_fea) x_b_fea = self.b_fea_conv(x_grad) - x_grad, a3 = self.sw_grad(x_b_fea, att_in=torch.cat([x1, x_b_fea], dim=1), output_attention_weights=True) + grad_mplex_in = self.sw_grad_mplex_converge(x1, passthrough=x_b_fea) + x_grad, a3 = self.sw_grad(x_b_fea, att_in=grad_mplex_in, output_attention_weights=True) x_grad = self.grad_lr_conv(x_grad) x_grad = self.grad_hr_conv(x_grad) x_out_branch = self.upsample_grad(x_grad) diff --git a/codes/models/archs/arch_util.py b/codes/models/archs/arch_util.py index 5af4e6ea..125a2222 100644 --- a/codes/models/archs/arch_util.py +++ b/codes/models/archs/arch_util.py @@ -433,6 +433,21 @@ class ConjoinBlock(nn.Module): return self.process(x) +# Similar to ExpansionBlock2 but does not upsample. +class ConjoinBlock2(nn.Module): + def __init__(self, filters_in, filters_out=None, block=ConvGnSilu, norm=True): + super(ConjoinBlock2, self).__init__() + if filters_out is None: + filters_out = filters_in + self.process = block(filters_in*2, filters_in*2, kernel_size=3, bias=False, activation=True, norm=norm) + self.decimate = block(filters_in*2, filters_out, kernel_size=1, bias=False, activation=False, norm=norm) + + def forward(self, input, passthrough): + x = torch.cat([input, passthrough], dim=1) + x = self.process(x) + return self.decimate(x) + + # Basic convolutional upsampling block that uses interpolate. class UpconvBlock(nn.Module): def __init__(self, filters_in, filters_out=None, block=ConvGnSilu, norm=True, activation=True, bias=False): diff --git a/codes/models/networks.py b/codes/models/networks.py index ff536519..85397f33 100644 --- a/codes/models/networks.py +++ b/codes/models/networks.py @@ -113,10 +113,6 @@ def define_G(opt, net_key='network_G'): nb=opt_net['nb'], upscale=opt_net['scale']) elif which_model == "spsr_switched": netG = spsr.SwitchedSpsr(in_nc=3, out_nc=3, nf=opt_net['nf'], upscale=opt_net['scale']) - elif which_model == "spsr_switched_lr": - netG = spsr.SwitchedSpsrLr(in_nc=3, out_nc=3, nf=opt_net['nf'], upscale=opt_net['scale']) - elif which_model == "spsr_switched_lr2": - netG = spsr.SwitchedSpsrLr2(in_nc=3, out_nc=3, nf=opt_net['nf'], upscale=opt_net['scale']) # image corruption elif which_model == 'HighToLowResNet': diff --git a/codes/train.py b/codes/train.py index c6f28bef..86b29f17 100644 --- a/codes/train.py +++ b/codes/train.py @@ -161,7 +161,7 @@ def main(): current_step = resume_state['iter'] model.resume_training(resume_state) # handle optimizers and schedulers else: - current_step = 0 + current_step = -1 start_epoch = 0 #### training