Spaces:
Running
on
Zero
Running
on
Zero
File size: 8,156 Bytes
981b0ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
import torch
from torch import nn
from torch.nn import functional as F
import torch.nn.utils.spectral_norm as SpectralNorm
import random
from .helper_arch import ResTextBlockV2, adaptive_instance_normalization
class SRNet(nn.Module):
def __init__(self, in_channel=3, dim_channel=256):
super().__init__()
self.conv_first_32 = nn.Sequential(
SpectralNorm(nn.Conv2d(in_channel, dim_channel//4, 3, 1, 1)),
nn.LeakyReLU(0.2),
)
self.conv_first_16 = nn.Sequential(
SpectralNorm(nn.Conv2d(dim_channel//4, dim_channel//2, 3, 2, 1)),
nn.LeakyReLU(0.2),
)
self.conv_first_8 = nn.Sequential(
SpectralNorm(nn.Conv2d(dim_channel//2, dim_channel, 3, 2, 1)),
nn.LeakyReLU(0.2),
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
)
self.conv_body_16 = nn.Sequential(
SpectralNorm(nn.Conv2d(dim_channel+dim_channel//2, dim_channel, 3, 1, 1)),
nn.LeakyReLU(0.2),
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
)
self.conv_body_32 = nn.Sequential(
SpectralNorm(nn.Conv2d(dim_channel+dim_channel//4, dim_channel, 3, 1, 1)),
nn.LeakyReLU(0.2),
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
)
self.conv_up = nn.Sequential(
nn.Upsample(scale_factor=2, mode='bilinear'), #64*64*256
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
nn.LeakyReLU(0.2),
ResTextBlockV2(dim_channel, dim_channel),
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
)
self.conv_final = nn.Sequential(
SpectralNorm(nn.Conv2d(dim_channel, dim_channel//2, 3, 1, 1)),
nn.LeakyReLU(0.2),
nn.Upsample(scale_factor=2, mode='bilinear'), #128*128*256
SpectralNorm(nn.Conv2d(dim_channel//2, dim_channel//4, 3, 1, 1)),
nn.LeakyReLU(0.2),
ResTextBlockV2(dim_channel//4, dim_channel//4),
SpectralNorm(nn.Conv2d(dim_channel//4, 3, 3, 1, 1)),
nn.Tanh()
)
self.conv_32_scale = nn.Sequential(
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
nn.LeakyReLU(0.2),
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
)
self.conv_32_shift = nn.Sequential(
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
nn.LeakyReLU(0.2),
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
)
self.conv_32_fuse = nn.Sequential(
ResTextBlockV2(2*dim_channel, dim_channel)
)
self.conv_32_to256 = nn.Sequential(
SpectralNorm(nn.Conv2d(512, dim_channel, 3, 1, 1)),
nn.LeakyReLU(0.2),
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
)
self.conv_64_scale = nn.Sequential(
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
nn.LeakyReLU(0.2),
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
)
self.conv_64_shift = nn.Sequential(
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
nn.LeakyReLU(0.2),
SpectralNorm(nn.Conv2d(dim_channel, dim_channel, 3, 1, 1)),
)
self.conv_64_fuse = nn.Sequential(
ResTextBlockV2(2*dim_channel, dim_channel)
)
def forward(self, lq, priors64, priors32, locs): #
lq_f_32 = self.conv_first_32(lq)
lq_f_16 = self.conv_first_16(lq_f_32)
lq_f_8 = self.conv_first_8(lq_f_16)
sq_f_16 = self.conv_body_16(torch.cat([F.interpolate(lq_f_8, scale_factor=2, mode='bilinear'), lq_f_16], dim=1))
sq_f_32 = self.conv_body_32(torch.cat([F.interpolate(sq_f_16, scale_factor=2, mode='bilinear'), lq_f_32], dim=1)) #
if priors32 is not None:
sq_f_32_ori = sq_f_32.clone()
sq_f_32_res = sq_f_32.clone().detach()*0
for b, p_32 in enumerate(priors32): #
p_32_256 = self.conv_32_to256(p_32.clone().detach())
for c in range(p_32_256.size(0)): #
center = int(locs[b][c].item()/4.0) #+ random.randint(-2,2)### no backward
width = 16
if center < width:
x1 = 0 #lq feature left
y1 = max(16 - center, 0)
else:
x1 = center - width
y1 = max(16 - width, 0)
# y1 = 16 - width
if center + width > sq_f_32.size(-1):
x2 = sq_f_32.size(-1) #lq feature right
else:
x2 = center + width
y2 = y1 + (x2 - x1)
'''
center align
'''
y1 = 16 - torch.div(x2-x1, 2, rounding_mode='trunc')
y2 = y1 + x2 - x1
char_prior_f = p_32_256[c:c+1, :, :, y1:y2].clone() #prior
char_lq_f = sq_f_32[b:b+1, :, :, x1:x2].clone()
adain_prior_f = adaptive_instance_normalization(char_prior_f, char_lq_f)
fuse_32_prior = self.conv_32_fuse(torch.cat((adain_prior_f, char_lq_f), dim=1))
scale = self.conv_32_scale(fuse_32_prior)
shift = self.conv_32_shift(fuse_32_prior)
sq_f_32_res[b, :, :, x1:x2] = sq_f_32_res[b, :, :, x1:x2] + sq_f_32[b, :, :, x1:x2].clone() * scale[0,...] + shift[0,...]
sq_pf_32_out = sq_f_32_ori + sq_f_32_res
else:
sq_pf_32_out = sq_f_32.clone()
sq_f_64 = self.conv_up(sq_pf_32_out) #64*1024
sq_f_64_ori = sq_f_64.clone()
sq_f_64_res = sq_f_64.clone().detach() * 0
# prior_full_64 = sq_f_64.clone().detach() * 0
if priors64 is not None:
for b, p_64_prior in enumerate(priors64): #
p_64 = p_64_prior.clone().detach() #no backward to prior
for c in range(p_64.size(0)): # for each character
center = int(locs[b][c].item()/2.0) #+ random.randint(-4,4)### no backward
width = 32
if center < width:
x1 = 0
y1 = max(32 - center, 0)
else:
x1 = center -width
y1 = max(32 - width, 0)
if center + width > sq_f_64.size(-1):
x2 = sq_f_64.size(-1)
else:
x2 = center + width
'''
center align
'''
y1 = 32 - torch.div(x2-x1, 2, rounding_mode='trunc')
y2 = y1 + x2 - x1
char_prior_f = p_64[c:c+1, :, :, y1:y2].clone()
char_lq_f = sq_f_64[b:b+1, :, :, x1:x2].clone()
adain_prior_f = adaptive_instance_normalization(char_prior_f, char_lq_f)
fuse_64_prior = self.conv_64_fuse(torch.cat((adain_prior_f, char_lq_f), dim=1))
scale = self.conv_64_scale(fuse_64_prior)
shift = self.conv_64_shift(fuse_64_prior)
sq_f_64_res[b, :, :, x1:x2] = sq_f_64_res[b, :, :, x1:x2] + sq_f_64[b, :, :, x1:x2].clone() * scale[0,...] + shift[0,...]
# prior_full_64[b, :, :, x1:x2] = prior_full_64[b, :, :, x1:x2] + char_prior_f.clone()
sq_pf_64 = sq_f_64_ori + sq_f_64_res
else:
sq_pf_64 = sq_f_64_ori.clone()
f256 = self.conv_final(sq_pf_64)
# adain_lr2prior = adaptive_instance_normalization(prior_full_64, F.interpolate(sq_f_32_ori, scale_factor=2, mode='bilinear'))
# prior_out = self.conv_priorout(adain_lr2prior)
return f256 #prior_out |