-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetEER.py
208 lines (136 loc) · 4.44 KB
/
getEER.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import numpy as np
import os
import pickle
import json
import shutil
import time
import scipy
from sklearn import metrics
from sklearn.metrics import auc
from scipy.optimize import brentq
from scipy.interpolate import interp1d
import sys
import scipy.io as io
import h5py
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
plt.switch_backend('agg')
# python getEER.py path/to/scores.txt rst_folder
if len(sys.argv) < 3:
print('getEER.py: input args error! using default ...')
pathScore = './scores.txt'
surname = 'scores'
else:
pathScore = sys.argv[1]
surname = sys.argv[2]
pathIn = os.path.dirname(pathScore)
scorename = os.path.basename(pathScore)
#print(sys.argv)
print('pathIn: ', pathIn)
print('scorename: ', scorename)
print('surname:', surname)
print('start to load matching scores ...')
pathOut = os.path.join(pathIn, surname)
if os.path.exists(pathOut)==False:
os.makedirs(pathOut)
# From .pkl:
# pathInner = os.path.join(pathIn, 'innerScore.pkl')
# pathOuter = os.path.join(pathIn, 'outerScore.pkl')
# pklfile = open(pathInner, 'rb')
# inner = pickle.load(pklfile, encoding='iso-8859-1')
# pklfile.close()
# pklfile = open(pathOuter, 'rb')
# outer = pickle.load(pklfile, encoding='iso-8859-1')
# pklfile.close()
# From .txt:
scores = np.loadtxt(pathScore)
# From old .mat:
# data = io.loadmat(pathScore)
# scores = data['rsts']
# From big .mat -v7:
# scores = h5py.File(pathScore)
# scores = scores['rsts']
# scores = np.transpose(scores)
#print(scores)
# genuine label == 1, impostor label == -1
# scores[matching score, label]
inscore = scores[scores[:, 1]==1, 0]
outscore = scores[scores[:,1]==-1, 0]
print('scores loading done!\n')
print('start to calculate EER ...')
start = time.time()
print('numbers of inner & outer matching:')
print(inscore.shape, outscore.shape)
# inner should bigger than outer (metrics.roc_curve requires similarity-based matching score)
mIn = inscore.mean()
mOut = outscore.mean()
if mIn < mOut:
inscore = -inscore
outscore = -outscore
y = np.vstack((np.ones((len(inscore),1)), np.zeros((len(outscore), 1)) ))
scores = np.vstack((inscore.reshape(-1, 1), outscore.reshape(-1, 1)))
fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=1)
roc_auc = auc(fpr, tpr)
eer = brentq(lambda x : 1. - x - interp1d(fpr, tpr)(x), 0., 1.)
thresh = interp1d(fpr, thresholds)(eer)
# reverse back
if mIn < mOut:
thresh = -thresh
thresholds = -thresholds
print('eer: %.6f%% th: %.3f auc: %.10f'%(eer*100, thresh, roc_auc))
diffV = np.abs(fpr-(1-tpr))
idx = np.argmin(diffV)
eer_1_2 = (fpr[idx]+(1-tpr[idx]))/2.0
th_1_2 = thresholds[idx]
print('eer_1/2: %.6f%% th_1/2: %.3f auc: %.10f'%(eer_1_2*100, th_1_2, roc_auc))
with open(os.path.join(pathOut, 'rst_eer_th_auc.txt'), 'w') as f:
f.writelines('%.10f %.4f %.10f\n'%(eer*100, thresh, roc_auc)) # fitted EER curve
f.writelines('%.10f %.4f %.10f\n'%(eer_1_2*100, th_1_2, -1)) # mean EER, roc_auc: -1 not used
fnr = 1-tpr
with open(os.path.join(pathOut, 'DET_th_far_frr.txt'), 'w') as f:
# f.writelines('th FAR FRR\n')
for i in range(len(fpr)):
f.writelines('%.6f\t%.10f\t%.10f\n'%(thresholds[i], fpr[i], fnr[i])) #
pdf = PdfPages(os.path.join(pathOut, 'roc_det.pdf'))
fpr = fpr*100
tpr = tpr*100
fnr = fnr*100
# ROC Curve
plt.figure()
plt.plot(fpr, tpr, color='b', linestyle='-', marker='^', label='ROC curve')
plt.plot(np.linspace(0, 100, 101), np.linspace(100, 0, 101), 'k-', label='EER')
plt.xlim([0, 5])
plt.ylim([90, 100])
plt.legend(loc='best')
plt.grid(True)
plt.title('ROC curve')
plt.xlabel('FAR (%)')
plt.ylabel('GAR (%)')
plt.savefig(os.path.join(pathOut, 'ROC.png'))
pdf.savefig()
# DET curve
plt.figure()
plt.plot(fpr, fnr, color='b', linestyle='-', marker='^', label='DET curve')
plt.plot(np.linspace(0, 100, 101), np.linspace(0, 100, 101), 'k-', label='EER')
plt.xlim([0, 5])
plt.ylim([0, 5])
plt.legend(loc='best')
plt.grid(True)
plt.title('DET curve')
plt.xlabel('FAR (%)')
plt.ylabel('FRR (%)')
plt.savefig(os.path.join(pathOut, 'DET.png'))
pdf.savefig()
# FAR FRR curve
plt.figure()
plt.plot(thresholds, fpr, color='r', linestyle='-', marker='.', label='FAR')
plt.plot(thresholds, fnr, color='b', linestyle='-', marker='^', label='FRR')
plt.legend(loc='best')
plt.grid(True)
plt.title('FAR and FRR Curves')
plt.xlabel('Thresholds')
plt.ylabel('FAR, FRR (%)')
plt.savefig(os.path.join(pathOut, 'FAR_FRR.png'))
pdf.savefig()
pdf.close()
print('done')