-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTile_model.py
72 lines (62 loc) · 2.62 KB
/
Tile_model.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
import torch
from torch.nn import functional as F
from basicsr.utils.registry import MODEL_REGISTRY
from basicsr.models.sr_model import SRModel
import math
from tqdm import tqdm
from os import path as osp
@MODEL_REGISTRY.register()
class TileModel(SRModel):
def get_optimizer(self, optim_type, params, lr, **kwargs):
if optim_type == 'Adam':
optimizer = torch.optim.Adam(params, lr, **kwargs)
elif optim_type == 'AdamW':
optimizer = torch.optim.AdamW(params, lr, **kwargs)
elif optim_type == 'Adamax':
optimizer = torch.optim.Adamax(params, lr, **kwargs)
elif optim_type == 'SGD':
optimizer = torch.optim.SGD(params, lr, **kwargs)
elif optim_type == 'ASGD':
optimizer = torch.optim.ASGD(params, lr, **kwargs)
elif optim_type == 'RMSprop':
optimizer = torch.optim.RMSprop(params, lr, **kwargs)
elif optim_type == 'Rprop':
optimizer = torch.optim.Rprop(params, lr, **kwargs)
else:
raise NotImplementedError(f'optimizer {optim_type} is not supported yet.')
return optimizer
def test(self):
if hasattr(self, 'net_g_ema'):
self.net_g_ema.eval()
with torch.no_grad():
self.output = self.tile_test(self.lq, self.net_g_ema)
else:
self.net_g.eval()
with torch.no_grad():
self.output = self.tile_test(self.lq, self.net_g)
self.net_g.train()
def tile_test(self, img_lq, model):
tile = self.opt['tile']
if tile == 0:
# test the image as a whole
output = model(img_lq)
else:
# test the image tile by tile
b, c, h, w = img_lq.size()
tile = min(tile, h, w)
tile_overlap = tile//16
sf = self.opt['scale']
stride = tile - tile_overlap
h_idx_list = list(range(0, h-tile, stride)) + [h-tile]
w_idx_list = list(range(0, w-tile, stride)) + [w-tile]
E = torch.zeros(b, c, h*sf, w*sf).type_as(img_lq)
W = torch.zeros_like(E)
for h_idx in h_idx_list:
for w_idx in w_idx_list:
in_patch = img_lq[..., h_idx:h_idx+tile, w_idx:w_idx+tile]
out_patch = model(in_patch)
out_patch_mask = torch.ones_like(out_patch)
E[..., h_idx*sf:(h_idx+tile)*sf, w_idx*sf:(w_idx+tile)*sf].add_(out_patch)
W[..., h_idx*sf:(h_idx+tile)*sf, w_idx*sf:(w_idx+tile)*sf].add_(out_patch_mask)
output = E.div_(W)
return output