-
Notifications
You must be signed in to change notification settings - Fork 11
/
Mixture.cpp
262 lines (200 loc) · 6.03 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
//--------------------------------------------------------------------------------------------------
// 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 "Intersector.h"
#include "Mixture.h"
#include <algorithm>
#include <fstream>
using namespace Eigen;
using namespace FFLD;
using namespace std;
Mixture::Mixture() : cached_(false)
{
}
Mixture::Mixture(const vector<Model> & models) : models_(models), cached_(false)
{
}
bool Mixture::empty() const
{
return models_.empty();
}
const vector<Model> & Mixture::models() const
{
return models_;
}
pair<int, int> Mixture::minSize() const
{
pair<int, int> size(0, 0);
if (!models_.empty()) {
size = models_[0].rootSize();
for (unsigned 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 (unsigned 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;
}
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 = models_.size();
const int nbLevels = pyramid.levels().size();
// Convolve with all the models
vector<vector<HOGPyramid::Matrix> > tmp(nbModels);
convolve(pyramid, tmp, positions);
// In case of error
if (tmp.empty()) {
scores.clear();
argmaxes.clear();
if (positions)
positions->clear();
return;
}
// Resize the scores and argmaxes
scores.resize(nbLevels);
argmaxes.resize(nbLevels);
int i;
#pragma omp parallel for private(i)
for (i = 0; i < nbLevels; ++i) {
scores[i].resize(pyramid.levels()[i].rows() - maxSize().first + 1,
pyramid.levels()[i].cols() - maxSize().second + 1);
argmaxes[i].resize(scores[i].rows(), scores[i].cols());
for (int y = 0; y < scores[i].rows(); ++y) {
for (int x = 0; x < scores[i].cols(); ++x) {
int argmax = 0;
for (int j = 1; j < nbModels; ++j)
if (tmp[j][i](y, x) > tmp[argmax][i](y, x))
argmax = j;
scores[i](y, x) = tmp[argmax][i](y, x);
argmaxes[i](y, x) = argmax;
}
}
}
}
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();
}
const int nbModels = models_.size();
scores.resize(nbModels);
if (positions)
positions->resize(nbModels);
// Transform the filters if needed
#pragma omp critical
if (filterCache_.empty())
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
int i;
#pragma omp parallel for private(i)
for (i = 0; i < nbModels; ++i) {
vector<vector<HOGPyramid::Matrix> > tmp(models_[i].parts_.size());
for (size_t j = 0; j < tmp.size(); ++j)
tmp[j].swap(convolutions[offsets[i] + j]);
models_[i].convolve(pyramid, tmp, scores[i], positions ? &(*positions)[i] : 0);
}
}
void Mixture::cacheFilters() const
{
// Count the number of filters
int nbFilters = 0;
for (size_t i = 0; i < models_.size(); ++i)
nbFilters += models_[i].parts_.size();
// Transform all the filters
filterCache_.resize(nbFilters);
for (size_t i = 0, j = 0; i < models_.size(); ++i) {
int k;
#pragma omp parallel for private(k)
for (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;
}
ostream & FFLD::operator<<(ostream & os, const Mixture & mixture)
{
// Save the number of models (mixture components)
os << mixture.models().size() << endl;
// Save the models themselves
for (unsigned int i = 0; i < mixture.models().size(); ++i)
os << mixture.models()[i] << endl;
return os;
}
istream & FFLD::operator>>(istream & is, Mixture & mixture)
{
int nbModels;
is >> nbModels;
if (!is || (nbModels <= 0)) {
mixture = Mixture();
return is;
}
vector<Model> models(nbModels);
for (int i = 0; i < nbModels; ++i) {
is >> models[i];
if (!is || models[i].empty()) {
mixture = Mixture();
return is;
}
}
mixture = Mixture(models);
return is;
}