-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
651 lines (567 loc) · 24.4 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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Created by Brian B. Moser.
# Contact: Brian.Moser@DFKI.de
import datetime
import torch
import sys
import os
import numpy as np
import random
from tqdm import tqdm
import torch.nn as nn
from copy import deepcopy
from lucky_trainer.metrics import get_accuracy_metric, CustomLoss
from lucky_trainer.early_stopper import EarlyStopper
class Trainer(object):
"""
Class, which manages the training of the model. The main benefit of this
class is that it extracts important information out of the train parameter
dictionary to setup the EarlyStop Mechanism, the optimizer and so on.
"""
def __init__(self, model, model_params, train_params, dataset_params,
train_loader, val_loader, test_loader,
output_directory, filename, current_epoch=0,
print_param_size=True, clip_grad_norm=True,
print_progress=True, checkpoint_patience=-1,
checkpoint_epoch_notation=True, save_whole_model=False,
use_cudnn_benchmark=True, optimizer=None):
"""
Initialization of the Trainer.
:param checkpoint_epoch_notation: Boolean, if the epoch should be appear
on the saved checkpoints (it generates a new file).
:param dataset_params: Parameters of the dataset (path + name).
:param output_directory: Path to the saved file
:param filename: Name of the saved file
:param current_epoch: May be important if an experiment is continued.
:param print_param_size: Boolean, prints the amount of parameters at
the beginning of the training.
:param clip_grad_norm: Boolean, applying grad norm since RNNs tend to
have exploding gradients.
:param checkpoint_patience: Int, if it's greater than 0, than it saves
on every checkpoint_patience-th step.
:param save_whole_model: Boolean, if you only want to save the model
state or the whole model.
:param print_progress: Boolean, if current loss and maybe accuracy
should be printed after each iteration.
:param model: The model, which has to be trained
:param model_params: Model parameters for saving process.
:param train_params: Train parameter dictionary with information like
loss function, optimizer, amount of max. epochs etc.
:param train_loader: Train dataset
:param val_loader: Validation dataset
:param test_loader: Test dataset
"""
# Use CUDNN benchmark
torch.backends.cudnn.benchmark = use_cudnn_benchmark
# Make everything deterministic
self.seed_everything()
# Parameter size
if print_param_size:
self.print_param_size(model)
# Create output folder for saving the model
# and track date for saving name
os.makedirs(output_directory, exist_ok=True)
# Cosmetic settings like saving the whole model,
# get start time, save name, output directory, ...
self.start_time = self.get_start_time()
self.save_whole_model = save_whole_model
self.checkpoint_epoch_notation = checkpoint_epoch_notation
self.print_progress = print_progress
self.output_directory = os.path.abspath(output_directory)
self.filename = filename
# Get the parameters of the model, training and the dataset
self.model_params, self.train_params = model_params, train_params
self.dataset_params = dataset_params
self.current_epoch = current_epoch
self.max_epochs = train_params['max_epochs']
# Track loss/accuracy
self.history = ([], [])
# Gradient clipping for RNNs
self.clip_grad_norm = clip_grad_norm
# Setting Checkpoints
self.checkpoint_patience = checkpoint_patience
# Send to model to GPU, if enabled
self.device = torch.device('cuda'
if torch.cuda.is_available()
else 'cpu')
self.model = model
if 'multi_gpu' in train_params.keys():
if train_params['multi_gpu']:
self.model = nn.DataParallel(self.model)
self.model = self.model.to(self.device)
# Initialization of the model
if 'init' in train_params.keys():
for _init in train_params['init']:
def set_init(m, init=None, nn_type=None):
if type(m) == nn_type:
getattr(torch.nn.init, init)(m.weight)
self.model.apply(
lambda m: set_init(m, init=_init[0], nn_type=_init[1])
)
# Datasets
self.train_loader = train_loader
self.val_loader = val_loader
self.test_loader = test_loader
# Get Loss Object & Optimizer Class reference
try:
self.loss = getattr(torch.nn, train_params['loss'])(reduction='sum')
except AttributeError:
self.loss = getattr(CustomLoss, train_params['loss'])
self.opt = getattr(torch.optim, train_params['optimizer'])
# Creates optimizer with parameters given in params and a prefix "opt_"
# E.g. "opt_lr": 0.0001 item will be the parameter lr=0.0001
if optimizer is None:
self.optimizer = self.init_optimizer()
else:
self.optimizer = optimizer
# Early Stopping
self.enable_early_stopping = False
self.early_stop_flag = False
if 'early_stopping_patience' in train_params.keys():
self.enable_early_stopping = True
min_epochs = 0
if 'early_stopping_min_epochs' in train_params.keys():
min_epochs = train_params['early_stopping_min_epochs']
self.early_stopping = EarlyStopper(
self, train_params['early_stopping_patience'], min_epochs
)
# If accuracy is enabled
self.enable_accuracy_metric = False
if 'acc_metric' in train_params.keys():
self.enable_accuracy_metric = True
try:
self.acc_function, self.acc_params = get_accuracy_metric(
train_params
)
except TypeError:
print("Accuracy metric <" + str(train_params['acc_metric'])
+ "> is not defined. Disabling Accuracy measurement.")
self.enable_accuracy_metric = False
# Backpropagation through time
self.enable_bppt = 'bppt_axis' in train_params.keys()
self.bppt_axis = train_params['bppt_axis'] \
if self.enable_bppt else None
self.skip_test = train_params['skip_test'] \
if 'skip_test' in train_params.keys() else False
self.k_fold = train_params['k_fold'] \
if 'k_fold' in train_params.keys() else 0
self.enable_aux_training = train_params['aux_training'] \
if 'aux_training' in train_params.keys() else False
# If learn-rate decay is enabled
self.lr_decay = False
self.scheduler, self.lr_decay_step_size = None, None
self.lr_decay_steps = None
self.setup_lr_decay()
def train(self):
# Normal training is like training for one fold
if self.k_fold == 0:
return self.train_one_fold(
self.train_loader,
self.val_loader,
self.test_loader
)
else:
if type(self.k_fold) is int:
ks_list = range(self.k_fold)
fold_amount = self.k_fold
else:
ks_list = self.k_fold[1]
fold_amount = self.k_fold[0]
# Get the points to divide the dataset into folds
fold_size = len(self.train_loader.dataset) // fold_amount
split_size = [fold_size
for _ in range(fold_amount - 1)]
split_size.append(
len(self.train_loader.dataset) - (fold_amount - 1) * fold_size
)
folds = torch.utils.data.random_split(
self.train_loader.dataset,
lengths=split_size
)
# Save some configs for k-fold
model_backup = deepcopy(self.model)
original_filename = self.filename
self.skip_test = True
result = np.zeros(4 if self.enable_accuracy_metric else 2)
# Apply K-Fold
for i, k in enumerate(ks_list):
# Config filename corresponding to
# the current k-cross-validation
self.filename = original_filename + "_K" + str(k+1) + "_"
# Reset values for training
if i > 0:
self.model = deepcopy(model_backup)
self.early_stopping.reset()
self.setup_lr_decay()
self.optimizer = self.init_optimizer()
self.current_epoch = 0
# Assign train and test/val folds
train_set = torch.utils.data.ConcatDataset(
folds[:k] + folds[k + 1:]
)
val_set = folds[k]
# Make a DataLoader out of them
train_set = torch.utils.data.DataLoader(
train_set,
batch_size=self.train_loader.batch_size,
shuffle=True,
num_workers=self.train_loader.num_workers
)
val_set = torch.utils.data.DataLoader(
val_set,
batch_size=self.train_loader.batch_size,
shuffle=False,
num_workers=self.train_loader.num_workers
)
# Apply one fold
result += np.array(self.train_one_fold(
train_set,
val_set,
None
))
print("Fold", str(k), "completed.")
# Calculate overall performance of K-Fold
result /= len(ks_list)
print("Average Performance of "
+ str(self.k_fold)
+ "-Fold Cross-Validation:"
+ str(result))
# Releases all unoccupied cached memory currently held by
# the caching allocator so that those can be used in other
# GPU application and visible in nvidia-smi
torch.cuda.empty_cache()
def train_one_fold(self, train_set, val_set, test_set):
"""
Trains the model given by the params of the initialization.
:return: Returns nothing, but the model is now trained.
"""
train_result, val_result = None, None
epoch, checkpoint_counter = 0, 0
for epoch in range(self.current_epoch, self.max_epochs):
# Check if Early Stop is activated
if self.early_stop_flag:
break
# Learn-rate counter
if self.lr_decay:
if self.lr_decay_steps is not None:
if len(self.lr_decay_steps) > 0:
if epoch == self.lr_decay_steps[0][0]:
for params in self.optimizer.param_groups:
params['lr'] = self.lr_decay_steps[0][1]
del self.lr_decay_steps[0]
elif (type(self.lr_decay_step_size) is int
or epoch in self.lr_decay_step_size):
self.scheduler.step()
# Train one epoch
train_result, val_result = self.train_one_epoch(train_set, val_set)
# Print result of one epoch
self.print_result('train', train_result, epoch)
self.print_result('val', val_result, epoch)
# Update Early Stopping Class, if enabled
if self.enable_early_stopping:
self.early_stopping.update(
epoch,
train_result[0],
val_result[0]
)
# Checkpoint Counter for saving the model during training
if self.checkpoint_patience > -1:
if checkpoint_counter == self.checkpoint_patience:
checkpoint_counter = 0
# For saving with epoch in filename
overhead = "_epoch_" + str(epoch) \
if self.checkpoint_epoch_notation else ""
# For saving with accuracy
if self.enable_accuracy_metric:
save_name = str(val_result[1]) + "_val_acc"
else:
save_name = str(val_result[0]) + "_val_loss"
# Save a checkpoint
self.save_progress(self.output_directory,
self.filename + overhead,
save_name)
else:
checkpoint_counter += 1
# Test Phase
if self.enable_early_stopping:
# Load the best working model
self.model.load_state_dict(self.early_stopping.bestModelState)
self.model.eval()
# Calculate and print performance in the end
if not self.skip_test:
test_result = self.calculate_loss_and_acc(test_set)
else:
test_result = self.calculate_loss_and_acc(val_set)
self.print_result('test', test_result, epoch)
# Save the last model with its performance
if self.enable_accuracy_metric:
self.save_progress(
self.output_directory, self.filename, test_result[1]
)
return train_result[0], train_result[1], test_result[0], test_result[1]
else:
self.save_progress(
self.output_directory, self.filename, test_result[0]
)
return train_result[0], test_result[0]
def calculate_loss_and_acc(self, loader):
"""
Calculates the loss and accuracy of the model applied on a given
dataset (Training or Validation).
:param loader: The (Training or Validation) dataset
:return: Loss and Accuracy of the model of the current epoch.
"""
with torch.no_grad():
loss_total = 0
total = 0
sum_correct = 0
for _input, _target in loader:
_target = _target.to(self.device)
_input = _input.to(self.device)
outputs = self.model(_input)
if self.enable_aux_training:
outputs = outputs[0]
total += _target.size(0)
# Count for accuracy, if enabled
if self.enable_accuracy_metric:
sum_correct += self.acc_function(
outputs,
_target,
**self.acc_params
)
loss_total += self.loss(outputs, _target).item()
loss = loss_total / total
acc = None
if self.enable_accuracy_metric:
acc = 100 * sum_correct / total
return loss, acc
def train_one_epoch(self, train_set, val_set):
# Train Phase
self.model.train()
loss_total, total, sum_correct = 0, 0, 0
pbar = tqdm(train_set, leave=False,
file=sys.stdout, ascii=True)
for _input, _target in pbar:
# Load input and target to device (like GPU)
_input = _input.to(self.device)
_target = _target.to(self.device)
# Count amount of images
total += _target.size(0)
# Clear gradients w.r.t. parameters
self.optimizer.zero_grad()
# Calculate output
outputs = self.model(_input)
# Calculate Loss
# Backpropagation through time
if self.enable_bppt:
loss = 0
for timestep in range(_target.shape[self.bppt_axis]):
timestep_loss = self.loss(
outputs.select(dim=self.bppt_axis, index=timestep),
_target.select(dim=self.bppt_axis, index=timestep)
)
loss += timestep_loss
# If auxiliary classifiers (multiple outputs)
elif self.enable_aux_training:
loss = 0
for output in outputs:
loss += self.loss(output, _target)
outputs = outputs[0]
# Simple loss calculation
else:
loss = self.loss(outputs, _target)
loss_total += loss.item()
# Calculate accuracy on train
if self.enable_accuracy_metric:
sum_correct += self.acc_function(
outputs,
_target,
**self.acc_params
)
# Getting gradients w.r.t. parameters
loss.backward()
# RNNs tend to have exploding gradients
# (see https://arxiv.org/pdf/1211.5063.pdf)
if self.clip_grad_norm:
nn.utils.clip_grad_norm_(self.model.parameters(), 1)
# Updating parameters
self.optimizer.step()
# tqdm with current loss & acc
if self.print_progress:
desc = "[loss:" + str(loss_total / total)
if self.enable_accuracy_metric:
desc += ", acc:" + str(100 * sum_correct / total)
desc += "]"
pbar.set_description(desc)
# End of train, calculating validation and increase epoch
self.current_epoch += 1
self.model.eval()
# Calculate Avg. Loss & Avg. Accuracy for train
train_loss = loss_total / total
train_acc = None
if self.enable_accuracy_metric:
train_acc = 100 * sum_correct / total
# Calculate Avg. Loss & Avg. Accuracy for val
val_loss, val_acc = self.calculate_loss_and_acc(val_set)
# Save losses and accuracies
self.history[0].append((train_loss, train_acc))
self.history[1].append((val_loss, val_acc))
return [train_loss, train_acc], [val_loss, val_acc]
def get_state_dict(self):
"""
Returns the state dict. of the model.
:return: State dict. of the model
"""
if self.enable_early_stopping:
return self.early_stopping.get_state_dict()
else:
if torch.cuda.is_available():
model_state = self.model.cpu().state_dict()
self.model.to(self.device)
return model_state, self.optimizer.state_dict()
else:
return self.model.state_dict(), self.optimizer.state_dict()
def save_progress(self, output_directory, file_name, performance):
"""
Saving function. It splits up in two parts: whole model or not (saving
just the weights of the model).
:param output_directory: Output directory (path to it)
:param file_name: Name of the saving.
:param performance: It will be appended on the name. Accuracy or loss,
if accuracy is not given.
"""
# Loading the results and the states of the training (model and
# optimizer state).
self.model.cpu()
model_state, optimizer_state = self.model.state_dict(), self.optimizer.state_dict()
# Saving the whole model.
if self.save_whole_model:
save_dict = {
'model': self.model,
'train_params': self.train_params,
'dataset_params': self.dataset_params,
'history': self.history,
'model_state': model_state,
'optimizer_state': optimizer_state,
}
file_name += "_wholeModel_"
# Saving just the weights and the params to build the model.
else:
save_dict = {
'model_params': self.model_params,
'train_params': self.train_params,
'dataset_params': self.dataset_params,
'history': self.history,
'model_state': model_state,
'optimizer_state': optimizer_state,
}
# Actual saving process
print("\nSaving Model in "
+ output_directory + '/'
+ file_name + " ...")
torch.save(save_dict, output_directory + '/' + file_name
+ "_" + str(performance)
+ "_" + self.start_time
+ ".pth")
print("Saved.")
self.model.to(self.device)
def init_optimizer(self):
return self.opt(
self.model.parameters(),
**dict((key[4:], value)
for key, value in self.train_params.items()
if key[:4] == "opt_")
)
def setup_lr_decay(self):
if 'lr_cosine_decay_min' in self.train_params.keys():
self.scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
self.optimizer,
self.train_params['max_epochs'],
eta_min=self.train_params['lr_cosine_decay_min'])
elif 'lr_decay_patience' in self.train_params.keys() and \
'lr_decay_gamma' in self.train_params.keys():
threshold = 0.0001
if 'lr_threshold' in self.train_params.keys():
threshold = self.train_params['lr_threshold']
self.scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
self.optimizer,
factor=self.train_params['lr_decay_gamma'],
mode='min',
threshold=threshold,
patience=self.train_params['lr_decay_patience']
)
elif 'lr_decay_gamma' in self.train_params.keys() and \
'lr_decay_step_size' in self.train_params.keys():
self.lr_decay = True
if type(self.train_params['lr_decay_step_size']) is int:
self.scheduler = torch.optim.lr_scheduler.StepLR(
self.optimizer,
self.train_params['lr_decay_step_size'],
self.train_params['lr_decay_gamma']
)
else:
self.scheduler = torch.optim.lr_scheduler.StepLR(
self.optimizer,
1,
self.train_params['lr_decay_gamma']
)
self.lr_decay_step_size = self.train_params['lr_decay_step_size']
elif 'lr_decay_steps' in self.train_params.keys():
self.lr_decay = True
self.lr_decay_steps = self.train_params['lr_decay_steps']
@staticmethod
def print_result(text_type, result, epoch):
text_snippets = {
'train': [
'Training Results - Epoch: {} | ',
'Avg train-loss: {:.5f}',
' | Avg train-accuracy: '
],
'val': [
'Validation Results - Epoch: {} | ',
'Avg val-loss: {:.5f}',
' | Avg val-accuracy: '
],
'test': [
'Test Results - Epoch: {} | ',
'Avg test-loss: {:.5f}',
' | Avg test-accuracy: '
]
}
(loss, acc) = result
text_snippet = text_snippets[text_type]
result = text_snippet[0].format(epoch)
result += text_snippet[1].format(loss)
if acc is not None:
result += text_snippet[2] + str(acc)
print(result)
@staticmethod
def seed_everything(seed=1337):
"""
Makes the model nearly deterministic (it's not deterministic because
of the cudnn.benchmark=True statement in the beginning of the script).
:param seed: Seed number like for random. Default: 1337
"""
random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
torch.backends.cudnn.deterministic = True
@staticmethod
def get_start_time():
currentDT = datetime.datetime.now()
return "{0}-{1}-{2}--{3}-{4}".format(
str(currentDT.year), str(currentDT.month), str(currentDT.day),
str(currentDT.hour), str(currentDT.minute))
@staticmethod
def print_param_size(model):
model_parameters = filter(
lambda p: p.requires_grad, model.parameters()
)
params = sum([
np.prod(p.size()) for p in model_parameters
])
print("Param-Size:", params)