-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetrics.py
69 lines (55 loc) · 1.79 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
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
from math import log10
import torch
import numpy as np
from sklearn.metrics import roc_auc_score, precision_recall_fscore_support
import torch.nn.functional as F
from scipy.special import softmax
import scipy.stats
def PSNR(mse, peak=1.):
return 10 * log10((peak ** 2) / mse)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def accuracy(preds, labels):
"""Accuracy, auc with masking.Acc of the masked samples"""
correct_prediction = np.equal(np.argmax(preds, 1), labels).astype(np.float32)
return np.sum(correct_prediction), np.mean(correct_prediction)
def auc(preds, labels, is_logit=True):
''' input: logits, labels '''
if is_logit:
pos_probs = softmax(preds, axis=1)[:, 1]
else:
pos_probs = preds[:,1]
try:
auc_out = roc_auc_score(labels, pos_probs)
except:
auc_out = 0
return auc_out
def prf(preds, labels, is_logit=True):
''' input: logits, labels '''
pred_lab= np.argmax(preds, 1)
p,r,f,s = precision_recall_fscore_support(labels, pred_lab, average='binary')
return [p,r,f]
def numeric_score(pred, gt):
FP = np.float(np.sum((pred == 1) & (gt == 0)))
FN = np.float(np.sum((pred == 0) & (gt == 1)))
TP = np.float(np.sum((pred == 1) & (gt == 1)))
TN = np.float(np.sum((pred == 0) & (gt == 0)))
return FP, FN, TP, TN
def metrics(preds, labels):
preds = np.argmax(preds, 1)
FP, FN, TP, TN = numeric_score(preds, labels)
sen = TP / (TP + FN + 1e-10) # recall sensitivity
spe = TN / (TN + FP + 1e-10)
return sen, spe