-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathverification.py
143 lines (133 loc) · 5.72 KB
/
verification.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
import torch
import torch.nn.functional as F
import numpy as np
import pickle
import json
from PIL import Image
from tqdm import tqdm
from torchvision import transforms
from utils.dataset import FaceDataset
from torch.utils.data import DataLoader
from arcface import ArcFaceModel
class Verification:
def __init__(self, config) -> None:
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.config = config
self.cosine = torch.nn.CosineSimilarity()
self.train_set = FaceDataset(root_dir=config['trainset_path'])
self.model = ArcFaceModel(backbone_name=config['backbone'],
input_size=[112,112],
num_classes=self.train_set.num_classes)
try:
self.embedder = self.model.backbone
self.embedder.load_state_dict(torch.load(config['pretrained_backbone_path']))
self.embedder.eval()
self.embedder.to(self.device)
except:
raise ValueError("No feature extractions were found!")
self.transform = self.train_set.transform
def get_base_embedding(self,
saving=True,
prefix = ''):
labels = []
train_embs = []
with open(self.config['label_dict_path'], 'rb') as f:
label_dict = pickle.load(f)
for label_idx in tqdm(label_dict.keys()):
embs = []
_, samples = self.train_set.get_items_by_class(self.config['label_dict_path'],
label_idx)
for path in samples:
image = Image.open(path)
image = self.transform(image)
image = torch.stack([image]).to(self.device)
with torch.no_grad():
emb = self.embedder(image).cpu().squeeze()
del image
torch.cuda.empty_cache()
embs.append(emb)
embs = torch.stack(embs)
train_embs.append(torch.mean(embs, axis=0))
labels.append(label_idx)
train_embs = torch.stack(train_embs)
if saving:
torch.save(train_embs, 'logs/'+prefix+'base_embedding.pth')
torch.save(labels, 'logs/'+prefix+'base_label.pth')
return train_embs, labels
def verify(self,
mode = 'emb',
threshold = 0.7,
faces = None,
embeddings = None,
base_embedding = None,
base_labels = None):
'''
mode:
> emb: use embedding vectors to verify
> img: use raw face images to verify
faces: single or a batch of face images, not None if mode is img
embeddings: embedding vector to verify, not None if mode is emb
base_embedding: path to base embedding tensor (.pth file), auto generate if it is None
labels: path to base labels (.pth file), auto generate if it is None
'''
ids = []
user_names = []
if (base_embedding is None) or (base_labels is None):
train_embs, base_labels = self.get_base_embedding()
else:
train_embs = torch.load(base_embedding)
base_labels = torch.load(base_labels)
if mode == 'img':
assert faces is not None
if len(faces.shape) < 4:
faces = torch.stack([faces])
faces = faces.to(self.device)
embeddings = self.embedder(faces).cpu()
elif mode == 'emb':
assert embeddings is not None
for idx in range(embeddings.shape[0]):
with torch.no_grad():
out = self.cosine(F.normalize(train_embs), F.normalize(embeddings[idx:idx+1, :]))
if abs(torch.max(out)) < threshold:
ids.append(-1)
user_names.append("Unknown")
else:
label_idx = base_labels[torch.argmax(out)]
ids.append(label_idx)
user_names.append(self.train_set.convert_id2name(label_idx))
return torch.Tensor(ids), user_names
if __name__ == '__main__':
with open('configs/arcface.json', "r") as jsonfile:
config = json.load(jsonfile)['verification']
verification = Verification(config)
# # Pick some images
# faces = []
# test_img = Image.open("./data/datav2/tungng/out2_surgical.jpg")
# faces.append(verification.transform(test_img))
# test_img = Image.open("./data/datav2/tungng/out2.jpg")
# faces.append(verification.transform(test_img))
# faces = torch.stack(faces)
test_set = FaceDataset(root_dir=config['testset_path'])
test_loader = DataLoader(test_set,
batch_size = 64,
shuffle = False,
num_workers = 8)
temp_acc = []
for idx, (images, labels) in tqdm(enumerate(test_loader)):
faces = images
shape = faces.shape
# Filling hair
if config["fill_hair"]:
thickness = config["hair_filling_thickness"]
mask_shape = (shape[0], shape[1], thickness, shape[3])
faces[:, :, 0:thickness, :] = torch.zeros(mask_shape)
ids, _ = verification.verify(faces=faces,
embeddings = faces,
threshold = 0.5,
mode='img',
base_embedding=config["base_embedding"],
base_labels=config["base_labels"])
correct = (ids == labels).type(torch.FloatTensor)
temp_acc.append(correct.mean())
accuracy = sum(temp_acc)/len(temp_acc)
print("Accuracy: ", round(accuracy.item()*100, 4), '%')