-
Notifications
You must be signed in to change notification settings - Fork 4
/
val.py
72 lines (56 loc) · 1.91 KB
/
val.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
import torch
import torch.nn as nn
from sklearn.metrics import confusion_matrix, balanced_accuracy_score
def compute_bacc(model, dataloader, get_confusion_matrix, args):
all_preds = []
all_labels = []
model.eval()
with torch.no_grad():
for (x, label) in dataloader:
x = x.cuda()
_, logits = model(x)
pred = torch.argmax(logits, dim=1)
all_preds.append(pred.cpu())
all_labels.append(label)
all_labels = torch.cat(all_labels).numpy()
all_preds = torch.cat(all_preds).numpy()
acc = balanced_accuracy_score(all_labels, all_preds)
if get_confusion_matrix:
conf_matrix = confusion_matrix(all_labels, all_preds)
if get_confusion_matrix:
return acc, conf_matrix
else:
return acc
def compute_loss(model, dataloader):
criterion = nn.CrossEntropyLoss()
model.eval()
loss = 0.
with torch.no_grad():
for (x, label) in dataloader:
if isinstance(x, list):
x = x[0]
x, label = x.cuda(), label.cuda()
_, logits = model(x)
loss += criterion(logits, label)
return loss
def compute_loss_of_classes(model, dataloader, n_classes):
criterion = nn.CrossEntropyLoss(reduction="none")
model.eval()
loss_class = torch.zeros(n_classes).float()
loss_list = []
label_list = []
with torch.no_grad():
for (x, label) in dataloader:
if isinstance(x, list):
x = x[0]
x, label = x.cuda(), label.cuda()
_, logits = model(x)
loss = criterion(logits, label)
loss_list.append(loss)
label_list.append(label)
loss_list = torch.cat(loss_list).cpu()
label_list = torch.cat(label_list).cpu()
for i in range(n_classes):
idx = torch.where(label_list==i)[0]
loss_class[i] = loss_list[idx].sum()
return loss_class