-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_Compute_EER.py
275 lines (224 loc) · 9.65 KB
/
08_Compute_EER.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python
# coding: utf-8
# Verification Tasks
# Authors: Stefan Jokic, Andrea Schwaller, Filipe Barata, David Cleres
# Libraries
import numpy as np
import numpy.random as rng
import os
import pickle
import sys
import csv
import time
import tensorflow as tf
import tensorflow.keras.backend as K
# Parameters
# Mel-scaled spectrogram parameters
img_width = 237
img_height = 80
# Model & Training
# Directories
weights = sys.argv[1]
data = sys.argv[2]
data_split_name = data.split("/")
data_name = data_split_name[0] + "_" + data_split_name[1]
data_path = "%s%s/" % ("./data/", data)
weights_path = "%s%s/" % ("./weights_testing/", weights)
testing_path = "./testing/"
# Select number of enrollment and test samples
n_enrollment = 10 # TODO: If you have all the data set uncomment this.
n_test = 5
thresholds = np.linspace(2.0, 2.2, 100)
# Load Model
from Model import get_triplet_network, get_embedding_cnn
model = get_triplet_network((img_height, img_width, 1))
embedding_cnn = get_embedding_cnn((img_height, img_width, 1))
# Load Test Data from Pickle File
with open(os.path.join(data_path, "test.pickle"), "rb") as f:
(X, n_samples) = pickle.load(f)
# Test Model via verification tasks
print("\n-------------------------------------")
print("Start testing process!")
print("-------------------------------------\n")
# Print parameter configuration
print("Parameter Configuration:\n")
print("Script Filename:", sys.argv[0])
print("Data Path:", sys.argv[1])
print("Weights Path:", sys.argv[2])
print("\n ------------- \n")
# Set up csv output file
timestamp = time.strftime("%Y%m%d_%H%M%S__")
os.makedirs(os.path.join(testing_path, timestamp), exist_ok=True)
with open(
"%s%s%s%s%s%s%s" % (testing_path, timestamp, "train_", weights, "_test_", data_name, "_EER.csv"), "w", newline=""
) as file:
writer = csv.writer(file, quoting=csv.QUOTE_NONNUMERIC)
# Write header line to csv file
participant_list = list(range(len(n_samples)))
participant_list.insert(0, "Participants")
writer.writerow(participant_list)
participant_list.pop(0)
# Loop over all weight files
for weight_file in os.listdir(weights_path):
np.random.seed(0)
# Load weights
model.load_weights(os.path.join(weights_path, weight_file))
embedding_cnn.set_weights(model.get_weights())
counters = [[0] * 4 for i in range(len(thresholds))]
# Loop over all participants
for participant in participant_list:
print("Current participant to be verified: ", participant)
P1_num_enrollment = min(n_enrollment, n_samples[participant] - n_test)
# First select (P1_num_enrollment + n_test) samples u.a.r. from P1
P1_samples = rng.choice(range(n_samples[participant]), size=(P1_num_enrollment + n_test,), replace=False)
# Select first P1_num_enrollment samples from P1 as enrollment samples
P1_enrollment_samples = P1_samples[0:P1_num_enrollment]
# Select the remaining n_test samples (different from the enrollment samples) from P1 as test samples
P1_test_samples = P1_samples[P1_num_enrollment:]
print(
"For the participant (ID {userID}) to be verified, the number of enrollment samples is: ".format(
userID=participant
),
P1_num_enrollment,
)
print(
"For the participant (ID {userID}) to be verified, the following enrollment samples have been selected: ".format(
userID=participant
),
P1_enrollment_samples,
)
print(
"For the participant (ID {userID}) to be verified, the following test samples have been selected: ".format(
userID=participant
),
P1_test_samples,
)
writer.writerow(
[
"For the participant (ID {userID}) to be verified, the number of enrollment samples is: ".format(
userID=participant
),
P1_num_enrollment,
]
)
writer.writerow(
[
"For the participant (ID {userID}) to be verified, the following enrollment samples have been selected: ".format(
userID=participant
),
P1_enrollment_samples,
]
)
writer.writerow(
[
"For the participant (ID {userID}) to be verified, the following test samples have been selected: ".format(
userID=participant
),
P1_test_samples,
]
)
# Select n_test participants u.a.r. that are different from P1 (same participant can be selected multiple times)
participants = []
for i in range(n_test):
participants.append((participant + rng.randint(1, len(n_samples))) % len(n_samples))
# Count how many times each participant has been selected
participants_count = {i: participants.count(i) for i in np.unique(participants)}
print(
"The following participants and number of samples per participant have been selected for the verification test: "
)
print(participants_count)
writer.writerow(
[
"The following participants and number of samples per participant have been selected for the verification test: ",
participants_count,
]
)
test_samples = []
for p in participants_count.keys():
num_samples = participants_count[p]
samples = rng.choice(range(n_samples[p]), size=(num_samples,), replace=False)
print(
"For participant {userID}, the following test samples have been selected: ".format(userID=p),
samples,
)
writer.writerow(
[
"For participant {userID}, the following test samples have been selected: ".format(userID=p),
samples,
]
)
imgs = X[p, samples, :, :].reshape(num_samples, img_height, img_width, 1)
test_samples.append(imgs)
test_samples = np.concatenate(test_samples)
# Retrieve images from P1
P1_enrollment = X[participant, P1_enrollment_samples, :, :].reshape(
P1_num_enrollment, img_height, img_width, 1
)
P1_test = X[participant, P1_test_samples, :, :].reshape(n_test, img_height, img_width, 1)
# Compute embeddings
emb_P1_enrollment = embedding_cnn.predict(P1_enrollment)
emb_P1_test = embedding_cnn.predict(P1_test)
emb_others = embedding_cnn.predict(test_samples)
m = tf.keras.metrics.MeanTensor()
print()
n_threshold = 0
for THRESHOLD in thresholds:
for test_sample in emb_P1_test:
for enrollment_sample in emb_P1_enrollment:
distance = K.sum(K.square(test_sample - enrollment_sample), axis=0)
m.update_state(distance)
mean_distance = m.result()
print(
"Mean distance between enrollment samples of participant {userID} and sample from same participant: ".format(
userID=participant
),
mean_distance,
)
if mean_distance < THRESHOLD:
counters[n_threshold][0] += 1
else:
counters[n_threshold][3] += 1
m.reset_states()
for test_sample in emb_others:
for enrollment_sample in emb_P1_enrollment:
distance = K.sum(K.square(test_sample - enrollment_sample), axis=0)
m.update_state(distance)
mean_distance = m.result()
print(
"Mean distance between enrollment samples of participant {userID} and sample from other participant: ".format(
userID=participant
),
mean_distance,
)
if mean_distance < THRESHOLD:
counters[n_threshold][1] += 1
else:
counters[n_threshold][2] += 1
m.reset_states()
n_threshold += 1
print()
print("----------")
print()
# Store and print output metrics
metrics = []
metrics.append(("Model Weights: ", weight_file))
writer.writerow(metrics[0])
n_threshold = 0
eer = 0
for THRESHOLD in thresholds:
# For each threshold: Compute accuracy, FAR and FRR over all participants
tp = counters[n_threshold][0]
fp = counters[n_threshold][1]
tn = counters[n_threshold][2]
fn = counters[n_threshold][3]
acc = (tp + tn) / (tp + tn + fp + fn)
far = fp / (fp + tn)
frr = fn / (fn + tp)
if far == frr:
eer = far
n_threshold += 1
metrics.append(["------------------"])
writer.writerow(metrics[-1])
metrics.append(("EER: ", eer))
writer.writerow(metrics[-1])
print("EER: ", eer)