-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvectorize.py
139 lines (119 loc) · 3.7 KB
/
vectorize.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
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--calculate_attr', help='specify the name of the attribut for which the vector should be calculated')
import torch
import joblib
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from torchvision import datasets, transforms
from vae import model
from transformer import FaceTransformer
model.load_state_dict(torch.load('./data/weights.pth'))
attributes = [
'image',
'5_o_Clock_Shadow',
'Arched_Eyebrows',
'Attractive',
'Bags_Under_Eyes',
'Bald',
'Bangs',
'Big_Lips',
'Big_Nose',
'Black_Hair',
'Blond_Hair',
'Blurry',
'Brown_Hair',
'Bushy_Eyebrows',
'Chubby',
'Double_Chin',
'Eyeglasses',
'Goatee',
'Gray_Hair',
'Heavy_Makeup',
'High_Cheekbones',
'Male',
'Mouth_Slightly_Open',
'Mustache',
'Narrow_Eyes',
'No_Beard',
'Oval_Face',
'Pale_Skin',
'Pointy_Nose',
'Receding_Hairline',
'Rosy_Cheeks',
'Sideburns',
'Smiling',
'Straight_Hair',
'Wavy_Hair',
'Wearing_Earrings',
'Wearing_Hat',
'Wearing_Lipstick',
'Wearing_Necklace',
'Wearing_Necktie',
'Young'
]
num_samples = 200
def readAttributes():
data = []
with open('./data/list_attr_celeba.txt') as f:
for line in f:
line = line.split()
data.append([line[0]] + [i == '1' for i in line[1:]])
return pd.DataFrame(data, columns=attributes)
df = readAttributes()
def getAttributeSplit(attribute):
pos_images = df.loc[df[attribute]]['image']
neg_images = df.loc[~df[attribute]]['image']
return pos_images.sample(frac=1), neg_images.sample(frac=1)
def imagesToBatch(image_names):
batch = []
totensor = transforms.ToTensor()
for image_name in image_names:
transformer = FaceTransformer()
transformer.detect_faces('./images/img_align_celeba/img_align_celeba/' + image_name)
if len(transformer.faces) > 0:
img = transformer.faces[0].face.image.astype(np.float32)
batch.append(totensor(img))
return torch.stack(batch, dim=0)
def encodeImages(image_names):
vectors = []
for i in range(0, len(image_names), num_samples):
print(f'processed {i} of {len(image_names)} images')
batch = imagesToBatch(image_names.iloc[i:i+num_samples])
vector = model.get_latent_var(batch).detach().numpy().mean(axis=0)
vectors.append(vector)
return vectors
def dumpAttributeVectors(attribute):
pos_images, neg_images = getAttributeSplit(attribute)
n = 5000
result = {
'pos': encodeImages(pos_images.iloc[:n]),
'neg': encodeImages(neg_images.iloc[:n])
}
joblib.dump(result, './expression_vectors/' + attribute + '.pkl')
def boxplot(attribute):
result = joblib.load('./expression_vectors/' + attribute + '.pkl')
vectors = []
for smile, non_smile in zip(result['pos'], result['neg']):
vectors.append(non_smile - smile)
vectors = np.array(vectors)
print(vectors.shape)
X = np.arange(vectors[0].shape[0])
plt.boxplot(vectors)
plt.show()
def loadVector(attribute, i):
result = joblib.load('./expression_vectors/' + attribute + '.pkl')
return result['pos'][i] - result['neg'][i]
def summarizeVectors():
result = {
'Smiling': loadVector('Smiling', 5),
'Mustache': loadVector('Mustache', 2),
'Young': loadVector('Young', 0)
}
joblib.dump(result, './expression_vectors/summary.pkl')
if __name__ == "__main__":
args = parser.parse_args()
print(f'calculating expression vector for attribute {args.calculate_attr}')
dumpAttributeVectors(args.calculate_attr)