-
Notifications
You must be signed in to change notification settings - Fork 63
/
trainer.py
634 lines (539 loc) · 22.1 KB
/
trainer.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
from __future__ import division, print_function
import abc
import numpy as np
from menpo.image import Image
from menpo.feature import sparse_hog
from menpo.visualize import print_dynamic, progress_bar_str
from menpofit.base import noisy_align, build_sampling_grid
from menpofit.fittingresult import (NonParametricFittingResult,
SemiParametricFittingResult,
ParametricFittingResult)
from .base import (NonParametricRegressor, SemiParametricRegressor,
ParametricRegressor)
from .parametricfeatures import extract_parametric_features, weights
from .regressors import mlr
class RegressorTrainer(object):
r"""
An abstract base class for training regressors.
Parameters
----------
reference_shape : :map:`PointCloud`
The reference shape that will be used.
regression_type : `callable`, optional
A `callable` that defines the regression technique to be used.
Examples of such callables can be found in
:ref:`regression_callables`
regression_features : ``None`` or `string` or `function`, optional
The features that are used during the regression.
noise_std : `float`, optional
The standard deviation of the gaussian noise used to produce the
training shapes.
rotation : boolean, optional
Specifies whether ground truth in-plane rotation is to be used
to produce the training shapes.
n_perturbations : `int`, optional
Defines the number of perturbations that will be applied to the
training shapes.
"""
__metaclass__ = abc.ABCMeta
def __init__(self, reference_shape, regression_type=mlr,
regression_features=None, noise_std=0.04, rotation=False,
n_perturbations=10):
self.reference_shape = reference_shape
self.regression_type = regression_type
self.regression_features = regression_features
self.rotation = rotation
self.noise_std = noise_std
self.n_perturbations = n_perturbations
def _regression_data(self, images, gt_shapes, perturbed_shapes,
verbose=False):
r"""
Method that generates the regression data : features and delta_ps.
Parameters
----------
images : list of :map:`MaskedImage`
The set of landmarked images.
gt_shapes : :map:`PointCloud` list
List of the ground truth shapes that correspond to the images.
perturbed_shapes : :map:`PointCloud` list
List of the perturbed shapes in order to regress.
verbose : `boolean`, optional
If ``True``, the progress is printed.
"""
if verbose:
print_dynamic('- Generating regression data')
n_images = len(images)
features = []
delta_ps = []
for j, (i, s, p_shape) in enumerate(zip(images, gt_shapes,
perturbed_shapes)):
if verbose:
print_dynamic('- Generating regression data - {}'.format(
progress_bar_str((j + 1.) / n_images, show_bar=False)))
for ps in p_shape:
features.append(self.features(i, ps))
delta_ps.append(self.delta_ps(s, ps))
return np.asarray(features), np.asarray(delta_ps)
@abc.abstractmethod
def features(self, image, shape):
r"""
Abstract method to generate the features for the regression.
Parameters
----------
image : :map:`MaskedImage`
The current image.
shape : :map:`PointCloud`
The current shape.
"""
pass
@abc.abstractmethod
def delta_ps(self, gt_shape, perturbed_shape):
r"""
Abstract method to generate the delta_ps for the regression.
Parameters
----------
gt_shape : :map:`PointCloud`
The ground truth shape.
perturbed_shape : :map:`PointCloud`
The perturbed shape.
"""
pass
def train(self, images, shapes, perturbed_shapes=None, verbose=False,
**kwargs):
r"""
Trains a Regressor given a list of landmarked images.
Parameters
----------
images : list of :map:`MaskedImage`
The set of landmarked images from which to train the regressor.
shapes : :map:`PointCloud` list
List of the shapes that correspond to the images.
perturbed_shapes : :map:`PointCloud` list, optional
List of the perturbed shapes used for the regressor training.
verbose : `boolean`, optional
Flag that controls information and progress printing.
Returns
-------
regressor : :map:`Regressor`
A regressor object.
Raises
------
ValueError
The number of shapes must be equal to the number of images.
ValueError
The number of perturbed shapes must be equal or multiple to
the number of images.
"""
n_images = len(images)
n_shapes = len(shapes)
# generate regression data
if n_images != n_shapes:
raise ValueError("The number of shapes must be equal to "
"the number of images.")
elif not perturbed_shapes:
perturbed_shapes = self.perturb_shapes(shapes)
features, delta_ps = self._regression_data(
images, shapes, perturbed_shapes, verbose=verbose)
elif n_images == len(perturbed_shapes):
features, delta_ps = self._regression_data(
images, shapes, perturbed_shapes, verbose=verbose)
else:
raise ValueError("The number of perturbed shapes must be "
"equal or multiple to the number of images.")
# perform regression
if verbose:
print_dynamic('- Performing regression...')
# Expected to be a callable
regressor = self.regression_type(features, delta_ps, **kwargs)
# compute regressor RMSE
estimated_delta_ps = regressor(features)
error = np.sqrt(np.mean(np.sum((delta_ps - estimated_delta_ps) ** 2,
axis=1)))
if verbose:
print_dynamic('- Regression RMSE is {0:.5f}.\n'.format(error))
return self._build_regressor(regressor, self.features)
def perturb_shapes(self, gt_shape):
r"""
Perturbs the given shapes. The number of perturbations is defined by
``n_perturbations``.
Parameters
----------
gt_shape : :map:`PointCloud` list
List of the shapes that correspond to the images.
will be perturbed.
Returns
-------
perturbed_shapes : :map:`PointCloud` list
List of the perturbed shapes.
"""
return [[self._perturb_shape(s) for _ in range(self.n_perturbations)]
for s in gt_shape]
def _perturb_shape(self, gt_shape):
r"""
Method that performs noisy alignment between the given ground truth
shape and the reference shape.
Parameters
----------
gt_shape : :map:`PointCloud`
The ground truth shape.
"""
return noisy_align(self.reference_shape, gt_shape,
noise_std=self.noise_std
).apply(self.reference_shape)
@abc.abstractmethod
def _build_regressor(self, regressor, features):
r"""
Abstract method to build a regressor model.
"""
pass
class NonParametricRegressorTrainer(RegressorTrainer):
r"""
Class for training a Non-Parametric Regressor.
Parameters
----------
reference_shape : :map:`PointCloud`
The reference shape that will be used.
regression_type : `callable`, optional
A `callable` that defines the regression technique to be used.
Examples of such callables can be found in
:ref:`regression_callables`
regression_features : `function`, optional
The features that are used during the regression.
See `menpo.features` for details more details on
Menpo's standard image features and feature options.
See :ref:`feature_functions` for non standard
features definitions.
patch_shape : tuple, optional
The shape of the patches that will be extracted.
noise_std : `float`, optional
The standard deviation of the gaussian noise used to produce the
training shapes.
rotation : `boolean`, optional
Specifies whether ground truth in-plane rotation is to be used
to produce the training shapes.
n_perturbations : `int`, optional
Defines the number of perturbations that will be applied to the
training shapes.
"""
def __init__(self, reference_shape, regression_type=mlr,
regression_features=sparse_hog, patch_shape=(16, 16),
noise_std=0.04, rotation=False, n_perturbations=10):
super(NonParametricRegressorTrainer, self).__init__(
reference_shape, regression_type=regression_type,
regression_features=regression_features, noise_std=noise_std,
rotation=rotation, n_perturbations=n_perturbations)
self.patch_shape = patch_shape
self._set_up()
def _set_up(self):
# work out feature length per patch
patch_img = Image.init_blank(self.patch_shape, fill=0)
self._feature_patch_length = self.regression_features(patch_img).n_parameters
@property
def algorithm(self):
r"""
Returns the algorithm name.
"""
return "Non-Parametric"
def _create_fitting(self, image, shapes, gt_shape=None):
r"""
Method that creates the fitting result object.
Parameters
----------
image : :map:`MaskedImage`
The image object.
shapes : :map:`PointCloud` list
The shapes.
gt_shape : :map:`PointCloud`
The ground truth shape.
"""
return NonParametricFittingResult(image, self, parameters=[shapes],
gt_shape=gt_shape)
def features(self, image, shape):
r"""
Method that extracts the features for the regression, which in this
case are patch based.
Parameters
----------
image : :map:`MaskedImage`
The current image.
shape : :map:`PointCloud`
The current shape.
"""
# extract patches
patches = image.extract_patches(shape, patch_size=self.patch_shape)
features = np.zeros((shape.n_points, self._feature_patch_length))
for j, patch in enumerate(patches):
# compute features
features[j, ...] = self.regression_features(patch).as_vector()
return np.hstack((features.ravel(), 1))
def delta_ps(self, gt_shape, perturbed_shape):
r"""
Method to generate the delta_ps for the regression.
Parameters
----------
gt_shape : :map:`PointCloud`
The ground truth shape.
perturbed_shape : :map:`PointCloud`
The perturbed shape.
"""
return (gt_shape.as_vector() -
perturbed_shape.as_vector())
def _build_regressor(self, regressor, features):
r"""
Method to build the NonParametricRegressor regressor object.
"""
return NonParametricRegressor(regressor, features)
class SemiParametricRegressorTrainer(NonParametricRegressorTrainer):
r"""
Class for training a Semi-Parametric Regressor.
This means that a parametric shape model and a non-parametric appearance
representation are employed.
Parameters
----------
reference_shape : PointCloud
The reference shape that will be used.
regression_type : `callable`, optional
A `callable` that defines the regression technique to be used.
Examples of such callables can be found in
:ref:`regression_callables`
regression_features : `function`, optional
The features that are used during the regression.
See :ref:`menpo.features` for details more details on
Menpos standard image features and feature options.
patch_shape : tuple, optional
The shape of the patches that will be extracted.
update : 'compositional' or 'additive'
Defines the way to update the warp.
noise_std : `float`, optional
The standard deviation of the gaussian noise used to produce the
training shapes.
rotation : `boolean`, optional
Specifies whether ground truth in-plane rotation is to be used
to produce the training shapes.
n_perturbations : `int`, optional
Defines the number of perturbations that will be applied to the
training shapes.
"""
def __init__(self, transform, reference_shape, regression_type=mlr,
regression_features=sparse_hog, patch_shape=(16, 16),
update='compositional', noise_std=0.04, rotation=False,
n_perturbations=10):
super(SemiParametricRegressorTrainer, self).__init__(
reference_shape, regression_type=regression_type,
regression_features=regression_features, patch_shape=patch_shape,
noise_std=noise_std, rotation=rotation,
n_perturbations=n_perturbations)
self.transform = transform
self.update = update
@property
def algorithm(self):
r"""
Returns the algorithm name.
"""
return "Semi-Parametric"
def _create_fitting(self, image, shapes, gt_shape=None):
r"""
Method that creates the fitting result object.
Parameters
----------
image : :map:`MaskedImage`
The image object.
shapes : :map:`PointCloud` list
The shapes.
gt_shape : :map:`PointCloud`
The ground truth shape.
"""
return SemiParametricFittingResult(image, self, parameters=[shapes],
gt_shape=gt_shape)
def delta_ps(self, gt_shape, perturbed_shape):
r"""
Method to generate the delta_ps for the regression.
Parameters
----------
gt_shape : :map:`PointCloud`
The ground truth shape.
perturbed_shape : :map:`PointCloud`
The perturbed shape.
"""
self.transform.set_target(gt_shape)
gt_ps = self.transform.as_vector()
self.transform.set_target(perturbed_shape)
perturbed_ps = self.transform.as_vector()
return gt_ps - perturbed_ps
def _build_regressor(self, regressor, features):
r"""
Method to build the NonParametricRegressor regressor object.
"""
return SemiParametricRegressor(regressor, features, self.transform,
self.update)
class ParametricRegressorTrainer(RegressorTrainer):
r"""
Class for training a Parametric Regressor.
Parameters
----------
appearance_model : :map:`PCAModel`
The appearance model to be used.
transform : :map:`Affine`
The transform used for warping.
reference_shape : :map:`PointCloud`
The reference shape that will be used.
regression_type : `callable`, optional
A `callable` that defines the regression technique to be used.
Examples of such callables can be found in
:ref:`regression_callables`
regression_features : ``None`` or `function`, optional
The parametric features that are used during the regression.
If ``None``, the reconstruction appearance weights will be used as
feature.
If `string` or `function`, the feature representation will be
computed using one of the function in:
If `string`, the feature representation will be extracted by
executing a parametric feature function.
Note that this feature type can only be one of the parametric
feature functions defined :ref:`parametric_features`.
patch_shape : tuple, optional
The shape of the patches that will be extracted.
update : 'compositional' or 'additive'
Defines the way to update the warp.
noise_std : `float`, optional
The standard deviation of the gaussian noise used to produce the
training shapes.
rotation : `boolean`, optional
Specifies whether ground truth in-plane rotation is to be used
to produce the training shapes.
n_perturbations : `int`, optional
Defines the number of perturbations that will be applied to the
training shapes.
"""
def __init__(self, appearance_model, transform, reference_shape,
regression_type=mlr, regression_features=weights,
update='compositional', noise_std=0.04, rotation=False,
n_perturbations=10):
super(ParametricRegressorTrainer, self).__init__(
reference_shape, regression_type=regression_type,
regression_features=regression_features, noise_std=noise_std,
rotation=rotation, n_perturbations=n_perturbations)
self.appearance_model = appearance_model
self.template = appearance_model.mean()
self.regression_features = regression_features
self.transform = transform
self.update = update
@property
def algorithm(self):
r"""
Returns the algorithm name.
"""
return "Parametric"
def _create_fitting(self, image, shapes, gt_shape=None):
r"""
Method that creates the fitting result object.
Parameters
----------
image : :map:`MaskedImage`
The image object.
shapes : :map:`PointCloud` list
The shapes.
gt_shape : :map:`PointCloud`
The ground truth shape.
"""
return ParametricFittingResult(image, self, parameters=[shapes],
gt_shape=gt_shape)
def features(self, image, shape):
r"""
Method that extracts the features for the regression, which in this
case are patch based.
Parameters
----------
image : :map:`MaskedImage`
The current image.
shape : :map:`PointCloud`
The current shape.
"""
self.transform.set_target(shape)
# TODO should the template be a mask or a shape? warp_to_shape here
warped_image = image.warp_to_mask(self.template.mask, self.transform,
warp_landmarks=False)
features = extract_parametric_features(
self.appearance_model, warped_image, self.regression_features)
return np.hstack((features, 1))
def delta_ps(self, gt_shape, perturbed_shape):
r"""
Method to generate the delta_ps for the regression.
Parameters
----------
gt_shape : :map:`PointCloud`
The ground truth shape.
perturbed_shape : :map:`PointCloud`
The perturbed shape.
"""
self.transform.set_target(gt_shape)
gt_ps = self.transform.as_vector()
self.transform.set_target(perturbed_shape)
perturbed_ps = self.transform.as_vector()
return gt_ps - perturbed_ps
def _build_regressor(self, regressor, features):
r"""
Method to build the NonParametricRegressor regressor object.
"""
return ParametricRegressor(
regressor, features, self.appearance_model, self.transform,
self.update)
class SemiParametricClassifierBasedRegressorTrainer(
SemiParametricRegressorTrainer):
r"""
Class for training a Semi-Parametric Classifier-Based Regressor. This means
that the classifiers are used instead of features.
Parameters
----------
classifiers : list of :map:`classifiers`
List of classifiers.
transform : :map:`Affine`
The transform used for warping.
reference_shape : :map:`PointCloud`
The reference shape that will be used.
regression_type : `callable`, optional
A `callable` that defines the regression technique to be used.
Examples of such callables can be found in
:ref:`regression_callables`
patch_shape : tuple, optional
The shape of the patches that will be extracted.
noise_std : `float`, optional
The standard deviation of the gaussian noise used to produce the
training shapes.
rotation : `boolean`, optional
Specifies whether ground truth in-plane rotation is to be used
to produce the training shapes.
n_perturbations : `int`, optional
Defines the number of perturbations that will be applied to the
training shapes.
"""
def __init__(self, classifiers, transform, reference_shape,
regression_type=mlr, patch_shape=(16, 16),
update='compositional', noise_std=0.04, rotation=False,
n_perturbations=10):
super(SemiParametricClassifierBasedRegressorTrainer, self).__init__(
transform, reference_shape, regression_type=regression_type,
patch_shape=patch_shape, update=update,
noise_std=noise_std, rotation=rotation,
n_perturbations=n_perturbations)
self.classifiers = classifiers
def _set_up(self):
# TODO: CLMs should use slices instead of sampling grid, and the
# need of the _set_up method will probably disappear
# set up sampling grid
self.sampling_grid = build_sampling_grid(self.patch_shape)
def features(self, image, shape):
r"""
Method that extracts the features for the regression, which in this
case are patch based.
Parameters
----------
image : :map:`MaskedImage`
The current image.
shape : :map:`PointCloud`
The current shape.
"""
patches = image.extract_patches(shape, patch_size=self.patch_shape)
features = [clf(patch.as_vector(keep_channels=True))
for (clf, patch) in zip(self.classifiers, patches)]
return np.hstack((np.asarray(features).ravel(), 1))