-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.py
202 lines (164 loc) · 7.38 KB
/
utils.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import tensorflow as tf
import numpy as np
import random
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
import torch
import torch.nn as nn
from torchvision import models, transforms
import dlib
import os
from tqdm import tqdm
from PIL import Image
# Hide GPU from visible devices
tf.config.set_visible_devices([], 'GPU')
def image_grid(imgs, rows, cols, spacing = 20):
assert len(imgs) == rows * cols
w, h = imgs[0].size
grid = Image.new('RGB', size=(cols * w + (cols-1)*spacing, rows * h + (rows-1)*spacing ), color='white')
grid_w, grid_h = grid.size
for i, img in enumerate(imgs):
grid.paste(img, box=( i // rows * (w+spacing), i % rows * (h+spacing)))
return grid
def get_random(length):
random_list = []
while sum(random_list) != length/2:
random_list = [random.randint(0, 1) for i in range(length)]
return random_list
def face_existing(img, cnn_face_detector, default_max_size=800, size = 300, padding = 0.25):
old_height, old_width, _ = img.shape
if old_width > old_height:
new_width, new_height = default_max_size, int(default_max_size * old_height / old_width)
else:
new_width, new_height = int(default_max_size * old_width / old_height), default_max_size
img = dlib.resize_image(img, rows=new_height, cols=new_width)
dets = cnn_face_detector(img, 1)
num_faces = len(dets)
return num_faces
def detect_face(image_paths, SAVE_DETECTED_AT, cnn_face_detector, default_max_size=800, size = 300, padding = 0.25):
sp = dlib.shape_predictor('dlib_models/shape_predictor_5_face_landmarks.dat')
base = 2000 # largest width and height
for index, image_path in tqdm(enumerate(image_paths)):
if index % 1000 == 0:
print('---%d/%d---' %(index, len(image_paths)))
img = dlib.load_rgb_image(image_path)
#try:
# img = dlib.load_rgb_image(image_path)
#except:
# print(f"no face found {index}")
# continue
old_height, old_width, _ = img.shape
if old_width > old_height:
new_width, new_height = default_max_size, int(default_max_size * old_height / old_width)
else:
new_width, new_height = int(default_max_size * old_width / old_height), default_max_size
img = dlib.resize_image(img, rows=new_height, cols=new_width)
dets = cnn_face_detector(img, 1)
num_faces = len(dets)
if num_faces != 1:
print(f"no face found {index}")
continue
# Find the 5 face landmarks we need to do the alignment.
faces = dlib.full_object_detections()
for detection in dets:
rect = detection.rect
faces.append(sp(img, rect))
images = dlib.get_face_chips(img, faces, size=size, padding = padding)
for idx, image in enumerate(images):
img_name = image_path.split("/")[-1]
path_sp = img_name.split(".")
face_name = os.path.join(SAVE_DETECTED_AT, path_sp[0] + "_" + "face" + str(idx) + "." + path_sp[-1])
dlib.save_image(image, face_name)
def predict_age_gender_race(save_prediction_at, imgs_path = 'detected_faces/'):
img_names = [os.path.join(imgs_path, x) for x in os.listdir(imgs_path) if 'ipynb' not in x]
model_fair_7 = models.resnet34(pretrained=True)
model_fair_7.fc = nn.Linear(model_fair_7.fc.in_features, 18)
model_fair_7.load_state_dict(torch.load('dlib_models/res34_fair_align_multi_7_20190809.pt'))
model_fair_7 = model_fair_7.to('cuda')
model_fair_7.eval()
trans = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# img pth of face images
face_names = []
# list within a list. Each sublist contains scores for all races. Take max for predicted race
race_scores_fair = []
gender_scores_fair = []
age_scores_fair = []
race_preds_fair = []
gender_preds_fair = []
age_preds_fair = []
for index, img_name in tqdm(enumerate(img_names)):
if index % 1000 == 0:
print("Predicting... {}/{}".format(index, len(img_names)))
face_names.append(img_name)
image = dlib.load_rgb_image(img_name)
image = trans(image)
image = image.view(1, 3, 224, 224) # reshape image to match model dimensions (1 batch size)
image = image.to('cuda')
# fair
outputs = model_fair_7(image)
outputs = outputs.cpu().detach().numpy()
outputs = np.squeeze(outputs)
race_outputs = outputs[:7]
gender_outputs = outputs[7:9]
age_outputs = outputs[9:18]
race_score = np.exp(race_outputs) / np.sum(np.exp(race_outputs))
gender_score = np.exp(gender_outputs) / np.sum(np.exp(gender_outputs))
age_score = np.exp(age_outputs) / np.sum(np.exp(age_outputs))
race_pred = np.argmax(race_score)
gender_pred = np.argmax(gender_score)
age_pred = np.argmax(age_score)
race_scores_fair.append(race_score)
gender_scores_fair.append(gender_score)
age_scores_fair.append(age_score)
race_preds_fair.append(race_pred)
gender_preds_fair.append(gender_pred)
age_preds_fair.append(age_pred)
result = pd.DataFrame([face_names,
race_preds_fair,
gender_preds_fair,
age_preds_fair,
race_scores_fair,
gender_scores_fair,
age_scores_fair, ]).T
result.columns = ['face_name_align',
'race_preds_fair',
'gender_preds_fair',
'age_preds_fair',
'race_scores_fair',
'gender_scores_fair',
'age_scores_fair']
result.loc[result['race_preds_fair'] == 0, 'race'] = 'White'
result.loc[result['race_preds_fair'] == 1, 'race'] = 'Black'
result.loc[result['race_preds_fair'] == 2, 'race'] = 'Latino_Hispanic'
result.loc[result['race_preds_fair'] == 3, 'race'] = 'East Asian'
result.loc[result['race_preds_fair'] == 4, 'race'] = 'Southeast Asian'
result.loc[result['race_preds_fair'] == 5, 'race'] = 'Indian'
result.loc[result['race_preds_fair'] == 6, 'race'] = 'Middle Eastern'
# gender
result.loc[result['gender_preds_fair'] == 0, 'gender'] = 'Male'
result.loc[result['gender_preds_fair'] == 1, 'gender'] = 'Female'
# age
result.loc[result['age_preds_fair'] == 0, 'age'] = '0-2'
result.loc[result['age_preds_fair'] == 1, 'age'] = '3-9'
result.loc[result['age_preds_fair'] == 2, 'age'] = '10-19'
result.loc[result['age_preds_fair'] == 3, 'age'] = '20-29'
result.loc[result['age_preds_fair'] == 4, 'age'] = '30-39'
result.loc[result['age_preds_fair'] == 5, 'age'] = '40-49'
result.loc[result['age_preds_fair'] == 6, 'age'] = '50-59'
result.loc[result['age_preds_fair'] == 7, 'age'] = '60-69'
result.loc[result['age_preds_fair'] == 8, 'age'] = '70+'
result[['face_name_align',
'race',
'gender', 'age',
'race_scores_fair',
'gender_scores_fair', 'age_scores_fair']].to_csv(save_prediction_at, index=False)
return result
def ensure_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)