-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
276 lines (222 loc) · 9.58 KB
/
train.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
import argparse
import sys
import time
import math
from datetime import datetime
from typing import List, Tuple
import numpy as np
from tensorboardX import SummaryWriter
from torch.optim.optimizer import Optimizer
from torch.utils.data import DataLoader
from models import GeneralModel
from utils.constants import *
from utils.model_utils import save_models, calculate_accuracy
from utils.system_utils import setup_directories, save_codebase_of_run
class Trainer:
def __init__(self,
data_loader_train: DataLoader,
data_loader_validation: DataLoader,
model: GeneralModel,
optimizer: Optimizer,
loss_function: GeneralModel,
args: argparse.Namespace,
patience: int,
device="cpu"):
self.arguments = args
self.loss_function = loss_function
self.optimizer = optimizer
self.model = model
self.data_loader_validation = data_loader_validation
self.data_loader_train = data_loader_train
self._log_header = ' Time Epoch Iteration Progress (%Epoch) | Train Loss Train Acc. | Valid Loss Valid Acc. | Best | VAE-stuff'
self._log_template = ' '.join(
'{:>6.0f},{:>5.0f},{:>9.0f},{:>5.0f}/{:<5.0f} {:>7.0f}%,| {:>10.6f} {:>10.6f} | {:>10.6f} {:>10.6f} | {:>4s} | {:>4s}'.split(
','))
self._start_time = time.time()
self._device = device
self._patience = patience
# validate input to class
self._validate_self()
# init current runs timestamp
DATA_MANAGER.set_date_stamp(addition=args.run_name)
# initialize tensorboardx
self.writer = SummaryWriter(os.path.join(GITIGNORED_DIR, RESULTS_DIR, DATA_MANAGER.stamp, SUMMARY_DIR))
self._start_time = time.time()
def _validate_self(self):
pass #todo
def train(self) -> bool:
"""
main training function
"""
# setup data output directories:
setup_directories()
save_codebase_of_run(self.arguments)
# data gathering
progress = []
epoch = 0
try:
print(f"{PRINTCOLOR_BOLD}Started training with the following config:{PRINTCOLOR_END}\n{self.arguments}\n\n")
print(self._log_header)
best_metrics = (math.inf, 0)
patience = self._patience
# run
for epoch in range(self.arguments.epochs):
# do epoch
epoch_progress, best_metrics, patience = self._epoch_iteration(epoch, best_metrics, patience)
# add progress-list to global progress-list
progress += epoch_progress
# write progress to pickle file (overwrite because there is no point keeping seperate versions)
DATA_MANAGER.save_python_obj(progress,
os.path.join(RESULTS_DIR, DATA_MANAGER.stamp, PROGRESS_DIR,
"progress_list"),
print_success=False)
# flush prints
sys.stdout.flush()
if patience == 0:
break
except KeyboardInterrupt as e:
print(f"Killed by user: {e}")
save_models([self.model], f"KILLED_at_epoch_{epoch}")
return False
except Exception as e:
print(e)
save_models([self.model], f"CRASH_at_epoch_{epoch}")
raise e
# flush prints
sys.stdout.flush()
# example last save
save_models([self.model], "finished")
return True
def _epoch_iteration(
self,
epoch_num: int,
best_metrics: Tuple[float, float],
patience: int) -> Tuple[List, Tuple, int]:
"""
one epoch implementation
"""
if not self.arguments.train_classifier:
self.loss_function.reset()
progress = []
train_accuracy = 0
train_loss = 0
data_loader_length = len(self.data_loader_train)
for i, (batch, targets, lengths) in enumerate(self.data_loader_train):
print(f'Train: {i}/{data_loader_length} \r', end='')
# do forward pass and whatnot on batch
loss_batch, accuracy_batch = self._batch_iteration(batch, targets, lengths, i)
train_loss += loss_batch
train_accuracy += accuracy_batch
# add to list somehow:
progress.append({"loss": loss_batch, "acc": accuracy_batch})
# calculate amount of batches and walltime passed
batches_passed = i + (epoch_num * len(self.data_loader_train))
time_passed = datetime.now() - DATA_MANAGER.actual_date
# run on validation set and print progress to terminal
# if we have eval_frequency or if we have finished the epoch
if (batches_passed % self.arguments.eval_freq) == 0 or (i + 1 == data_loader_length):
loss_validation, acc_validation = self._evaluate()
new_best = False
if self.model.compare_metric(best_metrics, loss_validation, acc_validation):
save_models([self.model], 'model_best')
best_metrics = (loss_validation, acc_validation)
new_best = True
patience = self._patience
else:
patience -= 1
self._log(
loss_validation,
acc_validation,
(train_loss / (i + 1)),
(train_accuracy / (i + 1)),
batches_passed,
float(time_passed.microseconds),
epoch_num,
i,
data_loader_length,
new_best)
# check if runtime is expired
if (time_passed.total_seconds() > (self.arguments.max_training_minutes * 60)) \
and self.arguments.max_training_minutes > 0:
raise KeyboardInterrupt(f"Process killed because {self.arguments.max_training_minutes} minutes passed "
f"since {DATA_MANAGER.actual_date}. Time now is {datetime.now()}")
if patience == 0:
break
return progress, best_metrics, patience
def _batch_iteration(self,
batch: torch.Tensor,
targets: torch.Tensor,
lengths: torch.Tensor,
step: int,
train_mode: bool = True) -> Tuple[float, float]:
"""
runs forward pass on batch and backward pass if in train_mode
"""
batch = batch.to(self._device)
targets = targets.to(self._device)
lengths = lengths.to(self._device)
if train_mode:
self.model.train()
self.optimizer.zero_grad()
else:
self.model.eval()
output = self.model.forward(batch, lengths=lengths, step=step, label=targets)
loss = self.loss_function.forward(targets, *output)
if train_mode:
loss.backward()
torch.nn.utils.clip_grad_norm_(self.model.parameters(), max_norm=5.0)
self.optimizer.step()
accuracy = 0
if self.arguments.train_classifier:
accuracy = calculate_accuracy(targets, *output).item()
return loss.item(), accuracy
def _evaluate(self) -> Tuple[float, float]:
"""
runs iteration on validation set
"""
accuracies = []
losses = []
data_loader_length = len(self.data_loader_validation)
for i, (batch, targets, lengths) in enumerate(self.data_loader_validation):
print(f'Validation: {i}/{data_loader_length} \r', end='')
# do forward pass and whatnot on batch
loss_batch, accuracy_batch = self._batch_iteration(batch, targets, lengths, i, train_mode=False)
accuracies.append(accuracy_batch)
losses.append(loss_batch)
return float(np.mean(losses)), float(np.mean(accuracies))
def _log(self,
loss_validation: float,
acc_validation: float,
loss_train: float,
acc_train: float,
batches_done: int,
time_passed: float,
epoch: int,
iteration: int,
iterations: int,
new_best: bool):
"""
logs progress to user through tensorboard and terminal
"""
if self.arguments.train_classifier:
self.writer.add_scalar("Accuracy_validation", acc_validation, batches_done, time_passed)
self.writer.add_scalar("Accuracy_train", acc_train, batches_done, time_passed)
else:
for key, value in self.loss_function.get_losses().items():
self.writer.add_scalar(key, value, batches_done, time_passed)
print(self._log_template.format(
time.time() - self._start_time,
epoch,
iteration,
1 + iteration,
iterations,
100. * (1 + iteration) / iterations,
loss_train,
acc_train,
loss_validation,
acc_validation,
"BEST" if new_best else "",
"" if self.arguments.train_classifier else str(self.loss_function.get_losses())
))
self.writer.add_scalar("Loss_validation", loss_validation, batches_done, time_passed)
self.writer.add_scalar("Loss_train", loss_train, batches_done, time_passed)