-
Notifications
You must be signed in to change notification settings - Fork 1
/
TTSR_bn_auto.py
466 lines (363 loc) · 17.1 KB
/
TTSR_bn_auto.py
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import models
def auto_S(S):
for i in range(S.size()[0]): #对每一个batch处理
l = S[i][0].cpu().detach().numpy().reshape(1,1600).tolist()[0]
data = sorted(l)
size = len(data)
if size % 2 == 0:
median = (data[size//2]+data[size//2-1])/2
if size % 2 == 1:
median = data[(size-1)//2]
#median=np.median(l)
for j in range(1,S.size()[2]-1):
for k in range(1,S.size()[3]-1):
if S[i][0][j][k].item()>median and S[i][0][j-1][k].item()>median and S[i][0][j+1][k].item()>median and S[i][0][j][k-1].item()>median and S[i][0][j][k+1].item()>median:
S[i][0][j][k]+=0.1
if S[i][0][j][k]>0.95:
S[i][0][j][k]=0.95
return S
def conv1x1(in_channels, out_channels, stride=1):
return nn.Conv2d(in_channels, out_channels, kernel_size=1,
stride=stride, padding=0, bias=True)
def conv3x3(in_channels, out_channels, stride=1):
return nn.Conv2d(in_channels, out_channels, kernel_size=3,
stride=stride, padding=1, bias=True)
class ResBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1, downsample=None, res_scale=1):
super(ResBlock, self).__init__()
self.res_scale = res_scale
self.conv1 = conv3x3(in_channels, out_channels, stride)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(out_channels, out_channels)
def forward(self, x):
x1 = x
out = self.conv1(x)
out = self.relu(out)
out = self.conv2(out)
out = out * self.res_scale + x1
return out
class SFE(nn.Module):
def __init__(self, num_res_blocks, n_feats, res_scale):
super(SFE, self).__init__()
self.num_res_blocks = num_res_blocks
self.conv_head = conv3x3(3, n_feats)
self.RBs = nn.ModuleList()
for i in range(self.num_res_blocks):
self.RBs.append(ResBlock(in_channels=n_feats, out_channels=n_feats,
res_scale=res_scale))
self.conv_tail = conv3x3(n_feats, n_feats)
def forward(self, x):
x = F.relu(self.conv_head(x))
x1 = x
for i in range(self.num_res_blocks):
x = self.RBs[i](x)
x = self.conv_tail(x)
x = x + x1
return x
class CSFI2(nn.Module):
def __init__(self, n_feats):
super(CSFI2, self).__init__()
self.conv12 = conv1x1(n_feats, n_feats)
self.conv21 = conv3x3(n_feats, n_feats, 2)
self.conv_merge1 = conv3x3(n_feats*2, n_feats)
self.conv_merge2 = conv3x3(n_feats*2, n_feats)
def forward(self, x1, x2):
x12 = F.interpolate(x1, scale_factor=2, mode='bicubic')
x12 = F.relu(self.conv12(x12))
x21 = F.relu(self.conv21(x2))
x1 = F.relu(self.conv_merge1( torch.cat((x1, x21), dim=1) ))
x2 = F.relu(self.conv_merge2( torch.cat((x2, x12), dim=1) ))
return x1, x2
class CSFI3(nn.Module):
def __init__(self, n_feats):
super(CSFI3, self).__init__()
self.conv12 = conv1x1(n_feats, n_feats)
self.conv13 = conv1x1(n_feats, n_feats)
self.conv21 = conv3x3(n_feats, n_feats, 2)
self.conv23 = conv1x1(n_feats, n_feats)
self.conv31_1 = conv3x3(n_feats, n_feats, 2)
self.conv31_2 = conv3x3(n_feats, n_feats, 2)
self.conv32 = conv3x3(n_feats, n_feats, 2)
self.conv_merge1 = conv3x3(n_feats*3, n_feats)
self.conv_merge2 = conv3x3(n_feats*3, n_feats)
self.conv_merge3 = conv3x3(n_feats*3, n_feats)
def forward(self, x1, x2, x3):
x12 = F.interpolate(x1, scale_factor=2, mode='bicubic')
x12 = F.relu(self.conv12(x12))
x13 = F.interpolate(x1, scale_factor=4, mode='bicubic')
x13 = F.relu(self.conv13(x13))
x21 = F.relu(self.conv21(x2))
x23 = F.interpolate(x2, scale_factor=2, mode='bicubic')
x23 = F.relu(self.conv23(x23))
x31 = F.relu(self.conv31_1(x3))
x31 = F.relu(self.conv31_2(x31))
x32 = F.relu(self.conv32(x3))
x1 = F.relu(self.conv_merge1( torch.cat((x1, x21, x31), dim=1) ))
x2 = F.relu(self.conv_merge2( torch.cat((x2, x12, x32), dim=1) ))
x3 = F.relu(self.conv_merge3( torch.cat((x3, x13, x23), dim=1) ))
return x1, x2, x3
class MergeTail(nn.Module):
def __init__(self, n_feats):
super(MergeTail, self).__init__()
self.conv13 = conv1x1(n_feats, n_feats)
self.conv23 = conv1x1(n_feats, n_feats)
self.conv_merge = conv3x3(n_feats*3, n_feats)
self.conv_tail1 = conv3x3(n_feats, n_feats//2)
self.conv_tail2 = conv1x1(n_feats//2, 3)
def forward(self, x1, x2, x3):
x13 = F.interpolate(x1, scale_factor=4, mode='bicubic')
x13 = F.relu(self.conv13(x13))
x23 = F.interpolate(x2, scale_factor=2, mode='bicubic')
x23 = F.relu(self.conv23(x23))
x = F.relu(self.conv_merge( torch.cat((x3, x13, x23), dim=1) ))
x = self.conv_tail1(x)
x = self.conv_tail2(x)
x = torch.clamp(x, -1, 1)
return x
class MainNet(nn.Module):
def __init__(self, num_res_blocks, n_feats, res_scale):
super(MainNet, self).__init__()
self.num_res_blocks = num_res_blocks ### a list containing number of resblocks of different stages
self.n_feats = n_feats
self.SFE = SFE(self.num_res_blocks[0], n_feats, res_scale)
### stage11
self.conv11_head = conv3x3(256+n_feats, n_feats)
self.RB11 = nn.ModuleList()
for i in range(self.num_res_blocks[1]):
self.RB11.append(ResBlock(in_channels=n_feats, out_channels=n_feats,
res_scale=res_scale))
self.conv11_tail = conv3x3(n_feats, n_feats)
### subpixel 1 -> 2
self.conv12 = conv3x3(n_feats, n_feats*4)
self.ps12 = nn.PixelShuffle(2)
### stage21, 22
#self.conv21_head = conv3x3(n_feats, n_feats)
self.conv22_head = conv3x3(128+n_feats, n_feats)
self.ex12 = CSFI2(n_feats)
self.RB21 = nn.ModuleList()
self.RB22 = nn.ModuleList()
for i in range(self.num_res_blocks[2]):
self.RB21.append(ResBlock(in_channels=n_feats, out_channels=n_feats,
res_scale=res_scale))
self.RB22.append(ResBlock(in_channels=n_feats, out_channels=n_feats,
res_scale=res_scale))
self.conv21_tail = conv3x3(n_feats, n_feats)
self.conv22_tail = conv3x3(n_feats, n_feats)
### subpixel 2 -> 3
self.conv23 = conv3x3(n_feats, n_feats*4)
self.ps23 = nn.PixelShuffle(2)
### stage31, 32, 33
#self.conv31_head = conv3x3(n_feats, n_feats)
#self.conv32_head = conv3x3(n_feats, n_feats)
self.conv33_head = conv3x3(64+n_feats, n_feats)
self.ex123 = CSFI3(n_feats)
self.RB31 = nn.ModuleList()
self.RB32 = nn.ModuleList()
self.RB33 = nn.ModuleList()
for i in range(self.num_res_blocks[3]):
self.RB31.append(ResBlock(in_channels=n_feats, out_channels=n_feats,
res_scale=res_scale))
self.RB32.append(ResBlock(in_channels=n_feats, out_channels=n_feats,
res_scale=res_scale))
self.RB33.append(ResBlock(in_channels=n_feats, out_channels=n_feats,
res_scale=res_scale))
self.conv31_tail = conv3x3(n_feats, n_feats)
self.conv32_tail = conv3x3(n_feats, n_feats)
self.conv33_tail = conv3x3(n_feats, n_feats)
self.merge_tail = MergeTail(n_feats)
def forward(self, x, S=None, T_lv3=None, T_lv2=None, T_lv1=None):
### shallow feature extraction
S=auto_S(S)
x = self.SFE(x)
### stage11
x11 = x
### soft-attention
x11_res = x11
x11_res = torch.cat((x11_res, T_lv3), dim=1)
x11_res = self.conv11_head(x11_res) #F.relu(self.conv11_head(x11_res))
x11_res = x11_res * S
x11 = x11 + x11_res
x11_res = x11
for i in range(self.num_res_blocks[1]):
x11_res = self.RB11[i](x11_res)
x11_res = self.conv11_tail(x11_res)
x11 = x11 + x11_res
### stage21, 22
x21 = x11
x21_res = x21
x22 = self.conv12(x11)
x22 = F.relu(self.ps12(x22))
### soft-attention
x22_res = x22
x22_res = torch.cat((x22_res, T_lv2), dim=1)
x22_res = self.conv22_head(x22_res) #F.relu(self.conv22_head(x22_res))
x22_res = x22_res * F.interpolate(S, scale_factor=2, mode='bicubic')
x22 = x22 + x22_res
x22_res = x22
x21_res, x22_res = self.ex12(x21_res, x22_res)
for i in range(self.num_res_blocks[2]):
x21_res = self.RB21[i](x21_res)
x22_res = self.RB22[i](x22_res)
x21_res = self.conv21_tail(x21_res)
x22_res = self.conv22_tail(x22_res)
x21 = x21 + x21_res
x22 = x22 + x22_res
### stage31, 32, 33
x31 = x21
x31_res = x31
x32 = x22
x32_res = x32
x33 = self.conv23(x22)
x33 = F.relu(self.ps23(x33))
### soft-attention
x33_res = x33
x33_res = torch.cat((x33_res, T_lv1), dim=1)
x33_res = self.conv33_head(x33_res) #F.relu(self.conv33_head(x33_res))
x33_res = x33_res * F.interpolate(S, scale_factor=4, mode='bicubic')
x33 = x33 + x33_res
x33_res = x33
x31_res, x32_res, x33_res = self.ex123(x31_res, x32_res, x33_res)
for i in range(self.num_res_blocks[3]):
x31_res = self.RB31[i](x31_res)
x32_res = self.RB32[i](x32_res)
x33_res = self.RB33[i](x33_res)
x31_res = self.conv31_tail(x31_res)
x32_res = self.conv32_tail(x32_res)
x33_res = self.conv33_tail(x33_res)
x31 = x31 + x31_res
x32 = x32 + x32_res
x33 = x33 + x33_res
x = self.merge_tail(x31, x32, x33)
return x
class SearchTransfer(nn.Module):
def __init__(self):
super(SearchTransfer, self).__init__()
def bis(self, input, dim, index):
# batch index select
# input: [N, ?, ?, ...]
# dim: scalar > 0
# index: [N, idx]
views = [input.size(0)] + [1 if i!=dim else -1 for i in range(1, len(input.size()))]
expanse = list(input.size())
expanse[0] = -1
expanse[dim] = -1
index = index.view(views).expand(expanse)
return torch.gather(input, dim, index)
def forward(self, lrsr_lv3, refsr_lv3, ref_lv1, ref_lv2, ref_lv3):
### search
lrsr_lv3_unfold = F.unfold(lrsr_lv3, kernel_size=(3, 3), padding=1)
refsr_lv3_unfold = F.unfold(refsr_lv3, kernel_size=(3, 3), padding=1)
refsr_lv3_unfold = refsr_lv3_unfold.permute(0, 2, 1) #变成(N,L,```)
refsr_lv3_unfold = F.normalize(refsr_lv3_unfold, dim=2) # [N, Hr*Wr, C*k*k] #此时第三维是特征
lrsr_lv3_unfold = F.normalize(lrsr_lv3_unfold, dim=1) # [N, C*k*k, H*W] #此时第二维是特征
R_lv3 = torch.bmm(refsr_lv3_unfold, lrsr_lv3_unfold) #[N, Hr*Wr, H*W]
R_lv3_star, R_lv3_star_arg = torch.max(R_lv3, dim=1) #[N, H*W]
### transfer
ref_lv3_unfold = F.unfold(ref_lv3, kernel_size=(3, 3), padding=1)
ref_lv2_unfold = F.unfold(ref_lv2, kernel_size=(6, 6), padding=2, stride=2)
ref_lv1_unfold = F.unfold(ref_lv1, kernel_size=(12, 12), padding=4, stride=4)
T_lv3_unfold = self.bis(ref_lv3_unfold, 2, R_lv3_star_arg)
T_lv2_unfold = self.bis(ref_lv2_unfold, 2, R_lv3_star_arg)
T_lv1_unfold = self.bis(ref_lv1_unfold, 2, R_lv3_star_arg)
T_lv3 = F.fold(T_lv3_unfold, output_size=lrsr_lv3.size()[-2:], kernel_size=(3,3), padding=1) / (3.*3.)
T_lv2 = F.fold(T_lv2_unfold, output_size=(lrsr_lv3.size(2)*2, lrsr_lv3.size(3)*2), kernel_size=(6,6), padding=2, stride=2) / (3.*3.)
T_lv1 = F.fold(T_lv1_unfold, output_size=(lrsr_lv3.size(2)*4, lrsr_lv3.size(3)*4), kernel_size=(12,12), padding=4, stride=4) / (3.*3.)
S = R_lv3_star.view(R_lv3_star.size(0), 1, lrsr_lv3.size(2), lrsr_lv3.size(3))
return S, T_lv3, T_lv2, T_lv1
class MeanShift(nn.Conv2d):
def __init__(self, rgb_range, rgb_mean, rgb_std, sign=-1):
super(MeanShift, self).__init__(3, 3, kernel_size=1)
std = torch.Tensor(rgb_std)
self.weight.data = torch.eye(3).view(3, 3, 1, 1)
self.weight.data.div_(std.view(3, 1, 1, 1))
self.bias.data = sign * rgb_range * torch.Tensor(rgb_mean)
self.bias.data.div_(std)
# self.requires_grad = False
self.weight.requires_grad = False
self.bias.requires_grad = False
class LTE(torch.nn.Module):
def __init__(self, requires_grad=True, rgb_range=1):
super(LTE, self).__init__()
### use vgg19 weights to initialize
'''
vgg_pretrained_features=models.vgg19()
pre=torch.load('model_save/vgg19-dcbb9e9d.pth')
vgg_pretrained_features.load_state_dict(pre)
vgg_pretrained_features=vgg_pretrained_features.features
self.slice1 = torch.nn.Sequential()
self.slice2 = torch.nn.Sequential()
self.slice3 = torch.nn.Sequential()
for x in range(2):
self.slice1.add_module(str(x), vgg_pretrained_features[x])
for x in range(2, 7):
self.slice2.add_module(str(x), vgg_pretrained_features[x])
for x in range(7, 12):
self.slice3.add_module(str(x), vgg_pretrained_features[x])
if not requires_grad:
for param in self.slice1.parameters():
param.requires_grad = requires_grad
for param in self.slice2.parameters():
param.requires_grad = requires_grad
for param in self.slice3.parameters():
param.requires_grad = requires_grad
'''
### use vgg19 weights to initialize
vgg_pretrained_features=models.vgg19_bn()
pre=torch.load('model_save/vgg19_bn-c79401a0.pth')
vgg_pretrained_features.load_state_dict(pre)
vgg_pretrained_features=vgg_pretrained_features.features
self.slice1 = torch.nn.Sequential()
self.slice2 = torch.nn.Sequential()
self.slice3 = torch.nn.Sequential()
for x in range(3):
self.slice1.add_module(str(x), vgg_pretrained_features[x])
for x in range(3, 10):
self.slice2.add_module(str(x), vgg_pretrained_features[x])
for x in range(10, 17):
self.slice3.add_module(str(x), vgg_pretrained_features[x])
if not requires_grad:
for param in self.slice1.parameters():
param.requires_grad = requires_grad
for param in self.slice2.parameters():
param.requires_grad = requires_grad
for param in self.slice3.parameters():
param.requires_grad = requires_grad
vgg_mean = (0.485, 0.456, 0.406)
vgg_std = (0.229 * rgb_range, 0.224 * rgb_range, 0.225 * rgb_range)
self.sub_mean = MeanShift(rgb_range, vgg_mean, vgg_std)
def forward(self, x):
x = self.sub_mean(x)
x = self.slice1(x)
x_lv1 = x
x = self.slice2(x)
x_lv2 = x
x = self.slice3(x)
x_lv3 = x
return x_lv1, x_lv2, x_lv3
class TTSR(nn.Module):
def __init__(self, args):
super(TTSR, self).__init__()
self.args = args
self.num_res_blocks = list( map(int, args.num_res_blocks.split('+')) )
self.MainNet = MainNet(num_res_blocks=self.num_res_blocks, n_feats=args.n_feats,
res_scale=args.res_scale)
self.LTE = LTE(requires_grad=True)
self.LTE_copy = LTE(requires_grad=False) ### used in transferal perceptual loss
self.SearchTransfer = SearchTransfer()
def forward(self, lr=None, lrsr=None, ref=None, refsr=None, sr=None):
if (type(sr) != type(None)):
### used in transferal perceptual loss
self.LTE_copy.load_state_dict(self.LTE.state_dict())
sr_lv1, sr_lv2, sr_lv3 = self.LTE_copy((sr + 1.) / 2.)
return sr_lv1, sr_lv2, sr_lv3
_, _, lrsr_lv3 = self.LTE((lrsr.detach() + 1.) / 2.) #Q?
_, _, refsr_lv3 = self.LTE((refsr.detach() + 1.) / 2.) #K
ref_lv1, ref_lv2, ref_lv3 = self.LTE((ref.detach() + 1.) / 2.) #V
S, T_lv3, T_lv2, T_lv1 = self.SearchTransfer(lrsr_lv3, refsr_lv3, ref_lv1, ref_lv2, ref_lv3)
sr = self.MainNet(lr, S, T_lv3, T_lv2, T_lv1)
#print("S:",S)
return sr, S, T_lv3, T_lv2, T_lv1