-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtiny_solver.h
More file actions
594 lines (514 loc) · 20 KB
/
tiny_solver.h
File metadata and controls
594 lines (514 loc) · 20 KB
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
// Ceres Solver - A fast non-linear least squares minimizer
// Copyright 2017 Google Inc. All rights reserved.
// http://ceres-solver.org/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Author: mierle@gmail.com (Keir Mierle)
//
// WARNING WARNING WARNING
// WARNING WARNING WARNING Tiny solver is experimental and will change.
// WARNING WARNING WARNING
//
// A tiny least squares solver using Levenberg-Marquardt, intended for solving
// small dense problems with low latency and low overhead. The implementation
// takes care to do all allocation up front, so that no memory is allocated
// during solving. This is especially useful when solving many similar problems;
// for example, inverse pixel distortion for every pixel on a grid.
//
// Note: This code has no dependencies beyond Eigen, including on other parts of
// Ceres, so it is possible to take this file alone and put it in another
// project without the rest of Ceres.
//
// Algorithm based off of:
//
// [1] K. Madsen, H. Nielsen, O. Tingleoff.
// Methods for Non-linear Least Squares Problems.
// http://www2.imm.dtu.dk/pubdb/views/edoc_download.php/3215/pdf/imm3215.pdf
#pragma once
#include <cassert>
#include <cmath>
#include <Eigen/Cholesky> //for default LLDT
#include <type_traits>
namespace ts {
// To use tiny solver, create a class or struct that allows computing the cost
// function (described below). This is similar to a ceres::CostFunction, but is
// different to enable statically allocating all memory for the solver
// (specifically, enum sizes). Key parts are the Scalar typedef, the enums to
// describe problem sizes (needed to remove all heap allocations), and the
// operator() overload to evaluate the cost and (optionally) jacobians.
//
// struct TinySolverCostFunctionTraits {
// typedef double Scalar;
// enum {
// NUM_RESIDUALS = <int> OR Eigen::Dynamic,
// NUM_PARAMETERS = <int> OR Eigen::Dynamic,
// };
// bool operator()(const double* parameters,
// double* residuals,
// double* jacobian) const;
//
// int NumResiduals() const; -- Needed if NUM_RESIDUALS == Eigen::Dynamic.
// int NumParameters() const; -- Needed if NUM_PARAMETERS == Eigen::Dynamic.
// };
//
// For operator(), the size of the objects is:
//
// double* parameters -- NUM_PARAMETERS or NumParameters()
// double* residuals -- NUM_RESIDUALS or NumResiduals()
// double* jacobian -- NUM_RESIDUALS * NUM_PARAMETERS in column-major format
// (Eigen's default); or NULL if no jacobian requested.
//
// An example (fully statically sized):
//
// struct MyCostFunctionExample {
// typedef double Scalar;
// enum {
// NUM_RESIDUALS = 2,
// NUM_PARAMETERS = 3,
// };
// bool operator()(const double* parameters,
// double* residuals,
// double* jacobian) const {
// residuals[0] = x + 2*y + 4*z;
// residuals[1] = y * z;
// if (jacobian) {
// jacobian[0 * 2 + 0] = 1; // First column (x).
// jacobian[0 * 2 + 1] = 0;
//
// jacobian[1 * 2 + 0] = 2; // Second column (y).
// jacobian[1 * 2 + 1] = z;
//
// jacobian[2 * 2 + 0] = 4; // Third column (z).
// jacobian[2 * 2 + 1] = y;
// }
// return true;
// }
// };
//
// The solver supports either statically or dynamically sized cost
// functions. If the number of residuals is dynamic then the CostFunction
// must define:
//
// int NumResiduals() const;
//
// If the number of parameters is dynamic then the CostFunction must
// define:
//
// int NumParameters() const;
//
// Custom parameterizations:
//
// In order to support operations such as unit quaternion updates you can
// override the update parameterization by defining your own function
//
// template<typename Scalar>
// struct CustomExtraScalingParameterization {
// void operator()(
// const Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> x_prev,
// const Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> dx,
// Eigen::Ref<Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> x_new)
// {
// x_new = x_prev + 3*dx;
// }
// };
//
// To change just change the template parameter ParameterizationFunction.
// By default it is set to the normal addition operation
// In addition to the standard cost function above this version supports
// directly building the gradient(j*r) and the hessian(jtj).
// Some advanced usages this allows for:
// 1) Custom accumulator classes to build Hessian matrix a lot faster( see the
// dense visual slam projects from TUM)
// 2) Custom scaling of certain parts of the hessian matrix
//
// struct MyCostFunctionExampleHessian {
// typedef double Scalar;
// enum {
// NUM_RESIDUALS = 2,
// NUM_PARAMETERS = 3,
// };
// bool operator()(const double* parameters,
// double* residuals,
// double* gradient,
// double* hessian) const {
// residuals[0] = x + 2*y + 4*z;
// residuals[1] = y * z;
//
// if (gradient && hessian) {
//
// Eigen::Matrix<Scalar, NUM_RESIDUALS, NUM_PARAMETERS> jac;
// //compute Jacobian
// Eigen::Map<Eigen::Matrix<Scalar, NUM_RESIDUALS, 1>> error(residuals);
// Eigen::Map<Eigen::Matrix<Scalar, NUM_PARAMETERS, 1>> grad(gradient);
// Eigen::Map< Eigen::Matrix<Scalar, NUM_PARAMETERS, NUM_PARAMETERS>>
// jtj(hessian);
// jtj=jac.transpose()*jac;
// grad=jac.transpose()*-error;
//
// }
// return true;
// }
// };
// The standard way to add the delta updates to the parameters. Through
// simple addition. x_new= x_prev+dx
template<typename Scalar>
struct DefaultAdditionParameterization {
void operator()(
const Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> x_prev,
const Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> dx,
Eigen::Ref<Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> x_new) const {
x_new = x_prev + dx;
}
};
enum MinimizerMethod {
LM, //levenberg marquadt
DOGLEG, //powell dogleg
GAUSSNEWTON
};
template<typename CostFunction,
typename LinearSolver = Eigen::LDLT<
Eigen::Matrix<typename CostFunction::Scalar,
CostFunction::NUM_PARAMETERS,
CostFunction::NUM_PARAMETERS> >,
typename ParameterizationFunction =
DefaultAdditionParameterization<typename CostFunction::Scalar> >
class TinySolver {
public:
// This class needs to have an Eigen aligned operator new as it contains
// fixed-size Eigen types.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
enum {
NUM_RESIDUALS = CostFunction::NUM_RESIDUALS,
NUM_PARAMETERS = CostFunction::NUM_PARAMETERS
};
typedef typename CostFunction::Scalar Scalar;
typedef typename Eigen::Matrix<Scalar, NUM_PARAMETERS, 1> Parameters;
enum Status {
GRADIENT_TOO_SMALL, // eps > max(J'*f(x))
RELATIVE_STEP_SIZE_TOO_SMALL, // eps > ||dx|| / (||x|| + eps)
COST_TOO_SMALL, // eps > ||f(x)||^2 / 2
HIT_MAX_ITERATIONS,
COST_FUNCTION_FAIL,
COST_INCREASED
};
struct Options {
Scalar gradient_tolerance = 1e-10; // eps > max(J'*f(x))
Scalar parameter_tolerance = 1e-8; // eps > ||dx|| / ||x||
Scalar cost_threshold = // eps > ||f(x)||
std::numeric_limits<Scalar>::epsilon();
Scalar initial_trust_region_radius = 1e4;
int max_num_iterations = 50;
MinimizerMethod minimizer;
};
struct Summary {
Scalar initial_cost = -1; // 1/2 ||f(x)||^2
Scalar final_cost = -1; // 1/2 ||f(x)||^2
Scalar gradient_max_norm = -1; // max(J'f(x))
int iterations = -1;
Status status = HIT_MAX_ITERATIONS;
};
bool Update(CostFunction &function, const Parameters &x,
bool only_compute_cost = false) {
//Call either the standard jacobian+ error cost function and build
// jtj and g_ , or call the hessian version
return UpdateCostFunction(function, x, only_compute_cost);
}
const Summary &Solve(CostFunction &function,
Parameters *x_and_min,
const ParameterizationFunction ¶meterization_function
= ParameterizationFunction()) {
Initialize<NUM_RESIDUALS, NUM_PARAMETERS>(function);
assert(x_and_min);
Parameters &x = *x_and_min;
summary = Summary();
summary.iterations = 0;
// TODO(sameeragarwal): Deal with failure here.
bool suc = Update(function, x);
if (!suc) {
summary.status = COST_FUNCTION_FAIL;
return summary;
}
summary.initial_cost = cost_;
summary.final_cost = cost_;
Scalar prev_cost = cost_;
if (summary.gradient_max_norm < options.gradient_tolerance) {
summary.status = GRADIENT_TOO_SMALL;
return summary;
}
if (cost_ < options.cost_threshold) {
summary.status = COST_TOO_SMALL;
return summary;
}
switch (options.minimizer) {
case GAUSSNEWTON: {
for (summary.iterations = 1;
summary.iterations < options.max_num_iterations;
summary.iterations++) {
linear_solver_.compute(jtj_);
dx_ = linear_solver_.solve(g_);
const Scalar parameter_tolerance =
options.parameter_tolerance *
(x.norm() + options.parameter_tolerance);
if (dx_.norm() < parameter_tolerance) {
summary.status = RELATIVE_STEP_SIZE_TOO_SMALL;
break;
}
//By default just does x_new_ = x + dx_;
parameterization_function(x, dx_, x_new_);
suc = Update(function, x_new_);
if (!suc) {
summary.status = COST_FUNCTION_FAIL;
break;
}
//Update made it worse so stop iterations
if (cost_ > prev_cost) {
summary.status = COST_INCREASED;
cost_ = prev_cost; //ensures final cost is last good one
break;
}
prev_cost = cost_;
x = x_new_;
if (summary.gradient_max_norm < options.gradient_tolerance) {
summary.status = GRADIENT_TOO_SMALL;
break;
}
if (cost_ < options.cost_threshold) {
summary.status = COST_TOO_SMALL;
break;
}
}
break;
}
case DOGLEG: {
Scalar delta_k = 2.0; // trust region radius
const Scalar delta_max = 8.0; // max trust region radius
const Scalar eta = 0.125; // min reduction ratio allowed (0<eta<0.25)
break;
}
default: //default is Levengberg Marquadt
case LM: {
Scalar u = 1.0 / options.initial_trust_region_radius;
Scalar v = 2;
for (summary.iterations = 1;
summary.iterations < options.max_num_iterations;
summary.iterations++) {
jtj_regularized_ = jtj_;
const Scalar min_diagonal = 1e-6;
const Scalar max_diagonal = 1e32;
for (int i = 0; i < lm_diagonal_.rows(); ++i) {
lm_diagonal_[i] = std::sqrt(
u * std::min(std::max(jtj_(i, i), min_diagonal), max_diagonal));
jtj_regularized_(i, i) += lm_diagonal_[i] * lm_diagonal_[i];
}
// TODO(sameeragarwal): Check for failure and deal with it.
linear_solver_.compute(jtj_regularized_);
lm_step_ = linear_solver_.solve(g_);
dx_ = jacobi_scaling_.asDiagonal() * lm_step_;
// Adding parameter_tolerance to x.norm() ensures that this
// works if x is near zero.
const Scalar parameter_tolerance =
options.parameter_tolerance *
(x.norm() + options.parameter_tolerance);
if (dx_.norm() < parameter_tolerance) {
summary.status = RELATIVE_STEP_SIZE_TOO_SMALL;
break;
}
//By default just does x_new_ = x + dx_;
parameterization_function(x, dx_, x_new_);
// Compute costs with new parameters
suc = Update(function, x_new_, true);
if (!suc) {
summary.status = COST_FUNCTION_FAIL;
break;
}
const Scalar cost_change = (2 * cost_ - f_x_new_.squaredNorm());
// TODO(sameeragarwal): Better more numerically stable evaluation.
const Scalar
model_cost_change = lm_step_.dot(2 * g_ - jtj_ * lm_step_);
// rho is the ratio of the actual reduction in error to the reduction
// in error that would be obtained if the problem was linear. See [1]
// for details.
Scalar rho(cost_change / model_cost_change);
if (rho > 0) {
// Accept the Levenberg-Marquardt step because the linear
// model fits well.
x = x_new_;
suc = Update(function, x);
if (!suc) {
summary.status = COST_FUNCTION_FAIL;
break;
}
if (summary.gradient_max_norm < options.gradient_tolerance) {
summary.status = GRADIENT_TOO_SMALL;
break;
}
if (cost_ < options.cost_threshold) {
summary.status = COST_TOO_SMALL;
break;
}
Scalar tmp = Scalar(2 * rho - 1);
u = u * std::max(1 / 3., 1 - tmp * tmp * tmp);
v = 2;
continue;
}
// Reject the update because either the normal equations failed to
// solve or the local linear model was not good (rho < 0). Instead,
// increase u to move closer to gradient descent.
u *= v;
v *= 2;
}
break;//end of LM
}
} //end of switch
summary.final_cost = cost_;
return summary;
}
Options options;
Summary summary;
private:
// Preallocate everything, including temporary storage needed for solving the
// linear system. This allows reusing the intermediate storage across solves.
LinearSolver linear_solver_;
Scalar cost_;
Parameters dx_, x_new_, g_, jacobi_scaling_, lm_diagonal_, lm_step_;
Eigen::Matrix<Scalar, NUM_RESIDUALS, 1> error_, f_x_new_, weights_;
Eigen::Matrix<Scalar, NUM_RESIDUALS, NUM_PARAMETERS> jacobian_;
Eigen::Matrix<Scalar, NUM_PARAMETERS, NUM_PARAMETERS> jtj_, jtj_regularized_;
// The following definitions are needed for template metaprogramming.
template<bool Condition, typename T>
struct enable_if;
template<typename T>
struct enable_if<true, T> {
typedef T type;
};
//Template magic to deal with two types of cost functions
// 1) is the standard ceres cost function computing residuals and
// and the jacobians.
// 2) You compute the hessian and gradient directly.
template<typename TFunc>
typename std::enable_if<std::is_same<typename std::result_of<
TFunc(const double *parameters,
double *residuals,
double *jacobian)>::type, bool>::value, bool>::type
UpdateCostFunction(TFunc &func, const Parameters &x, bool only_cost) {
if (only_cost) {
return func(x.data(), f_x_new_.data(), NULL);
}
if (!func(x.data(), error_.data(), jacobian_.data())) {
return false;
}
error_ = -error_;
// On the first iteration, compute a diagonal (Jacobi) scaling
// matrix, which we store as a vector.
if (summary.iterations == 0) {
// jacobi_scaling = 1 / (1 + diagonal(J'J))
//
// 1 is added to the denominator to regularize small diagonal
// entries.
jacobi_scaling_ = 1.0 / (1.0 + jacobian_.colwise().norm().array());
jacobi_scaling_.setConstant(1.0);
}
// This explicitly computes the normal equations, which is numerically
// unstable. Nevertheless, it is often good enough and is fast.
//
// TODO(sameeragarwal): Refactor this to allow for DenseQR
// factorization.
//jacobian_ = jacobian_ * jacobi_scaling_.asDiagonal();
jtj_ = jacobian_.transpose() * jacobian_;
g_ = jacobian_.transpose() * error_;
summary.gradient_max_norm = g_.array().abs().maxCoeff();
cost_ = error_.squaredNorm() / 2;
return true;
}
template<typename TFunc>
typename std::enable_if<std::is_same<typename std::result_of<
TFunc(const double *parameters,
double *residuals,
double *gradient,
double *hessian)>::type, bool>::value, bool>::type
UpdateCostFunction(TFunc &func, const Parameters &x, bool only_cost) {
//Only compute the cost(error) with given parameter
if (only_cost) {
return func(x.data(), f_x_new_.data(), NULL, NULL);
}
//this keeps the end user safe from accidentally not resetting the matrices
jtj_.setZero();
g_.setZero();
//Call the cost function that automatically fills the hessian JtJ and the
// gradient g_
if (!func(x.data(), error_.data(), g_.data(), jtj_.data())) {
return false;
}
if (summary.iterations == 0) {
//no scaling. End user is responsible for making sure the hessian
//is properly conditioned
jacobi_scaling_.setConstant(1.0);
}
error_ = -error_;
summary.gradient_max_norm = g_.array().abs().maxCoeff();
cost_ = error_.squaredNorm() / 2;
return true;
}
// The number of parameters and residuals are dynamically sized.
template<int R, int P>
typename enable_if<(R == Eigen::Dynamic && P == Eigen::Dynamic), void>::type
Initialize(const CostFunction &function) {
Initialize(function.NumResiduals(), function.NumParameters());
}
// The number of parameters is dynamically sized and the number of
// residuals is statically sized.
template<int R, int P>
typename enable_if<(R == Eigen::Dynamic && P != Eigen::Dynamic), void>::type
Initialize(const CostFunction &function) {
Initialize(function.NumResiduals(), P);
}
// The number of parameters is statically sized and the number of
// residuals is dynamically sized.
template<int R, int P>
typename enable_if<(R != Eigen::Dynamic && P == Eigen::Dynamic), void>::type
Initialize(const CostFunction &function) {
Initialize(R, function.NumParameters());
}
// The number of parameters and residuals are statically sized.
template<int R, int P>
typename enable_if<(R != Eigen::Dynamic && P != Eigen::Dynamic), void>::type
Initialize(const CostFunction & /* function */) {}
void Initialize(int num_residuals, int num_parameters) {
dx_.resize(num_parameters);
x_new_.resize(num_parameters);
g_.resize(num_parameters);
jacobi_scaling_.resize(num_parameters);
lm_diagonal_.resize(num_parameters);
lm_step_.resize(num_parameters);
error_.resize(num_residuals);
f_x_new_.resize(num_residuals);
weights_.resize(num_residuals);
jacobian_.resize(num_residuals, num_parameters);
jtj_.resize(num_parameters, num_parameters);
jtj_regularized_.resize(num_parameters, num_parameters);
}
};
} // namespace ts