-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheval_gender_Age_with_label.py
510 lines (408 loc) · 16.4 KB
/
eval_gender_Age_with_label.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import numpy as np
import tensorflow as tf
from PIL import Image
from Net import triplet_loss as triplet
import time
import threading
from queue import Queue
import os
import dlib
import argparse
def pre_process(input_batch):
imgs = (input_batch - 255.0/2) / (255.0/2)
return imgs
def load_path_lists(data_dir):
lists = os.listdir(data_dir)
lists = [os.path.join(data_dir,f) for f in lists]
lists = [f for f in lists if os.path.isdir(f)]
results = []
for f in lists:
temp_array = [os.path.join(f,path) for path in os.listdir(f)]
results.append(temp_array)
return results
def load_path_lm_lists(data_dir,flie_name):
with open(os.path.join(data_dir,flie_name),"r") as f:
lines = f.readlines()
lines = [f.strip() for f in lines]
temp_path = np.array([data_dir+"/"+f.strip().split(" ",1)[0] for f in lines])
temp_lm_list = np.array([f.strip().split(" ",1)[1].split(" ")[1:] for f in lines],dtype = np.float32)
label_list = [f.split("/")[2] for f in temp_path]
hush_table = {}
path_list = []
lm_list =[]
keys_list_temp=[]
for i,l in enumerate(label_list):
if l not in hush_table.keys():
hush_table[l] = [i]
keys_list_temp.append(l)
else:
hush_table[l] += [i]
#for k,v in hush_table.items():
# path_list.append(temp_path[v])
# lm_list.append(temp_lm_list[v])
for i,l in enumerate(keys_list_temp):
path_list.append(temp_path[hush_table[l]])
lm_list.append(temp_lm_list[hush_table[l]])
del temp_path
del temp_lm_list
del label_list
del hush_table
return path_list,lm_list
class Data_Thread(threading.Thread):
def __init__(self, threadID,batch_size, img_height,img_width, jitter_count,padding,q):
threading.Thread.__init__(self)
self.threadID = threadID
self.queue = q
self._batch_size = batch_size
self._img_height = img_height
self._img_width = img_width
self._channels = 3
self._thread_stop = False
self._detector = dlib.get_frontal_face_detector()
self._sp = dlib.shape_predictor("shape_predictor_5_face_landmarks.dat")
self._padding = padding
self.start_index = 0
self._jitter_count = jitter_count
def get_data(self):
global index
global path_list
global label_list
global g_Lock
global detect_c
global Img_Error_list_path
with g_Lock:
m_index = index
global_list_len = len(path_list)
end = min(m_index + self._batch_size, global_list_len)
if (m_index == end):
return None
m_path_list = path_list[m_index:end]
m_label_list = label_list[m_index:end]
index = end
res = {
"img": np.ndarray(shape=(self._batch_size, self._img_height, self._img_width, self._channels), dtype=np.float32),
"label":np.ndarray(shape=(self._batch_size), dtype=np.int32),
"path_list": [],
"last_batch": False
}
if end == global_list_len:
res["last_batch"] = True
count = 0
for i,path in enumerate(m_path_list):
try :
img = Image.open(path)
img = img.convert('RGB')
#h,w = img.size
#if (h == 150 and w == 150):
# crop_img = img.resize((self._img_height, self._img_width), Image.ANTIALIAS)
# crop_img = np.array(crop_img)
#else:
crop_img, _ = self.Crop_1_face_no_FD(img, self._img_height , self._padding)
res["img"][count,:,:,:] = crop_img
res["label"][count] = m_label_list[i]
res["path_list"].append(path)
count += 1
except:
print("index= ",i+ m_index)
print("load_img_error: ",path)
with open(Img_Error_list_path,"a") as f:
f.write(path+"\n")
if len(res["path_list"]) == 0:
return res
elif (len(res["path_list"]) < self._batch_size):
res["img"] = res["img"][0:len(res["path_list"])]
res["label"] = res["label"][0:len(res["path_list"])]
detect_c += res["img"].shape[0]
if self._jitter_count:
list_imgs =[]
for i in range(res["img"].shape[0]):
list_imgs += dlib.jitter_image(np.uint8(res["img"][i]), num_jitters=self._jitter_count, disturb_colors=True)
res["img"] = np.array(list_imgs,dtype = np.float32)
#res["img"] = pre_process(res["img"])
return res
def Crop_1_face_no_FD (self,img, size = 224 , padding = 0.25):
h,w = img.size
#eye_dist = lm[2] - lm[0]
#extend = 1.5
left = 0
top = 0
rihgt = w
bottom = h
dlib_rect = dlib.rectangle(left,top,rihgt,bottom)
#img = img.crop((left, top, rihgt, bottom))
img = np.array(img)
faces = dlib.full_object_detections()
faces.append(self._sp(img, dlib_rect))
image = dlib.get_face_chip(img, faces[0], size, padding)
return image,1
def FD_Crop_1_face (self,img , size = 224 , padding = 0.25):
img = np.array(img)
dets = self._detector(img)
num_face = len(dets)
index = 0
if num_face == 0:
#print ("no_face")
return None , num_face, None, None
elif num_face > 1:
distance = 100000000;
img_center_x = img.shape[0] * 0.5;
img_center_y = img.shape[1] * 0.5;
for i,det in enumerate(dets):
center_x = ( det.left() + det.right() ) * 0.5;
center_y = ( det.bottom() + det.top() ) * 0.5;
temp_dis = (img_center_x - center_x)**2 + (img_center_y - center_y)**2
if (temp_dis < distance):
distance = temp_dis
index = i
faces = dlib.full_object_detections()
faces.append(self._sp(img, dets[index]))
image = dlib.get_face_chip(img, faces[0], size, padding)
return image, num_face, dets[index], faces[0]
def run(self):
global index
global path_list
while not self._thread_stop:
if index > len(path_list):
self._thread_stop = True
break
datas = self.get_data()
if datas != None:
if len(datas["path_list"]) == 0:
continue
self.queue.put(datas)
else:
self._thread_stop = True
break
#try:
#self.queue.put(datas,True,100)
#except:
# print ("get time_out Thread_ID = %d" % self.threadID)
print ("Load_Thread_ID = %d run end" % self.threadID)
def load_graph(frozen_graph_path):
graph = tf.Graph()
with tf.gfile.GFile(frozen_graph_path, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# Then, we import the graph_def into a new Graph and returns it
with graph.as_default() as graph:
tf.import_graph_def(graph_def, name="")
return graph
def inference_img(graph,input_batch,dim):
imgs = (input_batch["img"]- 255.0/2) / (255.0/2)
with graph.as_default():
x = graph.get_tensor_by_name('input:0')
Gender = graph.get_tensor_by_name('Gender:0')
cut_interval = 20
with tf.Session(graph = graph) as sess:
total_num = imgs.shape[0]
sofemax_np = np.ndarray(shape=[total_num,dim], dtype=np.float32)
cut_ind = np.arange(0,total_num,cut_interval)
if cut_ind[-1] != total_num:
cut_ind = np.append(cut_ind,total_num)
for i in range (cut_ind.shape[0]-1):
start = cut_ind[i]
end = cut_ind[i+1]
temp = sess.run(Gender,feed_dict = {x:imgs[start:end]})
#print (temp.shape)
sofemax_np[start:end] = temp
return sofemax_np
def Confusion_M(preds, labels):
preds_1D = np.argmax(preds, 1)
if len(labels.shape) == 1:
labels_1D = labels
else:
labels_1D = np.argmax(labels, 1)
confusion_cnt =np.zeros((preds.shape[1],preds.shape[1]), dtype=np.int16)
mapping_img_path = { "label_"+str(i) : {} for i in range(preds.shape[1])}
for k,_ in mapping_img_path.items():
mapping_img_path[k] = { "pred_"+str(i) : [] for i in range(preds.shape[1])}
#print (k,mapping_img_path[k])
for i in range(labels_1D.shape[0]):
confusion_cnt[labels_1D[i],preds_1D[i]] += 1
mapping_img_path["label_"+str(labels_1D[i])]["pred_"+str(preds_1D[i])].append(i)
confusion_acc = confusion_cnt.astype(float) / \
np.expand_dims(np.sum(confusion_cnt,axis = 1),axis = 1) *100
confusion_acc [ confusion_acc != confusion_acc] = 0 # replace nan as 0
return confusion_acc , confusion_cnt, mapping_img_path
parser = argparse.ArgumentParser(description = 'eval gender Age with label')
parser.add_argument('Gender_or_Age', type=str, help='pls type Gender or Age"')
parser.add_argument('-dir', required=True, type=str, help='Path to floder of eval dataset')
parser.add_argument('-model', '--load_model_path', required=True, type=str, help='Path to trained FR model, type is tensorflow pb file')
parser.add_argument('-img_w', '--imgage_width', type=int, default = 112, help='(optional) imgage_width Default: 112')
parser.add_argument('-fr_dim', '--FR_Emb_Dim', type=int, default = 512, help='(optional) FR_Embedding_Dims Default: 512')
parser.add_argument('-p', '--padding_ratio', type=float, default = 0.25, help='(optional) padding_ratio Default: 0.25')
args = parser.parse_args()
task = ["Gender","Age"]
eval_item = task.index(args.Gender_or_Age) if (args.Gender_or_Age in task) else -1
emb_dim = args.FR_Emb_Dim
data_floder = args.dir
model_path = args.load_model_path
img_W = args.imgage_width
img_H = img_W
padding = args.padding_ratio
print("{:15}{}".format("eval_item",task[eval_item]))
print("{:15}{}".format("data_floder",data_floder))
print("{:15}{}".format("model_path",model_path))
print("{:15}{}".format("image_width",img_W))
print("{:15}{}".format("emb_dim",emb_dim))
print("{:15}{}".format("padding",padding))
if (eval_item < 0):
print("training_item not found in task, task = [\"Gender\",\"Age\"]")
else:
tf.reset_default_graph()
#emb_dim = 512
index = 0
FD_Lost_c = 0
detect_c = 0
g_Lock = threading.Lock()
if eval_item:
#Age_Dataset_dir = "training/age_data/__age_valid_2"
path_lists_temp = load_path_lists(data_floder)
print(len(path_lists_temp))
else:
#Gender_Dataset_dir = "training/gender_data/__gender_valid"
path_lists_temp = load_path_lists(data_floder)
path_lists_temp = path_lists_temp[0:2]
Num_Classes = len(path_lists_temp)
path_list = []
label_list = []
for i in range(len(path_lists_temp)):
label_list += [i for l in path_lists_temp[i]]
path_list += path_lists_temp[i]
my_queue = Queue(maxsize=100)
batch_size = 20
thread_num = 5
jitter_count = 0
g2 = load_graph(model_path)
data_loader = []
for i in range(thread_num):
data_loader.append(Data_Thread(i+1,batch_size, img_H,img_W, jitter_count,padding, my_queue))
data_loader[i].start()
"""
jitter_count = 0
run_count = 0
last_batch = False
while(1):
if run_count%50==0:
print ("batch_run= ",run_count," Index= ", index)
run_count += 1
if last_batch and my_queue.empty():
test_bool = True
for i in range(thread_num):
test_bool = (test_bool and data_loader[i]._thread_stop)
if test_bool:
break
test_batch = my_queue.get()
my_queue.task_done()
if not last_batch:
last_batch = test_batch["last_batch"]
count = 0
if jitter_count:
list_imgs =[]
for i in range(test_batch["img"].shape[0]):
print(count)
count += 1
list_imgs += dlib.jitter_image(np.uint8(test_batch["img"][i]), num_jitters=jitter_count, disturb_colors=True)
imgs = np.array(list_imgs,dtype = np.float32)
else:
imgs = test_batch["img"]
floder = "landmark_check"
if jitter_count:
for i,path in enumerate(test_batch["path_list"]):
file_name, ext = path.rsplit("\\",1)[-1].rsplit(".",1)
for j in range(jitter_count):
img_s= Image.fromarray(np.uint8(imgs[i*jitter_count + j]))
new_path = os.path.join(floder,file_name+"_"+str(j)+"."+ext)
print(new_path)
img_s.save(new_path)
else:
for i,path in enumerate(test_batch["path_list"]):
img_s= Image.fromarray(np.uint8(imgs[i]))
img_s.save(os.path.join(floder,path.rsplit("\\",1)[-1]))
"""
print ("total_img_no = ",len(path_list))
last_batch = False
tt1 = tt2 = tt3 = tt4 = tt5 = tts = 0
t1 = time.time()
Total_Pred = np.ndarray(shape=[len(path_list),Num_Classes],dtype = np.float32)
Total_label = np.ndarray(shape=[len(path_list)],dtype = np.int32)
start = 0
with g2.as_default() :
x = g2.get_tensor_by_name('input:0')
Gender_out = g2.get_tensor_by_name('Gender:0')
Age_output = g2.get_tensor_by_name('Age:0')
if eval_item:
output = Age_output
else:
output = Gender_out
with tf.Session(graph = g2) as sess:
print ("start_inference")
run_count = 0
#tt1 = tt2 = tt3 = tt4 = tt5 = tts = 0
while(1):
if run_count%50==0:
print ("batch_run= ",run_count," Index= ", index," queue_size= ",my_queue.qsize()," ",tt1,tt3,tt4)
#print ("my_queue.size= ",my_queue.qsize())
#print (tt1,tt2,tt3,tt4)
tt1 = tt2 = tt3 = tt4 = tt5 = tts = 0
run_count += 1
if last_batch and my_queue.empty():
test_bool = True
for i in range(thread_num):
test_bool = (test_bool and data_loader[i]._thread_stop)
if test_bool:
break
tt_s = time.time()
test_batch = my_queue.get()
my_queue.task_done()
tt1 += time.time() - tt_s
tt_s = time.time()
if not last_batch:
last_batch = test_batch["last_batch"]
#ttt_s = time.time()
#for i in range(50):
# pre_process_dlib(test_batch["img"])
#print(time.time() - ttt_s)
#print(test_batch["img"].shape, test_batch["img"].dtype)
#print(test_batch["img_test"].shape, test_batch["img_test"].dtype)
#Num_img = len(test_batch["path_list"])
#test_batch["img"] = np.concatenate((test_batch["img"],test_batch["img"][:,:,::-1,:]),axis = 0) #img flip
imgs = pre_process(test_batch["img"])
if jitter_count:
N = len(test_batch["path_list"])
emb_np = np.ndarray(shape=[N,emb_dim],dtype = np.float32)
emb_np_temp = sess.run(output,feed_dict = {x:imgs})
for i in range(N):
emb_np[i] = np.mean(emb_np_temp[i*jitter_count: (i+1)*jitter_count],axis = 0)
else:
emb_np = sess.run(output,feed_dict = {x:imgs})
Total_Pred[start:start+emb_np.shape[0]] = emb_np
Total_label[start:start+emb_np.shape[0]] = test_batch["label"]
start += emb_np.shape[0]
tt3 += time.time() - tt_s
tt_s = time.time()
#emb_add = emb_np[0:Num_img] + emb_np[Num_img:]
#emb_add = emb_add / np.linalg.norm(emb_add,axis = 1).reshape(-1,1)
tt4 += time.time() - tt_s
tt_s = time.time()
#print ("total_time= ", time.time()-t1)
for i in range(thread_num):
data_loader[i]._thread_stop=True
data_loader[i].join()
acc,cnt,_ = Confusion_M(Total_Pred,Total_label)
#print(Total_Pred.shape)
#print(Total_label.shape)
average_acc = np.average(acc.diagonal())
print("average_accuracy: ",average_acc)
print("-------------------\n")
print("Confusion matrix by count\n")
for a in cnt:
print(a)
print("-------------------\n")
print("Confusion matrix by accuracy\n")
for a in acc:
print(a)
"""
if __name__ == "__main__":
main()
"""