-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtest_with_lfw.py
More file actions
184 lines (141 loc) · 6.98 KB
/
test_with_lfw.py
File metadata and controls
184 lines (141 loc) · 6.98 KB
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
"""
This script was modified from https://github.com/peteryuX/arcface-tf2/blob/66d0afb124c74b6c1e0b79a464325472ede6fb45/modules/evaluations.py
"""
import os
import cv2
import bcolz
import numpy as np
import tensorflow as tf
import tqdm
from sklearn.model_selection import KFold
def l2_norm(x, axis=1):
"""l2 norm"""
norm = np.linalg.norm(x, axis=axis, keepdims=True)
output = x / norm
return output
def get_val_pair(path, name):
carray = bcolz.carray(rootdir=os.path.join(path, name), mode='r')
issame = np.load('{}/{}_list.npy'.format(path, name))
return carray, issame
def get_lfw_data(data_path):
"""get validation data"""
_lfw, _lfw_issame = get_val_pair(data_path, 'lfw_align_112/lfw')
return _lfw, _lfw_issame
def get_val_data(data_path):
"""get validation data"""
_lfw, _lfw_issame = get_val_pair(data_path, 'lfw_align_112/lfw')
_agedb_30, _agedb_30_issame = get_val_pair(data_path, 'AgeDB/agedb_30')
_cfp_fp, _cfp_fp_issame = get_val_pair(data_path, 'cfp_align_112/cfp_fp')
return _lfw, _agedb_30, _cfp_fp, _lfw_issame, _agedb_30_issame, _cfp_fp_issame
def hflip_batch(imgs):
return imgs[:, :, ::-1, :]
def calculate_accuracy(threshold, dist, actual_issame):
predict_issame = np.less(dist, threshold)
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))
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
return tpr, fpr, _acc
def calculate_roc(thresholds, embeddings1, embeddings2, actual_issame,
nrof_folds=10):
assert (embeddings1.shape[0] == embeddings2.shape[0])
assert (embeddings1.shape[1] == embeddings2.shape[1])
nrof_pairs = min(len(actual_issame), embeddings1.shape[0])
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,))
best_thresholds = np.zeros((nrof_folds,))
indices = np.arange(nrof_pairs)
diff = np.subtract(embeddings1, embeddings2)
dist = np.sum(np.square(diff), 1)
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, dist[train_set], actual_issame[train_set])
best_threshold_index = np.argmax(acc_train)
best_thresholds[fold_idx] = thresholds[best_threshold_index]
for threshold_idx, threshold in enumerate(thresholds):
tprs[fold_idx, threshold_idx], fprs[fold_idx, threshold_idx], _ = \
calculate_accuracy(threshold,
dist[test_set],
actual_issame[test_set])
_, _, accuracy[fold_idx] = calculate_accuracy(
thresholds[best_threshold_index],
dist[test_set],
actual_issame[test_set])
tpr = np.mean(tprs, 0)
fpr = np.mean(fprs, 0)
return tpr, fpr, accuracy, best_thresholds
def evaluate(embeddings, actual_issame, nrof_folds=10):
# Calculate evaluation metrics
thresholds = np.arange(0, 4, 0.01)
embeddings1 = embeddings[0::2]
embeddings2 = embeddings[1::2]
tpr, fpr, accuracy, best_thresholds = calculate_roc(
thresholds, embeddings1, embeddings2, np.asarray(actual_issame),
nrof_folds=nrof_folds)
return tpr, fpr, accuracy, best_thresholds
def perform_val_arcface(embedding_size, batch_size, model,
carray, issame, nrof_folds=10, is_ccrop=False, is_flip=True):
"""perform val"""
embeddings = np.zeros([len(carray), embedding_size])
for idx in tqdm.tqdm(range(0, len(carray), batch_size)):
batch = carray[idx:idx + batch_size]
batch = np.transpose(batch, [0, 2, 3, 1])
b, g, r = tf.split(batch, 3, axis=-1)
batch = tf.concat([r, g, b], -1)
if is_flip:
flipped = hflip_batch(batch)
emb_batch = model([batch, tf.ones((batch.shape[0],), dtype=tf.int64)], training=False)[-1] + model([flipped, tf.ones((batch.shape[0],), dtype=tf.int64)], training=False)[-1]
embeddings[idx:idx + batch_size] = l2_norm(emb_batch)
else:
emb_batch = model([batch, tf.ones((batch.shape[0],), dtype=tf.int64)], training=False)[-1]
embeddings[idx:idx + batch_size] = l2_norm(emb_batch)
tpr, fpr, accuracy, best_thresholds = evaluate(
embeddings, issame, nrof_folds)
return accuracy.mean(), best_thresholds.mean()
def perform_val(embedding_size, batch_size, model,
carray, issame, nrof_folds=10, is_ccrop=False, is_flip=True):
"""perform val"""
embeddings = np.zeros([len(carray), embedding_size])
for idx in tqdm.tqdm(range(0, len(carray), batch_size)):
batch = carray[idx:idx + batch_size]
batch = np.transpose(batch, [0, 2, 3, 1])
b, g, r = tf.split(batch, 3, axis=-1)
batch = tf.concat([r, g, b], -1)
if is_flip:
flipped = hflip_batch(batch)
emb_batch = model(batch, training=False) + model(flipped, training=False)
embeddings[idx:idx + batch_size] = l2_norm(emb_batch)
else:
emb_batch = model(batch, training=False)
embeddings[idx:idx + batch_size] = l2_norm(emb_batch)
tpr, fpr, accuracy, best_thresholds = evaluate(
embeddings, issame, nrof_folds)
return accuracy.mean(), best_thresholds.mean()
if __name__ == '__main__':
lfw, agedb_30, cfp_fp, lfw_issame, agedb_30_issame, cfp_fp_issame = get_val_data("../datasets/")
model_ai = tf.keras.models.load_model("arcface_final.h5")
print("-----------------------------------")
print("Testing on LFW...")
acc, best_th = perform_val(512, 32, model_ai, lfw, lfw_issame, is_ccrop=False)
print(f"Results on LFW, Accuracy --> {acc} || Best Threshold --> {best_th}")
print("-----------------------------------")
print("-----------------------------------")
print("Testing on AgeDB 30...")
acc, best_th = perform_val(512, 32, model_ai, agedb_30, agedb_30_issame, is_ccrop=False)
print(f"Results on AgeDB 30, Accuracy --> {acc} || Best Threshold --> {best_th}")
print("-----------------------------------")
print("-----------------------------------")
print("Testing on CFP...")
acc, best_th = perform_val(512, 32, model_ai, cfp_fp, cfp_fp_issame, is_ccrop=True)
print(f"Results on CFP, Accuracy --> {acc} || Best Threshold --> {best_th}")
print("-----------------------------------")