-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
276 lines (223 loc) · 10.9 KB
/
engine.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
"""
Contains functions for training and testing a PyTorch model.
"""
import torch
from timm.utils import ModelEma
from typing import Optional
from collections import Counter
import numpy as np
from sklearn.utils.class_weight import compute_class_weight
from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score, accuracy_score, \
balanced_accuracy_score
import models
def train_step(model: torch.nn.Module,
dataloader: torch.utils.data.DataLoader,
criterion: torch.nn.Module,
optimizer: torch.optim.Optimizer,
device: torch.device,
epoch: int,
loss_scaler,
max_norm: float=0.0,
lr_scheduler=None,
wandb=print,
model_ema: Optional[ModelEma] = None,
args = None):
# Put model in train mode
model.train()
# Setup train loss and train accuracy values
train_loss, train_acc = 0, 0
train_stats = {}
lr_num_updates = epoch * len(dataloader)
preds = []; targs = []
# Loop through data loader data batches
for batch_idx, (input, target) in enumerate(dataloader):
# Send data to device
input, target = input.to(device), target.to(device)
# 1. Clear gradients
optimizer.zero_grad()
with torch.cuda.amp.autocast():
output = model(input)
loss = criterion(output, target)
train_loss += loss.item()
if loss_scaler is not None:
is_second_order = hasattr(optimizer, 'is_second_order') and optimizer.is_second_order # this attribute is added by timm on one optimizer (adahessian)
loss_scaler(loss, optimizer, clip_grad=max_norm, parameters=model.parameters(), create_graph=is_second_order)
else:
loss.backward()
optimizer.step()
# Update LR Scheduler
if not args.cosine_one_cycle and lr_scheduler is not None:
lr_scheduler.step_update(num_updates=lr_num_updates)
# Update Model Ema
if model_ema is not None:
if device == 'cuda:0' or device == 'cuda:1':
torch.cuda.synchronize()
model_ema.update(model)
# Calculate and accumulate accuracy metric across all batches
predictions = torch.argmax(output, dim=1)
train_acc += (predictions == target).sum().item()/len(predictions)
preds.append(predictions.cpu().numpy()); targs.append(target.cpu().numpy())
# Adjust metrics to get average loss and accuracy per batch
train_loss = train_loss / len(dataloader); train_acc = train_acc / len(dataloader)
train_stats['train_loss'] = train_loss
train_stats['train_acc'] = train_acc
train_stats['train_lr'] = optimizer.param_groups[0]['lr']
if wandb!=print:
wandb.log({"Train Loss":train_loss}, step=epoch)
wandb.log({"Train Accuracy":train_acc},step=epoch)
wandb.log({"Train LR":optimizer.param_groups[0]['lr']},step=epoch)
# Compute Metrics
preds=np.concatenate(preds); targs=np.concatenate(targs)
train_stats['confusion_matrix'], train_stats['f1_score'] = confusion_matrix(targs, preds), f1_score(targs, preds, average=None)
train_stats['precision'], train_stats['recall'] = precision_score(targs, preds, average=None), recall_score(targs, preds, average=None)
train_stats['bacc'] = balanced_accuracy_score(targs, preds)
train_stats['acc1'], train_stats['loss'] = train_acc, train_loss
return train_stats
@torch.no_grad()
def evaluation(model: torch.nn.Module,
dataloader: torch.utils.data.DataLoader,
criterion: torch.nn.Module,
device: torch.device,
epoch: int,
wandb=print,
args=None):
# Switch to evaluation mode
model.eval()
preds = []
targs = []
test_loss, test_acc = 0, 0
results = {}
deit_cls_dist = {'mean':[], 'mel':[], 'nv':[]}
for input, target in dataloader:
input, target = input.to(device, non_blocking=True), target.to(device, non_blocking=True)
# Compute output
output = model(input)
loss = criterion(output, target)
test_loss += loss.item()
# Calculate and accumulate accuracy
predictions = torch.argmax(output, dim=1)
test_acc += ((predictions == target).sum().item()/len(predictions))
preds.append(predictions.cpu().numpy()); targs.append(target.cpu().numpy())
if args.model in models.deits_baselines and args.visualize_cls_token:
models.cls_token_dist(model, deit_cls_dist, predictions,args)
# Adjust metrics to get average loss and accuracy per batch
test_loss = test_loss/len(dataloader); test_acc = test_acc/len(dataloader)
if wandb!=print:
wandb.log({"Val Loss":test_loss},step=epoch)
wandb.log({"Val Accuracy":test_acc},step=epoch)
# Compute Metrics
preds=np.concatenate(preds); targs=np.concatenate(targs)
results['confusion_matrix'], results['f1_score'] = confusion_matrix(targs, preds), f1_score(targs, preds, average=None)
results['precision'], results['recall'] = precision_score(targs, preds, average=None), recall_score(targs, preds, average=None)
results['bacc'] = balanced_accuracy_score(targs, preds)
results['acc1'], results['loss'] = accuracy_score(targs, preds), test_loss
return results, deit_cls_dist
class EarlyStopping:
"""Early stops the training if validation loss doesn't improve after a given patience."""
def __init__(self, patience=7, verbose=False, delta=0, path='checkpoint.pt', trace_func=print):
"""
Args:
patience (int): How long to wait after last time validation loss improved.
Default: 7
verbose (bool): If True, prints a message for each validation loss improvement.
Default: False
delta (float): Minimum change in the monitored quantity to qualify as an improvement.
Default: 0
path (str): Path for the checkpoint to be saved to.
Default: 'checkpoint.pt'
trace_func (function): trace print function.
Default: print
"""
self.patience = patience
self.verbose = verbose
self.counter = 0
self.best_score = None
self.early_stop = False
self.val_loss_min = np.Inf
self.delta = delta
self.path = path
self.trace_func = trace_func
def __call__(self, val_loss, model):
score = -val_loss
if self.best_score is None:
self.best_score = score
self.save_checkpoint(val_loss, model)
elif score < self.best_score - self.delta:
# If we don't have an improvement, increase the counter
self.counter += 1
#self.trace_func(f'\tEarlyStopping counter: {self.counter} out of {self.patience}')
if self.counter >= self.patience:
self.early_stop = True
else:
# If we have an imporvement, save the model
self.best_score = score
self.save_checkpoint(val_loss, model)
self.counter = 0
def save_checkpoint(self, val_loss, model):
'''Saves model when validation loss decrease.'''
if self.verbose:
#self.trace_func(f'\tValidation loss decreased ({self.val_loss_min:.6f} --> {val_loss:.6f}). Saving model as checkpoint.pth')
self.trace_func(f'\tValidation loss decreased ({self.val_loss_min:.6f} --> {val_loss:.6f}).')
#torch.save(model.state_dict(), self.path)
self.val_loss_min = val_loss
def Class_Weighting(train_set:torch.utils.data.Dataset,
val_set:torch.utils.data.Dataset,
device:str='cuda:0',
args=None):
""" Class weighting for imbalanced datasets.
Args:
train_set (torch.utils.data.Dataset): Training set.
val_set (torch.utils.data.Dataset): Validation set.
device (str): Device to use.
args (*args): Arguments.
Returns:
torch.Tensor: Class weights. (shape: (2,))
"""
train_dist = dict(Counter(train_set.targets))
val_dist = dict(Counter(val_set.targets))
if args.class_weights == 'median':
class_weights = torch.Tensor([(len(train_set)/x) for x in train_dist.values()]).to(device)
else:
class_weights = torch.Tensor(compute_class_weight(class_weight=args.class_weights,
classes=np.unique(train_set.targets), y=train_set.targets)).to(device)
print(f"Classes map: {train_set.class_to_idx}"); print(f"Train distribution: {train_dist}"); print(f"Val distribution: {val_dist}")
print(f"Class weights: {class_weights}\n")
return class_weights
def Classifier_Warmup(model: torch.nn.Module,
current_epoch: int,
warmup_epochs: int,
args=None):
"""Function that defines if we are in the warmup phase or not.
Args:
model (torch.nn.Module): _description_
current_epoch (int): _description_
warmup_epochs (int): _description_
flag (bool): _description_
args (_type_): _description_
Returns:
_type_: _description_
"""
if current_epoch==0 and warmup_epochs>0:
for param in model.parameters():
param.requires_grad = False
if args.model in models.resnet_baselines:
for param in model.fc.parameters():
param.requires_grad = True
elif args.model in models.other_cnn_baselines:
for param in model.classifier.parameters():
param.requires_grad = True
elif args.model == 'vit_b_16':
for param in model.heads.head.parameters():
param.requires_grad = True
elif args.model in models.transformers_baselines:
for param in model.head.parameters():
param.requires_grad = True
trainable_params={name: param for name, param in model.named_parameters() if param.requires_grad}
print(f"[Info] - Warmup phase: Only the head is trainable.")
#print(f"Trainable parameters: {trainable_params.keys()}.")
elif current_epoch == warmup_epochs:
for param in model.parameters():
param.requires_grad = True
trainable_params={name: param for name, param in model.named_parameters() if param.requires_grad}
print(f"[Info] - Finetune phase: All parameters are trainable.")
#print(f"Trainable parameters: {trainable_params.keys()}.")