-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseTrainer.py
478 lines (402 loc) · 19.2 KB
/
BaseTrainer.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
467
468
469
470
471
472
473
474
475
476
477
478
import os
import math
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.init as init
import time
import numpy as np
from Utils.tools import AverageMeter, ensure_dir, match_image_label
from metrics import Metrics
from opts import visdom_init, visdom_update
class BaseTrainer(object):
def __init__(self,
model,
configs,
args,
loader_train,
loader_valid,
begin_time,
resume_file,
loss_weight,
visdom):
super(BaseTrainer, self).__init__()
print(" + Training Start ... ...")
# for general
self.configs = configs
self.args = args
self.device = (self._device(self.args.gpu))
self.model = model.to(self.device)
self.loader_train = loader_train
# TODO validation phase controller
self.loader_valid = loader_valid
# for time
self.begin_time = begin_time # part of ckpt name
self.save_period = self.configs.save_period # for save ckpt
self.dis_period = self.configs.dis_period # for display
# directory
self.path_checkpoints = os.path.join(self.configs.path_output, 'checkpoints', model.name, self.begin_time)
self.path_logs = os.path.join(self.configs.path_output, 'logs', model.name, self.begin_time)
ensure_dir(self.path_checkpoints)
ensure_dir(self.path_logs)
if self.loader_train is not None and self.loader_valid is not None:
self.history = {
'train': {
'epoch': [],
'loss': [],
'accuracy': [],
'miou': [],
'f1score': [],
},
'valid': {
'epoch': [],
'loss': [],
'accuracy': [],
'miou': [],
'f1score': [],
}
}
else:
self.history = {
'train': {
'epoch': [],
'loss': [],
'accuracy': [],
'miou': [],
'f1score': [],
}
}
# TODO
# for resume update curve
self.windows_name = {
'miou': [],
'loss': [],
'accuracy': [],
'lr': [],
'f1score': [],
}
# for optimizer
self.loss_weight = loss_weight.to(self.device)
self.loss = self._loss(loss_function=self.configs.loss_fn).to(self.device)
self.optimizer = self._optimizer(lr_algorithm=self.configs.optimizer)
self.lr_scheduler = self._lr_scheduler()
self.weight_init_algorithm = self.configs.weight_init_algorithm
self.current_lr = self.configs.init_lr
print(self.optimizer)
print(self.loss)
# for training
self.start_epoch = 1
self.early_stop = self.configs.early_stop # early stop steps
self.monitor_mode = self.configs.monitor.split('/')[0]
self.monitor_metric = self.configs.monitor.split('/')[1]
self.monitor_best = -math.inf
self.best_epoch = -1
# monitor
if self.monitor_mode != 'off':
assert self.monitor_mode in ['min', 'max']
self.monitor_best = math.inf if self.monitor_mode == 'min' else -math.inf
# for resuming
self.resume_file = resume_file
self.resume = True if self.resume_file is not None else False
if self.resume:
self._resume_ckpt(resume_file = self.resume_file)
self.visdom = visdom
def train(self):
if self.visdom is not None:
if self.resume == False:
# create panes for training phase for loss metrics learning_rate
#print(" + Visualization init ... ...")
visdom_windows = visdom_init(self.visdom,
['train', 'valid'] if self.loader_valid is not None else ['train', 'test'],
['loss','accuracy','f1score','lr','miou'],configs=self.configs)
self.windows_name['loss'].append(str(visdom_windows[0]))
self.windows_name['accuracy'].append(str(visdom_windows[1]))
self.windows_name['f1score'].append(str(visdom_windows[2]))
self.windows_name['lr'].append(str(visdom_windows[3]))
self.windows_name['miou'].append(str(visdom_windows[4]))
else:
# resume condition here already loaded the resume_file in the init phase of the class
print(" + Loading visdom file ... ... Done!")
print(" + Visdom Loaded, Training !")
else:
print(" + Visdom unabled, Training !")
total_epochs = self.configs.epochs
for epoch in range(self.start_epoch, total_epochs + 1):
train_log = self._train_epoch(epoch)
if self.loader_valid is not None:
eval_log = self._eval_epoch(epoch)
# if self.loader_valid is None, choose loader_test to get best ckpt
visdom_update(self.visdom, ['loss', 'accuracy', 'f1score', 'lr', 'iou'],
epoch, self.windows_name, self.current_lr, train_log=train_log, eval_log=eval_log)
# save ckpt and best ckpt
best = False
not_improved_count = 0
if self.monitor_mode != 'off':
improved = (self.monitor_mode == 'min' and eval_log['val_'+self.monitor_metric] < self.monitor_best) or \
(self.monitor_mode == 'max' and eval_log['val_'+self.monitor_metric] > self.monitor_best)
if improved:
# TODO need to confirm
self.monitor_best = eval_log['val_'+self.monitor_metric]
best = True
self.best_epoch = eval_log['epoch']
else:
not_improved_count += 1
if not_improved_count > self.early_stop:
print(" + Validation Performance didn\'t improve for {} epochs."
" + Training stop :/"
.format(not_improved_count))
break
if epoch % self.save_period == 0 or best == True:
self._save_ckpt(epoch, is_best = best)
# saving the history when training is done
print(" + Saving History ... ... ")
hist_path = os.path.join(self.path_logs, 'history_train_valid.txt')
with open(hist_path, 'w') as f:
f.write(str(self.history))
def _train_epoch(self, epoch):
# lr update
if self.lr_scheduler is not None:
self.lr_scheduler.step(epoch)
for param_group in self.optimizer.param_groups:
self.current_lr = param_group['lr']
batch_time = AverageMeter()
data_time = AverageMeter()
ave_loss = AverageMeter()
ave_acc = AverageMeter()
ave_iou = AverageMeter()
ave_iou_pc = AverageMeter() # per-class
ave_f1 = AverageMeter()
ave_f1_pc = AverageMeter() # per-class
self.model.train()
tic = time.time()
for step, (data, target) in enumerate(self.loader_train, start = 1):
data = data.to(self.device, non_blocking = True)
target = target.to(self.device, non_blocking = True)
data_time.update(time.time() - tic)
# forward
logits = self.model(data)
loss = self.loss(logits, target) # CELoss includes softmax
# metrics
metrics = Metrics(logits, target, self.configs.nb_classes)
# TODO return cpu version: test
acc = metrics.acc
f1_score_pc, f1_score_overall = metrics.f1_score, metrics.mean_f1_score
iou_pc, iou_overall = metrics.iou, metrics.mean_iou
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
# update average metrics
# multi-class item() can not perform well
batch_time.update(time.time() - tic)
ave_loss.update(loss.data.item())
# TODO return cpu version: test
ave_acc.update(acc.data.item())
ave_f1_pc.update(f1_score_pc.data)
ave_f1.update(f1_score_overall.data.item())
ave_iou_pc.update(iou_pc.data)
ave_iou.update(iou_overall.data.item())
# TODO nan detector
assert ave_acc.average() != float('nan') and ave_f1.average() != float('nan') and \
(ave_f1_pc.average() != float('nan')).all(), \
'Appears nan value in {}epoch {}step in training phase!'.format(epoch, step)
if step % self.dis_period == 0:
print('Epoch: [{}][{}/{}],\n'
'Learning_Rate: {:.6f},\n'
'Time: {:.4f}, Data: {:.4f},\n'
'F1_Score: {:6.4f}, IoU:{:6.4f}\n'
'class0: {:6.4f}, {:6.4f}\n'
'class1: {:6.4f}, {:6.4f}\n'
'class2: {:6.4f}, {:6.4f}\n'
'class3: {:6.4f}, {:6.4f}\n'
'class4: {:6.4f}, {:6.4f}\n'
'class5: {:6.4f}, {:6.4f}\n'
'Accuracy: {:6.4f}, Loss: {:.6f}'
.format(epoch, step, len(self.loader_train),
self.current_lr,
batch_time.average(), data_time.average(),
ave_f1.average(), ave_iou.average(),
ave_f1_pc.average()[0], ave_iou_pc.average()[0],
ave_f1_pc.average()[1], ave_iou_pc.average()[1],
ave_f1_pc.average()[2], ave_iou_pc.average()[2],
ave_f1_pc.average()[3], ave_iou_pc.average()[3],
ave_f1_pc.average()[4], ave_iou_pc.average()[4],
ave_f1_pc.average()[5], ave_iou_pc.average()[5],
ave_acc.average(), ave_loss.average()))
tic = time.time()
# train log and return
self.history['train']['epoch'].append(epoch)
self.history['train']['loss'].append(ave_loss.average())
self.history['train']['accuracy'].append(ave_acc.average())
self.history['train']['f1score'].append(ave_f1.average())
self.history['train']['miou'].append(ave_iou.average())
return {
'epoch': epoch,
'loss': ave_loss.average(),
'accuracy': ave_acc.average(),
'miou': ave_iou.average(),
'f1score': ave_f1.average(),
}
def _eval_epoch(self, epoch):
batch_time = AverageMeter()
data_time = AverageMeter()
ave_loss = AverageMeter()
ave_acc = AverageMeter()
ave_iou = AverageMeter()
ave_iou_pc = AverageMeter()
ave_f1 = AverageMeter()
ave_f1_pc = AverageMeter()
self.model.eval()
with torch.no_grad():
tic = time.time()
for steps, (data, target) in enumerate(self.loader_valid, start=1):
# processing no blocking
# non_blocking tries to convert asynchronously with respect to the host if possible
# converting CPU tensor with pinned memory to CUDA tensor
# overlap transfer if pinned memory
data = data.to(self.device, non_blocking=True)
target = target.to(self.device, non_blocking=True)
data_time.update(time.time() - tic)
logits = self.model(data)
loss = self.loss(logits, target)
# TODO return cpu version :test
metrics = Metrics(logits, target, self.configs.nb_classes)
acc = metrics.acc
f1_score_per_class, f1_score_overall = metrics.f1_score, metrics.mean_f1_score
iou_per_class, iou_overall = metrics.iou, metrics.mean_iou
# update ave metrics
batch_time.update(time.time()-tic)
ave_loss.update(loss.data.item())
# TODO return cpu version : test
ave_acc.update(acc.data.item()) # overall
ave_f1.update(f1_score_overall.data.item()) # overall
ave_f1_pc.update(f1_score_per_class.data) # per_class
ave_iou_pc.update(iou_per_class.data)
ave_iou.update(iou_overall.data.item())
tic = time.time()
# TODO nan detector
assert ave_acc.average() != float('nan') and ave_f1.average() != float('nan') and \
(ave_f1_pc.average() != float('nan')).all(), \
'Appears nan value in {}epoch {}step of valid phase!'.format(epoch ,steps)
# display validation at the end
print('Epoch {} validation done !'.format(epoch))
print('Time: {:.4f}, Data: {:.4f},\n'
'F1_Score: {:6.4f}, IoU:{:6.4f}\n'
'class0: {:6.4f}, {:6.4f}\n'
'class1: {:6.4f}, {:6.4f}\n'
'class2: {:6.4f}, {:6.4f}\n'
'class3: {:6.4f}, {:6.4f}\n'
'class4: {:6.4f}, {:6.4f}\n'
'class5: {:6.4f}, {:6.4f}\n'
'Accuracy: {:6.4f}, Loss: {:.6f}'
.format(batch_time.average(), data_time.average(),
ave_f1.average(), ave_iou.average(),
ave_f1_pc.average()[0], ave_iou_pc.average()[0],
ave_f1_pc.average()[1], ave_iou_pc.average()[1],
ave_f1_pc.average()[2], ave_iou_pc.average()[2],
ave_f1_pc.average()[3], ave_iou_pc.average()[3],
ave_f1_pc.average()[4], ave_iou_pc.average()[4],
ave_f1_pc.average()[5], ave_iou_pc.average()[5],
ave_acc.average(), ave_loss.average()))
self.history['valid']['epoch'].append(epoch)
self.history['valid']['loss'].append(ave_loss.average())
self.history['valid']['f1score'].append(ave_f1.average())
self.history['valid']['accuracy'].append(ave_acc.average())
self.history['valid']['miou'].append(ave_iou.average())
# validation log and return
return {
'epoch': epoch,
'val_loss': ave_loss.average(),
'val_accuracy': ave_acc.average(),
'val_miou': ave_iou.average(),
'val_f1score': ave_f1.average()
}
def _device(self, gpu):
if gpu == -1:
device = torch.device('cpu')
return device
else:
device = torch.device('cuda:{}'.format(gpu))
return device
def _loss(self, loss_function):
"""
add the loss function that you need
:param loss_function: cross_entropy
:return:
"""
if loss_function == 'crossentropy':
loss = nn.CrossEntropyLoss(weight=self.loss_weight)
return loss
def _optimizer(self, lr_algorithm):
if lr_algorithm == 'adam':
optimizer = optim.Adam(self.model.parameters(),
lr=self.configs.init_lr,
betas=(0.9, 0.999),
eps=self.configs.epsilon,
weight_decay=self.configs.weight_decay,
amsgrad=False)
return optimizer
if lr_algorithm == 'sgd':
optimizer = optim.SGD(self.model.parameters(),
lr=self.configs.init_lr,
momentum=self.configs.momentum,
dampening=0,
weight_decay=self.configs.weight_decay,
nesterov=True)
return optimizer
def _lr_scheduler(self):
# poly learning scheduler
lambda1 = lambda epoch: pow((1-((epoch-1)/self.configs.epochs)), 0.9)
lr_scheduler = optim.lr_scheduler.LambdaLR(self.optimizer, lr_lambda=lambda1)
return lr_scheduler
def _weight_init(self, module):
# no bias use
#classname = module.__class__.__name__
#if classname.find('Conv') != -1:
if isinstance(module, nn.Conv2d):
if self.weight_init_algorithm == 'kaiming':
init.kaiming_normal_(module.weight.data)
else:
init.xavier_normal_(module.weight.data)
#elif classname.find('BatchNorm') != -1:
elif isinstance(module, nn.BatchNorm2d):
module.weight.data.normal_(1.0, 0.02)
module.bias.data.fill_(0)
def _save_ckpt(self, epoch, is_best):
state = {
'epoch': epoch + 1,
'arch': str(self.model),
'state_dict': self.model.state_dict(),
'optimizer': str(self.optimizer),
'optimizer_state_dict': self.optimizer.state_dict(),
'monitor_metric': self.monitor_metric,
'monitor_best': self.monitor_best,
'history': self.history,
'windows_name': self.windows_name,
}
filename = os.path.join(self.path_checkpoints, 'checkpoint-epoch{}.pth'.format(epoch))
if is_best:
best_filename = os.path.join(self.path_checkpoints, 'checkpoint-best.pth')
print(" + Saving Best Checkpoint : Epoch {} path: {} ... ".format(epoch, best_filename))
torch.save(state, best_filename)
else:
print(" + Saving Checkpoint per {} epochs, path: {} ... ".format(self.save_period, filename))
torch.save(state, filename)
def _resume_ckpt(self, resume_file):
resume_path = os.path.join(resume_file)
print(" + Loading Checkpoint: {} ... ".format(resume_path))
checkpoint = torch.load(resume_path)
self.start_epoch = checkpoint['epoch']
assert str(self.model) == checkpoint['arch'], \
'The model architecture of the checkpoint is not matched to the current model architecture'
self.model.load_state_dict(checkpoint['state_dict'])
assert str(self.optimizer) == checkpoint['optimizer'], \
'The optimizer of the checkpoint is not matched to the current optimizer'
self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
assert self.monitor_metric == checkpoint['monitor_metric'], \
'The monitor metric is not matched the current monitor metric'
self.monitor_best = checkpoint['monitor_best']
self.history = checkpoint['history']
self.windows_name = checkpoint['windows_name']
print(" + Checkpoint file: '{}' , Start epoch {} Loaded !\n"
" + Prepare to run ! ! !"
.format(resume_path, self.start_epoch))