forked from fanxu/ffld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.cpp
366 lines (295 loc) · 9.85 KB
/
Model.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
//--------------------------------------------------------------------------------------------------
// 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 "Model.h"
#include <algorithm>
#include <cmath>
using namespace Eigen;
using namespace FFLD;
using namespace std;
Model::Model() : parts_(1), bias_(0.0)
{
parts_[0].offset.setZero();
parts_[0].deformation.setZero();
}
bool Model::empty() const
{
return !parts_[0].filter.size() && !nbParts();
}
pair<int, int> Model::rootSize() const
{
return pair<int, int>(parts_[0].filter.rows(), parts_[0].filter.cols());
}
int Model::nbParts() const
{
return parts_.size() - 1;
}
pair<int, int> Model::partSize() const
{
if (nbParts())
return pair<int, int>(parts_[1].filter.rows(), parts_[1].filter.cols());
else
return pair<int, int>(0, 0);
}
Model::Scalar Model::bias() const
{
return bias_;
}
void Model::convolve(const HOGPyramid & pyramid, vector<HOGPyramid::Matrix> & scores,
vector<vector<Positions> > * positions) const
{
// Invalid parameters
if (empty() || pyramid.empty())
return;
// The only necessary constant
const int nbFilters = parts_.size();
// Convolve the pyramid with all the filters
vector<vector<HOGPyramid::Matrix> > convolutions(nbFilters);
int i;
#pragma omp parallel for private(i)
for (i = 0; i < nbFilters; ++i)
pyramid.convolve(parts_[i].filter, convolutions[i]);
convolve(pyramid, convolutions, scores, positions);
}
Model Model::flip() const
{
Model model;
if (!empty()) {
model.parts_.resize(parts_.size());
// Flip the root
model.parts_[0].filter = HOGPyramid::Flip(parts_[0].filter);
model.parts_[0].offset = parts_[0].offset;
model.parts_[0].deformation = parts_[0].deformation;
// Flip the parts
for (int i = 1; i < parts_.size(); ++i) {
model.parts_[i].filter = HOGPyramid::Flip(parts_[i].filter);
model.parts_[i].offset(0) = parts_[0].filter.cols() * 2 - parts_[i].filter.cols() -
parts_[i].offset(0);
model.parts_[i].offset(1) = parts_[i].offset(1);
model.parts_[i].deformation = parts_[i].deformation;
model.parts_[i].deformation(1) = -model.parts_[i].deformation(1);
}
}
model.bias_ = bias_;
return model;
}
void Model::convolve(const HOGPyramid & pyramid, vector<vector<HOGPyramid::Matrix> > & convolutions,
vector<HOGPyramid::Matrix> & scores,
vector<vector<Positions> > * positions) const
{
// Invalid parameters
if (empty() || pyramid.empty() || (convolutions.size() != parts_.size()))
return;
// All the constants relative to the model and the pyramid
const int nbFilters = parts_.size();
const int nbParts = nbFilters - 1;
const int padx = pyramid.padx();
const int pady = pyramid.pady();
const int interval = pyramid.interval();
const int nbLevels = pyramid.levels().size();
// Resize the positions
if (positions && nbParts) {
positions->resize(nbParts);
for (int i = 0; i < nbParts; ++i)
(*positions)[i].resize(nbLevels);
}
// Temporary data needed by the distance transforms
vector<Scalar> tmp(pyramid.levels()[0].size());
// For each part
for (int i = 0; i < nbParts; ++i) {
// For each part level (the root is interval higher in the pyramid)
for (int j = 0; j < nbLevels - interval; ++j) {
DT2D(convolutions[i + 1][j], parts_[i + 1], &tmp[0],
positions ? &(*positions)[i][j] : 0);
// Add the distance transforms of the part one octave below
for (int y = 0; y < convolutions[0][j + interval].rows(); ++y) {
for (int x = 0; x < convolutions[0][j + interval].cols(); ++x) {
// The position of the root one octave below
const int x2 = x * 2 - padx;
const int y2 = y * 2 - pady;
// Nearest-neighbor interpolation
if ((x2 >= 0) && (y2 >= 0) && (x2 < convolutions[i + 1][j].cols()) &&
(y2 < convolutions[i + 1][j].rows()))
convolutions[0][j + interval](y, x) += convolutions[i + 1][j](y2, x2);
else
convolutions[0][j + interval](y, x) =-numeric_limits<Scalar>::infinity();
}
}
}
}
scores.swap(convolutions[0]);
// Add the bias if necessary
if (bias_) {
int i;
#pragma omp parallel for private(i)
for (i = 0; i < nbLevels; ++i)
scores[i].array() += bias_;
}
}
void Model::DT1D(const Scalar * x, int n, Scalar a, Scalar b, Scalar * z, int * v, Scalar * y,
int * m, int offset, const Scalar * t, int incx, int incy, int incm)
{
// Early return in case any of the parameters is invalid
if (!x || (n <= 0) || (a >= 0) || !z || !v || !y || !incx || !incy || !incm)
return;
z[0] =-numeric_limits<Scalar>::infinity();
z[1] = numeric_limits<Scalar>::infinity();
v[0] = 0;
Scalar xvk = x[0];
// Same version of the algorithm except that the first version uses a lookup table to replace
// the division by (a * (i - v[k]))
if (t) {
int k = 0;
for (int i = 1; i < n;) {
const Scalar s = (x[i * incx] - xvk) * t[i - v[k]] + (i + v[k]) - b / a;
if (s <= z[k]) {
--k;
xvk = x[v[k] * incx];
}
else {
++k;
v[k] = i;
z[k] = s;
xvk = x[i * incx];
++i;
}
}
z[k + 1] = numeric_limits<Scalar>::infinity();
}
else {
int k = 0;
for (int i = 1; i < n;) {
const Scalar s = (x[i * incx] - xvk) / (a * (i - v[k])) + (i + v[k]) - b / a;
if (s <= z[k]) {
--k;
xvk = x[v[k] * incx];
}
else {
++k;
v[k] = i;
z[k] = s;
xvk = x[i * incx];
++i;
}
}
z[k + 1] = numeric_limits<Scalar>::infinity();
}
for (int i = 0, k = 0; i < n; ++i) {
while (z[k + 1] < (i + offset) * 2)
++k;
y[i * incy] = (a * (i + offset - v[k]) + b) * (i + offset - v[k]) + x[v[k] * incx];
if (m)
m[i * incm] = v[k];
}
}
void Model::DT2D(HOGPyramid::Matrix & matrix, const Model::Part & part, Scalar * tmp,
Positions * positions)
{
// Nothing to do if the matrix is empty
if (!matrix.size())
return;
const int rows = matrix.rows();
const int cols = matrix.cols();
if (positions)
positions->resize(rows, cols);
// Temporary vectors
vector<Scalar> z(max(rows, cols) + 1);
vector<int> v(max(rows, cols) + 1);
vector<Scalar> t(max(rows, cols));
t[0] = numeric_limits<Scalar>::infinity();
for (int x = 1; x < cols; ++x)
t[x] = 1 / (part.deformation(0) * x);
// Filter the rows in tmp
for (int y = 0; y < rows; ++y)
DT1D(matrix.row(y).data(), cols, part.deformation(0), part.deformation(1), &z[0], &v[0],
tmp + y * cols, positions ? positions->row(y).data()->data() : 0, part.offset(0),
&t[0], 1, 1, 2);
for (int y = 1; y < rows; ++y)
t[y] = 1 / (part.deformation(2) * y);
// Filter the columns back to the original matrix
for (int x = 0; x < cols; x += 2)
DT1D(tmp + x, rows, part.deformation(2), part.deformation(3), &z[0], &v[0],
matrix.data() + x, positions ? ((positions->data() + x)->data() + 1) : 0,
part.offset(1), &t[0], cols, cols, cols * 2);
// Re-index the best x positions now that the best y changed
if (positions) {
Map<MatrixXi> tmp2(reinterpret_cast<int *>(tmp), rows, cols);
for (int y = 0; y < rows; ++y)
for (int x = 0; x < cols; x += 2)
tmp2(y, x) = (*positions)(y, x)(0);
for (int y = 0; y < rows; ++y)
for (int x = 0; x < cols; x += 2)
(*positions)(y, x)(0) = tmp2((*positions)(y, x)(1), x);
}
}
ostream & FFLD::operator<<(ostream & os, const Model & model)
{
// Save the number of parts and the bias
os << model.parts_.size() << ' ' << model.bias_ << endl;
// Save the parts themselves
for (int i = 0; i < model.parts_.size(); ++i) {
os << model.parts_[i].filter.rows() << ' ' << model.parts_[i].filter.cols() << ' '
<< HOGPyramid::NbFeatures << ' ' << model.parts_[i].offset(0) << ' '
<< model.parts_[i].offset(1) << ' ' << model.parts_[i].deformation(0) << ' '
<< model.parts_[i].deformation(1) << ' ' << model.parts_[i].deformation(2) << ' '
<< model.parts_[i].deformation(3) << endl;
for (int y = 0; y < model.parts_[i].filter.rows(); ++y) {
for (int x = 0; x < model.parts_[i].filter.cols(); ++x)
for (int j = 0; j < HOGPyramid::NbFeatures; ++j)
os << model.parts_[i].filter(y, x)(j) << ' ';
os << endl;
}
}
return os;
}
istream & FFLD::operator>>(istream & is, Model & model)
{
int nbParts;
Model::Scalar bias;
is >> nbParts >> bias;
if (!is) {
model = Model();
return is;
}
model.parts_.resize(nbParts);
model.bias_ = bias;
for (int i = 0; i < nbParts; ++i) {
int rows, cols, nbFeatures;
is >> rows >> cols >> nbFeatures >> model.parts_[i].offset(0) >> model.parts_[i].offset(1)
>> model.parts_[i].deformation(0) >> model.parts_[i].deformation(1)
>> model.parts_[i].deformation(2) >> model.parts_[i].deformation(3);
// Always set the deformation of the root to zero
if (!i)
model.parts_[0].deformation.setZero();
model.parts_[i].filter.resize(rows, cols);
for (int y = 0; y < rows; ++y) {
for (int x = 0; x < cols; ++x) {
for (int j = 0; j < nbFeatures; ++j) {
Model::Scalar f;
is >> f;
if (j < HOGPyramid::NbFeatures)
model.parts_[i].filter(y, x)(j) = f;
}
}
}
}
if (!is)
model = Model();
return is;
}