-
Notifications
You must be signed in to change notification settings - Fork 5
/
violajones.py
439 lines (340 loc) · 16.2 KB
/
violajones.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
""" violajones.py
Author: Brian Tuan
Last Modified: February 13, 2017
"""
import click
from multiprocessing import cpu_count, Pool
import numpy as np
import json
from util import import_img_dir, import_jpg, integral_image, draw_bounding_boxes
"""
FEATURE PROCESSING FUNCTIONS
============================
Each feature can be uniquely specified by a 6-tuple containing the following parameters:
(x_position, y_position, width, height, polarity, threshold)
We use the following four Haar wavelet rectangular feature archetypes:
- Type 1 - Type 2 - Type 3 - Type 4
++++++++ ++++0000 ++++0000 0000++++0000
++++++++ ++++0000 ++++0000 0000++++0000
00000000 ++++0000 0000++++ 0000++++0000
00000000 ++++0000 0000++++ 0000++++0000
In addition, for any given dimension of the filter, where:
D is the length of the image along that dimension,
K is the length of the filter along that dimension, and
S is the length of the stride (assumed to be equal in all directions,
the number of filters that fit is: 1 + (D - K) / S. We do not pad the input in this implementation.
In each feature generating function, we iterate over each possible (x, y) location and each possible
filter size stemming from that location. Consequently, many filters are created.
"""
def gen_type1(width, height, stride=1, increment=1):
""" Type 1 filters must have an even height.
Reference points:
D C
E B
F A
Formula: A + C + 2E - 2B - D - F
"""
features = []
for w in range(1, width, 1 * increment):
for h in range(2, height, 2 * increment):
for x in range(0, 1 + (width - w) // stride, stride):
for y in range(0, 1 + (height - h) // stride, stride):
A = (x + w - 1, y + h - 1)
B = (x + w - 1, y + (h // 2) - 1)
C = (x + w - 1, y - 1)
D = (x - 1, y - 1)
E = (x - 1, y + (h // 2) - 1)
F = (x - 1, y + h - 1)
add = [i for i in [A, C, E, E] if i[0] >= 0 and i[1] >= 0]
sub = [i for i in [B, B, D, F] if i[0] >= 0 and i[1] >= 0]
features.append((tuple(add), tuple(sub)))
return features
def gen_type2(width, height, stride=1, increment=1):
""" Type 2 filters must have even width.
Reference points:
D E F
C B A
Formula: A + C + 2E - 2B - D - F
"""
features = []
for w in range(2, width, 2 * increment):
for h in range(1, height, 1 * increment):
for x in range(0, 1 + (width - w) // stride, stride):
for y in range(0, 1 + (height - h) // stride, stride):
A = (x + w - 1, y + h - 1)
B = (x + (w // 2) - 1, y + h - 1)
C = (x - 1, y + h - 1)
D = (x - 1, y - 1)
E = (x + (w // 2) - 1, y - 1)
F = (x + w - 1, y - 1)
add = [i for i in [A, C, E, E] if i[0] >= 0 and i[1] >= 0]
sub = [i for i in [B, B, D, F] if i[0] >= 0 and i[1] >= 0]
features.append((tuple(add), tuple(sub), 1, 0))
return features
def gen_type3(width, height, stride=1, increment=1):
""" Type 3 filters must have even width and even height.
Reference points:
I H G
F E D
C B A
Formula: A + C + 4E + G + I - 2B - 2D - 2F - 2H
"""
features = []
for w in range(2, width, 2 * increment):
for h in range(2, height, 2 * increment):
for x in range(0, 1 + (width - w) // stride, stride):
for y in range(0, 1 + (height - h) // stride, stride):
A = (x + w - 1, y + h - 1)
B = (x + (w // 2) - 1, y + h - 1)
C = (x - 1, y + h - 1)
D = (x + w - 1, y + (h // 2) - 1)
E = (x + (w // 2) - 1, y + (h // 2) - 1)
F = (x - 1, y + (h // 2) - 1)
G = (x + w - 1, y - 1)
H = (x + (w // 2) - 1, y - 1)
I = (x - 1, y - 1)
add = [i for i in [A, C, E, E, E, E, G, I] if i[0] >= 0 and i[1] >= 0]
sub = [i for i in [B, B, D, D, F, F, H, H] if i[0] >= 0 and i[1] >= 0]
features.append((tuple(add), tuple(sub)))
return features
def gen_type4(width, height, stride=1, increment=1):
""" Type 4 filters must have a width that is a multiple of 3.
Reference points:
E F G H
D C B A
Formula: A + 2C + E + 2G - 2B - D - 2F - H
"""
features = []
for w in range(3, width, 3 * increment):
for h in range(1, height, 1 * increment):
for x in range(0, 1 + (width - w) // stride, stride):
for y in range(0, 1 + (height - h) // stride, stride):
A = (x + w - 1, y + h - 1)
B = (x + 2 * (w // 3) - 1, y + h - 1)
C = (x + (w // 3) - 1, y + h - 1)
D = (x - 1, y + h - 1)
E = (x - 1, y - 1)
F = (x + (w // 3) - 1, y - 1)
G = (x + 2 * (w // 3) - 1, y - 1)
H = (x + w - 1, y - 1)
add = [i for i in [A, C, C, E, G, G] if i[0] >= 0 and i[1] >= 0]
sub = [i for i in [B, B, D, F, F, H] if i[0] >= 0 and i[1] >= 0]
features.append((tuple(add), tuple(sub)))
return features
def generate_features(width, height, stride=1, increment=1, verbose=False):
""" Generate features based on integral image representation.
Each feature is represented by points to add, points to subtract, polarity, and threshold.
"""
features = []
if verbose:
print("Generating type 1 features...")
features.extend(gen_type1(width, height, stride=stride, increment=increment))
if verbose:
print("Generating type 2 features...")
features.extend(gen_type2(width, height, stride=stride, increment=increment))
if verbose:
print("Generating type 3 features...")
features.extend(gen_type3(width, height, stride=stride, increment=increment))
if verbose:
print("Generating type 4 features...\n")
features.extend(gen_type4(width, height, stride=stride, increment=increment))
return features
"""
FEATURE EVALUATION AND PROCESSING FUNCTIONS
===========================================
"""
def eval_feature(feature, data):
add, sub = feature[0], feature[1]
to_add = data[:, [x[0] for x in add], [y[1] for y in add]].sum(axis=-1)
to_sub = data[:, [x[0] for x in sub], [y[1] for y in sub]].sum(axis=-1)
return to_add - to_sub
def _train_features(features, together, indicator, t_face, t_back, verbose=False):
""" Train polarity and threshold for each feature on the test set. """
for ind, feature in enumerate(features):
if verbose and ind % 1000 == 0:
print('\rTrained {} features...'.format(ind), end='')
add, sub = feature[0], feature[1]
scores = eval_feature(feature, together)
sort_perm = scores.argsort()
scores, indicator_perm = scores[sort_perm], indicator[sort_perm]
s_face = 0 if indicator_perm[0] < 0 else indicator_perm[0]
s_back = 0 if indicator_perm[0] >= 0 else -1 * indicator_perm[0]
error_min = min(s_face + t_back - s_back, s_back + t_face - s_face)
polarity_min = +1 if error_min == s_face + t_back - s_back else -1
threshold = scores[0]
for j in range(1, scores.shape[0]):
if indicator_perm[j] < 0:
s_back -= indicator_perm[j]
else:
s_face += indicator_perm[j]
left = s_face + t_back - s_back
right = s_back + t_face - s_face
error = min(left, right)
if error < error_min:
error_min = error
polarity_min = +1 if left < right else -1
threshold = scores[j]
features[ind] = tuple((add, sub, polarity_min, threshold, abs(error_min)))
if verbose:
print('\rFinished training {} features.'.format(len(features)))
return features
def train_features(features, faces, background, faces_dist, background_dist, threadpool, verbose=False):
norm = faces_dist.sum() + background_dist.sum()
faces_dist /= norm
background_dist /= norm
t_face, t_back = faces_dist.sum(), background_dist.sum()
together = np.concatenate((faces, background))
indicator = np.concatenate((faces_dist, -1 * background_dist))
NUM_PROCS = cpu_count() * 3
args = []
chunk = len(features) // NUM_PROCS
for cpu in range(cpu_count()):
if cpu + 1 == NUM_PROCS:
args.append((
features[cpu * chunk:], together, indicator, t_face, t_back, False
))
else:
args.append((
features[cpu * chunk: (cpu + 1) * chunk], together, indicator, t_face,
t_back, False
))
result = [y for x in threadpool.starmap_async(_train_features, args).get() for y in x]
result.sort(key=lambda x: x[-1])
return result
"""
ADABOOST ENSEMBLE FUNCTIONS
"""
def calculate_ensemble_error(classifiers, alphas, threshold, faces, background):
face_scores = np.zeros(faces.shape[0])
background_scores = np.zeros(background.shape[0])
for ind, classifier in enumerate(classifiers):
_, _, polarity, theta, _ = classifier
face_scores += alphas[ind] * np.sign(polarity * (eval_feature(classifier, faces) - theta))
background_scores += alphas[ind] * np.sign(polarity * (eval_feature(classifier, background) - theta))
face_scores -= threshold
background_scores -= threshold
false_negatives = face_scores < 0
false_positives = background_scores >= 0
error = (false_negatives.sum() + false_positives.sum()) / (faces.shape[0] + background.shape[0] + 1e-100)
return error, face_scores, background_scores, false_negatives, false_positives
def construct_boosted_classifier(features, faces, background, threadpool, target_false_pos_rate=0.3, verbose=False):
eps = 1E-100
classifiers, alphas = [], []
faces_dist = np.full((faces.shape[0]), 1 / (faces.shape[0] + background.shape[0]))
background_dist = np.full((background.shape[0]), 1 / (faces.shape[0] + background.shape[0]))
while True:
# Take classifier with minimum error on the distribution.
add, sub, polarity, theta, err = train_features(
features, faces, background, faces_dist, background_dist, threadpool
)[0]
if verbose:
print("\nSelected", add, sub, polarity, theta, err)
err += eps
classifiers.append((add, sub, polarity, theta, err))
alphas.append((1 / 2) * np.log((1 - err) / err))
zt = 2 * np.sqrt(err * (1 - err))
ht = np.sign(polarity * (eval_feature((add, sub), faces) - theta) + eps)
faces_dist = (faces_dist / zt) * np.exp(-1 * alphas[-1] * ht)
ht = np.sign(polarity * (eval_feature((add, sub), background) - theta) + eps)
background_dist = (background_dist / zt) * np.exp(+1 * alphas[-1] * ht)
# Calculate threshold
face_scores = np.zeros(faces.shape[0])
for ind, classifier in enumerate(classifiers):
_, _, polarity, theta, _ = classifier
face_scores += alphas[ind] * np.sign(polarity * (eval_feature(classifier, faces) - theta))
threshold = np.amin(face_scores)
error, _, _, _, false_positives = calculate_ensemble_error(classifiers, alphas, threshold, faces, background)
false_positive_rate = false_positives.sum() / background.shape[0]
if verbose:
print("Boosted classifier has {} features with ensemble false positive rate {:0.5f} and error {:0.5f}.".
format(len(classifiers), false_positive_rate, error)
)
if false_positive_rate < target_false_pos_rate:
break
return classifiers, alphas, threshold, error
"""
CASCADE FUNCTIONS
"""
def evaluate_cascade_error(cascade, faces, background, verbose=False):
for step in cascade:
classifiers, alphas, threshold = step
error, _, _, false_negatives, false_positives = calculate_ensemble_error(
classifiers, alphas, threshold, faces, background
)
faces = faces[~false_negatives]
background = background[false_positives]
return faces, background
def construct_classifier_cascade(features, faces, background, verbose=False):
if verbose:
print("Training {} features...".format(len(features)))
NUM_PROCS = cpu_count() * 3
pool = Pool(processes=NUM_PROCS)
cascade = []
num_initial_background = background.shape[0]
while True:
if verbose:
print("\nBOOSTING ROUND {}".format(len(cascade) + 1))
print("================")
classifiers, alphas, threshold, error = construct_boosted_classifier(
features, faces, background, pool, target_false_pos_rate=0.4, verbose=verbose
)
cascade.append((classifiers, alphas, threshold))
faces, background = evaluate_cascade_error(cascade, faces, background, verbose=verbose)
if verbose:
print("Boosting concluded with {} classifiers and remaining background proportion: {:0.5f}".format(
len(classifiers), background.shape[0] / num_initial_background
))
print("================")
if background.shape[0] / num_initial_background < 0.01:
break
return cascade
def get_cascade_prediction(cascade, integral_images, face_indices, verbose=False):
if verbose:
print("Evaluating cascade in {} image patches.".format(integral_images.shape[0]))
for ind, step in enumerate(cascade):
classifiers, alphas, theta = step
threshold = sum(alphas)
_, scores, _, negatives, _ = calculate_ensemble_error(
classifiers, alphas, +0.25 * threshold, integral_image(integral_images), integral_images[:1]
)
integral_images, face_indices = integral_images[~negatives], face_indices[~negatives]
if verbose:
print("After {} cascade steps, {} potential faces.".format(ind + 1, integral_images.shape[0]))
return face_indices
@click.command()
@click.option("-f", "--faces", help="Path to directory containing face examples.", required=True)
@click.option("-b", "--background", help="Path to directory containing background examples.", required=True)
@click.option("-l", "--load", help="Load saved cascade configuration.", default=None)
@click.option("-t", "--test", help="Test image.", default=None)
@click.option("-v", "--verbose", default=False, is_flag=True, help="Toggle for verbosity.")
def run(faces, background, load, test, verbose):
if load is None:
stride = 2
increment = 4
if verbose:
print("Importing face examples from: {} ...".format(faces))
faces = integral_image(import_img_dir(faces))
if verbose:
print("Importing background examples from: {} ...\n".format(background))
background = integral_image(import_img_dir(background))
features = generate_features(64, 64, stride=stride, increment=increment, verbose=verbose)
cascade = construct_classifier_cascade(features, faces, background, verbose=verbose)
with open('cascade_save.json', 'w') as f:
json.dump(cascade, f)
else:
stride = 3
with open(load, 'r') as f:
cascade = json.load(f)
original_image = import_jpg(test)
bounding_boxes = []
integral_images = []
indices = []
for x in range(0, original_image.shape[0] - 64, stride):
for y in range(0, original_image.shape[1] - 64, stride):
bounding_boxes.append((x, y))
indices.append(len(indices))
integral_images.append(original_image[x: x + 64, y: y + 64])
face_indices = get_cascade_prediction(cascade, np.array(integral_images), np.array(indices), verbose=verbose)
draw_bounding_boxes(original_image, np.array(bounding_boxes)[face_indices], 64, 64)
if __name__ == "__main__":
run()