forked from ronghuaiyang/arcface-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
174 lines (132 loc) · 4.41 KB
/
test.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
# -*- coding: utf-8 -*-
"""
Created on 18-5-30 下午4:55
@author: ronghuaiyang
"""
from __future__ import print_function
import os
import cv2
from models import *
import torch
import numpy as np
import time
from config import Config
from torch.nn import DataParallel
def get_lfw_list(pair_list):
with open(pair_list, 'r') as fd:
pairs = fd.readlines()
data_list = []
for pair in pairs:
splits = pair.split()
if splits[0] not in data_list:
data_list.append(splits[0])
if splits[1] not in data_list:
data_list.append(splits[1])
return data_list
def load_image(img_path):
image = cv2.imread(img_path, 0)
if image is None:
return None
image = np.dstack((image, np.fliplr(image)))
image = image.transpose((2, 0, 1))
image = image[:, np.newaxis, :, :]
image = image.astype(np.float32, copy=False)
image -= 127.5
image /= 127.5
return image
def get_featurs(model, test_list, batch_size=10):
images = None
features = None
cnt = 0
for i, img_path in enumerate(test_list):
image = load_image(img_path)
if image is None:
print('read {} error'.format(img_path))
if images is None:
images = image
else:
images = np.concatenate((images, image), axis=0)
if images.shape[0] % batch_size == 0 or i == len(test_list) - 1:
cnt += 1
data = torch.from_numpy(images)
data = data.to(torch.device("cuda"))
output = model(data)
output = output.data.cpu().numpy()
fe_1 = output[::2]
fe_2 = output[1::2]
feature = np.hstack((fe_1, fe_2))
# print(feature.shape)
if features is None:
features = feature
else:
features = np.vstack((features, feature))
images = None
return features, cnt
def load_model(model, model_path):
model_dict = model.state_dict()
pretrained_dict = torch.load(model_path)
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
def get_feature_dict(test_list, features):
fe_dict = {}
for i, each in enumerate(test_list):
# key = each.split('/')[1]
fe_dict[each] = features[i]
return fe_dict
def cosin_metric(x1, x2):
return np.dot(x1, x2) / (np.linalg.norm(x1) * np.linalg.norm(x2))
def cal_accuracy(y_score, y_true):
y_score = np.asarray(y_score)
y_true = np.asarray(y_true)
best_acc = 0
best_th = 0
for i in range(len(y_score)):
th = y_score[i]
y_test = (y_score >= th)
acc = np.mean((y_test == y_true).astype(int))
if acc > best_acc:
best_acc = acc
best_th = th
return (best_acc, best_th)
def test_performance(fe_dict, pair_list):
with open(pair_list, 'r') as fd:
pairs = fd.readlines()
sims = []
labels = []
for pair in pairs:
splits = pair.split()
fe_1 = fe_dict[splits[0]]
fe_2 = fe_dict[splits[1]]
label = int(splits[2])
sim = cosin_metric(fe_1, fe_2)
sims.append(sim)
labels.append(label)
acc, th = cal_accuracy(sims, labels)
return acc, th
def lfw_test(model, img_paths, identity_list, compair_list, batch_size):
s = time.time()
features, cnt = get_featurs(model, img_paths, batch_size=batch_size)
print(features.shape)
t = time.time() - s
print('total time is {}, average time is {}'.format(t, t / cnt))
fe_dict = get_feature_dict(identity_list, features)
acc, th = test_performance(fe_dict, compair_list)
print('lfw face verification accuracy: ', acc, 'threshold: ', th)
return acc
if __name__ == '__main__':
opt = Config()
if opt.backbone == 'resnet18':
model = resnet_face18(opt.use_se)
elif opt.backbone == 'resnet34':
model = resnet34()
elif opt.backbone == 'resnet50':
model = resnet50()
model = DataParallel(model)
# load_model(model, opt.test_model_path)
model.load_state_dict(torch.load(opt.test_model_path))
model.to(torch.device("cuda"))
identity_list = get_lfw_list(opt.lfw_test_list)
img_paths = [os.path.join(opt.lfw_root, each) for each in identity_list]
model.eval()
lfw_test(model, img_paths, identity_list, opt.lfw_test_list, opt.test_batch_size)