-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
164 lines (131 loc) · 5.32 KB
/
utils.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
import random
import os
import numpy as np
import torch
import wandb
import pandas as pd
from tqdm import tqdm
from pathlib import Path
from sklearn.metrics import balanced_accuracy_score, roc_auc_score, confusion_matrix, recall_score
def ensure_dir(dirname):
dirname = Path(dirname)
if not dirname.is_dir():
dirname.mkdir(parents=True, exist_ok=False)
def set_seed(seed):
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
torch.manual_seed(seed)
def binary_accuracy(outputs, labels):
preds = (torch.sigmoid(outputs) > 0.5).long()
correct = preds.eq(labels.long()).sum()
return (correct.float() / float(len(outputs))).item()
def binary_ba(outputs, labels):
preds = (torch.sigmoid(outputs) > 0.5).long()
return balanced_accuracy_score(labels.cpu().numpy(), preds.cpu().numpy())
def roc(outputs, labels, average='macro', multi_class='raise'):
if average is None:
outputs = torch.softmax(outputs, dim=1)
else:
outputs = torch.sigmoid(outputs)
return {c: r for c,r in enumerate(roc_auc_score(labels.cpu().numpy(), outputs.cpu().numpy(), average=average, multi_class=multi_class))}
def binary_metrics(outputs, labels):
return dict(
accuracy=binary_accuracy(outputs, labels),
ba=binary_ba(outputs, labels),
roc=roc(outputs, labels)
)
def accuracy(outputs, labels):
_, preds = torch.max(outputs, dim=1)
return (preds.eq(labels.long()).sum().float() / labels.shape[0]).item()
def ba(outputs, labels):
_, preds = torch.max(outputs, dim=1)
return balanced_accuracy_score(labels.cpu().numpy(), preds.cpu().numpy())
def class_ba(outputs, labels):
_, preds = torch.max(outputs, dim=1)
preds = preds.cpu().numpy()
targets = torch.unique(labels.long()).cpu().numpy()
labels = labels.long().cpu().numpy()
class_ba = {}
for target in targets:
class_labels = (labels == target).astype(np.uint8)
class_preds = (preds == target).astype(np.uint8)
class_ba[int(target)] = balanced_accuracy_score(class_labels, class_preds)
return class_ba
def recall(outputs, labels, average='binary'):
_, preds = torch.max(outputs, dim=1)
return {c: r for c, r in enumerate(recall_score(labels.cpu().numpy(), preds.cpu().numpy(), average=average))}
def cm(outputs, labels):
_, preds = torch.max(outputs, dim=1)
cm = confusion_matrix(labels.cpu().numpy(), preds.cpu().numpy())
print(cm)
return cm
def metrics(outputs, labels):
return dict(
accuracy=accuracy(outputs, labels),
ba=ba(outputs, labels),
class_ba=class_ba(outputs, labels),
recall=recall(outputs, labels, average=None),
#roc=roc(outputs, labels, average=None, multi_class='ovo'),
cm=wandb.Table(dataframe=pd.DataFrame(cm(outputs, labels)))
)
def train(model, dataloader, criterion, optimizer, device, metrics, accumulation_steps=1, scaler=None, verbose=True):
num_samples, tot_loss = 0., 0.
all_outputs, all_labels = [], []
model.train()
itr = tqdm(dataloader, leave=False) if verbose else dataloader
for step, (data, labels) in enumerate(itr):
data, labels = data.to(device), labels.to(device)
outputs, loss = None, None
if scaler is None:
with torch.enable_grad():
outputs = model(data)
loss = criterion(outputs, labels) / accumulation_steps
else:
with torch.cuda.amp.autocast():
outputs = model(data)
loss = criterion(outputs, labels) / accumulation_steps
if scaler is None:
loss.backward()
else:
scaler.scale(loss).backward()
if (step+1) % accumulation_steps == 0 or step == len(dataloader)-1:
if scaler is None:
optimizer.step()
else:
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
all_outputs.append(outputs.detach())
all_labels.append(labels.detach())
batch_size = data.shape[0]
num_samples += batch_size
tot_loss += loss.item() * accumulation_steps * batch_size
all_outputs = torch.cat(all_outputs, dim=0)
all_labels = torch.cat(all_labels, dim=0)
tracked_metrics = metrics(all_outputs, all_labels)
tracked_metrics.update({'loss': tot_loss / num_samples})
return tracked_metrics
def test(model, dataloader, criterion, device, metrics):
num_samples, tot_loss = 0., 0.
all_outputs, all_labels = [], []
model.eval()
for data, labels in tqdm(dataloader, leave=False):
data, labels = data.to(device), labels.to(device)
with torch.no_grad():
outputs = model(data)
loss = criterion(outputs, labels)
all_outputs.append(outputs.detach())
all_labels.append(labels.detach())
batch_size = data.shape[0]
num_samples += batch_size
tot_loss += loss.item() * batch_size
all_outputs = torch.cat(all_outputs, dim=0)
all_labels = torch.cat(all_labels, dim=0)
tracked_metrics = metrics(all_outputs, all_labels)
tracked_metrics.update({'loss': tot_loss / num_samples})
return tracked_metrics