-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
267 lines (200 loc) · 8.67 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
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
import os
import math
from decimal import Decimal
import utility
import matplotlib
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import numpy as np
from torch.autograd import Variable
from torch.nn import functional as F
from tqdm import tqdm
from collections import defaultdict
from mat_sci_torch_quats.quats import fz_reduce, scalar_last2first, scalar_first2last
from mat_sci_torch_quats.symmetries import hcp_syms
from collections import defaultdict
import time
class Trainer():
def __init__(self, args, loader_train, loader_val, loader_test, model, loss, ckp):
self.args = args
self.scale = args.scale
self.ckp = ckp # checkpoint
self.loader_train = loader_train
self.loader_val = loader_val
self.loader_test = loader_test
self.model = model
self.loss = loss
self.epoch = args.current_epoch
self.total_val_loss_all = [0]
self.epoch_list = [0]
self.mis_orient = utility.Misorientation_dist(args)
self.optimizer = utility.make_optimizer(args, self.model)
self.scheduler = utility.make_warmup_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()
self.error_last = 1e8
self.epsilon = 0.001
def train(self):
self.loss.step()
self.epoch = self.epoch + 1
epoch = self.epoch
lr = self.scheduler.get_last_lr()[0]
self.ckp.write_log(
'[Epoch {}]\tLearning rate: {:.2e}'.format(epoch, Decimal(lr))
)
self.loss.start_log()
self.model.train()
timer_data, timer_model = utility.timer(), utility.timer()
total_train_loss = 0
for batch, (lr, hr, filename_lr, filename_hr) in enumerate(self.loader_train):
lr, hr = self.prepare([lr, hr])
timer_data.hold()
timer_model.tic()
self.optimizer.zero_grad()
sr = self.model(lr, self.scale)
_, ch, _, _ = sr.shape
loss = self.loss(sr, hr)
if loss.item() < self.args.skip_threshold * self.error_last:
loss.backward()
self.optimizer.step()
else:
print('Skip this batch {}! (Loss: {})'.format(
batch + 1, loss.item()
))
timer_model.hold()
if (batch + 1) % self.args.print_every == 0:
self.ckp.write_log('[{}/{}]\t{}\t{:.1f}+{:.1f}s'.format(
(batch + 1) * self.args.batch_size,
len(self.loader_train.dataset),
self.loss.display_loss(batch),
timer_model.release(),
timer_data.release()))
timer_data.tic()
total_train_loss += loss
avg_train_loss = total_train_loss / (batch+1)
self.loss.end_log(len(self.loader_train))
self.error_last = self.loss.log[-1, -1]
self.scheduler.step()
def val_error(self):
epoch = self.epoch
self.ckp.write_log('\nEvaluation:')
timer_model, timer_data = utility.timer(), utility.timer()
self.model.eval()
with torch.no_grad():
total_val_loss = 0
count = 0
for batch, (lr, hr, filename_lr, filename_hr) in enumerate(self.loader_val):
eval_acc = 0
#print(f'processing the {filename_lr}')
lr, hr = self.prepare([lr, hr])
sr = self.model(lr, self.scale)
_, ch, _, _ = sr.shape
org_shape = hr.shape
sr = self.post_process(sr, org_shape)
lr = lr.permute(0,2,3,1)
hr = hr.permute(0,2,3,1)
val_loss = self.mis_orient(sr, hr)
val_loss = torch.mean(val_loss)
val_loss = val_loss.detach().cpu().numpy()
#print("val loss:", val_loss)
total_val_loss += val_loss
count += 1
avg_val_loss = total_val_loss / count
print("avg Val loss:", avg_val_loss)
wandb.log({'val_loss': avg_val_loss})
#import pdb; pdb.set_trace()
self.total_val_loss_all.append(avg_val_loss)
if avg_val_loss <= min(self.total_val_loss_all[1:]):
self.ckp.save(self, epoch, is_best = True)
self.epoch_list.append(epoch)
self.ckp.plot_val_loss(self.total_val_loss_all, self.epoch_list)
if self.args.save_results and (epoch % self.args.save_model_freq) == 0:
print("--------------------Saving Model----------------------------")
self.ckp.save(self,epoch)
def test(self, is_trad_results= False):
#import pdb; pdb.set_trace()
self.model.eval()
keys = [f'sr','bilinear', 'bicubic', 'nearest']
total_psnr_dict = dict.fromkeys(keys,0)
count = 0
total_dist = 0
with torch.no_grad():
for batch, (lr, hr, filename_lr, filename_hr) in enumerate(self.loader_test):
start_time = time.time()
print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
print(f' LR Image: {filename_lr} and HR Image: {filename_hr}')
print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
modes = []
sr_up_trad = []
psnr_dict = defaultdict()
lr, hr = self.prepare([lr, hr])
#import pdb; pdb.set_trace()
sr = self.model(lr, self.scale)
org_shape = hr.shape
#Interpolations
if is_trad_results:
modes = ['bilinear', 'bicubic', 'nearest']
sr_up_trad = []
for mode in modes:
upsampling = nn.Upsample(scale_factor=self.scale, mode=mode)
sr_up = upsampling(lr)
sr_up = self.post_process(sr_up, org_shape)
sr_up_trad.append(sr_up)
sr = self.post_process(sr, org_shape)
#import pdb; pdb.set_trace()
hr = hr.permute(0,2,3,1)
lr = lr.permute(0,2,3,1)
save_list = [lr, hr, sr] + sr_up_trad
#import pdb; pdb.set_trace()
modes = ['LR', 'HR', f'SR_{self.args.model}_{self.args.model_to_load}_{self.args.dist_type}'] + modes
filenames = filename_hr
if self.args.save_results:
self.ckp.save_results(filenames, save_list, modes, self.scale, epoch = self.args.model_to_load, dataset=self.args.test_dataset_type)
end_time = time.time()
t = end_time - start_time
print("Time:", t)
def post_process(self, x, org_shape):
b, ch, h, w = org_shape
x = self.normalize(x)
x = x[:,:,0:h,0:w]
x = x.permute(0,2,3,1)
# fz_reduction
x = scalar_last2first(x)
x = fz_reduce(x, hcp_syms)
x = scalar_first2last(x)
return x
def normalize(self,x):
x_norm = torch.norm(x, dim=1, keepdim=True)
# make ||q|| = 1
y_norm = torch.div(x, x_norm)
return y_norm
def prepare(self, l, volatile=False):
device = torch.device('cpu' if self.args.cpu else 'cuda')
def _prepare(tensor):
if self.args.precision == 'half':
tensor = tensor.half()
return tensor.cuda()
return [_prepare(_l) for _l in l]
def upsample(mode, scale):
upsampling = nn.Upsample(scale_factor=scale, mode=mode)
sr_up = upsampling(lr)
sr_up = self.normalize(sr_up)
return sr_up
def terminate(self):
if self.args.test_only:
self.test()
return True
else:
#epoch = self.scheduler.last_epoch + 1
epoch = self.epoch + 1
return epoch >= self.args.epochs
def is_val(self):
epoch = self.epoch
if epoch % self.args.val_freq == 0:
return True
else:
return False