-
Notifications
You must be signed in to change notification settings - Fork 4
/
metrics.py
38 lines (30 loc) · 1.41 KB
/
metrics.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
from sklearn.metrics import roc_auc_score, roc_curve
import torch
import numpy as np
def get_cam_1d(classifier, features):
tweight = list(classifier.parameters())[-2]
cam_maps = torch.einsum('bgf,cf->bcg', [features, tweight])
return cam_maps
def roc_threshold(label, prediction):
fpr, tpr, threshold = roc_curve(label, prediction, pos_label=1)
fpr_optimal, tpr_optimal, threshold_optimal = optimal_thresh(fpr, tpr, threshold)
c_auc = roc_auc_score(label, prediction)
return c_auc, threshold_optimal
def optimal_thresh(fpr, tpr, thresholds, p=0):
loss = (fpr - tpr) - p * tpr / (fpr + tpr + 1)
idx = np.argmin(loss, axis=0)
return fpr[idx], tpr[idx], thresholds[idx]
def eval_metric(oprob, label):
auc, threshold = roc_threshold(label.cpu().numpy(), oprob.detach().cpu().numpy())
prob = oprob > threshold
label = label > threshold
TP = (prob & label).sum(0).float()
TN = ((~prob) & (~label)).sum(0).float()
FP = (prob & (~label)).sum(0).float()
FN = ((~prob) & label).sum(0).float()
accuracy = torch.mean(( TP + TN ) / ( TP + TN + FP + FN + 1e-12))
precision = torch.mean(TP / (TP + FP + 1e-12))
recall = torch.mean(TP / (TP + FN + 1e-12))
specificity = torch.mean( TN / (TN + FP + 1e-12))
F1 = 2*(precision * recall) / (precision + recall+1e-12)
return accuracy, precision, recall, specificity, F1, auc