-
Notifications
You must be signed in to change notification settings - Fork 92
/
predict.py
218 lines (186 loc) · 8.62 KB
/
predict.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from __future__ import print_function, division
import warnings
warnings.filterwarnings("ignore")
import os.path
import pandas as pd
import torch
import torch.nn as nn
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import dlib
import os
import argparse
def rect_to_bb(rect):
# take a bounding predicted by dlib and convert it
# to the format (x, y, w, h) as we would normally do
# with OpenCV
x = rect.left()
y = rect.top()
w = rect.right() - x
h = rect.bottom() - y
# return a tuple of (x, y, w, h)
return (x, y, w, h)
def detect_face(image_paths, SAVE_DETECTED_AT, default_max_size=800,size = 300, padding = 0.25):
cnn_face_detector = dlib.cnn_face_detection_model_v1('dlib_models/mmod_human_face_detector.dat')
sp = dlib.shape_predictor('dlib_models/shape_predictor_5_face_landmarks.dat')
base = 2000 # largest width and height
for index, image_path in enumerate(image_paths):
if index % 1000 == 0:
print('---%d/%d---' %(index, len(image_paths)))
img = dlib.load_rgb_image(image_path)
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 == 0:
print("Sorry, there were no faces found in '{}'".format(image_path))
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 predidct_age_gender_race(save_prediction_at, imgs_path = 'cropped_faces/'):
img_names = [os.path.join(imgs_path, x) for x in os.listdir(imgs_path)]
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model_fair_7 = torchvision.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('fair_face_models/fairface_alldata_20191111.pt'))
model_fair_7 = model_fair_7.to(device)
model_fair_7.eval()
model_fair_4 = torchvision.models.resnet34(pretrained=True)
model_fair_4.fc = nn.Linear(model_fair_4.fc.in_features, 18)
model_fair_4.load_state_dict(torch.load('fair_face_models/fairface_alldata_4race_20191111.pt'))
model_fair_4 = model_fair_4.to(device)
model_fair_4.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 = []
race_scores_fair_4 = []
race_preds_fair_4 = []
for index, img_name in 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(device)
# 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)
# fair 4 class
outputs = model_fair_4(image)
outputs = outputs.cpu().detach().numpy()
outputs = np.squeeze(outputs)
race_outputs = outputs[:4]
race_score = np.exp(race_outputs) / np.sum(np.exp(race_outputs))
race_pred = np.argmax(race_score)
race_scores_fair_4.append(race_score)
race_preds_fair_4.append(race_pred)
result = pd.DataFrame([face_names,
race_preds_fair,
race_preds_fair_4,
gender_preds_fair,
age_preds_fair,
race_scores_fair, race_scores_fair_4,
gender_scores_fair,
age_scores_fair, ]).T
result.columns = ['face_name_align',
'race_preds_fair',
'race_preds_fair_4',
'gender_preds_fair',
'age_preds_fair',
'race_scores_fair',
'race_scores_fair_4',
'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'
# race fair 4
result.loc[result['race_preds_fair_4'] == 0, 'race4'] = 'White'
result.loc[result['race_preds_fair_4'] == 1, 'race4'] = 'Black'
result.loc[result['race_preds_fair_4'] == 2, 'race4'] = 'Asian'
result.loc[result['race_preds_fair_4'] == 3, 'race4'] = 'Indian'
# 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', 'race4',
'gender', 'age',
'race_scores_fair', 'race_scores_fair_4',
'gender_scores_fair', 'age_scores_fair']].to_csv(save_prediction_at, index=False)
print("saved results at ", save_prediction_at)
def ensure_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
if __name__ == "__main__":
#Please create a csv with one column 'img_path', contains the full paths of all images to be analyzed.
#Also please change working directory to this file.
parser = argparse.ArgumentParser()
parser.add_argument('--csv', dest='input_csv', action='store',
help='csv file of image path where col name for image path is "img_path')
dlib.DLIB_USE_CUDA = True
print("using CUDA?: %s" % dlib.DLIB_USE_CUDA)
args = parser.parse_args()
SAVE_DETECTED_AT = "detected_faces"
ensure_dir(SAVE_DETECTED_AT)
imgs = pd.read_csv(args.input_csv)['img_path']
detect_face(imgs, SAVE_DETECTED_AT)
print("detected faces are saved at ", SAVE_DETECTED_AT)
#Please change test_outputs.csv to actual name of output csv.
predidct_age_gender_race("test_outputs.csv", SAVE_DETECTED_AT)