This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
metrics.py
95 lines (77 loc) · 2.52 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
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
import numpy as np
def numeric_score(pred, gts):
"""Computation of statistical numerical scores:
* FP = False Positives
* FN = False Negatives
* TP = True Positives
* TN = True Negatives
return: tuple (FP, FN, TP, TN)
"""
np_pred = pred.numpy()
np_gts = [gts[:,i,:,:].numpy() for i in range(gts.size()[1])]
FP = []
FN = []
TP = []
TN = []
for i in range(len(np_gts)):
FP.append(np.float(np.sum((np_pred == i) & (np_gts[i] == 0))))
FN.append(np.float(np.sum((np_pred != i) & (np_gts[i] == 1))))
TP.append(np.float(np.sum((np_pred == i) & (np_gts[i] == 1))))
TN.append(np.float(np.sum((np_pred != i) & (np_gts[i] == 0))))
return FP, FN, TP, TN
def precision_score(FP, FN, TP, TN):
# PPV
precision = []
for i in range(len(FP)):
if (TP[i] + FP[i]) <= 0.0:
precision.append(0.0)
else:
precision.append(np.divide(TP[i], TP[i] + FP[i])* 100.0)
return precision
def recall_score(FP, FN, TP, TN):
# TPR, sensitivity
TPR = []
for i in range(len(FP)):
if (TP[i] + FN[i]) <= 0.0:
TPR.append(0.0)
else:
TPR.append(np.divide(TP[i], TP[i] + FN[i]) * 100.0)
return TPR
def specificity_score(FP, FN, TP, TN):
TNR = []
for i in range(len(FP)):
if (TN[i] + FP[i]) <= 0.0:
TNR.append(0.0)
else:
TNR.append(np.divide(TN[i], TN[i] + FP[i]) * 100.0)
return TNR
def intersection_over_union(FP, FN, TP, TN):
IOU = []
for i in range(len(FP)):
if (TP[i] + FP[i] + FN[i]) <= 0.0:
IOU.append(0.0)
else:
IOU.append(TP[i] / (TP[i] + FP[i] + FN[i]) * 100.0)
return IOU
def accuracy_score(FP, FN, TP, TN):
accuracy = []
for i in range(len(FP)):
N = FP[i] + FN[i] + TP[i] + TN[i]
accuracy.append(np.divide(TP[i] + TN[i], N) * 100.0)
return accuracy
def dice_score(pred, gts):
dice = []
np_pred = pred.numpy()[:,0,:,:]
np_gts = [gts[:,i,:,:].numpy() for i in range(gts.size()[1])]
for i in range(len(np_gts)):
intersection = ((np_pred==i)*np_gts[i]).sum()
card_sum = (np_pred==i).sum()+np_gts[i].sum()
dice.append(2*intersection/card_sum)
return dice
def jaccard_score(pred, gts):
jaccard = []
for i in range(gts.size()[1]):
intersection = ((pred==i)*gts[:,i,:,:]).sum()
union = (pred==i).sum()+gts[:,i,:,:].sum()-intersection
jaccard.append(float(intersection)/union)
return jaccard