-
Notifications
You must be signed in to change notification settings - Fork 62
/
solver.py
174 lines (140 loc) · 5.69 KB
/
solver.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
"""
CutBlur
Copyright 2020-present NAVER corp.
MIT license
"""
import os
import time
import skimage.io as io
import torch
import torch.nn as nn
import torch.nn.functional as F
import utils
import augments
from data import generate_loader
class Solver():
def __init__(self, module, opt):
self.opt = opt
self.dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.net = module.Net(opt).to(self.dev)
print("# params:", sum(map(lambda x: x.numel(), self.net.parameters())))
if opt.pretrain:
self.load(opt.pretrain)
self.loss_fn = nn.L1Loss()
self.optim = torch.optim.Adam(
self.net.parameters(), opt.lr,
betas=(0.9, 0.999), eps=1e-8
)
self.scheduler = torch.optim.lr_scheduler.MultiStepLR(
self.optim, [1000*int(d) for d in opt.decay.split("-")],
gamma=opt.gamma,
)
if not opt.test_only:
self.train_loader = generate_loader("train", opt)
self.test_loader = generate_loader("test", opt)
self.t1, self.t2 = None, None
self.best_psnr, self.best_step = 0, 0
def fit(self):
opt = self.opt
self.t1 = time.time()
for step in range(opt.max_steps):
try:
inputs = next(iters)
except (UnboundLocalError, StopIteration):
iters = iter(self.train_loader)
inputs = next(iters)
HR = inputs[0].to(self.dev)
LR = inputs[1].to(self.dev)
# match the resolution of (LR, HR) due to CutBlur
if HR.size() != LR.size():
scale = HR.size(2) // LR.size(2)
LR = F.interpolate(LR, scale_factor=scale, mode="nearest")
HR, LR, mask, aug = augments.apply_augment(
HR, LR,
opt.augs, opt.prob, opt.alpha,
opt.aux_alpha, opt.aux_alpha, opt.mix_p
)
SR = self.net(LR)
if aug == "cutout":
SR, HR = SR*mask, HR*mask
loss = self.loss_fn(SR, HR)
self.optim.zero_grad()
loss.backward()
if opt.gclip > 0:
torch.nn.utils.clip_grad_value_(self.net.parameters(), opt.gclip)
self.optim.step()
self.scheduler.step()
if (step+1) % opt.eval_steps == 0:
self.summary_and_save(step)
def summary_and_save(self, step):
step, max_steps = (step+1)//1000, self.opt.max_steps//1000
psnr = self.evaluate()
self.t2 = time.time()
if psnr >= self.best_psnr:
self.best_psnr, self.best_step = psnr, step
self.save(step)
curr_lr = self.scheduler.get_lr()[0]
eta = (self.t2-self.t1) * (max_steps-step) / 3600
print("[{}K/{}K] {:.2f} (Best: {:.2f} @ {}K step) LR: {}, ETA: {:.1f} hours"
.format(step, max_steps, psnr, self.best_psnr, self.best_step,
curr_lr, eta))
self.t1 = time.time()
@torch.no_grad()
def evaluate(self):
opt = self.opt
self.net.eval()
if opt.save_result:
save_root = os.path.join(opt.save_root, opt.dataset)
os.makedirs(save_root, exist_ok=True)
psnr = 0
for i, inputs in enumerate(self.test_loader):
HR = inputs[0].to(self.dev)
LR = inputs[1].to(self.dev)
# match the resolution of (LR, HR) due to CutBlur
if HR.size() != LR.size():
scale = HR.size(2) // LR.size(2)
LR = F.interpolate(LR, scale_factor=scale, mode="nearest")
SR = self.net(LR).detach()
HR = HR[0].clamp(0, 255).round().cpu().byte().permute(1, 2, 0).numpy()
SR = SR[0].clamp(0, 255).round().cpu().byte().permute(1, 2, 0).numpy()
if opt.save_result:
save_path = os.path.join(save_root, "{:04d}.png".format(i+1))
io.imsave(save_path, SR)
HR = HR[opt.crop:-opt.crop, opt.crop:-opt.crop, :]
SR = SR[opt.crop:-opt.crop, opt.crop:-opt.crop, :]
if opt.eval_y_only:
HR = utils.rgb2ycbcr(HR)
SR = utils.rgb2ycbcr(SR)
psnr += utils.calculate_psnr(HR, SR)
self.net.train()
return psnr/len(self.test_loader)
def load(self, path):
state_dict = torch.load(path, map_location=lambda storage, loc: storage)
if self.opt.strict_load:
self.net.load_state_dict(state_dict)
return
# when to fine-tune the pre-trained model
own_state = self.net.state_dict()
for name, param in state_dict.items():
if name in own_state:
if isinstance(param, nn.Parameter):
param = param.data
try:
own_state[name].copy_(param)
except Exception:
# head and tail modules can be different
if name.find("head") == -1 and name.find("tail") == -1:
raise RuntimeError(
"While copying the parameter named {}, "
"whose dimensions in the model are {} and "
"whose dimensions in the checkpoint are {}."
.format(name, own_state[name].size(), param.size())
)
else:
raise RuntimeError(
"Missing key {} in model's state_dict".format(name)
)
def save(self, step):
os.makedirs(self.opt.ckpt_root, exist_ok=True)
save_path = os.path.join(self.opt.ckpt_root, str(step)+".pt")
torch.save(self.net.state_dict(), save_path)