forked from fanxu/ffld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffld.cpp
823 lines (669 loc) · 24.4 KB
/
ffld.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
//--------------------------------------------------------------------------------------------------
// Implementation of the paper "Exact Acceleration of Linear Object Detectors", 12th European
// Conference on Computer Vision, 2012.
//
// Copyright (c) 2012 Idiap Research Institute, <http://www.idiap.ch/>
// Written by Charles Dubout <[email protected]>
//
// This file is part of FFLD (the Fast Fourier Linear Detector)
//
// FFLD is free software: you can redistribute it and/or modify it under the terms of the GNU
// General Public License version 3 as published by the Free Software Foundation.
//
// FFLD 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 General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with FFLD. If not, see
// <http://www.gnu.org/licenses/>.
//--------------------------------------------------------------------------------------------------
#include "SimpleOpt.h"
#include "Intersector.h"
#include "Mixture.h"
#include "Scene.h"
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#ifndef _WIN32
#include <sys/time.h>
timeval Start, Stop;
inline void start()
{
gettimeofday(&Start, 0);
}
inline int stop()
{
gettimeofday(&Stop, 0);
timeval duration;
timersub(&Stop, &Start, &duration);
return duration.tv_sec * 1000 + (duration.tv_usec + 500) / 1000;
}
#else
#include <time.h>
#include <windows.h>
ULARGE_INTEGER Start, Stop;
inline void start()
{
GetSystemTimeAsFileTime((FILETIME *)&Start);
}
inline int stop()
{
GetSystemTimeAsFileTime((FILETIME *)&Stop);
Stop.QuadPart -= Start.QuadPart;
return (Stop.QuadPart + 5000) / 10000;
}
#endif
using namespace FFLD;
using namespace std;
struct Detection : public FFLD::Rectangle
{
HOGPyramid::Scalar score;
int l;
int x;
int y;
Detection() : score(0), l(0), x(0), y(0)
{
}
Detection(HOGPyramid::Scalar score, int l, int x, int y, FFLD::Rectangle bndbox) :
FFLD::Rectangle(bndbox), score(score), l(l), x(x), y(y)
{
}
bool operator<(const Detection & detection) const
{
return score > detection.score;
}
};
// SimpleOpt array of valid options
enum
{
OPT_HELP, OPT_MODEL, OPT_NAME, OPT_RESULTS, OPT_IMAGES, OPT_NB_NEG, OPT_PADDING, OPT_INTERVAL,
OPT_THRESHOLD, OPT_OVERLAP
};
CSimpleOpt::SOption SOptions[] =
{
{ OPT_HELP, "-h", SO_NONE },
{ OPT_HELP, "--help", SO_NONE },
{ OPT_MODEL, "-m", SO_REQ_SEP },
{ OPT_MODEL, "--model", SO_REQ_SEP },
{ OPT_NAME, "-n", SO_REQ_SEP },
{ OPT_NAME, "--name", SO_REQ_SEP },
{ OPT_RESULTS, "-r", SO_REQ_SEP },
{ OPT_RESULTS, "--results", SO_REQ_SEP },
{ OPT_IMAGES, "-i", SO_REQ_SEP },
{ OPT_IMAGES, "--images", SO_REQ_SEP },
{ OPT_NB_NEG, "-z", SO_REQ_SEP },
{ OPT_NB_NEG, "--nb-negatives", SO_REQ_SEP },
{ OPT_PADDING, "-p", SO_REQ_SEP },
{ OPT_PADDING, "--padding", SO_REQ_SEP },
{ OPT_INTERVAL, "-e", SO_REQ_SEP },
{ OPT_INTERVAL, "--interval", SO_REQ_SEP },
{ OPT_THRESHOLD, "-t", SO_REQ_SEP },
{ OPT_THRESHOLD, "--threshold", SO_REQ_SEP },
{ OPT_OVERLAP, "-v", SO_REQ_SEP },
{ OPT_OVERLAP, "--overlap", SO_REQ_SEP },
SO_END_OF_OPTIONS
};
void showUsage()
{
cout << "Usage: test [options] image.jpg, or\n test [options] image_set.txt\n\n"
"Options:\n"
" -h,--help Display this information\n"
" -m,--model <file> Read the input model from <file> (default \"model.txt\")\n"
" -n,--name <arg> Name of the object to detect (default \"person\")\n"
" -r,--results <file> Write the detection results to <file> (default none)\n"
" -i,--images <folder> Draw the detections to <folder> (default none)\n"
" -z,--nb-negatives <arg> Maximum number of negative images to consider (default all)\n"
" -p,--padding <arg> Amount of zero padding in HOG cells (default 12)\n"
" -e,--interval <arg> Number of levels per octave in the HOG pyramid (default 10)\n"
" -t,--threshold <arg> Minimum detection threshold (default -10)\n"
" -v,--overlap <arg> Minimum overlap in non maxima suppression (default 0.5)"
<< endl;
}
void draw(JPEGImage & image, const FFLD::Rectangle & rect, uint8_t r, uint8_t g, uint8_t b,
int linewidth)
{
if (image.empty() || rect.empty() || (image.depth() < 3))
return;
const int width = image.width();
const int height = image.height();
const int depth = image.depth();
uint8_t * bits = image.bits();
// Draw 2 horizontal lines
const int top = min(max(rect.top(), 1), height - linewidth - 1);
const int bottom = min(max(rect.bottom(), 1), height - linewidth - 1);
for (int x = max(rect.left() - 1, 0); x <= min(rect.right() + linewidth, width - 1); ++x) {
if ((x != max(rect.left() - 1, 0)) && (x != min(rect.right() + linewidth, width - 1))) {
for (int i = 0; i < linewidth; ++i) {
bits[((top + i) * width + x) * depth ] = r;
bits[((top + i) * width + x) * depth + 1] = g;
bits[((top + i) * width + x) * depth + 2] = b;
bits[((bottom + i) * width + x) * depth ] = r;
bits[((bottom + i) * width + x) * depth + 1] = g;
bits[((bottom + i) * width + x) * depth + 2] = b;
}
}
// Draw a white line below and above the line
if ((bits[((top - 1) * width + x) * depth ] != 255) &&
(bits[((top - 1) * width + x) * depth + 1] != 255) &&
(bits[((top - 1) * width + x) * depth + 2] != 255))
for (int i = 0; i < 3; ++i)
bits[((top - 1) * width + x) * depth + i] = 255;
if ((bits[((top + linewidth) * width + x) * depth ] != 255) &&
(bits[((top + linewidth) * width + x) * depth + 1] != 255) &&
(bits[((top + linewidth) * width + x) * depth + 2] != 255))
for (int i = 0; i < 3; ++i)
bits[((top + linewidth) * width + x) * depth + i] = 255;
if ((bits[((bottom - 1) * width + x) * depth ] != 255) &&
(bits[((bottom - 1) * width + x) * depth + 1] != 255) &&
(bits[((bottom - 1) * width + x) * depth + 2] != 255))
for (int i = 0; i < 3; ++i)
bits[((bottom - 1) * width + x) * depth + i] = 255;
if ((bits[((bottom + linewidth) * width + x) * depth ] != 255) &&
(bits[((bottom + linewidth) * width + x) * depth + 1] != 255) &&
(bits[((bottom + linewidth) * width + x) * depth + 2] != 255))
for (int i = 0; i < 3; ++i)
bits[((bottom + linewidth) * width + x) * depth + i] = 255;
}
// Draw 2 vertical lines
const int left = min(max(rect.left(), 1), width - linewidth - 1);
const int right = min(max(rect.right(), 1), width - linewidth - 1);
for (int y = max(rect.top() - 1, 0); y <= min(rect.bottom() + linewidth, height - 1); ++y) {
if ((y != max(rect.top() - 1, 0)) && (y != min(rect.bottom() + linewidth, height - 1))) {
for (int i = 0; i < linewidth; ++i) {
bits[(y * width + left + i) * depth ] = r;
bits[(y * width + left + i) * depth + 1] = g;
bits[(y * width + left + i) * depth + 2] = b;
bits[(y * width + right + i) * depth ] = r;
bits[(y * width + right + i) * depth + 1] = g;
bits[(y * width + right + i) * depth + 2] = b;
}
}
// Draw a white line left and right the line
if ((bits[(y * width + left - 1) * depth ] != 255) &&
(bits[(y * width + left - 1) * depth + 1] != 255) &&
(bits[(y * width + left - 1) * depth + 2] != 255))
for (int i = 0; i < 3; ++i)
bits[(y * width + left - 1) * depth + i] = 255;
if ((bits[(y * width + left + linewidth) * depth ] != 255) &&
(bits[(y * width + left + linewidth) * depth + 1] != 255) &&
(bits[(y * width + left + linewidth) * depth + 2] != 255))
for (int i = 0; i < 3; ++i)
bits[(y * width + left + linewidth) * depth + i] = 255;
if ((bits[(y * width + right - 1) * depth ] != 255) &&
(bits[(y * width + right - 1) * depth + 1] != 255) &&
(bits[(y * width + right - 1) * depth + 2] != 255))
for (int i = 0; i < 3; ++i)
bits[(y * width + right - 1) * depth + i] = 255;
if ((bits[(y * width + right + linewidth) * depth ] != 255) &&
(bits[(y * width + right + linewidth) * depth + 1] != 255) &&
(bits[(y * width + right + linewidth) * depth + 2] != 255))
for (int i = 0; i < 3; ++i)
bits[(y * width + right + linewidth) * depth + i] = 255;
}
}
void detect(const Mixture & mixture, int width, int height, const HOGPyramid & pyramid,
double threshold, double overlap, const string image, ostream & out,
const string & images, vector<Detection> & detections)
{
// Compute the scores
vector<HOGPyramid::Matrix> scores;
vector<Mixture::Indices> argmaxes;
vector<vector<vector<Model::Positions> > > positions;
if (!images.empty())
mixture.convolve(pyramid, scores, argmaxes, &positions);
else
mixture.convolve(pyramid, scores, argmaxes);
// Cache the size of the models
vector<pair<int, int> > sizes(mixture.models().size());
for (int i = 0; i < sizes.size(); ++i)
sizes[i] = mixture.models()[i].rootSize();
// For each scale
for (int i = pyramid.interval(); i < scores.size(); ++i) {
// Scale = 8 / 2^(1 - i / interval)
const double scale = pow(2.0, static_cast<double>(i) / pyramid.interval() + 2.0);
const int rows = scores[i].rows();
const int cols = scores[i].cols();
for (int y = 0; y < rows; ++y) {
for (int x = 0; x < cols; ++x) {
const float score = scores[i](y, x);
if (score > threshold) {
if (((y == 0) || (x == 0) || (score > scores[i](y - 1, x - 1))) &&
((y == 0) || (score > scores[i](y - 1, x))) &&
((y == 0) || (x == cols - 1) || (score > scores[i](y - 1, x + 1))) &&
((x == 0) || (score > scores[i](y, x - 1))) &&
((x == cols - 1) || (score > scores[i](y, x + 1))) &&
((y == rows - 1) || (x == 0) || (score > scores[i](y + 1, x - 1))) &&
((y == rows - 1) || (score > scores[i](y + 1, x))) &&
((y == rows - 1) || (x == cols - 1) || (score > scores[i](y + 1, x + 1)))) {
FFLD::Rectangle bndbox((x - pyramid.padx()) * scale + 0.5,
(y - pyramid.pady()) * scale + 0.5,
sizes[argmaxes[i](y, x)].second * scale + 0.5,
sizes[argmaxes[i](y, x)].first * scale + 0.5);
// Truncate the object
bndbox.setX(max(bndbox.x(), 0));
bndbox.setY(max(bndbox.y(), 0));
bndbox.setWidth(min(bndbox.width(), width - bndbox.x()));
bndbox.setHeight(min(bndbox.height(), height - bndbox.y()));
if (!bndbox.empty())
detections.push_back(Detection(score, i, x, y, bndbox));
}
}
}
}
}
// Non maxima suppression
sort(detections.begin(), detections.end());
for (int i = 1; i < detections.size(); ++i)
detections.resize(remove_if(detections.begin() + i, detections.end(),
Intersector(detections[i - 1], overlap, true)) -
detections.begin());
// Print the detection
const size_t lastDot = image.find_last_of('.');
string id = image.substr(0, lastDot);
const size_t lastSlash = id.find_last_of("/\\");
if (lastSlash != string::npos)
id = id.substr(lastSlash + 1);
if (out) {
#pragma omp critical
for (int i = 0; i < detections.size(); ++i)
out << id << ' ' << detections[i].score << ' ' << (detections[i].left() + 1) << ' '
<< (detections[i].top() + 1) << ' ' << (detections[i].right() + 1) << ' '
<< (detections[i].bottom() + 1) << endl;
}
if (!images.empty()) {
JPEGImage im(image);
for (int j = 0; j < detections.size(); ++j) {
// The position of the root one octave below
const int argmax = argmaxes[detections[j].l](detections[j].y, detections[j].x);
const int x2 = detections[j].x * 2 - pyramid.padx();
const int y2 = detections[j].y * 2 - pyramid.pady();
const int l = detections[j].l - pyramid.interval();
// Scale = 8 / 2^(1 - j / interval)
const double scale = pow(2.0, static_cast<double>(l) / pyramid.interval() + 2.0);
for (int k = 0; k < positions[argmax].size(); ++k) {
const FFLD::Rectangle bndbox((positions[argmax][k][l](y2, x2)(0) - pyramid.padx()) *
scale + 0.5,
(positions[argmax][k][l](y2, x2)(1) - pyramid.pady()) *
scale + 0.5,
mixture.models()[argmax].partSize().second * scale + 0.5,
mixture.models()[argmax].partSize().second * scale + 0.5);
draw(im, bndbox, 0, 0, 255, 2);
}
// Draw the root last
draw(im, detections[j], 255, 0, 0, 2);
}
im.save(images + '/' + id + ".jpg");
}
}
// Test a mixture model (compute a ROC curve)
int main(int argc, char * argv[])
{
// Default parameters
string model("model.txt");
Object::Name name = Object::PERSON;
string results;
string images;
int nbNegativeScenes = -1;
int padding = 12;
int interval = 10;
double threshold =-10.0;
double overlap = 0.5;
// Parse the parameters
CSimpleOpt args(argc, argv, SOptions);
while (args.Next()) {
if (args.LastError() == SO_SUCCESS) {
if (args.OptionId() == OPT_HELP) {
showUsage();
return 0;
}
else if (args.OptionId() == OPT_MODEL) {
model = args.OptionArg();
}
else if (args.OptionId() == OPT_NAME) {
string arg = args.OptionArg();
transform(arg.begin(), arg.end(), arg.begin(), static_cast<int (*)(int)>(tolower));
const string Names[20] =
{
"aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor"
};
const string * iter = find(Names, Names + 20, arg);
// Error checking
if (iter == Names + 20) {
showUsage();
cerr << "\nInvalid name arg " << args.OptionArg() << endl;
return -1;
}
else {
name = static_cast<Object::Name>(iter - Names);
}
}
else if (args.OptionId() == OPT_RESULTS) {
results = args.OptionArg();
}
else if (args.OptionId() == OPT_IMAGES) {
images = args.OptionArg();
}
else if (args.OptionId() == OPT_NB_NEG) {
nbNegativeScenes = atoi(args.OptionArg());
// Error checking
if (nbNegativeScenes < 0) {
showUsage();
cerr << "\nInvalid nb-negatives arg " << args.OptionArg() << endl;
return -1;
}
}
else if (args.OptionId() == OPT_PADDING) {
padding = atoi(args.OptionArg());
// Error checking
if (padding <= 1) {
showUsage();
cerr << "\nInvalid padding arg " << args.OptionArg() << endl;
return -1;
}
}
else if (args.OptionId() == OPT_INTERVAL) {
interval = atoi(args.OptionArg());
// Error checking
if (interval <= 0) {
showUsage();
cerr << "\nInvalid interval arg " << args.OptionArg() << endl;
return -1;
}
}
else if (args.OptionId() == OPT_THRESHOLD) {
threshold = atof(args.OptionArg());
}
else if (args.OptionId() == OPT_OVERLAP) {
overlap = atof(args.OptionArg());
// Error checking
if ((overlap <= 0.0) || (overlap >= 1.0)) {
showUsage();
cerr << "\nInvalid overlap arg " << args.OptionArg() << endl;
return -1;
}
}
}
else {
showUsage();
cerr << "\nUnknown option " << args.OptionText() << endl;
return -1;
}
}
if (!args.FileCount()) {
showUsage();
cerr << "\nNo image/dataset provided" << endl;
return -1;
}
else if (args.FileCount() > 1) {
showUsage();
cerr << "\nMore than one image/dataset provided" << endl;
return -1;
}
// Try to open the mixture
ifstream in(model.c_str(), ios::binary);
if (!in.is_open()) {
showUsage();
cerr << "\nInvalid model file " << model << endl;
return -1;
}
Mixture mixture;
in >> mixture;
if (mixture.empty()) {
showUsage();
cerr << "\nInvalid model file " << model << endl;
return -1;
}
// The image/dataset
const string file(args.File(0));
const size_t lastDot = file.find_last_of('.');
if ((lastDot == string::npos) ||
((file.substr(lastDot) != ".jpg") && (file.substr(lastDot) != ".txt"))) {
showUsage();
cerr << "\nInvalid file " << file << ", should be .jpg or .txt" << endl;
return -1;
}
// Try to open the results
ofstream out;
if (!results.empty()) {
out.open(results.c_str(), ios::binary);
if (!out.is_open()) {
showUsage();
cerr << "\nInvalid results file " << results << endl;
return -1;
}
}
// Try to load the image
if (file.substr(lastDot) == ".jpg") {
JPEGImage image(file);
if (image.empty()) {
showUsage();
cerr << "\nInvalid image " << file << endl;
return -1;
}
// Compute the HOG features
start();
HOGPyramid pyramid(image, padding, padding, interval);
if (pyramid.empty()) {
showUsage();
cerr << "\nInvalid image " << file << endl;
return -1;
}
cout << "Computed HOG features in " << stop() << " ms" << endl;
// Initialize the Patchwork class
start();
if (!Patchwork::Init((pyramid.levels()[0].rows() - padding + 15) & ~15,
(pyramid.levels()[0].cols() - padding + 15) & ~15)) {
cerr << "\nCould not initialize the Patchwork class" << endl;
return -1;
}
cout << "Initialized FFTW in " << stop() << " ms" << endl;
start();
mixture.cacheFilters();
cout << "Transformed the filters in " << stop() << " ms" << endl;
// Compute the detections
start();
vector<Detection> detections;
detect(mixture, image.width(), image.height(), pyramid, threshold, overlap, file, out,
images, detections);
cout << "Computed the convolutions and distance transforms in " << stop() << " ms" << endl;
}
else { // ".txt"
in.close();
in.open(file.c_str(), ios::binary);
if (!in.is_open()) {
showUsage();
cerr << "\nInvalid image_set file " << args.File(0) << endl;
return -1;
}
// Find the annotations' folder (not sure that will work under Windows)
size_t lastSlash = file.find_last_of("/\\");
string folder;
if (lastSlash != string::npos)
folder = file.substr(0, lastSlash + 1);
folder += "../../Annotations/";
// Load all the scenes
vector<Scene> scenes;
int maxRows = 0;
int maxCols = 0;
while (in) {
string line;
getline(in, line);
// Skip empty lines
if (line.empty() || (line.size() < 3))
continue;
// A positive scene
const bool positive = line.substr(line.size() - 2) == " 1";
if (positive || nbNegativeScenes) {
Scene scene(folder + line.substr(0, line.find(' ')) + ".xml");
if (!scene.empty()) {
scenes.push_back(scene);
maxRows = max(maxRows, (scene.height() + 3) / 4 + padding);
maxCols = max(maxCols, (scene.width() + 3) / 4 + padding);
if (!positive)
--nbNegativeScenes;
}
}
}
// Initialize the Patchwork class
start();
if (!Patchwork::Init((maxRows + 15) & ~15, (maxCols + 15) & ~15)) {
cerr << "\nCould not initialize the Patchwork class" << endl;
return -1;
}
cout << "Initialized FFTW in " << stop() << " ms" << endl;
start();
mixture.cacheFilters();
cout << "Transformed the filters in " << stop() << " ms" << endl << "Testing "
#ifndef _WIN32
// << scenes.size() << " scenes: \0337" << flush;
<< scenes.size() << " scenes:" << flush;
#else
<< scenes.size() << " scenes:" << flush;
#endif
start();
// The score of the detection associated to each object
vector<vector<float> > detected(scenes.size());
// Whether each object is difficult
vector<vector<bool> > difficult(scenes.size());
// The score of the detections associated to no object
vector<float> negatives;
int nbScenes = 0;
// Most of the computations inside are already multi-threaded but the performance is higher
// (~20% on my machine) if the threading is done at the level of the scenes rather than at a
// lower level (pyramid levels/filters)
// The performace measurements reported in the paper were done without this scene level
// threading
int i;
#pragma omp parallel for private(i)
for (i = 0; i < scenes.size(); ++i) {
JPEGImage image(scenes[i].filename());
HOGPyramid pyramid(image, padding, padding, interval);
// Compute the detections
vector<Detection> detections;
detect(mixture, image.width(), image.height(), pyramid, threshold, overlap,
scenes[i].filename(), out, images, detections);
// Consider only objects of the right class
for (int j = 0; j < scenes[i].objects().size(); ++j) {
if (scenes[i].objects()[j].name() == name) {
detected[i].push_back(-numeric_limits<float>::infinity());
difficult[i].push_back(scenes[i].objects()[j].difficult());
}
}
#pragma omp critical
{
for (int j = 0; j < detections.size(); ++j) {
// Find the most overlapped object with the same label
Intersector intersector(detections[j]);
double maxScore = 0.0;
int object = -1;
for (int k = 0, l = 0; k < scenes[i].objects().size(); ++k) {
if (scenes[i].objects()[k].name() == name) {
double score = 0.0;
if (intersector(scenes[i].objects()[k].bndbox(), &score)) {
if (score > maxScore) {
maxScore = score;
object = l;
}
}
++l;
}
}
if (object == -1) {
negatives.push_back(detections[j].score);
detections[j].score = -numeric_limits<float>::infinity();
}
else if (detections[j].score > detected[i][object]) {
// Punish multiple detections
if (detected[i][object] > -numeric_limits<float>::infinity())
negatives.push_back(detected[i][object]);
detected[i][object] = detections[j].score;
}
else if (detected[i][object] > -numeric_limits<float>::infinity()) {
// Punish multiple detections
negatives.push_back(detections[j].score);
}
}
++nbScenes;
#ifndef _WIN32
// cout << "\0338" << fixed << setprecision(1) << (nbScenes * 100.0 / scenes.size())
cout << ' ' << fixed << setprecision(1) << (nbScenes * 100.0 / scenes.size())
#else
cout << ' ' << fixed << setprecision(1) << (nbScenes * 100.0 / scenes.size())
#endif
<< "% (" << stop() << " ms)" << flush;
}
}
#ifndef _WIN32
// cout << "\0338100.0% (" << stop() << " ms)" << endl;
#endif
// The score of the detections associated to objects
vector<float> positives;
int nbTotalPositives = 0;
for (int i = 0; i < scenes.size(); ++i) {
for (int j = 0; j < detected[i].size(); ++j) {
if (!difficult[i][j]) {
if (detected[i][j] > -numeric_limits<float>::infinity())
positives.push_back(detected[i][j]);
++nbTotalPositives;
}
}
}
// Sort the detections in decresing order
sort(positives.begin(), positives.end(), greater<float>());
sort(negatives.begin(), negatives.end(), greater<float>());
positives.push_back(-numeric_limits<float>::infinity());
negatives.push_back(-numeric_limits<float>::infinity());
// Compute the Precision/Recall curve
int nbTruePositives = 0;
int nbFalsePositives = 0;
vector<pair<double, double> > pr;
for (int i = 0, j = 0; (i < positives.size() - 1) && (j < negatives.size() - 1);) {
const float threshold = max(positives[i], negatives[j]);
while (positives[i] >= threshold) {
++nbTruePositives;
++i;
}
while (negatives[j] >= threshold) {
++nbFalsePositives;
++j;
}
const double precision = static_cast<double>(nbTruePositives) /
(nbTruePositives + nbFalsePositives);
const double recall = static_cast<double>(nbTruePositives) / nbTotalPositives;
pr.push_back(make_pair(precision, recall));
}
// Add a point at 0 precision
if (!pr.empty() && pr.back().first)
pr.push_back(make_pair(0.0, pr.back().second));
// Set the precision to the maximum among precision with at least this recall
double previousPrecision = 0.0;
for (int i = pr.size() - 1; i >= 0; --i) {
pr[i].first = max(pr[i].first, previousPrecision);
previousPrecision = pr[i].first;
}
// Compute the average precision
cout << "Precision / Recall curve:" << endl;
double averagePrecision = 0.0;
double previousPrintedPrecision = 2.0;
double previousPrintedRecall = -1.0;
for (int i = 0; i < pr.size(); ++i) {
if (((pr[i].first <= previousPrintedPrecision - 0.01) &&
((i == pr.size() - 1) || (pr[i + 1].second != pr[i].second))) ||
((pr[i].second >= previousPrintedRecall + 0.01) &&
((i == pr.size() - 1) || (pr[i + 1].first != pr[i].first)))) {
cout << setw(5) << fixed << setprecision(3) << pr[i].first << ' '
<< setw(5) << fixed << setprecision(3) << pr[i].second << endl;
previousPrintedPrecision = pr[i].first;
previousPrintedRecall = pr[i].second;
}
if (i)
averagePrecision += (pr[i].first + pr[i - 1].first) * 0.5 *
(pr[i].second - pr[i - 1].second);
}
cout << "Average precision: " << setprecision(4) << averagePrecision << endl;
}
return EXIT_SUCCESS;
}