-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.py
155 lines (119 loc) · 4.55 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
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
import sys
import numpy as np
from sklearn.metrics import roc_auc_score
eps = sys.float_info.epsilon
def compute_IoU(positive_class, argmax_preds, gtLabels):
# Convert as binary classification (i.e. One vs Rest)
bin_preds = []
for id_preds in range(0, len(argmax_preds)):
if argmax_preds[id_preds] == positive_class:
bin_preds.append(1.)
else:
bin_preds.append(0.)
bin_preds = np.asarray(bin_preds)
bin_gt_labels = []
for id_preds in range(0, len(gtLabels)):
if gtLabels[id_preds] == positive_class:
bin_gt_labels.append(1.)
else:
bin_gt_labels.append(0.)
bin_gt_labels = np.asarray(bin_gt_labels)
All_P = np.sum(bin_gt_labels)
TP = np.sum(bin_preds*bin_gt_labels)
FN = np.sum((1-bin_preds)*bin_gt_labels)
FP = np.sum(bin_preds*(1-bin_gt_labels))
TN = np.sum((1-bin_preds)*(1-bin_gt_labels))
IoU = TP/(TP+FN+FP)
return IoU
def compute_mean_IoU(class_number, argmax_preds, gtLabels):
all_IoU = []
for pos_class in range(0, class_number):
pos_class_IoU = compute_IoU(pos_class, argmax_preds, gtLabels)
all_IoU.append(pos_class_IoU)
mean_IoU = np.mean(np.asarray(all_IoU))
return mean_IoU
def compute_AUC(positive_class, softmax_preds, gtLabels):
bin_gt_labels = []
for id_preds in range(0, len(gtLabels)):
if gtLabels[id_preds] == positive_class:
bin_gt_labels.append(1.)
else:
bin_gt_labels.append(0.)
bin_gt_labels = np.asarray(bin_gt_labels)
AUC_score = roc_auc_score(bin_gt_labels, softmax_preds[:,positive_class])
return AUC_score
def compute_mean_AUC(class_number, softmax_preds, gtLabels):
all_AUC_scores = []
for pos_class in range(0, class_number):
pos_class_AUC = compute_AUC(pos_class, softmax_preds, gtLabels)
all_AUC_scores.append(pos_class_AUC)
mean_AUC_score = np.mean(np.asarray(all_AUC_scores))
return mean_AUC_score
def compute_gmean(positive_class, argmax_preds, gtLabels):
# Convert as binary classification (i.e. One vs Rest)
bin_preds = []
for id_preds in range(0, len(argmax_preds)):
if argmax_preds[id_preds] == positive_class:
bin_preds.append(1.)
else:
bin_preds.append(0.)
bin_preds = np.asarray(bin_preds)
bin_gt_labels = []
for id_preds in range(0, len(gtLabels)):
if gtLabels[id_preds] == positive_class:
bin_gt_labels.append(1.)
else:
bin_gt_labels.append(0.)
bin_gt_labels = np.asarray(bin_gt_labels)
#All_P = np.sum(bin_gt_labels)
TP = np.sum(bin_preds*bin_gt_labels)
FN = np.sum((1-bin_preds)*bin_gt_labels)
FP = np.sum(bin_preds*(1-bin_gt_labels))
TN = np.sum((1-bin_preds)*(1-bin_gt_labels))
TPR = TP/(TP+FN)
TPN = TN/(TN+FP)
# g-mean
g_mean = np.sqrt(TPR*TPN)
return g_mean
def compute_mean_gmean(class_number, argmax_preds, gtLabels):
all_gmean = []
for pos_class in range(0, class_number):
pos_class_gmean = compute_gmean(pos_class, argmax_preds, gtLabels)
all_gmean.append(pos_class_gmean)
mean_gmean = np.mean(np.asarray(all_gmean))
return mean_gmean
def compute_CBA(positive_class, argmax_preds, gtLabels):
# Convert as binary classification (i.e. One vs Rest)
bin_preds = []
for id_preds in range(0, len(argmax_preds)):
if argmax_preds[id_preds] == positive_class:
bin_preds.append(1.)
else:
bin_preds.append(0.)
bin_preds = np.asarray(bin_preds)
bin_gt_labels = []
for id_preds in range(0, len(gtLabels)):
if gtLabels[id_preds] == positive_class:
bin_gt_labels.append(1.)
else:
bin_gt_labels.append(0.)
bin_gt_labels = np.asarray(bin_gt_labels)
#All_P = np.sum(bin_gt_labels)
TP = np.sum(bin_preds*bin_gt_labels)
FN = np.sum((1-bin_preds)*bin_gt_labels)
FP = np.sum(bin_preds*(1-bin_gt_labels))
#TN = np.sum((1-bin_preds)*(1-bin_gt_labels))
pos_cls_CBA = None
if TP+FN >= TP+FP:
pos_cls_CBA = TP/(TP+FN)
else:
pos_cls_CBA = TP/(TP+FP)
#pos_cls_CBA = int(TP)/int(np.max(TP+FN,TP+FP))
return pos_cls_CBA
def compute_mean_CBA(class_number, argmax_preds, gtLabels):
all_CBA = []
for pos_class in range(0, class_number):
pos_class_CBA = compute_CBA(pos_class, argmax_preds, gtLabels)
all_CBA.append(pos_class_CBA)
mean_CBA = np.mean(np.asarray(all_CBA))
return mean_CBA