-
Notifications
You must be signed in to change notification settings - Fork 50
/
trainer.py
206 lines (173 loc) · 7.72 KB
/
trainer.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
import os
import utility
import torch
from decimal import Decimal
import torch.nn.functional as F
from utils import util
class Trainer():
def __init__(self, args, loader, my_model, my_loss, ckp):
self.args = args
self.scale = args.scale
self.ckp = ckp
self.loader_train = loader.loader_train
self.loader_test = loader.loader_test
self.model = my_model
self.model_E = torch.nn.DataParallel(self.model.get_model().E, range(self.args.n_GPUs))
self.loss = my_loss
self.contrast_loss = torch.nn.CrossEntropyLoss().cuda()
self.optimizer = utility.make_optimizer(args, self.model)
self.scheduler = utility.make_scheduler(args, self.optimizer)
if self.args.load != '.':
self.optimizer.load_state_dict(
torch.load(os.path.join(ckp.dir, 'optimizer.pt'))
)
for _ in range(len(ckp.log)): self.scheduler.step()
def train(self):
self.scheduler.step()
self.loss.step()
epoch = self.scheduler.last_epoch + 1
# lr stepwise
if epoch <= self.args.epochs_encoder:
lr = self.args.lr_encoder * (self.args.gamma_encoder ** (epoch // self.args.lr_decay_encoder))
for param_group in self.optimizer.param_groups:
param_group['lr'] = lr
else:
lr = self.args.lr_sr * (self.args.gamma_sr ** ((epoch - self.args.epochs_encoder) // self.args.lr_decay_sr))
for param_group in self.optimizer.param_groups:
param_group['lr'] = lr
self.ckp.write_log('[Epoch {}]\tLearning rate: {:.2e}'.format(epoch, Decimal(lr)))
self.loss.start_log()
self.model.train()
degrade = util.SRMDPreprocessing(
self.scale[0],
kernel_size=self.args.blur_kernel,
blur_type=self.args.blur_type,
sig_min=self.args.sig_min,
sig_max=self.args.sig_max,
lambda_min=self.args.lambda_min,
lambda_max=self.args.lambda_max,
noise=self.args.noise
)
timer = utility.timer()
losses_contrast, losses_sr = utility.AverageMeter(), utility.AverageMeter()
for batch, (hr, _, idx_scale) in enumerate(self.loader_train):
hr = hr.cuda() # b, n, c, h, w
lr, b_kernels = degrade(hr) # bn, c, h, w
self.optimizer.zero_grad()
timer.tic()
# forward
## train degradation encoder
if epoch <= self.args.epochs_encoder:
_, output, target = self.model_E(im_q=lr[:,0,...], im_k=lr[:,1,...])
loss_constrast = self.contrast_loss(output, target)
loss = loss_constrast
losses_contrast.update(loss_constrast.item())
## train the whole network
else:
sr, output, target = self.model(lr)
loss_SR = self.loss(sr, hr[:,0,...])
loss_constrast = self.contrast_loss(output, target)
loss = loss_constrast + loss_SR
losses_sr.update(loss_SR.item())
losses_contrast.update(loss_constrast.item())
# backward
loss.backward()
self.optimizer.step()
timer.hold()
if epoch <= self.args.epochs_encoder:
if (batch + 1) % self.args.print_every == 0:
self.ckp.write_log(
'Epoch: [{:03d}][{:04d}/{:04d}]\t'
'Loss [contrastive loss: {:.3f}]\t'
'Time [{:.1f}s]'.format(
epoch, (batch + 1) * self.args.batch_size, len(self.loader_train.dataset),
losses_contrast.avg,
timer.release()
))
else:
if (batch + 1) % self.args.print_every == 0:
self.ckp.write_log(
'Epoch: [{:04d}][{:04d}/{:04d}]\t'
'Loss [SR loss:{:.3f} | contrastive loss: {:.3f}]\t'
'Time [{:.1f}s]'.format(
epoch, (batch + 1) * self.args.batch_size, len(self.loader_train.dataset),
losses_sr.avg, losses_contrast.avg,
timer.release(),
))
self.loss.end_log(len(self.loader_train))
# save model
target = self.model.get_model()
model_dict = target.state_dict()
keys = list(model_dict.keys())
for key in keys:
if 'E.encoder_k' in key or 'queue' in key:
del model_dict[key]
torch.save(
model_dict,
os.path.join(self.ckp.dir, 'model', 'model_{}.pt'.format(epoch))
)
def test(self):
self.ckp.write_log('\nEvaluation:')
self.ckp.add_log(torch.zeros(1, len(self.scale)))
self.model.eval()
timer_test = utility.timer()
with torch.no_grad():
for idx_scale, scale in enumerate(self.scale):
self.loader_test.dataset.set_scale(idx_scale)
eval_psnr = 0
eval_ssim = 0
degrade = util.SRMDPreprocessing(
self.scale[0],
kernel_size=self.args.blur_kernel,
blur_type=self.args.blur_type,
sig=self.args.sig,
lambda_1=self.args.lambda_1,
lambda_2=self.args.lambda_2,
theta=self.args.theta,
noise=self.args.noise
)
for idx_img, (hr, filename, _) in enumerate(self.loader_test):
hr = hr.cuda() # b, 1, c, h, w
hr = self.crop_border(hr, scale)
lr, _ = degrade(hr, random=False) # b, 1, c, h, w
hr = hr[:, 0, ...] # b, c, h, w
# inference
timer_test.tic()
sr = self.model(lr[:, 0, ...])
timer_test.hold()
sr = utility.quantize(sr, self.args.rgb_range)
hr = utility.quantize(hr, self.args.rgb_range)
# metrics
eval_psnr += utility.calc_psnr(
sr, hr, scale, self.args.rgb_range,
benchmark=self.loader_test.dataset.benchmark
)
eval_ssim += utility.calc_ssim(
sr, hr, scale,
benchmark=self.loader_test.dataset.benchmark
)
# save results
if self.args.save_results:
save_list = [sr]
filename = filename[0]
self.ckp.save_results(filename, save_list, scale)
self.ckp.log[-1, idx_scale] = eval_psnr / len(self.loader_test)
self.ckp.write_log(
'[Epoch {}---{} x{}]\tPSNR: {:.3f} SSIM: {:.4f}'.format(
self.args.resume,
self.args.data_test,
scale,
eval_psnr / len(self.loader_test),
eval_ssim / len(self.loader_test),
))
def crop_border(self, img_hr, scale):
b, n, c, h, w = img_hr.size()
img_hr = img_hr[:, :, :, :int(h//scale*scale), :int(w//scale*scale)]
return img_hr
def terminate(self):
if self.args.test_only:
self.test()
return True
else:
epoch = self.scheduler.last_epoch + 1
return epoch >= self.args.epochs_encoder + self.args.epochs_sr