-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraining.py
443 lines (343 loc) · 14.7 KB
/
training.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
# -----------------------------------------------------------------------------
# -- Script to train, prepare and meaure error among data
# -----------------------------------------------------------------------------
# MIT License
#
# Copyright (c) 2018 Luca Anzalone
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# -----------------------------------------------------------------------------
import os
import cv2
import dlib
import utils
import numpy as np
from xml import Xml
from utils import show_properly
from utils import Keys, Colors
from utils import Annotation, Region
def region(face):
'''return a region (0, 0, w, h) for the cropped face'''
return Region(0, 0, face.shape[1], face.shape[0])
def my_noise(image):
'''add simulated camera noise to the given image'''
out = image.copy()
h, w = image.shape[:2]
amount = int((h * w) * (3 / 100))
for i in range(0, amount):
row = np.random.randint(0, h)
col = np.random.randint(0, w)
b = np.random.randint(0, 128)
g = np.random.randint(0, 180)
r = np.random.randint(0, 256)
out[row][col] = [b, g, r]
return out
def augment_data(image, face, landmarks):
'''produce 2 type of augumentation: rotation and noising,
returns an array of 3 tuple (image, landmarks)'''
assert(type(face) is Region)
h, w = image.shape[:2]
angle = 30
pivot = face.center()
# 30 degree rotation
A = utils.rotate_image(image, angle, pivot)
B = utils.rotate_landmarks(landmarks, pivot, angle)
R1 = utils.points_region(B)
# -30 degree rotatation
C = utils.rotate_image(image, -angle, pivot)
D = utils.rotate_landmarks(landmarks, pivot, -angle)
R2 = utils.points_region(D)
# mirroring
E = utils.flip_image(image)
F = utils.naive_flip_landmarks(landmarks, w)
R3 = face.flip(width=w)
return [(A, B, R1), (C, D, R2), (E, F, R3)]
def build_trainset(input_dir="data", output_dir="trainset", win_size=321):
'''scan the input folder and put every image with annotations
output folder'''
utils.init_face_detector(True, win_size)
utils.load_shape_predictor("dlib/shape_predictor_68_face_landmarks.dat")
count = int(utils.count_files_inside(output_dir) / 8)
window = "window"
cv2.namedWindow(window)
for face, box in utils.faces_inside(input_dir, 1, True):
face_copy = face.copy()
face_rect = region(face)
# detections
points = utils.detect_landmarks(face, face_rect)
utils.draw_points(face, points)
# show face
while (1):
h, w = face.shape[:2]
if h > 0 and w > 0:
cv2.imshow(window, show_properly(face))
else:
break
key = cv2.waitKey(20) & 0Xff
if key == Keys.ESC:
break # skip current face
elif key == Keys.S:
path = f"{output_dir}/face{count}"
# save image
cv2.imwrite(f"{path}.jpg", face_copy)
# save annotation relative to the current face
Annotation(f"{path}.ann", face_rect, points).save()
# generate and save augumetations
array = augment_data(face_copy, face_rect, points)
for i, x in enumerate(array):
# save image x[0]
cv2.imwrite(f"{path}_{i + 1}.jpg", x[0])
# save annotations
Annotation(f"{path}_{i + 1}.ann", face_rect, x[1]).save()
count = count + 1
break
elif key == Keys.Q:
# quit program
return cv2.destroyAllWindows()
cv2.destroyAllWindows()
def generate_training_xml(name, folder="trainset"):
'''create an xml file suitable for training dlib shape predictor model'''
xml = Xml(name)
i = 0
for annotation, path in Annotation.inside(folder):
xml.append(path, [annotation.box], [annotation.points])
i = i + 1
print("{} writing: {}".format(i, path))
xml.close()
def train_model(name, xml):
'''train and return a dlib shape predictor.
Training options are:
cascade_depth:
The number of cascades created to train the model with. The number of
trees in the model is = cascade_depth * num_trees_per_cascade_level.
> default: 10
feature_pool_region_padding:
Size of region within which to sample features for the feature pool,
e.g a padding of 0.5 would cause the algorithm to sample pixels from
a box that was 2x2 pixels
> default: 0
feature_pool_size:
Number of pixels used to generate features for the random trees at
each cascade. So in general larger settings of this parameter give
better accuracy but make the algorithm run slower.
> default: 400
lambda_param:
Controls how tight the feature sampling should be. Lower values enforce
closer features. To decide how to split nodes in the regression tree
the algorithm looks at pairs of pixels in the image. These pixel pairs
are sampled randomly but with a preference for selecting pixels that
are near each other.
> default: 0.1
nu:
The regularization parameter.
Larger values of this parameter will cause the algorithm to fit
the training data better but may also cause overfitting.
The value must be 0 < nu <= 1.
> default: 0.1
num_test_splits:
Number of split features at each node to sample.
Larger values of this parameter will usually give more accurate
outputs but take longer to train.
> default: 20
num_trees_per_cascade_level:
The number of trees created for each cascade.
> default: 500
oversampling_amount:
The number of randomly selected initial starting points sampled
for each training example. This parameter controls the number of
randomly selected deformation applied to the training data.
So the bigger this parameter the better (excepting that larger
values make training take longer).
> default: 20
oversampling_translation_jitter:
When generating the get_oversampling_amount() factor of extra training
samples you can also jitter the bounding box by adding random small
translational shifts. For instance, if you set it to 0.1 then it would
randomly translate the bounding boxes by between 0% and 10% their
width and height in the x and y directions respectively. Doing this is
essentially equivalent to randomly jittering the bounding boxes in the
training data. So doing this kind of jittering can help make the
learned model more robust against slightly misplaced bounding boxes.
> default: 0
random_seed:
The random seed used by the internal random number generator
> default: ""
tree_depth:
The depth of the trees used in each cascade.
There are pow(2, get_tree_depth()) leaves in each tree.
> default: 4
'''
options = dlib.shape_predictor_training_options()
options.tree_depth = 3 # 4
options.nu = 0.1
options.num_threads = 8
options.cascade_depth = 12 # 15
options.be_verbose = True
options.feature_pool_size = 400 + 20 # 400
options.num_test_splits = 20 + 5
options.oversampling_amount = 20
# options.oversampling_translation_jitter = 0.1
dlib.train_shape_predictor(xml, name, options)
def test(folder="testset", model="dlib/shape_predictor_68_face_landmarks.dat"):
utils.init_face_detector(True, 150)
utils.load_shape_predictor(model)
my_sp = utils.shape_predictor
dlib_sp = dlib.shape_predictor("dlib/shape_predictor_68_face_landmarks.dat")
for face, r in utils.faces_inside(folder):
box = region(face)
utils.shape_predictor = my_sp
lmarks0 = utils.detect_landmarks(face, box)
utils.shape_predictor = dlib_spq
lmarks1 = utils.detect_landmarks(face, box)
# draw results
utils.draw_points(face, lmarks1, color=Colors.green)
utils.draw_points(face, lmarks0, color=Colors.red)
utils.show_image(show_properly(face))
def test_augment():
utils.init_face_detector(True, 321)
utils.load_shape_predictor("dlib/shape_predictor_68_face_landmarks.dat")
for img in utils.images_inside("trainset"):
points = utils.detect_landmarks(img, region(img))
angle = 30
h, w = img.shape[:2]
center = (w / 2, h / 2)
# 30 degree rotation
rot1 = utils.rotate_image(img, angle)
rot_pts1 = utils.rotate_landmarks(points, center, angle)
# -30 degree rotatation
rot2 = utils.rotate_image(img, -angle)
rot_pts2 = utils.rotate_landmarks(points, center, -angle)
# mirroring
mir = utils.flip_image(img)
mir_pts = utils.detect_landmarks(mir, region(mir))
utils.draw_points(img, points)
utils.draw_points(rot1, rot_pts1, color=Colors.cyan)
utils.draw_points(rot2, rot_pts2, color=Colors.purple)
utils.draw_points(mir, mir_pts, color=Colors.green)
while True:
cv2.imshow("image", img)
cv2.imshow("mirrored", mir)
cv2.imshow("rotated30", rot1)
cv2.imshow("rotated-30", rot2)
key = cv2.waitKey(20) & 0Xff
if key == Keys.ESC:
break
elif key == Keys.Q:
return cv2.destroyAllWindows()
def another_test():
utils.load_shape_predictor("dlib/shape_predictor_68_face_landmarks.dat")
for img, p in utils.images_inside("uffa"):
fast = cv2.FastFeatureDetector_create()
# kp = fast.detect(img, None)
# draws:
face = utils.detect_faces(img, detector="dlib")[0]
# utils.draw_rect(img, face, color=Colors.green)
pts = utils.detect_landmarks(img, face)
pts = [pts[0], pts[3], pts[6], pts[10], pts[20], pts[22], pts[35]]
# utils.draw_points(img, pts)
# img = cv2.drawKeypoints(img, kp, None, color=Colors.cyan)
keypoints = []
for p in pts:
roi = Region(p[0] - 10, p[1] - 10, 20, 20)
patch = utils.crop_image(img, roi)
keypoints.append(fast.detect(patch, None))
# for kp in keypoints[2]:
# print(kp)
# # img = cv2.drawKeypoints(img, kp, None, color=Colors.cyan)
for p in pts:
for kp in keypoints:
for k in kp:
x = int(k.pt[0] + p[0])
y = int(k.pt[1] + p[1])
utils.draw_point(img, x, y, radius=1)
while True:
cv2.imshow("window", show_properly(img))
key = cv2.waitKey(20) & 0Xff
if key == Keys.ESC:
break
def adjust_landmarks(region, pts):
'''adjust the position of the landmarks to be inside region'''
x0, y0 = region.tl()
new_pts = []
for p in pts:
x = p[0] - x0
y = p[1] - y0
new_pts.append((x, y))
return new_pts
def build_trainset_auto(src="dataset", dst="trainset", debug=False):
'''build a trainset automatically from an ibug-like dataset,
the images are taken from [src] folder and saved to [dst] folder'''
utils.init_face_detector(True, 150)
qualiy = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
# file count for naming
count = int(utils.count_files_inside(dst) / 8)
for img, lmarks, path in utils.ibug_dataset(src):
h, w = img.shape[:2]
# crop a bigger region around landmarks
region = utils.points_region(lmarks)
scaled = region.scale(1.8, 1.8).ensure(w, h)
img = utils.crop_image(img, scaled)
# detect faces
face = utils.prominent_face(utils.detect_faces(img))
# if cnn fails try with dlib
if face is None:
faces = utils.detect_faces(img, detector="dlib")
# ..if dlib fails take the region around landmarks
if face is None:
face = region.copy()
else:
face = utils.prominent_face(faces)
# edit landmarks according to scaled region
lmarks = adjust_landmarks(scaled, lmarks)
# augumentations
i = 0
for image, landmarks, box in augment_data(img, face, lmarks):
i = i + 1
if debug:
utils.draw_rect(image, box, color=Colors.yellow)
utils.draw_points(image, landmarks, color=Colors.purple)
name = f"image{i}"
utils.show_image(show_properly(image), window=name)
else:
# save annotation and image
ipath = os.path.join(dst, f"face{count}_{i}.jpg")
apath = os.path.join(dst, f"face{count}_{i}.ann")
cv2.imwrite(ipath, image, qualiy)
Annotation(apath, box.as_list()[:4], landmarks).save()
if debug:
utils.draw_rect(img, face, color=Colors.red)
utils.draw_points(img, lmarks, color=Colors.green)
utils.show_image(show_properly(img))
else:
# save image and annotation
ipath = os.path.join(dst, f"face{count}.jpg")
apath = os.path.join(dst, f"face{count}.ann")
cv2.imwrite(ipath, img, qualiy)
Annotation(apath, face.as_list()[:4], lmarks).save()
count = count + 1
# info
print("{} processed: {}\r".format(count, ipath))
if __name__ == '__main__':
# build_trainset()
# test(folder="images", model="sp_68_fast.dat")
# xml_file = "new_sp_68.xml"
# generate_training_xml("boosted_ibug.xml")
# train_model("sp_68_mini.dat", "labels_ibug_300W_train.xml")
# another_test()
build_trainset_auto(src="dataset", debug=False)