Spaces:
Running
on
Zero
Running
on
Zero
File size: 10,780 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
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_priorout = 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_features:b*512*8*512
# priors: 8, 16,32,64,128
# locs: b*32, center+width for 128*2048 0~1
# locs: b*16, center for 128*2048, 0~2048
single_sr = True
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)) # 256*32*32
if priors32 is not None:
sq_f_32_ori = sq_f_32.clone()
# sq_f_32_res = sq_f_32.clone().detach()*0
prior_32_align = torch.zeros_like(sq_f_32_ori)
prior_32_mask = torch.zeros((sq_f_32_ori.size(0), 1, sq_f_32_ori.size(2), sq_f_32_ori.size(3)), dtype=sq_f_32_ori.dtype, layout=sq_f_32_ori.layout, device=sq_f_32_ori.device)
for b, p_32 in enumerate(priors32): #512*32*32, different batch
p_32_256 = self.conv_32_to256(p_32.clone())
for c in range(p_32_256.size(0)): #
center = (locs[b][c].detach()/4.0).int() #
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
if single_sr:
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)
prior_32_align[b, :, :, x1:x2] = prior_32_align[b, :, :, x1:x2] + sq_f_32[b, :, :, x1:x2].clone() * scale[0,...] + shift[0,...]
else:
prior_32_align[b, :, :, x1:x2] = prior_32_align[b, :, :, x1:x2] + p_32_256[c:c+1, :, :, y1:y2].clone()
# prior_32_mask[b, :, :, x1:x2] += 1.0
# prior_32_mask[prior_32_mask<2]=1.0
# prior_32_align = prior_32_align / prior_32_mask.repeat(1, prior_32_align.size(1), 1, 1)
if single_sr:
sq_pf_32_out = sq_f_32_ori + prior_32_align
else:
sq_f_32_norm = adaptive_instance_normalization(prior_32_align, sq_f_32)
sq_f_32_fuse = self.conv_32_fuse(torch.cat((sq_f_32_norm, sq_f_32), dim=1))
scale_32 = self.conv_32_scale(sq_f_32_fuse)
shift_32 = self.conv_32_shift(sq_f_32_fuse)
sq_f_32_res = sq_f_32_ori * scale_32 + shift_32
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()
prior_64_align = torch.zeros_like(sq_f_64_ori)
prior_64_mask = torch.zeros((sq_f_64_ori.size(0), 1, sq_f_64_ori.size(2), sq_f_64_ori.size(3)), dtype=sq_f_64_ori.dtype, layout=sq_f_64_ori.layout, device=sq_f_64_ori.device)
for b, p_64_prior in enumerate(priors64): #512*8*8, 512*16*16, 512*32*32, 256*64*64, 128*128*128 different batch
p_64 = p_64_prior.clone() #.detach() #no backward to prior
for c in range(p_64.size(0)): # for each character
center = (locs[b][c].detach()/2.0).int() #+ 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
if single_sr:
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)
prior_64_align[b, :, :, x1:x2] = prior_64_align[b, :, :, x1:x2] + sq_f_64[b, :, :, x1:x2].clone() * scale[0,...] + shift[0,...]
else:
prior_64_align[b, :, :, x1:x2] = prior_64_align[b, :, :, x1:x2] + p_64[c:c+1, :, :, y1:y2].clone()
# prior_64_mask[b, :, :, x1:x2] += 1.0
# prior_64_mask[prior_64_mask<2]=1.0
# prior_64_align = prior_64_align / prior_64_mask.repeat(1, prior_64_align.size(1), 1, 1)
if single_sr:
sq_pf_64 = sq_f_64_ori + prior_64_align
else:
sq_f_64_norm = adaptive_instance_normalization(prior_64_align, sq_f_64_ori)
sq_f_64_fuse = self.conv_64_fuse(torch.cat((sq_f_64_norm, sq_f_64_ori), dim=1))
scale_64 = self.conv_64_scale(sq_f_64_fuse)
shift_64 = self.conv_64_shift(sq_f_64_fuse)
sq_f_64_res = sq_f_64_ori * scale_64 + shift_64
sq_pf_64 = sq_f_64_ori + sq_f_64_res
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 |