-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_embeddings.py
114 lines (93 loc) · 3.12 KB
/
plot_embeddings.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
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
import argparse
import torch
import yaml
import pandas as pd
from model import FaceNetModel
from dataset import get_face_dataloader
if __name__ == "__main__":
config_file_path = "config.yaml"
try:
## loading the configuration
with open(config_file_path, "r") as stream:
config = yaml.safe_load(stream)
except:
print("Could not find the config file,please pass the path correctly")
exit()
# parser = argparse.ArgumentParser(description='Masked Face Recognition')
# parser.add_argument('--variant',type=str,help="Name of variant a) triplet b) quad triplet")
# args = vars(parser.parse_args())
variant = 'triplet_model'
config = config[variant]
ckpt_path = config['dataset']['save_dir'] + config['model']['best_ckpt_name']
dataset_dir = config['dataset']['root_dir']
batch_size = config['dataset']['batch_size']
num_workers = config['dataset']['num_workers']
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = FaceNetModel('casia-webface').to(device)
state_dict = torch.load(ckpt_path)['state_dict']
model.load_state_dict(state_dict)
embeddings = []
labels = []
dataset = get_face_dataloader(dataset_dir,batch_size,num_workers)
for idx,(batch_img,batch_label) in enumerate(dataset):
embs = model(batch_img.to(device))
embeddings.extend(embs.detach().cpu().numpy())
labels.extend(batch_label)
df = pd.DataFrame({
'embeddings':embeddings,
'labels':labels
})
pca = PCA(n_components=3)
pca_res = pca.fit_transform(embeddings)
df['pca-one'] = pca_res[:,0]
df['pca-two'] = pca_res[:,1]
df['pca-three'] = pca_res[:,2]
print('Explained variation per principal component: {}'.format(pca.explained_variance_ratio_))
plt.figure(figsize=(16,10))
sns.scatterplot(
x="pca-one", y="pca-two",
hue = "labels",
palette=sns.color_palette("hls", 10),
data=df,
legend="full",
alpha=0.3
)
tsne = TSNE(n_components=2, verbose=1, perplexity=40, n_iter=300)
tsne_results = tsne.fit_transform(embeddings)
df['tsne-2d-one'] = tsne_results[:,0]
df['tsne-2d-two'] = tsne_results[:,1]
plt.figure(figsize=(16,10))
sns.scatterplot(
x="tsne-2d-one", y="tsne-2d-two",
hue = "labels",
palette=sns.color_palette("hls", 10),
data=df,
legend="full",
alpha=0.3
)
plt.figure(figsize=(16,7))
ax1 = plt.subplot(1, 2, 1)
sns.scatterplot(
x="pca-one", y="pca-two",
hue = "labels",
palette=sns.color_palette("hls", 10),
data=df,
legend="full",
alpha=0.3,
ax=ax1
)
ax2 = plt.subplot(1, 2, 2)
sns.scatterplot(
x="tsne-2d-one", y="tsne-2d-two",
hue = "labels",
palette=sns.color_palette("hls", 10),
data=df,
legend="full",
alpha=0.3,
ax=ax2
)