-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_metrics.py
133 lines (110 loc) · 5.01 KB
/
eval_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
#written by Saurav Rai
import operator
import numpy as np
import sklearn.metrics
from sklearn import metrics
from scipy.optimize import brentq
from sklearn.model_selection import KFold
from scipy import interpolate
import tqdm
def myevaluate(scores,gt, nrof_folds=10):
fold_size = 600 # 600 pairs in each fold
roc_auc = np.zeros(10)
roc_eer = np.zeros(10)
for i in tqdm.tqdm(range(10)):
start = i * fold_size
end = (i+1) * fold_size
scores_fold = scores[start:end]
gt_fold = gt[start:end]
roc_auc[i] = sklearn.metrics.roc_auc_score(gt_fold, scores_fold)
fpr, tpr, _ = sklearn.metrics.roc_curve(gt_fold, scores_fold)
roc_eer[i] = brentq(lambda x: 1. - x - interpolate.interp1d(fpr, tpr)(x), 0., 1.)
print( 'AUC: %0.4f +/- %0.4f, EER: %0.4f +/- %0.4f' % (np.mean(roc_auc), np.std(roc_auc),np.mean(roc_eer), np.std(roc_eer)) )
def evaluate(distances, labels, nrof_folds=10):
# Calculate evaluation metrics
thresholds = np.arange(0, 1, 0.1)
#tpr, fpr, accuracy = calculate_roc(thresholds, distances,
accuracy = calculate_roc(thresholds, distances,
labels, nrof_folds=nrof_folds)
#print('sairam5')
'''
thresholds = np.arange(0, 30, 0.001)
val, val_std, far = calculate_val(thresholds, distances,
labels, 1e-3, nrof_folds=nrof_folds)
'''
#return tpr, fpr, accuracy#, val, val_std, far
return accuracy
def calculate_roc(thresholds, distances, labels, nrof_folds=10):
nrof_pairs = min(len(labels), len(distances))
nrof_thresholds = len(thresholds)
k_fold = KFold(n_splits=nrof_folds, shuffle=False)
tprs = np.zeros((nrof_folds,nrof_thresholds))
fprs = np.zeros((nrof_folds,nrof_thresholds))
accuracy = np.zeros((nrof_folds))
indices = np.arange(nrof_pairs)
#print('sairam2')
for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)):
# Find the best threshold for the fold
acc_train = np.zeros((nrof_thresholds))
for threshold_idx, threshold in enumerate(thresholds):
_, _, acc_train[threshold_idx] = calculate_accuracy(threshold, distances[train_set], labels[train_set])
best_threshold_index = np.argmax(acc_train)
'''
for threshold_idx, threshold in enumerate(thresholds):
tprs[fold_idx,threshold_idx], fprs[fold_idx,threshold_idx], _ = calculate_accuracy(threshold, distances[test_set], labels[test_set])
'''
_, _, accuracy[fold_idx] = calculate_accuracy(thresholds[best_threshold_index], distances[test_set], labels[test_set])
'''
tpr = np.mean(tprs,0)
fpr = np.mean(fprs,0)
'''
#print('sairam4')
return accuracy #tpr, fpr, accuracy
def calculate_accuracy(threshold, dist, actual_issame):
predict_issame = np.less(dist, threshold)
#print(predict_issame, actual_issame)
tp = np.sum(np.logical_and(predict_issame, actual_issame))
fp = np.sum(np.logical_and(predict_issame, np.logical_not(actual_issame)))
tn = np.sum(np.logical_and(np.logical_not(predict_issame), np.logical_not(actual_issame)))
fn = np.sum(np.logical_and(np.logical_not(predict_issame), actual_issame))
#print('sairam3')
tpr = 0 if (tp+fn==0) else float(tp) / float(tp+fn)
fpr = 0 if (fp+tn==0) else float(fp) / float(fp+tn)
acc = float(tp+tn)/dist.size
#print('Accuracy in cal accuracy fun',tp,fp,tn,fn,dist.size)
return tpr, fpr, acc
def calculate_val(thresholds, distances, labels, far_target=1e-3, nrof_folds=10):
nrof_pairs = min(len(labels), len(distances))
nrof_thresholds = len(thresholds)
k_fold = KFold(n_splits=nrof_folds, shuffle=False)
val = np.zeros(nrof_folds)
far = np.zeros(nrof_folds)
indices = np.arange(nrof_pairs)
for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)):
# Find the threshold that gives FAR = far_target
far_train = np.zeros(nrof_thresholds)
for threshold_idx, threshold in enumerate(thresholds):
_, far_train[threshold_idx] = calculate_val_far(threshold, distances[train_set], labels[train_set])
if np.max(far_train)>=far_target:
f = interpolate.interp1d(far_train, thresholds, kind='slinear')
threshold = f(far_target)
else:
threshold = 0.0
val[fold_idx], far[fold_idx] = calculate_val_far(threshold, distances[test_set], labels[test_set])
val_mean = np.mean(val)
far_mean = np.mean(far)
val_std = np.std(val)
return val_mean, val_std, far_mean
def calculate_val_far(threshold, dist, actual_issame):
predict_issame = np.less(dist, threshold)
true_accept = np.sum(np.logical_and(predict_issame, actual_issame))
false_accept = np.sum(np.logical_and(predict_issame, np.logical_not(actual_issame)))
n_same = np.sum(actual_issame)
n_diff = np.sum(np.logical_not(actual_issame))
if n_diff == 0:
n_diff = 1
if n_same == 0:
return 0,0
val = float(true_accept) / float(n_same)
far = float(false_accept) / float(n_diff)
return val, far