2019-08-23 13:42:47 +00:00
|
|
|
import torch
|
|
|
|
import models.archs.SRResNet_arch as SRResNet_arch
|
|
|
|
import models.archs.discriminator_vgg_arch as SRGAN_arch
|
2020-04-29 05:00:29 +00:00
|
|
|
import models.archs.DiscriminatorResnet_arch as DiscriminatorResnet_arch
|
2020-05-04 20:01:43 +00:00
|
|
|
import models.archs.DiscriminatorResnet_arch_passthrough as DiscriminatorResnet_arch_passthrough
|
2020-05-01 01:17:30 +00:00
|
|
|
import models.archs.FlatProcessorNetNew_arch as FlatProcessorNetNew_arch
|
2019-08-23 13:42:47 +00:00
|
|
|
import models.archs.RRDBNet_arch as RRDBNet_arch
|
2020-05-04 20:01:43 +00:00
|
|
|
import models.archs.RRDBNetXL_arch as RRDBNetXL_arch
|
2020-05-02 23:47:14 +00:00
|
|
|
#import models.archs.EDVR_arch as EDVR_arch
|
2020-04-24 06:00:46 +00:00
|
|
|
import models.archs.HighToLowResNet as HighToLowResNet
|
2020-04-28 17:48:05 +00:00
|
|
|
import models.archs.FlatProcessorNet_arch as FlatProcessorNet_arch
|
2020-04-30 02:51:57 +00:00
|
|
|
import models.archs.arch_util as arch_utils
|
2020-05-05 17:59:46 +00:00
|
|
|
import models.archs.ResGen_arch as ResGen_arch
|
2020-04-22 06:37:41 +00:00
|
|
|
import math
|
2019-08-23 13:42:47 +00:00
|
|
|
|
|
|
|
# Generator
|
|
|
|
def define_G(opt):
|
|
|
|
opt_net = opt['network_G']
|
|
|
|
which_model = opt_net['which_model_G']
|
2020-04-22 06:37:41 +00:00
|
|
|
scale = opt['scale']
|
2019-08-23 13:42:47 +00:00
|
|
|
|
|
|
|
# image restoration
|
|
|
|
if which_model == 'MSRResNet':
|
|
|
|
netG = SRResNet_arch.MSRResNet(in_nc=opt_net['in_nc'], out_nc=opt_net['out_nc'],
|
|
|
|
nf=opt_net['nf'], nb=opt_net['nb'], upscale=opt_net['scale'])
|
|
|
|
elif which_model == 'RRDBNet':
|
2020-04-22 06:37:41 +00:00
|
|
|
# RRDB does scaling in two steps, so take the sqrt of the scale we actually want to achieve and feed it to RRDB.
|
|
|
|
scale_per_step = math.sqrt(scale)
|
2019-08-23 13:42:47 +00:00
|
|
|
netG = RRDBNet_arch.RRDBNet(in_nc=opt_net['in_nc'], out_nc=opt_net['out_nc'],
|
2020-04-22 06:37:41 +00:00
|
|
|
nf=opt_net['nf'], nb=opt_net['nb'], interpolation_scale_factor=scale_per_step)
|
2020-05-04 20:01:43 +00:00
|
|
|
elif which_model == 'RRDBNetXL':
|
|
|
|
scale_per_step = math.sqrt(scale)
|
|
|
|
netG = RRDBNetXL_arch.RRDBNet(in_nc=opt_net['in_nc'], out_nc=opt_net['out_nc'],
|
|
|
|
nf=opt_net['nf'], nb_lo=opt_net['nblo'], nb_med=opt_net['nbmed'], nb_hi=opt_net['nbhi'],
|
|
|
|
interpolation_scale_factor=scale_per_step)
|
2020-05-05 17:59:46 +00:00
|
|
|
elif which_model == 'ResGen':
|
2020-05-06 23:27:30 +00:00
|
|
|
netG = ResGen_arch.fixup_resnet34(nb_denoiser=opt_net['nb_denoiser'], nb_upsampler=opt_net['nb_upsampler'],
|
2020-05-10 16:48:37 +00:00
|
|
|
upscale_applications=opt_net['upscale_applications'], num_filters=opt_net['nf'])
|
2020-05-12 16:09:02 +00:00
|
|
|
elif which_model == 'ResGenV2':
|
|
|
|
netG = ResGen_arch.fixup_resnet34_v2(nb_denoiser=opt_net['nb_denoiser'], nb_upsampler=opt_net['nb_upsampler'],
|
2020-05-12 22:26:29 +00:00
|
|
|
upscale_applications=opt_net['upscale_applications'], num_filters=opt_net['nf'],
|
|
|
|
inject_noise=opt_net['inject_noise'])
|
2020-05-05 17:59:46 +00:00
|
|
|
|
2020-04-24 06:00:46 +00:00
|
|
|
# image corruption
|
|
|
|
elif which_model == 'HighToLowResNet':
|
|
|
|
netG = HighToLowResNet.HighToLowResNet(in_nc=opt_net['in_nc'], out_nc=opt_net['out_nc'],
|
|
|
|
nf=opt_net['nf'], nb=opt_net['nb'], downscale=opt_net['scale'])
|
2020-04-28 17:48:05 +00:00
|
|
|
elif which_model == 'FlatProcessorNet':
|
2020-05-01 01:17:30 +00:00
|
|
|
'''netG = FlatProcessorNet_arch.FlatProcessorNet(in_nc=opt_net['in_nc'], out_nc=opt_net['out_nc'],
|
2020-04-28 17:48:05 +00:00
|
|
|
nf=opt_net['nf'], downscale=opt_net['scale'], reduce_anneal_blocks=opt_net['ra_blocks'],
|
2020-05-01 01:17:30 +00:00
|
|
|
assembler_blocks=opt_net['assembler_blocks'])'''
|
|
|
|
netG = FlatProcessorNetNew_arch.fixup_resnet34(num_filters=opt_net['nf'])
|
2019-08-23 13:42:47 +00:00
|
|
|
# video restoration
|
|
|
|
elif which_model == 'EDVR':
|
|
|
|
netG = EDVR_arch.EDVR(nf=opt_net['nf'], nframes=opt_net['nframes'],
|
|
|
|
groups=opt_net['groups'], front_RBs=opt_net['front_RBs'],
|
|
|
|
back_RBs=opt_net['back_RBs'], center=opt_net['center'],
|
|
|
|
predeblur=opt_net['predeblur'], HR_in=opt_net['HR_in'],
|
|
|
|
w_TSA=opt_net['w_TSA'])
|
2020-04-22 06:37:41 +00:00
|
|
|
|
2019-08-23 13:42:47 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError('Generator model [{:s}] not recognized'.format(which_model))
|
|
|
|
|
|
|
|
return netG
|
|
|
|
|
|
|
|
|
|
|
|
# Discriminator
|
|
|
|
def define_D(opt):
|
2020-04-22 06:37:41 +00:00
|
|
|
img_sz = opt['datasets']['train']['target_size']
|
2019-08-23 13:42:47 +00:00
|
|
|
opt_net = opt['network_D']
|
|
|
|
which_model = opt_net['which_model_D']
|
|
|
|
|
|
|
|
if which_model == 'discriminator_vgg_128':
|
2020-04-21 22:32:59 +00:00
|
|
|
netD = SRGAN_arch.Discriminator_VGG_128(in_nc=opt_net['in_nc'], nf=opt_net['nf'], input_img_factor=img_sz / 128)
|
2020-04-29 05:00:29 +00:00
|
|
|
elif which_model == 'discriminator_resnet':
|
2020-05-02 01:56:14 +00:00
|
|
|
netD = DiscriminatorResnet_arch.fixup_resnet34(num_filters=opt_net['nf'], num_classes=1, input_img_size=img_sz)
|
2020-05-04 20:01:43 +00:00
|
|
|
elif which_model == 'discriminator_resnet_passthrough':
|
2020-05-06 23:27:30 +00:00
|
|
|
netD = DiscriminatorResnet_arch_passthrough.fixup_resnet34(num_filters=opt_net['nf'], num_classes=1, input_img_size=img_sz, use_bn=True)
|
2019-08-23 13:42:47 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError('Discriminator model [{:s}] not recognized'.format(which_model))
|
|
|
|
return netD
|
|
|
|
|
|
|
|
|
|
|
|
# Define network used for perceptual loss
|
|
|
|
def define_F(opt, use_bn=False):
|
|
|
|
gpu_ids = opt['gpu_ids']
|
|
|
|
device = torch.device('cuda' if gpu_ids else 'cpu')
|
|
|
|
# PyTorch pretrained VGG19-54, before ReLU.
|
|
|
|
if use_bn:
|
|
|
|
feature_layer = 49
|
|
|
|
else:
|
|
|
|
feature_layer = 34
|
|
|
|
netF = SRGAN_arch.VGGFeatureExtractor(feature_layer=feature_layer, use_bn=use_bn,
|
|
|
|
use_input_norm=True, device=device)
|
|
|
|
netF.eval() # No need to train
|
|
|
|
return netF
|