-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrainer.py
339 lines (255 loc) · 10.5 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
import h5py
import logging
import numpy as np
from tqdm import tqdm
from os.path import join
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import ReduceLROnPlateau, StepLR
from tensorboardX import SummaryWriter
import torchvision
from torchvision.transforms import Compose, GaussianBlur, RandomErasing, RandomRotation, Resize, RandomHorizontalFlip, RandomGrayscale, RandomVerticalFlip
from torchvision.transforms import RandomCrop, CenterCrop, ToTensor, Lambda, RandomPerspective, ColorJitter, FiveCrop, TenCrop, ToPILImage
from utils import eval_metrics, eval_score_for_competition
def train(model: torch.nn.Module, loader: DataLoader, valid_loader: DataLoader, optimizer: torch.optim, training_classes_weights: np.array, epochs: int, train_iters: int, batch_accumulation: int, patience: int, tb_writer: SummaryWriter, log_freq: int, output_folder_path: str, device: str) -> None:
"""Train the model for a given number of epochs.
Parameters
----------
model : torch.nn.Module
The model to be trained
loader : DataLoader
Train data loader
valid_loader : DataLoader
Data loader for validation
optimizer : torch.optim
Optimizer
training_classes_weights : np.array
Numpy array containing the weigths for each class in the dataset
epochs : int
Number of training epochs
train_iters : int
Number of training iterations before to run a validation step
batch_accumulation : int
NUmber of accumulating batch iterations
patience : int
Validation step before to drop the learning rate if metric reached a plateau
tb_writer : SummaryWriter
Writer on tensorboard
log_freq : int
Number of training iterations after which print training stats
output_folder_path : str
Path to output folder to save model checkpoints
device : str
Device on which to run the computations
"""
logger = logging.getLogger()
scheduler = ReduceLROnPlateau(optimizer, mode='max', factor=0.1, patience=patience, threshold=0.001, verbose=1)
criterion = torch.nn.CrossEntropyLoss(weight=torch.from_numpy(training_classes_weights).float())
criterion.to(device)
model.to(device).train()
it_t = 0
it_v = 0
i = 0
# Validation statistics
best_val_acc = 0.
for epoch in range(epochs):
# Training statistics
tr_loss = 0.
correct = 0
n = 0
model.zero_grad()
optimizer.zero_grad()
ba = 1
pbar = tqdm(loader, total=len(loader), desc=f'Training at epoch: {epoch}', leave=False)
for idx, (x, y) in enumerate(pbar, 1):
n += x.shape[0]
x, y = x.to(device), y.to(device)
_, predictions = model(x)
loss_ = criterion(predictions, y)
loss_.backward()
tr_loss += loss_.item()
correct += predictions.max(-1)[1].eq(y).sum().item()
if ba % batch_accumulation == 0:
ba = 1
optimizer.step()
optimizer.zero_grad()
else:
ba += 1
pbar.set_postfix(Train_loss=f'{tr_loss/idx:.4f}', Accuracy=f'{correct/n * 100:.2f}%')
# Perform a validation run every train_iters trainin iterations
if idx % (len(loader)//2) == 0:
val_loss, val_acc = validate(model=model, loader=valid_loader, device=device)
# Update the learning rate scheduler
scheduler.step(val_acc)
tqdm.write(
f'####'
f'\tValidation at iter ([{idx}]/[{len(loader)}])'
f'\tValid loss: {val_loss:.4f} --- Accuracy: {val_acc * 100:.2f}%'
f'\t ####'
)
if tb_writer is not None:
tb_writer.add_scalar('valid/loss', val_loss, it_v)
tb_writer.add_scalar('valid/acc', val_acc, it_v)
it_v += 1
# If the current model is the best so far, then save a checkpoint
if best_val_acc < val_acc:
best_val_acc = val_acc
ckp_path = join(output_folder_path, 'best_model_ckp_%d.pt'%i)
i+=1
tqdm.write(
f'####'
f'\tSaving best model at {ckp_path}'
f'\t####'
)
torch.save({
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'epoch': epoch,
'train_iter': idx,
'best_acc': best_val_acc
},
ckp_path
)
# Set model in train mode after validation
model.train()
# Print training stats and save on tensorboard
if idx % log_freq == 0:
tqdm.write(
f'Train at epoch {epoch} ([{idx}]/[{len(loader)}])'
f'\tTrain loss: {tr_loss/idx:.4f} --- Accuracy: {correct/n * 100:.2f}%'
)
if tb_writer is not None:
tb_writer.add_scalar('train/loss', tr_loss/idx, it_t)
tb_writer.add_scalar('train/acc', correct/n, it_t)
it_t += 1
def validate(model: torch.nn.Module, loader: DataLoader, device: str) -> [float, float]:
"""Validate the model.
Parameters
----------
model : torch.nn.Module
The model to be trained
loader : DataLoader
Data loader
device : str
Device on which to run the computations
Returns
------
model : torch.nn.Model
Trained model
"""
n = 0
loss = 0.
correct = 0
criterion = torch.nn.CrossEntropyLoss()
model.eval()
with torch.no_grad():
for idx, (x, y) in enumerate(tqdm(loader, total=len(loader)//3, desc='Model validation', leave=False), 1):
n += x.shape[0]
x, y = x.to(device), y.to(device)
_, predictions = model(x)
loss_ = criterion(predictions, y)
loss += loss_.item()
correct += predictions.max(-1)[1].eq(y).sum().item()
# Test only on a portion of the entire validation set
if idx % (len(loader) // 3) == 0: break
return loss/idx, correct/n
def train_over_epoch(fold: int, model: torch.nn.Module, loader: DataLoader, optimizer: torch.optim, criterion: torch.nn.Module, batch_accumulation: int, tb_writer: SummaryWriter, log_freq: int, device: str, epoch: int) -> None:
"""Train the model for a given number of epochs.
Parameters
----------
fold : int
Current fold index
model : torch.nn.Module
The model to be trained
loader : DataLoader
Train data loader
optimizer : torch.optim
Optimizer
criterion : torch.nn.Module
Criterion to apply to evaluate the loss
batch_accumulation : int
NUmber of accumulating batch iterations
tb_writer : SummaryWriter
Writer on tensorboard
log_freq : int
Number of training iterations after which print training stats
device : str
Device on which to run the computations
epoch : int
Current epoch
"""
logger = logging.getLogger()
model.train()
# Training statistics
tr_loss = 0.
correct = 0
n = 0
model.zero_grad()
optimizer.zero_grad()
ba = 1
pbar = tqdm(loader, total=len(loader), desc=f'Training at epoch: {epoch}')
for idx, (x, y) in enumerate(pbar, 1):
n += x.shape[0]
x, y = x.to(device), y.to(device)
_, predictions = model(x)
loss_ = criterion(predictions, y)
loss_.backward()
tr_loss += loss_.item()
correct += predictions.max(-1)[1].eq(y).sum().item()
if ba % batch_accumulation == 0:
# nn.utils.clip_grad_norm_(parameters=model.parameters(), max_norm=10.0, norm_type=2.0)
ba = 1
optimizer.step()
optimizer.zero_grad()
else:
ba += 1
pbar.set_postfix(Train_loss=f'{tr_loss/idx:.4f}', Accuracy=f'{correct/n * 100:.2f}%')
if tb_writer is not None:
tb_writer.add_scalar(f'train/loss_{fold}', tr_loss/len(loader), epoch)
tb_writer.add_scalar(f'train/acc_{fold}', correct/n, epoch)
def test(model: torch.nn.Module, loader: DataLoader, device: str) -> None:
"""Test the model.
Parameters
----------
model : torch.nn.Module
The model to be trained
loader : DataLoader
Test data loader
device : str
Device on which to run the computations
Returns
------
model : torch.nn.Model
Trained model
"""
logger = logging.getLogger()
criterion = torch.nn.CrossEntropyLoss()
n = 0
loss = 0.
correct = 0
model.to(device).eval()
labels = []
predictions = []
with torch.no_grad():
for idx, (x, y) in enumerate(tqdm(loader, total=len(loader), desc='Model test', leave=False), 1):
n += x.shape[0]
x, y = x.to(device), y.to(device)
_, predictions_ = model(x)
loss_ = criterion(predictions_, y)
loss += loss_.item()
correct += predictions_.max(-1)[1].eq(y).sum().item()
labels.extend(y.cpu().numpy().tolist())
predictions.extend(predictions_.max(-1)[1].detach().cpu().numpy().tolist())
accuracy, f1_score, _ = eval_metrics(np.asarray(labels), np.asarray(predictions))
score = eval_score_for_competition(f1_score, accuracy)
logger.info(
f'\n#################################################'
f'\n#################################################'
f'\nTest results:'
f'\n\tLoss: {loss/len(loader):.4f} --- F1-score: {f1_score:.4f} --- Accuracy: {correct/n * 100:.2f}%'
f'\n\tCompetition score: {score:.4f}'
f'\n#################################################'
f'\n#################################################'
)
return accuracy, f1_score, score