forked from rkuo2000/tf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_recognition.py
108 lines (83 loc) · 2.83 KB
/
face_recognition.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
# $ cd ~/tf
# $ git clone https://github.com/rkuo2000/facenet-pytorch facenet_pytorch
# $ pip3 install opencv-python=4.5.3.56
# $ pip3 install pandas requests
import torch
import torchvision
from torch.utils.data import DataLoader
from torchvision import datasets
import numpy as np
import pandas as pd
import os
from torchvision import transforms
from facenet_pytorch import MTCNN, InceptionResnetV1
import matplotlib
import matplotlib.pyplot as plt
workers = 0 if os.name == 'nt' else 4
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(device)
# setup MTCNN and FaceNet model
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
)
net = InceptionResnetV1(pretrained='vggface2').eval().to(device)
# Prepare Faces Dataset
def collate_fn(x):
return x[0]
dataset = datasets.ImageFolder('facenet_pytorch/data/test_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)
print(dataset.idx_to_class)
aligned = []
names = []
for x, y in loader:
x_aligned, bbox, 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])
print(len(aligned))
print(names)
# calculate image embeddings
aligned = torch.stack(aligned).to(device)
embeddings = net(aligned).detach().cpu()
print(len(embeddings))
print(embeddings[0].shape)
# print distance matrix for classes
dists = [[(e1 - e2).norm().item() for e2 in embeddings] for e1 in embeddings]
print(pd.DataFrame(dists, columns=names, index=names))
# Test : single face picture
mtcnn = MTCNN(keep_all=False)
img = plt.imread("facenet_pytorch/data/test_images/angelina_jolie/1.jpg")
plt.axis('off')
plt.imshow(img)
plt.show()
x_test, bbox, prob = mtcnn(img, return_prob=True)
print(prob)
print(x_test.shape)
x_aligned=[]
x_aligned.append(x_test)
test_aligned = torch.stack(x_aligned).to(device)
test_embeddings = net(test_aligned).detach().cpu()
print(len(embeddings), len(test_embeddings))
e1 = test_embeddings
dists = [(e1 - e2).norm().item() for e2 in embeddings]
print(dists)
print(names[np.argmin(dists)])
# Test : multiple faces picture
img = plt.imread("facenet_pytorch/data/angelina-leonardo-bradley.jpg")
plt.axis('off')
plt.imshow(img)
plt.show()
mtcnn = MTCNN(keep_all=True)
x_test, bbox, prob = mtcnn(img, return_prob=True)
print(prob)
print(x_test.shape)
test_aligned = x_test.to(device)
test_embeddings = net(test_aligned).detach().cpu()
dists = [[(e1 - e2).norm().item() for e2 in embeddings] for e1 in test_embeddings]
test_no=[]
[test_no.append(i) for i in range(len(test_embeddings))]
print(pd.DataFrame(dists, columns=names, index=test_no))