Wanli commited on
Commit
f2e3176
·
1 Parent(s): 60ba673

Add script to evaluate face recognition by LFW (#72)

Browse files
models/face_recognition_sface/README.md CHANGED
@@ -7,6 +7,16 @@ Note:
7
  - [face_recognition_sface_2021sep.onnx](./face_recognition_sface_2021sep.onnx) is converted from the model from https://github.com/zhongyy/SFace thanks to [Chengrui Wang](https://github.com/crywang).
8
  - Support 5-landmark warpping for now (2021sep)
9
 
 
 
 
 
 
 
 
 
 
 
10
  ## Demo
11
 
12
  ***NOTE***: This demo uses [../face_detection_yunet](../face_detection_yunet) as face detector, which supports 5-landmark detection for now (2021sep).
@@ -17,6 +27,7 @@ Run the following command to try the demo:
17
  python demo.py --input1 /path/to/image1 --input2 /path/to/image2
18
  ```
19
 
 
20
  ## License
21
 
22
  All files in this directory are licensed under [Apache 2.0 License](./LICENSE).
 
7
  - [face_recognition_sface_2021sep.onnx](./face_recognition_sface_2021sep.onnx) is converted from the model from https://github.com/zhongyy/SFace thanks to [Chengrui Wang](https://github.com/crywang).
8
  - Support 5-landmark warpping for now (2021sep)
9
 
10
+ Results of accuracy evaluation with [tools/eval](../../tools/eval).
11
+
12
+ | Models | Accuracy |
13
+ |-------------|----------|
14
+ | SFace | 0.9940 |
15
+ | SFace quant | 0.9932 |
16
+
17
+ \*: 'quant' stands for 'quantized'.
18
+
19
+
20
  ## Demo
21
 
22
  ***NOTE***: This demo uses [../face_detection_yunet](../face_detection_yunet) as face detector, which supports 5-landmark detection for now (2021sep).
 
27
  python demo.py --input1 /path/to/image1 --input2 /path/to/image2
28
  ```
29
 
30
+
31
  ## License
32
 
33
  All files in this directory are licensed under [Apache 2.0 License](./LICENSE).
tools/eval/README.md CHANGED
@@ -4,6 +4,7 @@ Make sure you have the following packages installed:
4
 
5
  ```shell
6
  pip install tqdm
 
7
  pip install scipy
8
  ```
9
 
@@ -14,8 +15,10 @@ python eval.py -m model_name -d dataset_name -dr dataset_root_dir
14
  ```
15
 
16
  Supported datasets:
 
17
  - [ImageNet](#imagenet)
18
  - [WIDERFace](#widerface)
 
19
 
20
  ## ImageNet
21
 
@@ -94,3 +97,44 @@ Run evaluation with the following command:
94
  ```shell
95
  python eval.py -m yunet -d widerface -dr /path/to/widerface
96
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  ```shell
6
  pip install tqdm
7
+ pip install scikit-learn
8
  pip install scipy
9
  ```
10
 
 
15
  ```
16
 
17
  Supported datasets:
18
+
19
  - [ImageNet](#imagenet)
20
  - [WIDERFace](#widerface)
21
+ - [LFW](#lfw)
22
 
23
  ## ImageNet
24
 
 
97
  ```shell
98
  python eval.py -m yunet -d widerface -dr /path/to/widerface
99
  ```
100
+
101
+ ## LFW
102
+
103
+ The script is modified based on [evaluation of InsightFace](https://github.com/deepinsight/insightface/blob/f92bf1e48470fdd567e003f196f8ff70461f7a20/src/eval/lfw.py).
104
+
105
+ This evaluation uses [YuNet](../../models/face_detection_yunet) as face detector. The structure of the face bounding boxes saved in [lfw_face_bboxes.npy](../eval/datasets/lfw_face_bboxes.npy) is shown below.
106
+ Each row represents the bounding box of the main face that will be used in each image.
107
+
108
+ ```shell
109
+ [
110
+ [x, y, w, h, x_re, y_re, x_le, y_le, x_nt, y_nt, x_rcm, y_rcm, x_lcm, y_lcm],
111
+ ...
112
+ [x, y, w, h, x_re, y_re, x_le, y_le, x_nt, y_nt, x_rcm, y_rcm, x_lcm, y_lcm]
113
+ ]
114
+ ```
115
+
116
+ `x1, y1, w, h` are the top-left coordinates, width and height of the face bounding box, `{x, y}_{re, le, nt, rcm, lcm}` stands for the coordinates of right eye, left eye, nose tip, the right corner and left corner of the mouth respectively. Data type of this numpy array is `np.float32`.
117
+
118
+
119
+ ### Prepare data
120
+
121
+ Please visit http://vis-www.cs.umass.edu/lfw to download the LFW [all images](http://vis-www.cs.umass.edu/lfw/lfw.tgz)(needs to be decompressed) and [pairs.txt](http://vis-www.cs.umass.edu/lfw/pairs.txt)(needs to be placed in the `view2` folder). Organize files as follow:
122
+
123
+ ```shell
124
+ $ tree -L 2 /path/to/lfw
125
+ .
126
+ ├── lfw
127
+ │   ├── Aaron_Eckhart
128
+ │   ├── ...
129
+ │   └── Zydrunas_Ilgauskas
130
+ └── view2
131
+    └── pairs.txt
132
+ ```
133
+
134
+ ### Evaluation
135
+
136
+ Run evaluation with the following command:
137
+
138
+ ```shell
139
+ python eval.py -m sface -d lfw -dr /path/to/lfw
140
+ ```
tools/eval/datasets/__init__.py CHANGED
@@ -1,5 +1,6 @@
1
  from .imagenet import ImageNet
2
  from .widerface import WIDERFace
 
3
 
4
  class Registery:
5
  def __init__(self, name):
@@ -15,3 +16,4 @@ class Registery:
15
  DATASETS = Registery("Datasets")
16
  DATASETS.register(ImageNet)
17
  DATASETS.register(WIDERFace)
 
 
1
  from .imagenet import ImageNet
2
  from .widerface import WIDERFace
3
+ from .lfw import LFW
4
 
5
  class Registery:
6
  def __init__(self, name):
 
16
  DATASETS = Registery("Datasets")
17
  DATASETS.register(ImageNet)
18
  DATASETS.register(WIDERFace)
19
+ DATASETS.register(LFW)
tools/eval/datasets/lfw.py ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import absolute_import
2
+ from __future__ import division
3
+ from __future__ import print_function
4
+
5
+ import os
6
+ import numpy as np
7
+
8
+ from sklearn.model_selection import KFold
9
+ from scipy import interpolate
10
+ import sklearn
11
+ from sklearn.decomposition import PCA
12
+
13
+ import cv2 as cv
14
+ from tqdm import tqdm
15
+
16
+
17
+ def calculate_roc(thresholds,
18
+ embeddings1,
19
+ embeddings2,
20
+ actual_issame,
21
+ nrof_folds=10,
22
+ pca=0):
23
+ assert (embeddings1.shape[0] == embeddings2.shape[0])
24
+ assert (embeddings1.shape[1] == embeddings2.shape[1])
25
+ nrof_pairs = min(len(actual_issame), embeddings1.shape[0])
26
+ nrof_thresholds = len(thresholds)
27
+ k_fold = KFold(n_splits=nrof_folds, shuffle=False)
28
+
29
+ tprs = np.zeros((nrof_folds, nrof_thresholds))
30
+ fprs = np.zeros((nrof_folds, nrof_thresholds))
31
+ accuracy = np.zeros((nrof_folds))
32
+ indices = np.arange(nrof_pairs)
33
+ # print('pca', pca)
34
+
35
+ if pca == 0:
36
+ diff = np.subtract(embeddings1, embeddings2)
37
+ dist = np.sum(np.square(diff), 1)
38
+
39
+ for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)):
40
+ # print('train_set', train_set)
41
+ # print('test_set', test_set)
42
+ if pca > 0:
43
+ print('doing pca on', fold_idx)
44
+ embed1_train = embeddings1[train_set]
45
+ embed2_train = embeddings2[train_set]
46
+ _embed_train = np.concatenate((embed1_train, embed2_train), axis=0)
47
+ # print(_embed_train.shape)
48
+ pca_model = PCA(n_components=pca)
49
+ pca_model.fit(_embed_train)
50
+ embed1 = pca_model.transform(embeddings1)
51
+ embed2 = pca_model.transform(embeddings2)
52
+ embed1 = sklearn.preprocessing.normalize(embed1)
53
+ embed2 = sklearn.preprocessing.normalize(embed2)
54
+ # print(embed1.shape, embed2.shape)
55
+ diff = np.subtract(embed1, embed2)
56
+ dist = np.sum(np.square(diff), 1)
57
+
58
+ # Find the best threshold for the fold
59
+ acc_train = np.zeros((nrof_thresholds))
60
+ for threshold_idx, threshold in enumerate(thresholds):
61
+ _, _, acc_train[threshold_idx] = calculate_accuracy(
62
+ threshold, dist[train_set], actual_issame[train_set])
63
+ best_threshold_index = np.argmax(acc_train)
64
+ for threshold_idx, threshold in enumerate(thresholds):
65
+ tprs[fold_idx,
66
+ threshold_idx], fprs[fold_idx,
67
+ threshold_idx], _ = calculate_accuracy(
68
+ threshold, dist[test_set],
69
+ actual_issame[test_set])
70
+ _, _, accuracy[fold_idx] = calculate_accuracy(
71
+ thresholds[best_threshold_index], dist[test_set],
72
+ actual_issame[test_set])
73
+
74
+ tpr = np.mean(tprs, 0)
75
+ fpr = np.mean(fprs, 0)
76
+ return tpr, fpr, accuracy
77
+
78
+
79
+ def calculate_accuracy(threshold, dist, actual_issame):
80
+ predict_issame = np.less(dist, threshold)
81
+ tp = np.sum(np.logical_and(predict_issame, actual_issame))
82
+ fp = np.sum(np.logical_and(predict_issame, np.logical_not(actual_issame)))
83
+ tn = np.sum(
84
+ np.logical_and(np.logical_not(predict_issame),
85
+ np.logical_not(actual_issame)))
86
+ fn = np.sum(np.logical_and(np.logical_not(predict_issame), actual_issame))
87
+
88
+ tpr = 0 if (tp + fn == 0) else float(tp) / float(tp + fn)
89
+ fpr = 0 if (fp + tn == 0) else float(fp) / float(fp + tn)
90
+ acc = float(tp + tn) / dist.size
91
+ return tpr, fpr, acc
92
+
93
+
94
+ def calculate_val(thresholds,
95
+ embeddings1,
96
+ embeddings2,
97
+ actual_issame,
98
+ far_target,
99
+ nrof_folds=10):
100
+ assert (embeddings1.shape[0] == embeddings2.shape[0])
101
+ assert (embeddings1.shape[1] == embeddings2.shape[1])
102
+ nrof_pairs = min(len(actual_issame), embeddings1.shape[0])
103
+ nrof_thresholds = len(thresholds)
104
+ k_fold = KFold(n_splits=nrof_folds, shuffle=False)
105
+
106
+ val = np.zeros(nrof_folds)
107
+ far = np.zeros(nrof_folds)
108
+
109
+ diff = np.subtract(embeddings1, embeddings2)
110
+ dist = np.sum(np.square(diff), 1)
111
+ indices = np.arange(nrof_pairs)
112
+
113
+ for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)):
114
+
115
+ # Find the threshold that gives FAR = far_target
116
+ far_train = np.zeros(nrof_thresholds)
117
+ for threshold_idx, threshold in enumerate(thresholds):
118
+ _, far_train[threshold_idx] = calculate_val_far(
119
+ threshold, dist[train_set], actual_issame[train_set])
120
+ if np.max(far_train) >= far_target:
121
+ f = interpolate.interp1d(far_train, thresholds, kind='slinear')
122
+ threshold = f(far_target)
123
+ else:
124
+ threshold = 0.0
125
+
126
+ val[fold_idx], far[fold_idx] = calculate_val_far(
127
+ threshold, dist[test_set], actual_issame[test_set])
128
+
129
+ val_mean = np.mean(val)
130
+ far_mean = np.mean(far)
131
+ val_std = np.std(val)
132
+ return val_mean, val_std, far_mean
133
+
134
+
135
+ def calculate_val_far(threshold, dist, actual_issame):
136
+ predict_issame = np.less(dist, threshold)
137
+ true_accept = np.sum(np.logical_and(predict_issame, actual_issame))
138
+ false_accept = np.sum(
139
+ np.logical_and(predict_issame, np.logical_not(actual_issame)))
140
+ n_same = np.sum(actual_issame)
141
+ n_diff = np.sum(np.logical_not(actual_issame))
142
+ val = float(true_accept) / float(n_same)
143
+ far = float(false_accept) / float(n_diff)
144
+ return val, far
145
+
146
+
147
+ def evaluate(embeddings, actual_issame, nrof_folds=10, pca=0):
148
+ # Calculate evaluation metrics
149
+ thresholds = np.arange(0, 4, 0.01)
150
+ embeddings1 = embeddings[0::2]
151
+ embeddings2 = embeddings[1::2]
152
+ tpr, fpr, accuracy = calculate_roc(thresholds,
153
+ embeddings1,
154
+ embeddings2,
155
+ np.asarray(actual_issame),
156
+ nrof_folds=nrof_folds,
157
+ pca=pca)
158
+ thresholds = np.arange(0, 4, 0.001)
159
+ val, val_std, far = calculate_val(thresholds,
160
+ embeddings1,
161
+ embeddings2,
162
+ np.asarray(actual_issame),
163
+ 1e-3,
164
+ nrof_folds=nrof_folds)
165
+ return tpr, fpr, accuracy, val, val_std, far
166
+
167
+
168
+ class LFW:
169
+ def __init__(self, root, target_size=250):
170
+ self.LFW_IMAGE_SIZE = 250
171
+
172
+ self.lfw_root = root
173
+ self.target_size = target_size
174
+
175
+ self.lfw_pairs_path = os.path.join(self.lfw_root, 'view2/pairs.txt')
176
+ self.image_path_pattern = os.path.join(self.lfw_root, 'lfw', '{person_name}', '{image_name}')
177
+
178
+ self.lfw_image_paths, self.id_list = self.load_pairs()
179
+
180
+ @property
181
+ def name(self):
182
+ return 'LFW'
183
+
184
+ def __len__(self):
185
+ return len(self.lfw_image_paths)
186
+
187
+ @property
188
+ def ids(self):
189
+ return self.id_list
190
+
191
+ def load_pairs(self):
192
+ image_paths = []
193
+ id_list = []
194
+ with open(self.lfw_pairs_path, 'r') as f:
195
+ for line in f.readlines()[1:]:
196
+ line = line.strip().split()
197
+ if len(line) == 3:
198
+ person_name = line[0]
199
+ image1_name = '{}_{:04d}.jpg'.format(person_name, int(line[1]))
200
+ image2_name = '{}_{:04d}.jpg'.format(person_name, int(line[2]))
201
+ image_paths += [
202
+ self.image_path_pattern.format(person_name=person_name, image_name=image1_name),
203
+ self.image_path_pattern.format(person_name=person_name, image_name=image2_name)
204
+ ]
205
+ id_list.append(True)
206
+ elif len(line) == 4:
207
+ person1_name = line[0]
208
+ image1_name = '{}_{:04d}.jpg'.format(person1_name, int(line[1]))
209
+ person2_name = line[2]
210
+ image2_name = '{}_{:04d}.jpg'.format(person2_name, int(line[3]))
211
+ image_paths += [
212
+ self.image_path_pattern.format(person_name=person1_name, image_name=image1_name),
213
+ self.image_path_pattern.format(person_name=person2_name, image_name=image2_name)
214
+ ]
215
+ id_list.append(False)
216
+ return image_paths, id_list
217
+
218
+ def __getitem__(self, key):
219
+ img = cv.imread(self.lfw_image_paths[key])
220
+ if self.target_size != self.LFW_IMAGE_SIZE:
221
+ img = cv.resize(img, (self.target_size, self.target_size))
222
+ return img
223
+
224
+ def eval(self, model):
225
+ ids = self.ids
226
+ embeddings = np.zeros(shape=(len(self), 128))
227
+ face_bboxes = np.load("./datasets/lfw_face_bboxes.npy")
228
+ for idx, img in tqdm(enumerate(self), desc="Evaluating {} with {} val set".format(model.name, self.name)):
229
+ embedding = model.infer(img, face_bboxes[idx])
230
+ embeddings[idx] = embedding
231
+
232
+ embeddings = sklearn.preprocessing.normalize(embeddings)
233
+ self.tpr, self.fpr, self.acc, self.val, self.std, self.far = evaluate(embeddings, ids, nrof_folds=10)
234
+ self.acc, self.std = np.mean(self.acc), np.std(self.acc)
235
+
236
+ def print_result(self):
237
+ print("==================== Results ====================")
238
+ print("Average Accuracy: {:.4f}".format(self.acc))
239
+ print("=================================================")
tools/eval/eval.py CHANGED
@@ -64,7 +64,15 @@ models = dict(
64
  modelPath=os.path.join(root_dir, "models/face_detection_yunet/face_detection_yunet_2022mar-act_int8-wt_int8-quantized.onnx"),
65
  topK=5000,
66
  confThreshold=0.3,
67
- nmsThreshold=0.45)
 
 
 
 
 
 
 
 
68
  )
69
 
70
  datasets = dict(
@@ -74,7 +82,11 @@ datasets = dict(
74
  size=224),
75
  widerface=dict(
76
  name="WIDERFace",
77
- topic="face_detection")
 
 
 
 
78
  )
79
 
80
  def main(args):
 
64
  modelPath=os.path.join(root_dir, "models/face_detection_yunet/face_detection_yunet_2022mar-act_int8-wt_int8-quantized.onnx"),
65
  topK=5000,
66
  confThreshold=0.3,
67
+ nmsThreshold=0.45),
68
+ sface=dict(
69
+ name="SFace",
70
+ topic="face_recognition",
71
+ modelPath=os.path.join(root_dir, "models/face_recognition_sface/face_recognition_sface_2021dec.onnx")),
72
+ sface_q=dict(
73
+ name="SFace",
74
+ topic="face_recognition",
75
+ modelPath=os.path.join(root_dir, "models/face_recognition_sface/face_recognition_sface_2021dec-act_int8-wt_int8-quantized.onnx")),
76
  )
77
 
78
  datasets = dict(
 
82
  size=224),
83
  widerface=dict(
84
  name="WIDERFace",
85
+ topic="face_detection"),
86
+ lfw=dict(
87
+ name="LFW",
88
+ topic="face_recognition",
89
+ target_size=112),
90
  )
91
 
92
  def main(args):