-
Notifications
You must be signed in to change notification settings - Fork 1
/
face_detection.py
108 lines (86 loc) · 4.29 KB
/
face_detection.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
import cv2
from tensorflow import keras
import seaborn as sn
import matplotlib.pyplot as plt
import pandas as pd
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
from src.lib import *
from src.mtcnn_class import *
# from mtcnn.mtcnn import MTCNN
n_faces = 7
max_size = 90
confidence = 0.80
def main():
# =====================================================================
# LOAD AND STORE DATA
# =====================================================================
# DO ONLY FIRST TIME!!
# train_dict = initialize(dataset='train', keep_all=False)
# val_dict = initialize(dataset='val')
train_dict = pickle_load('input/train_dict.pickle')
val_dict = pickle_load('input/val_dict.pickle')
print("Total train images: ", len(train_dict))
print("Total val images: ", len(val_dict))
# EXAMPLE: HOW TO ACCESS AN IMAGE IN h5 AND PLOT IT WITH BOXES
# image = lib.hdf5_load(train_dict[1]['h5_path'])
# lib.plot_box(image, train_dict[1]['labels'])
# To plot the boxes on the image from the dicts
# lib.plot_box_from_dict(train_dict, max_iter=10, event='all')
train_heights = heights_description(train_dict)
val_heights = heights_description(val_dict)
# =====================================================================
# PREPROCESSING
# =====================================================================
h_cut = int(pd.Series(train_heights).quantile(q=0.95))
train_dict = [img for img in train_dict if img['shape'][0] <= h_cut]
val_dict = [img for img in val_dict if img['shape'][0] <= h_cut]
train_dict = filter_box(train_dict, box_size=1000, max_size=max_size, debug=False)
val_dict = too_many_faces(val_dict, n_faces=n_faces)
val_dict = filter_box(val_dict, box_size=400, max_size=max_size, debug=False)
# lib.plot_box_from_dict(train_dict, max_iter=5, event='all')
# =====================================================================
# DETECTION
# =====================================================================
print("Final train images: ", len(train_dict))
print("Final val images: ", len(val_dict))
acc = []
f1 = []
f1_dumb = []
result_matrix = np.zeros((11, 11)) # safe value
detector = MTCNN(min_face_size=12, scale_factor=0.5, steps_threshold=[0.95, 0.7, 0.7])
for i in range(0, len(val_dict[:])):
expected = val_dict[i]['labels']
if len(expected) > n_faces or len(expected) == 0:
continue
image = hdf5_load(val_dict[i]['h5_path'])
image_ds, factor = downscale(image, max_size=max_size)
result = detector.detect_faces(image_ds)
result = clean_result(result, conf_t=confidence)
result_matrix = n_faces_matrix(result, expected, result_matrix)
acc.append(face_pixel_metric(result, expected, factor, image_ds.shape[0], image_ds.shape[1]))
f1.append(f1_metric(result, expected, factor))
f1_dumb.append(f1_metric_dumb(result, expected))
if len(result) == 0:
continue
plot_comparison(image, image_ds, expected, result, color_e='r', color_r='cyan', line=1, pause=1.5)
# =====================================================================
# PLOT RESULTS
# =====================================================================
print("Pixel metric accuracy", np.mean(np.array(acc)))
print("f1 metric", np.mean(np.array(f1)))
print("f1 metric dumb", np.mean(np.array(f1_dumb)))
# Plot result matrix
result_matrix = result_matrix / np.tile((np.sum(result_matrix, axis=0).astype(float)+0.001), (11, 1))
diagonal = np.zeros((result_matrix.shape[1]-1, result_matrix.shape[1]-1))
for i in range(result_matrix.shape[1]-1):
diagonal[i, i] = result_matrix[i, i] + result_matrix[i - 1, i] + result_matrix[i + 1, i]
result_matrix_cut = result_matrix[0:n_faces+1, 0:n_faces+1]
diagonal_cut = diagonal[1:n_faces+1, 1:n_faces+1]
diagonal_cut = pd.DataFrame(diagonal_cut, columns=['1', '2', '3', '4', '5', '6', '7'], index=['1', '2', '3', '4', '5', '6', '7'])
sn.heatmap(result_matrix_cut, annot=True, cmap="Blues")
plt.show()
sn.heatmap(diagonal_cut, annot=True, cmap="Blues")
plt.show()
if __name__ == "__main__":
main()