-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathembedding_gen.py
More file actions
68 lines (52 loc) · 1.88 KB
/
embedding_gen.py
File metadata and controls
68 lines (52 loc) · 1.88 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
from facenet_pytorch import MTCNN, InceptionResnetV1
import torch
from torch.utils.data import DataLoader
from torchvision import datasets
import os
workers = 0 if os.name == 'nt' else 2
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print('Running on device: {}'.format(device))
mtcnn = MTCNN(
image_size=160, margin=0, min_face_size=20,
thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=True,
device=device
)
resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device)
try:
loaded_dict = torch.load("face_embeddings.pt")
except:
loaded_dict ={}
def collate_fn(x):
return x[0]
def get_embeddings():
dataset = datasets.ImageFolder(
"images")
dataset.idx_to_class = {i: c for c, i in dataset.class_to_idx.items()}
loader = DataLoader(dataset, collate_fn=collate_fn, num_workers=workers)
aligned = []
names = []
existingUser = []
for keys in loaded_dict:
existingUser.append(keys)
for x, y in loader:
if dataset.idx_to_class[y] in existingUser:
print("User already exists")
pass
else:
x_aligned, prob = mtcnn(x, return_prob=True)
if x_aligned is not None:
print('Face detected with probability: {:8f}'.format(prob))
aligned.append(x_aligned)
names.append(dataset.idx_to_class[y])
if(len(aligned)!= 0):
aligned = torch.stack(aligned).to(device)
embeddings = resnet(aligned).detach().cpu()
embeddings_dict = {}
for name in set(names):
embeddings_dict[name] = []
for i in range(len(names)):
if names[i] == name:
embeddings_dict[name].append(embeddings[i])
loaded_dict.update(embeddings_dict)
torch.save(loaded_dict, "face_embeddings.pt")
print("New embeddings added")