-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detection_with_retinanet.py
executable file
·281 lines (208 loc) · 6.77 KB
/
face_detection_with_retinanet.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
# -*- coding: utf-8 -*-
"""Face Detection with RetinaNet.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1h8PnIQULSuMPvogrTS_5mQVi0XDNv1kJ
"""
!git clone https://github.com/fizyr/keras-retinanet.git
# Commented out IPython magic to ensure Python compatibility.
# %cd keras-retinanet/
!pip install .
!python setup.py build_ext --inplace
# !pip install keras==2.3.1
# !pip install tensorflow==2.1.0
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import requests
import urllib
import os
from PIL import Image
from keras_retinanet import models
from keras_retinanet.utils.image import preprocess_image, resize_image
from keras_retinanet.utils.visualization import draw_box, draw_caption
from keras_retinanet.utils.colors import label_color
# Commented out IPython magic to ensure Python compatibility.
# https://www.kaggle.com/dataturks/face-detection-in-images
# %cd ..
!wget -nc https://lazyprogrammer.me/course_files/face_detection.json
!head face_detection.json
import json
j = json.loads(open('face_detection.json').readline())
j
df = pd.read_json('face_detection.json', lines=True)
df.head()
df.shape
r = requests.get('http://com.dataturks.a96-i23.open.s3.amazonaws.com/2c9fafb064277d86016431e33e4e003d/8186c3d1-e9d4-4550-8ec1-a062a7628787___0-26.jpg.jpeg')
with open('testimg.jpg', 'wb') as f:
f.write(r.content)
im = Image.open('testimg.jpg')
plt.imshow(im)
converted_data_train = {
'image_name': [],
'x_min': [],
'y_min': [],
'x_max': [],
'y_max': [],
'class_name': [],
}
converted_data_test = {
'image_name': [],
'x_min': [],
'y_min': [],
'x_max': [],
'y_max': [],
'class_name': [],
}
if not os.path.exists('faces'):
os.mkdir('faces')
idx = 0 # global counter for filenames
def map_to_data(row, converted_data):
global idx
r = requests.get(row['content'])
filepath = 'faces/face_%s.jpg' % idx
# don't bother to overwrite
if not os.path.exists(filepath):
with open(filepath, 'wb') as f:
f.write(r.content)
# there could be more than 1 face per image
for anno in row['annotation']:
converted_data['image_name'].append(filepath)
width = anno['imageWidth']
height = anno['imageHeight']
# calculate box coordinates
x1 = int(round(anno['points'][0]['x'] * width))
y1 = int(round(anno['points'][0]['y'] * height))
x2 = int(round(anno['points'][1]['x'] * width))
y2 = int(round(anno['points'][1]['y'] * height))
converted_data['x_min'].append(x1)
converted_data['y_min'].append(y1)
converted_data['x_max'].append(x2)
converted_data['y_max'].append(y2)
# they are all the same class
converted_data['class_name'].append('face')
# update counter
idx += 1
# we must split BEFORE converting the data
# after converting the data, multiple rows will have the same image
# we won't want to split then3
train_df, test_df = train_test_split(df, test_size=0.2)
# this will be slow since it has to download all the images
# just in case we run again later
idx = 0
# train
train_df.apply(lambda row: map_to_data(row, converted_data_train), axis=1)
# test
test_df.apply(lambda row: map_to_data(row, converted_data_test), axis=1)
# this will overwrite the previous dfs
train_df = pd.DataFrame(converted_data_train)
test_df = pd.DataFrame(converted_data_test)
train_df.head()
train_df.shape
train_df[train_df['image_name'] == 'faces/face_1.jpg']
def show_image_with_boxes(df):
# pick a random image
filepath = df.sample()['image_name'].values[0]
# get all rows for this image
df2 = df[df['image_name'] == filepath]
im = np.array(Image.open(filepath))
# if there's a PNG it will have alpha channel
im = im[:,:,:3]
for idx, row in df2.iterrows():
box = [
row['x_min'],
row['y_min'],
row['x_max'],
row['y_max'],
]
print(box)
draw_box(im, box, color=(255, 0, 0))
plt.axis('off')
plt.imshow(im)
plt.show()
show_image_with_boxes(train_df)
train_df.to_csv('annotations.csv', index=False, header=None)
classes = ['face']
with open('classes.csv', 'w') as f:
for i, class_name in enumerate(classes):
f.write(f'{class_name},{i}\n')
!head classes.csv
!head annotations.csv
if not os.path.exists('snapshots'):
os.mkdir('snapshots')
PRETRAINED_MODEL = 'snapshots/_pretrained_model.h5'
URL_MODEL = 'https://github.com/fizyr/keras-retinanet/releases/download/0.5.1/resnet50_coco_best_v2.1.0.h5'
urllib.request.urlretrieve(URL_MODEL, PRETRAINED_MODEL)
print('Downloaded pretrained model to ' + PRETRAINED_MODEL)
!ls keras-retinanet
batch_size = 8
num_images = len(train_df.groupby('image_name'))
steps_per_epoch = num_images // batch_size + 1
steps_per_epoch
!keras-retinanet/keras_retinanet/bin/train.py --freeze-backbone \
--random-transform \
--weights {PRETRAINED_MODEL} \
--batch-size {batch_size} \
--steps {steps_per_epoch} \
--epochs 15 \
csv annotations.csv classes.csv
!ls snapshots
from glob import glob
model_paths = glob('snapshots/resnet50_csv_*.h5')
latest_path = sorted(model_paths)[-1]
print("path:", latest_path)
model = models.load_model(latest_path, backbone_name='resnet50')
model = models.convert_model(model)
label_map = {}
for line in open('classes.csv'):
row = line.rstrip().split(',')
label_map[int(row[1])] = row[0]
def show_image_with_predictions(df, threshold=0.6):
# choose a random image
row = df.sample()
filepath = row['image_name'].values[0]
print("filepath:", filepath)
# get all rows for this image
df2 = df[df['image_name'] == filepath]
im = np.array(Image.open(filepath))
print("im.shape:", im.shape)
# if there's a PNG it will have alpha channel
im = im[:,:,:3]
# plot true boxes
for idx, row in df2.iterrows():
box = [
row['x_min'],
row['y_min'],
row['x_max'],
row['y_max'],
]
print(box)
draw_box(im, box, color=(255, 0, 0))
### plot predictions ###
# get predictions
imp = preprocess_image(im)
imp, scale = resize_image(im)
boxes, scores, labels = model.predict_on_batch(
np.expand_dims(imp, axis=0)
)
# standardize box coordinates
boxes /= scale
# loop through each prediction for the input image
for box, score, label in zip(boxes[0], scores[0], labels[0]):
# scores are sorted so we can quit as soon
# as we see a score below threshold
if score < threshold:
break
box = box.astype(np.int32)
color = label_color(label)
draw_box(im, box, color=color)
class_name = label_map[label]
caption = f"{class_name} {score:.3f}"
draw_caption(im, box, caption)
plt.axis('off')
plt.imshow(im)
plt.show()
plt.rcParams['figure.figsize'] = [20, 10]
show_image_with_predictions(train_df, threshold=0.3)
show_image_with_predictions(test_df, threshold=0.4)