-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMixture.cpp
1146 lines (880 loc) · 31.2 KB
/
Mixture.cpp
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//--------------------------------------------------------------------------------------------------
// Implementation of the papers "Exact Acceleration of Linear Object Detectors", 12th European
// Conference on Computer Vision, 2012 and "Deformable Part Models with Individual Part Scaling",
// 24th British Machine Vision Conference, 2013.
//
// Copyright (c) 2013 Idiap Research Institute, <http://www.idiap.ch/>
// Written by Charles Dubout <[email protected]>
//
// This file is part of FFLDv2 (the Fast Fourier Linear Detector version 2)
//
// FFLDv2 is free software: you can redistribute it and/or modify it under the terms of the GNU
// Affero General Public License version 3 as published by the Free Software Foundation.
//
// FFLDv2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
// General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with FFLDv2. If
// not, see <http://www.gnu.org/licenses/>.
//--------------------------------------------------------------------------------------------------
#include "Intersector.h"
#include "LBFGS.h"
#include "Mixture.h"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
using namespace Eigen;
using namespace FFLD;
using namespace std;
Mixture::Mixture() : cached_(false), zero_(true)
{
}
Mixture::Mixture(const vector<Model> & models) : models_(models), cached_(false), zero_(true)
{
}
Mixture::Mixture(int nbComponents, const vector<Scene> & scenes, Object::Name name) :
cached_(false), zero_(true)
{
// Create an empty mixture if any of the given parameters is invalid
if ((nbComponents <= 0) || scenes.empty()) {
cerr << "Attempting to create an empty mixture" << endl;
return;
}
// Compute the root filters' sizes using Felzenszwalb's heuristic
const vector<pair<int, int> > sizes = FilterSizes(nbComponents, scenes, name);
// Early return in case the root filters' sizes could not be determined
if (sizes.size() != nbComponents)
return;
// Initialize the models (with symmetry) to those sizes
models_.resize(2 * nbComponents);
for (int i = 0; i < nbComponents; ++i) {
models_[2 * i ] = Model(sizes[i]);
models_[2 * i + 1] = Model(sizes[i]);
}
}
bool Mixture::empty() const
{
return models_.empty();
}
const vector<Model> & Mixture::models() const
{
return models_;
}
vector<Model> & Mixture::models()
{
return models_;
}
pair<int, int> Mixture::minSize() const
{
pair<int, int> size(0, 0);
if (!models_.empty()) {
size = models_[0].rootSize();
for (int i = 1; i < models_.size(); ++i) {
size.first = min(size.first, models_[i].rootSize().first);
size.second = min(size.second, models_[i].rootSize().second);
}
}
return size;
}
pair<int, int> Mixture::maxSize() const
{
pair<int, int> size(0, 0);
if (!models_.empty()) {
size = models_[0].rootSize();
for (int i = 1; i < models_.size(); ++i) {
size.first = max(size.first, models_[i].rootSize().first);
size.second = max(size.second, models_[i].rootSize().second);
}
}
return size;
}
double Mixture::train(const vector<Scene> & scenes, Object::Name name, int padx, int pady,
int interval, int nbRelabel, int nbDatamine, int maxNegatives, double C,
double J, double overlap)
{
if (empty() || scenes.empty() || (padx < 1) || (pady < 1) || (interval < 1) ||
(nbRelabel < 1) || (nbDatamine < 1) || (maxNegatives < models_.size()) || (C <= 0.0) ||
(J <= 0.0) || (overlap <= 0.0) || (overlap >= 1.0)) {
cerr << "Invalid training parameters" << endl;
return numeric_limits<double>::quiet_NaN();
}
// Test if the models are really zero by looking at the first cell of the first filter of the
// first model
if (!models_[0].empty() && models_[0].parts()[0].filter.size() &&
!models_[0].parts()[0].filter(0, 0).isZero())
zero_ = false;
double loss = numeric_limits<double>::infinity();
for (int relabel = 0; relabel < nbRelabel; ++relabel) {
// Sample all the positives
vector<pair<Model, int> > positives;
posLatentSearch(scenes, name, padx, pady, interval, overlap, positives);
// Left-right clustering at the first iteration
if (zero_)
Cluster(static_cast<int>(models_.size()), positives);
// Cache of hard negative samples of maximum size maxNegatives
vector<pair<Model, int> > negatives;
// Previous loss on the cache
double prevLoss = -numeric_limits<double>::infinity();
for (int datamine = 0; datamine < nbDatamine; ++datamine) {
// Remove easy samples (keep hard ones)
int j = 0;
for (int i = 0; i < negatives.size(); ++i)
if ((negatives[i].first.parts()[0].deformation(3) =
models_[negatives[i].second].dot(negatives[i].first)) > -1.01)
negatives[j++] = negatives[i];
negatives.resize(j);
// Sample new hard negatives
negLatentSearch(scenes, name, padx, pady, interval, maxNegatives, negatives);
// Stop if there are no new hard negatives
if (datamine && (negatives.size() == j))
break;
// Merge the left / right samples for more efficient training
vector<int> posComponents(positives.size());
for (int i = 0; i < positives.size(); ++i) {
posComponents[i] = positives[i].second;
if (positives[i].second & 1)
positives[i].first = positives[i].first.flip();
positives[i].second >>= 1;
}
vector<int> negComponents(negatives.size());
for (int i = 0; i < negatives.size(); ++i) {
negComponents[i] = negatives[i].second;
if (negatives[i].second & 1)
negatives[i].first = negatives[i].first.flip();
negatives[i].second >>= 1;
}
// Merge the left / right models for more efficient training
for (int i = 1; i < models_.size() / 2; ++i)
models_[i] = models_[i * 2];
models_.resize(models_.size() / 2);
const int maxIterations =
min(max(10.0 * sqrt(static_cast<double>(positives.size())), 100.0), 1000.0);
loss = train(positives, negatives, C, J, maxIterations);
cout << "Relabel: " << relabel << ", datamine: " << datamine
<< ", # positives: " << positives.size() << ", # hard negatives: " << j
<< " (already in the cache) + " << (negatives.size() - j) << " (new) = "
<< negatives.size() << ", loss (cache): " << loss << endl;
// Unmerge the left / right samples
for (int i = 0; i < positives.size(); ++i) {
positives[i].second = posComponents[i];
if (positives[i].second & 1)
positives[i].first = positives[i].first.flip();
}
for (int i = 0; i < negatives.size(); ++i) {
negatives[i].second = negComponents[i];
if (negatives[i].second & 1)
negatives[i].first = negatives[i].first.flip();
}
// Unmerge the left / right models
models_.resize(models_.size() * 2);
for (int i = static_cast<int>(models_.size()) / 2 - 1; i >= 0; --i) {
models_[i * 2 ] = models_[i];
models_[i * 2 + 1] = models_[i].flip();
}
// The filters definitely changed
filterCache_.clear();
cached_ = false;
zero_ = false;
// Save the latest model so as to be able to look at it while training
ofstream out("tmp.txt");
out << (*this);
// Stop if we are not making progress
if ((0.999 * loss < prevLoss) && (negatives.size() < maxNegatives))
break;
prevLoss = loss;
}
}
return loss;
}
void Mixture::initializeParts(int nbParts, pair<int, int> partSize)
{
for (int i = 0; i < models_.size(); i += 2) {
models_[i].initializeParts(nbParts, partSize);
models_[i + 1] = models_[i].flip();
}
// The filters definitely changed
filterCache_.clear();
cached_ = false;
zero_ = false;
}
void Mixture::convolve(const HOGPyramid & pyramid, vector<HOGPyramid::Matrix> & scores,
vector<Indices> & argmaxes,
vector<vector<vector<Model::Positions> > > * positions) const
{
if (empty() || pyramid.empty()) {
scores.clear();
argmaxes.clear();
if (positions)
positions->clear();
return;
}
const int nbModels = static_cast<int>(models_.size());
const int nbLevels = static_cast<int>(pyramid.levels().size());
// Convolve with all the models
vector<vector<HOGPyramid::Matrix> > convolutions;
convolve(pyramid, convolutions, positions);
// In case of error
if (convolutions.empty()) {
scores.clear();
argmaxes.clear();
if (positions)
positions->clear();
return;
}
// Resize the scores and argmaxes
scores.resize(nbLevels);
argmaxes.resize(nbLevels);
#pragma omp parallel for
for (int z = 0; z < nbLevels; ++z) {
int rows = static_cast<int>(convolutions[0][z].rows());
int cols = static_cast<int>(convolutions[0][z].cols());
for (int i = 1; i < nbModels; ++i) {
rows = min(rows, static_cast<int>(convolutions[i][z].rows()));
cols = min(cols, static_cast<int>(convolutions[i][z].cols()));
}
scores[z].resize(rows, cols);
argmaxes[z].resize(rows, cols);
for (int y = 0; y < rows; ++y) {
for (int x = 0; x < cols; ++x) {
int argmax = 0;
for (int i = 1; i < nbModels; ++i)
if (convolutions[i][z](y, x) > convolutions[argmax][z](y, x))
argmax = i;
scores[z](y, x) = convolutions[argmax][z](y, x);
argmaxes[z](y, x) = argmax;
}
}
}
}
void Mixture::cacheFilters() const
{
// Count the number of filters
int nbFilters = 0;
for (int i = 0; i < models_.size(); ++i)
nbFilters += models_[i].parts().size();
// Transform all the filters
filterCache_.resize(nbFilters);
for (int i = 0, j = 0; i < models_.size(); ++i) {
#pragma omp parallel for
for (int k = 0; k < models_[i].parts().size(); ++k)
Patchwork::TransformFilter(models_[i].parts()[k].filter, filterCache_[j + k]);
j += models_[i].parts().size();
}
cached_ = true;
}
static inline void clipBndBox(Rectangle & bndbox, const Scene & scene, double alpha = 0.0)
{
// Compromise between clamping the bounding box to the image and penalizing bounding boxes
// extending outside the image
if (bndbox.left() < 0)
bndbox.setLeft(bndbox.left() * alpha - 0.5);
if (bndbox.top() < 0)
bndbox.setTop(bndbox.top() * alpha - 0.5);
if (bndbox.right() >= scene.width())
bndbox.setRight(scene.width() - 1 + (bndbox.right() - scene.width() + 1) * alpha + 0.5);
if (bndbox.bottom() >= scene.height())
bndbox.setBottom(scene.height() - 1 + (bndbox.bottom() - scene.height() + 1) * alpha + 0.5);
}
void Mixture::posLatentSearch(const vector<Scene> & scenes, Object::Name name, int padx, int pady,
int interval, double overlap,
vector<pair<Model, int> > & positives) const
{
if (scenes.empty() || (padx < 1) || (pady < 1) || (interval < 1) || (overlap <= 0.0) ||
(overlap >= 1.0)) {
positives.clear();
cerr << "Invalid training paramters" << endl;
return;
}
positives.clear();
for (int i = 0; i < scenes.size(); ++i) {
// Skip negative scenes
bool negative = true;
for (int j = 0; j < scenes[i].objects().size(); ++j)
if ((scenes[i].objects()[j].name() == name) && !scenes[i].objects()[j].difficult())
negative = false;
if (negative)
continue;
const JPEGImage image(scenes[i].filename());
if (image.empty()) {
positives.clear();
return;
}
const HOGPyramid pyramid(image, padx, pady, interval);
if (pyramid.empty()) {
positives.clear();
return;
}
vector<HOGPyramid::Matrix> scores;
vector<Indices> argmaxes;
vector<vector<vector<Model::Positions> > > positions;
if (!zero_)
convolve(pyramid, scores, argmaxes, &positions);
// For each object, set as positive the best (highest score or else most intersecting)
// position
for (int j = 0; j < scenes[i].objects().size(); ++j) {
// Ignore objects with a different name or difficult objects
if ((scenes[i].objects()[j].name() != name) || scenes[i].objects()[j].difficult())
continue;
const Intersector intersector(scenes[i].objects()[j].bndbox(), overlap);
// The model, level, position, score, and intersection of the best example
int argModel = -1;
int argX = -1;
int argY = -1;
int argZ = -1;
double maxScore = -numeric_limits<double>::infinity();
double maxInter = 0.0;
for (int z = 0; z < pyramid.levels().size(); ++z) {
const double scale = pow(2.0, static_cast<double>(z) / interval + 2);
int rows = 0;
int cols = 0;
if (!zero_) {
rows = static_cast<int>(scores[z].rows());
cols = static_cast<int>(scores[z].cols());
}
else if (z >= interval) {
rows = static_cast<int>(pyramid.levels()[z].rows()) - maxSize().first + 1;
cols = static_cast<int>(pyramid.levels()[z].cols()) - maxSize().second + 1;
}
for (int y = 0; y < rows; ++y) {
for (int x = 0; x < cols; ++x) {
// Find the best matching model (highest score or else most intersecting)
int model = zero_ ? 0 : argmaxes[z](y, x);
double intersection = 0.0;
// Try all models and keep the most intersecting one
if (zero_) {
for (int k = 0; k < models_.size(); ++k) {
// The bounding box of the model at this position
Rectangle bndbox;
bndbox.setX((x - padx) * scale + 0.5);
bndbox.setY((y - pady) * scale + 0.5);
bndbox.setWidth(models_[k].rootSize().second * scale + 0.5);
bndbox.setHeight(models_[k].rootSize().first * scale + 0.5);
// Trade-off between clipping and penalizing
clipBndBox(bndbox, scenes[i], 0.5);
double inter = 0.0;
if (intersector(bndbox, &inter)) {
if (inter > intersection) {
model = k;
intersection = inter;
}
}
}
}
// Just take the model with the best score
else {
// The bounding box of the model at this position
Rectangle bndbox;
bndbox.setX((x - padx) * scale + 0.5);
bndbox.setY((y - pady) * scale + 0.5);
bndbox.setWidth(models_[model].rootSize().second * scale + 0.5);
bndbox.setHeight(models_[model].rootSize().first * scale + 0.5);
clipBndBox(bndbox, scenes[i]);
intersector(bndbox, &intersection);
}
if ((intersection > maxInter) && (zero_ || (scores[z](y, x) > maxScore))) {
argModel = model;
argX = x;
argY = y;
argZ = z;
if (!zero_)
maxScore = scores[z](y, x);
maxInter = intersection;
}
}
}
}
if (maxInter >= overlap) {
Model sample;
models_[argModel].initializeSample(pyramid, argX, argY, argZ, sample,
zero_ ? 0 : &positions[argModel]);
if (!sample.empty())
positives.push_back(make_pair(sample, argModel));
}
}
}
}
static inline bool operator==(const Model & a, const Model & b)
{
return (a.parts()[0].offset == b.parts()[0].offset) &&
(a.parts()[0].deformation(0) == b.parts()[0].deformation(0)) &&
(a.parts()[0].deformation(1) == b.parts()[0].deformation(1));
}
static inline bool operator<(const Model & a, const Model & b)
{
return (a.parts()[0].offset(0) < b.parts()[0].offset(0)) ||
((a.parts()[0].offset(0) == b.parts()[0].offset(0)) &&
((a.parts()[0].offset(1) < b.parts()[0].offset(1)) ||
((a.parts()[0].offset(1) == b.parts()[0].offset(1)) &&
((a.parts()[0].deformation(0) < b.parts()[0].deformation(0)) ||
((a.parts()[0].deformation(0) == b.parts()[0].deformation(0)) &&
((a.parts()[0].deformation(1) < b.parts()[0].deformation(1))))))));
}
void Mixture::negLatentSearch(const vector<Scene> & scenes, Object::Name name, int padx, int pady,
int interval, int maxNegatives,
vector<pair<Model, int> > & negatives) const
{
// Sample at most (maxNegatives - negatives.size()) negatives with a score above -1.0
if (scenes.empty() || (padx < 1) || (pady < 1) || (interval < 1) || (maxNegatives <= 0) ||
(negatives.size() >= maxNegatives)) {
negatives.clear();
cerr << "Invalid training paramters" << endl;
return;
}
// The number of negatives already in the cache
const int nbCached = static_cast<int>(negatives.size());
for (int i = 0, j = 0; i < scenes.size(); ++i) {
// Skip positive scenes
bool positive = false;
for (int k = 0; k < scenes[i].objects().size(); ++k)
if (scenes[i].objects()[k].name() == name)
positive = true;
if (positive)
continue;
const JPEGImage image(scenes[i].filename());
if (image.empty()) {
negatives.clear();
return;
}
const HOGPyramid pyramid(image, padx, pady, interval);
if (pyramid.empty()) {
negatives.clear();
return;
}
vector<HOGPyramid::Matrix> scores;
vector<Indices> argmaxes;
vector<vector<vector<Model::Positions> > > positions;
if (!zero_)
convolve(pyramid, scores, argmaxes, &positions);
for (int z = 0; z < pyramid.levels().size(); ++z) {
int rows = 0;
int cols = 0;
if (!zero_) {
rows = static_cast<int>(scores[z].rows());
cols = static_cast<int>(scores[z].cols());
}
else if (z >= interval) {
rows = static_cast<int>(pyramid.levels()[z].rows()) - maxSize().first + 1;
cols = static_cast<int>(pyramid.levels()[z].cols()) - maxSize().second + 1;
}
for (int y = 0; y < rows; ++y) {
for (int x = 0; x < cols; ++x) {
const int argmax = zero_ ? (rand() % models_.size()) : argmaxes[z](y, x);
if (zero_ || (scores[z](y, x) > -1)) {
Model sample;
models_[argmax].initializeSample(pyramid, x, y, z, sample,
zero_ ? 0 : &positions[argmax]);
if (!sample.empty()) {
// Store all the information about the sample in the offset and
// deformation of its root
sample.parts()[0].offset(0) = i;
sample.parts()[0].offset(1) = z;
sample.parts()[0].deformation(0) = y;
sample.parts()[0].deformation(1) = x;
sample.parts()[0].deformation(2) = argmax;
sample.parts()[0].deformation(3) = zero_ ? 0.0 : scores[z](y, x);
// Look if the same sample was already sampled
while ((j < nbCached) && (negatives[j].first < sample))
++j;
// Make sure not to put the same sample twice
if ((j >= nbCached) || !(negatives[j].first == sample)) {
negatives.push_back(make_pair(sample, argmax));
if (negatives.size() == maxNegatives)
return;
}
}
}
}
}
}
}
}
namespace FFLD
{
namespace detail
{
class Loss : public LBFGS::IFunction
{
public:
Loss(vector<Model> & models, const vector<pair<Model, int> > & positives,
const vector<pair<Model, int> > & negatives, double C, double J, int maxIterations) :
models_(models), positives_(positives), negatives_(negatives), C_(C), J_(J),
maxIterations_(maxIterations)
{
}
virtual int dim() const
{
int d = 0;
for (int i = 0; i < models_.size(); ++i) {
for (int j = 0; j < models_[i].parts().size(); ++j) {
d += models_[i].parts()[j].filter.size() * HOGPyramid::NbFeatures; // Filter
if (j)
d += 6; // Deformation
}
++d; // Bias
}
return d;
}
virtual double operator()(const double * x, double * g = 0) const
{
// Recopy the features into the models
ToModels(x, models_);
// Compute the loss and gradient over the samples
double loss = 0.0;
vector<Model> gradients;
if (g) {
gradients.resize(models_.size());
for (int i = 0; i < models_.size(); ++i)
gradients[i] = Model(models_[i].rootSize(),
static_cast<int>(models_[i].parts().size()) - 1,
models_[i].partSize());
}
vector<double> posMargins(positives_.size());
#pragma omp parallel for
for (int i = 0; i < positives_.size(); ++i)
posMargins[i] = models_[positives_[i].second].dot(positives_[i].first);
for (int i = 0; i < positives_.size(); ++i) {
if (posMargins[i] < 1.0) {
loss += 1.0 - posMargins[i];
if (g)
gradients[positives_[i].second] -= positives_[i].first;
}
}
// Reweight the positives
if (J_ != 1.0) {
loss *= J_;
if (g) {
for (int i = 0; i < models_.size(); ++i)
gradients[i] *= J_;
}
}
vector<double> negMargins(negatives_.size());
#pragma omp parallel for
for (int i = 0; i < negatives_.size(); ++i)
negMargins[i] = models_[negatives_[i].second].dot(negatives_[i].first);
for (int i = 0; i < negatives_.size(); ++i) {
if (negMargins[i] > -1.0) {
loss += 1.0 + negMargins[i];
if (g)
gradients[negatives_[i].second] += negatives_[i].first;
}
}
// Add the loss and gradient of the regularization term
double maxNorm = 0.0;
int argNorm = 0;
for (int i = 0; i < models_.size(); ++i) {
if (g)
gradients[i] *= C_;
const double norm = models_[i].norm();
if (norm > maxNorm) {
maxNorm = norm;
argNorm = i;
}
}
// Recopy the gradient if needed
if (g) {
// Regularization gradient
gradients[argNorm] += models_[argNorm];
// Regularize the deformation 10 times more
for (int i = 1; i < gradients[argNorm].parts().size(); ++i)
gradients[argNorm].parts()[i].deformation +=
9.0 * models_[argNorm].parts()[i].deformation;
// Do not regularize the bias
gradients[argNorm].bias() -= models_[argNorm].bias();
// In case minimum constraints were applied
for (int i = 0; i < models_.size(); ++i) {
for (int j = 1; j < models_[i].parts().size(); ++j) {
if (models_[i].parts()[j].deformation(0) >= -0.005)
gradients[i].parts()[j].deformation(0) =
max(gradients[i].parts()[j].deformation(0), 0.0);
if (models_[i].parts()[j].deformation(2) >= -0.005)
gradients[i].parts()[j].deformation(2) =
max(gradients[i].parts()[j].deformation(2), 0.0);
if (models_[i].parts()[j].deformation(4) >= -0.005)
gradients[i].parts()[j].deformation(4) =
max(gradients[i].parts()[j].deformation(4), 0.0);
}
}
FromModels(gradients, g);
}
return 0.5 * maxNorm * maxNorm + C_ * loss;
}
static void ToModels(const double * x, vector<Model> & models)
{
for (int i = 0, j = 0; i < models.size(); ++i) {
for (int k = 0; k < models[i].parts().size(); ++k) {
const int nbFeatures = static_cast<int>(models[i].parts()[k].filter.size()) *
HOGPyramid::NbFeatures;
copy(x + j, x + j + nbFeatures, models[i].parts()[k].filter.data()->data());
j += nbFeatures;
if (k) {
// Apply minimum constraints
models[i].parts()[k].deformation(0) = min((x + j)[0],-0.005);
models[i].parts()[k].deformation(1) = (x + j)[1];
models[i].parts()[k].deformation(2) = min((x + j)[2],-0.005);
models[i].parts()[k].deformation(3) = (x + j)[3];
models[i].parts()[k].deformation(4) = min((x + j)[4],-0.005);
models[i].parts()[k].deformation(5) = (x + j)[5];
j += 6;
}
}
models[i].bias() = x[j];
++j;
}
}
static void FromModels(const vector<Model> & models, double * x)
{
for (int i = 0, j = 0; i < models.size(); ++i) {
for (int k = 0; k < models[i].parts().size(); ++k) {
const int nbFeatures = static_cast<int>(models[i].parts()[k].filter.size()) *
HOGPyramid::NbFeatures;
copy(models[i].parts()[k].filter.data()->data(),
models[i].parts()[k].filter.data()->data() + nbFeatures, x + j);
j += nbFeatures;
if (k) {
copy(models[i].parts()[k].deformation.data(),
models[i].parts()[k].deformation.data() + 6, x + j);
j += 6;
}
}
x[j] = models[i].bias();
++j;
}
}
private:
vector<Model> & models_;
const vector<pair<Model, int> > & positives_;
const vector<pair<Model, int> > & negatives_;
double C_;
double J_;
int maxIterations_;
};}
}
double Mixture::train(const vector<pair<Model, int> > & positives,
const vector<pair<Model, int> > & negatives, double C, double J,
int maxIterations)
{
detail::Loss loss(models_, positives, negatives, C, J, maxIterations);
LBFGS lbfgs(&loss, 0.001, maxIterations, 20, 20);
// Start from the current models
VectorXd x(loss.dim());
detail::Loss::FromModels(models_, x.data());
const double l = lbfgs(x.data());
detail::Loss::ToModels(x.data(), models_);
return l;
}
void Mixture::convolve(const HOGPyramid & pyramid,
vector<vector<HOGPyramid::Matrix> > & scores,
vector<vector<vector<Model::Positions> > > * positions) const
{
if (empty() || pyramid.empty()) {
scores.clear();
if (positions)
positions->clear();
return;
}
const int nbModels = static_cast<int>(models_.size());
// Resize the scores and positions
scores.resize(nbModels);
if (positions)
positions->resize(nbModels);
// Transform the filters if needed
#ifndef FFLD_MIXTURE_STANDARD_CONVOLUTION
#pragma omp critical
if (!cached_)
cacheFilters();
while (!cached_);
// Create a patchwork
const Patchwork patchwork(pyramid);
// Convolve the patchwork with the filters
vector<vector<HOGPyramid::Matrix> > convolutions(filterCache_.size());
patchwork.convolve(filterCache_, convolutions);
// In case of error
if (convolutions.empty()) {
scores.clear();
if (positions)
positions->clear();
return;
}
// Save the offsets of each model in the filter list
vector<int> offsets(nbModels);
for (int i = 0, j = 0; i < nbModels; ++i) {
offsets[i] = j;
j += models_[i].parts().size();
}
// For each model
#pragma omp parallel for
for (int i = 0; i < nbModels; ++i) {
vector<vector<HOGPyramid::Matrix> > tmp(models_[i].parts().size());
for (int j = 0; j < tmp.size(); ++j)
tmp[j].swap(convolutions[offsets[i] + j]);
models_[i].convolve(pyramid, scores[i], positions ? &(*positions)[i] : 0, &tmp);
}
// In case of error
for (int i = 0; i < nbModels; ++i) {
if (scores[i].empty()) {
scores.clear();
if (positions)
positions->clear();
}
}
#else
#pragma omp parallel for
for (int i = 0; i < nbModels; ++i)
models_[i].convolve(pyramid, scores[i], positions ? &(*positions)[i] : 0);
#endif
}
vector<pair<int, int> > Mixture::FilterSizes(int nbComponents, const vector<Scene> & scenes,
Object::Name name)
{
const string Names[80] =
{
"airplane", "apple", "backpack", "banana", "baseball bat",
"baseball glove", "bear", "bed", "bench", "bicycle", "bird",
"boat", "book", "bottle", "bowl", "broccoli", "bus", "cake",
"car", "carrot", "cat", "cell phone", "chair", "clock", "couch",
"cow", "cup", "dining table", "dog", "donut", "elephant",
"fire hydrant", "fork", "frisbee", "giraffe", "hair drier",
"handbag", "horse", "hot_dog", "keyboard", "kite", "knife",
"laptop", "microwave", "motorcycle", "mouse", "orange",
"oven", "parking meter", "person", "pizza", "potted plant",
"refrigerator", "remote", "sandwich", "scissors", "sheep",
"sink", "skateboard", "skis", "snowboard", "spoon", "sports ball",
"stop sign", "suitcase", "surfboard", "teddy bear", "tennis racket",
"tie", "toaster", "toilet", "toothbrush", "traffic light", "train",
"truck", "tv", "umbrella", "vase", "wine", "zebra"
};
// Early return in case the filters or the dataset are empty
if ((nbComponents <= 0) || scenes.empty())
return vector<pair<int, int> >();
// Sort the aspect ratio of all the (non difficult) samples
vector<double> ratios;
for (int i = 0; i < scenes.size(); ++i) {
for (int j = 0; j < scenes[i].objects().size(); ++j) {
const Object & obj = scenes[i].objects()[j];
if ((obj.name() == name) && !obj.difficult()){
double width = obj.bndbox().width();
double height = obj.bndbox().height();
if (width > 0 && height > 0)
ratios.push_back(static_cast<double>(width/ height));
}
}
}
// Early return if there is no object
if (ratios.empty())
return vector<pair<int, int> >();
// Sort the aspect ratio of all the samples
sort(ratios.begin(), ratios.end());
// For each mixture model
vector<double> references(nbComponents);