-
Notifications
You must be signed in to change notification settings - Fork 5
/
trainer.py
159 lines (117 loc) · 5.9 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
from base.trainer import GenericVideoTrainer
from base.scheduler import GradualWarmupScheduler, MyWarmupScheduler
from torch import optim
import torch
import time
import copy
import os
import numpy as np
class Trainer(GenericVideoTrainer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.best_epoch_info = {
'model_weights': copy.deepcopy(self.model.state_dict()),
'loss': 1e10,
'ccc': -1e10,
'acc': -1,
'p_r_f1': 0,
'kappa': 0,
'epoch': 0,
'metrics': {
'train_loss': -1,
'val_loss': -1,
'train_acc': -1,
'val_acc': -1,
}
}
def init_optimizer_and_scheduler(self, epoch=0):
self.optimizer = optim.Adam(self.get_parameters(), lr=self.learning_rate, weight_decay=0.001)
self.scheduler = MyWarmupScheduler(
optimizer=self.optimizer, lr = self.learning_rate, min_lr=self.min_learning_rate,
best=self.best_epoch_info['ccc'], mode="max", patience=self.patience,
factor=self.factor, num_warmup_epoch=self.min_epoch, init_epoch=epoch)
def fit(self, dataloader_dict, checkpoint_controller, parameter_controller):
if self.verbose:
print("------")
print("Starting training, on device:", self.device)
self.time_fit_start = time.time()
start_epoch = self.start_epoch
if self.best_epoch_info is None:
self.best_epoch_info = {
'model_weights': copy.deepcopy(self.model.state_dict()),
'loss': 1e10,
'ccc': -1e10
}
for epoch in np.arange(start_epoch, self.max_epoch):
if self.fit_finished:
if self.verbose:
print("\nEarly Stop!\n")
break
improvement = False
if epoch in self.milestone or (parameter_controller.get_current_lr() < self.min_learning_rate and epoch >= self.min_epoch and self.scheduler.relative_epoch > self.min_epoch):
parameter_controller.release_param(self.model.spatial, epoch)
if parameter_controller.early_stop:
break
self.model.load_state_dict(self.best_epoch_info['model_weights'])
time_epoch_start = time.time()
if self.verbose:
print("There are {} layers to update.".format(len(self.optimizer.param_groups[0]['params'])))
# Get the losses and the record dictionaries for training and validation.
train_kwargs = {"dataloader_dict": dataloader_dict, "epoch": epoch}
train_loss, train_record_dict = self.train(**train_kwargs)
validate_kwargs = {"dataloader_dict": dataloader_dict, "epoch": epoch}
validate_loss, validate_record_dict = self.validate(**validate_kwargs)
# if epoch % 1 == 0:
# test_kwargs = {"dataloader_dict": dataloader_dict, "epoch": None, "train_mode": 0}
# validate_loss, test_record_dict = self.test(checkpoint_controller=checkpoint_controller, feature_extraction=0, **test_kwargs)
# print(test_record_dict['overall']['ccc'])
if validate_loss < 0:
raise ValueError('validate loss negative')
self.train_losses.append(train_loss)
self.validate_losses.append(validate_loss)
validate_ccc = validate_record_dict['overall']['ccc']
self.scheduler.best = self.best_epoch_info['ccc']
if validate_ccc > self.best_epoch_info['ccc']:
torch.save(self.model.state_dict(), os.path.join(self.save_path, "model_state_dict" + str(validate_ccc) + ".pth"))
improvement = True
self.best_epoch_info = {
'model_weights': copy.deepcopy(self.model.state_dict()),
'loss': validate_loss,
'ccc': validate_ccc,
'epoch': epoch,
}
if self.verbose:
print(
"\n Fold {:2} Epoch {:2} in {:.0f}s || Train loss={:.3f} | Val loss={:.3f} | LR={:.1e} | Release_count={} | best={} | "
"improvement={}-{}".format(
self.fold,
epoch + 1,
time.time() - time_epoch_start,
train_loss,
validate_loss,
self.optimizer.param_groups[0]['lr'],
parameter_controller.release_count,
int(self.best_epoch_info['epoch']) + 1,
improvement,
self.early_stopping_counter))
print(train_record_dict['overall'])
print(validate_record_dict['overall'])
print("------")
checkpoint_controller.save_log_to_csv(
epoch, train_record_dict['overall'], validate_record_dict['overall'])
# Early stopping controller.
if self.early_stopping and self.scheduler.relative_epoch > self.min_epoch:
if improvement:
self.early_stopping_counter = self.early_stopping
else:
self.early_stopping_counter -= 1
if self.early_stopping_counter <= 0:
self.fit_finished = True
self.scheduler.step(metrics=validate_ccc, epoch=epoch)
self.start_epoch = epoch + 1
if self.load_best_at_each_epoch:
self.model.load_state_dict(self.best_epoch_info['model_weights'])
checkpoint_controller.save_checkpoint(self, parameter_controller, self.save_path)
self.fit_finished = True
checkpoint_controller.save_checkpoint(self, parameter_controller, self.save_path)
self.model.load_state_dict(self.best_epoch_info['model_weights'])