-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgraph_optimizer.h
554 lines (435 loc) · 14.6 KB
/
graph_optimizer.h
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
/**
* @author Hauke Strasdat
*
* Copyright (C) 2010 Hauke Strasdat
* Imperial College London
*
* grap_optimizer.h is part of RobotVision.
*
* RobotVision is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* RobotVision 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RV_GRAPH_OPTIMIZER_H
#define RV_GRAPH_OPTIMIZER_H
#include <vector>
#include <list>
#include <set>
#include <TooN/se3.h>
#include <TooN/Cholesky.h>
#include <TooN/helpers.h>
#include "sim3.h"
#include "transformations.h"
#include "sparse_cholesky.h"
namespace RobotVision
{
/**
* Compare two monocular SLAM trajctories (SE3) by minimising their
* difference wrt. the ambigious scale factor s as descibed in
*
* > H. Strasdat, J.M.M. Montiel, A.J. Davison:
* "Scale Drift-Aware Large Scale Monocular SLAM",
* Proc. of Robotics: Science and Systems (RSS),
* Zaragoza, Spain, 2010.
* http://www.roboticsproceedings.org/rss06/p10.html <
*/
class SE3CompareModScale
{
public:
SE3CompareModScale()
{
verbose = 0;
}
double optimize(const std::vector<TooN::SE3<> > & trans_vec1,
const std::vector<TooN::SE3<> > & trans_vec2,
double & s,
int num_iter)
{
int num_trans = trans_vec1.size();
double new_s;
TooN::Vector<>residual_vec (num_trans*3);
TooN::Matrix<> J = getJac(trans_vec1, trans_vec2, s);
double chi2 = getResidual(trans_vec1, trans_vec2, s, residual_vec);
if (isnan(chi2))
{
std::cerr << "chi2 is NAN\n";
exit(-1);
}
TooN::Matrix<> A = J.T()*J;
TooN::Vector<> g = -(J.T()*residual_vec);
double nu = 2;
double eps =0.000000000000001;
bool stop = false;
double mu = 0.0000000000000000001;
if(verbose>0)
std::cout << "chi2: " << chi2 << std::endl;
for (int i_g=0; i_g<num_iter; ++i_g){
double rho = 0;
if (verbose>0)
std::cout << "iteration: "
<< i_g << " of "<< num_iter << std::endl;
do
{
TooN::Matrix<> A_mu = A + TooN::Identity(A.num_cols())*mu;
TooN::Cholesky<> Ch(A_mu);
TooN::Vector<> delta = Ch.backsub(g);
if (verbose>0)
std::cout << "mu: " <<mu<< std::endl;
if (verbose>1)
std::cout << "delta: " << delta << std::endl;
new_s = s+delta[0];
double chi2_new = getResidual(trans_vec1,
trans_vec2,
new_s,
residual_vec);
rho = (chi2-chi2_new )/(delta*(mu*delta+g));
if(rho>0)
{
if(verbose>0)
std::cout << "chi2_new: " << chi2_new << std::endl;
s = new_s;
chi2 = chi2_new ;
TooN::Matrix<> J = getJac(trans_vec1, trans_vec2, s);
A = J.T()*J;
g = -(J.T()*residual_vec);
stop = norm_max(g)<=eps;
mu *= std::max(1./3.,1-Po3(2*rho-1));
nu = 2.;
}
else
{
if (verbose>0)
std::cout << "no update: chi2 vs.chi2_new "
<< chi2 << " vs. " << chi2_new << std::endl;
mu *= nu;
nu *= 2.;
stop = (mu>999999999.f);
}
}while(!(rho>0 || stop));
if (stop)
break;
}
return chi2;
}
int verbose;
private:
TooN::Matrix<3,1> singleJac(const TooN::SE3<> & pose1,
const TooN::SE3<> & pose2,
double s)
{
double h = 0.000000000001;
TooN::Matrix<3,1> J
= TooN::Zeros;
TooN::Vector<3> fun = diff(pose1,pose2,s);
J.T()[0] = (diff(pose1,pose2,s+h) -fun)/h ;
return J;
}
TooN::Matrix<> getJac(const std::vector<TooN::SE3<> > & trans_vec1,
const std::vector<TooN::SE3<> > & trans_vec2,
const double & s)
{
uint num_trans = trans_vec1.size();
TooN::Matrix<> J(num_trans*3, 1);
for (uint i=0 ; i<num_trans; ++i)
{
J.slice(i*3,0,3,1) = singleJac(trans_vec1[i], trans_vec2[i], s);
}
return J;
}
TooN::Vector<3> diff(const TooN::SE3<> & pose1,
const TooN::SE3<> & pose2,
const double & s)
{
return pose1.inverse().get_translation()
- s*pose2.inverse().get_translation();
}
double getResidual(const std::vector<TooN::SE3<> > & trans_vec1,
const std::vector<TooN::SE3<> > & trans_vec2,
const double & s,
TooN::Vector<> & residual_vec)
{
double sum = 0;
int trans_id=0;
for (uint i=0 ; i<trans_vec1.size(); ++i)
{
TooN::Vector<3> delta = diff(trans_vec1[i], trans_vec2[i], s);
residual_vec.slice(trans_id*3,3) = delta;
sum += delta*delta;
++trans_id;
}
return sum;
}
};
/** Helper class for pose-graph optimisation (see below)*/
template <typename Trans, int TransDoF> class Constraint
{
public:
Constraint(int trans_id1,
int trans_id2,
const Trans & mean,
const TooN::Matrix<TransDoF,TransDoF> & fisher_information)
: trans_id1(trans_id1),
trans_id2(trans_id2),
mean(mean),
fisher_information(fisher_information)
{}
int trans_id1;
int trans_id2;
Trans mean;
TooN::Matrix<TransDoF,TransDoF> fisher_information;
};
/** This class perfoms pose-graph optimisation as described in
*
* > H. Strasdat, J.M.M. Montiel, A.J. Davison:
* "Scale Drift-Aware Large Scale Monocular SLAM",
* Proc. of Robotics: Science and Systems (RSS),
* Zaragoza, Spain, 2010.
* http://www.roboticsproceedings.org/rss06/p10.html <
*
* The the update strategy of mu follows
* >M.I. A. Lourakis and A.A. Argyros, "The Design and Implementation
* of a Generic Sparse Bundle Adjustment Software Package Based on
* the Levenberg-Marquardt Algorithm", Technical Report, 2004.<
*
* Trans: pose transformation (e.g. SE2, SE3, Sim3...)
* TrandDoF: DoF of the corresponding transformation
*/
template <typename Trans, int TransDoF> class GraphOptimizer
{
typedef std::list<Constraint<Trans,TransDoF> >
_ConsList;
public:
GraphOptimizer()
{
verbose=0;
}
/** performs the pose-graph optimisation using LM
*
* trans_vec: set of absolute pose transformations
* contraint_list: set of relative pose-pose contraints
* num_fix_trans: number of fixed transformations
* num_iter: maximal number of iterations
* mu: initial LM parameter which interpolates between
* Gauss-Newton (0) and gradient descent (infinity)
*/
void optimize(std::vector<Trans> & trans_vec,
const std::list<Constraint<Trans, TransDoF> >
& constraint_list,
const AbstractConFun<Trans,TransDoF> & con_fun,
const int num_fixed_trans=0,
int num_iter=20,
double mu=0.0000000001)
{
int num_trans = trans_vec.size();
int num_constraints = constraint_list.size();
std::vector<Trans > new_trans_vec(num_trans);
TooN::Vector<>residual_vec (num_constraints*TransDoF);
double chi2 = getResidual(trans_vec,
constraint_list,
con_fun,
residual_vec);
if (isnan(chi2))
{
std::cerr << "chi2 is NAN\n";
exit(-1);
}
if(verbose>0)
std::cout << "chi2 " << chi2 << std::endl;
double nu = 2;
double eps =0.000000000000001;
bool stop = false;
int i_g = 0;
while(i_g<num_iter)
{
double rho = 0;
do
{
RowBlockMapVec<TransDoF> H(num_trans - num_fixed_trans);
TooN::Vector<> g
= TooN::Zeros(TransDoF*(num_trans - num_fixed_trans));
if (verbose>0)
std::cout << "iteration: "<< i_g << std::endl;
if (verbose>0)
std::cout << "mu: " <<mu<< std::endl;
calcHessianAndGradient(trans_vec,
constraint_list,
con_fun,
num_fixed_trans,
mu,
H,
g);
SparseMatrix<> sH(H);
TooN::Vector<> delta(TransDoF*(num_trans - num_fixed_trans));
try
{
SparseSolver<> spCh (sH);
delta = spCh.backsub(g);
}catch (NotPosSemiDefException & e) {
// not positive definite so increase mu and try again
std::cout << "Not pose Def" << std::endl;
mu *= nu;
nu *= 2.;
stop = (mu>999999999.f);
continue;
}
addTrans(trans_vec,
delta,
con_fun,
new_trans_vec,
num_fixed_trans);
double chi2_new = getResidual(new_trans_vec,
constraint_list,
con_fun,
residual_vec);
rho = (chi2-chi2_new)/(delta*(mu*delta+g));
if(rho>0)
{
++i_g;
if(verbose>0)
std::cout << "chi2_new: " << chi2_new << std::endl;
trans_vec = std::vector<Trans > (new_trans_vec);
chi2 = chi2_new;
stop = norm_max(g)<=eps;
mu *= std::max(1./3.,1-Po3(2*rho-1));
nu = 2.;
}
else
{
if (verbose>0)
std::cout << "no update: chi2 vs.chi2_new "
<< chi2 << " vs. " << chi2_new << std::endl;
mu *= nu;
nu *= 2.;
stop = (mu>999999999.f);
}
}while(!(rho>0 || stop || i_g>=num_iter));
if (stop)
break;
}
}
int verbose;
private:
void calcHessianAndGradient(const std::vector<Trans > & trans_vec,
const _ConsList & constraint_list,
const AbstractConFun<Trans,TransDoF> & con_fun,
const int num_fixed_trans,
double mu,
RowBlockMapVec<TransDoF> & H,
TooN::Vector<> & g)
{
TooN::Matrix<TransDoF,TransDoF> muInf = mu*TooN::Identity;
for (typename _ConsList::const_iterator it
= constraint_list.begin();
it!=constraint_list.end();
++it)
{
int id1 = it->trans_id1;
int id2 = it->trans_id2;
const Trans & mean = it->mean;
const TooN::Matrix<TransDoF,TransDoF> & inf
= it->fisher_information;
int col_id1 = id1-num_fixed_trans;
int col_id2 = id2-num_fixed_trans;
TooN::Matrix<TransDoF,TransDoF> J1;
TooN::Matrix<TransDoF,TransDoF> J2;
TooN::Vector<TransDoF> delta
= con_fun.diff(trans_vec[id1],
mean,
trans_vec[id2]);
if (col_id1>=0)
{
J1 = con_fun.d_diff_dT1(trans_vec[id1],
mean,
trans_vec[id2]);
H.add(J1.T()*inf*J1,
muInf,
col_id1,
col_id1);
g.slice(col_id1*TransDoF,TransDoF)
-= J1.T()*inf*delta;
}
if (col_id2>=0)
{
J2 = con_fun.d_diff_dT2(trans_vec[id1],
mean,
trans_vec[id2]);
H.add(J2.T()*inf*J2,
muInf,
col_id2,
col_id2);
g.slice(col_id2*TransDoF,TransDoF)
-= J2.T()*inf*delta;
if (col_id1>=0 )
{
TooN::Matrix<TransDoF,TransDoF> tmp = J1.T()*inf*J2;
if (col_id1<col_id2)
{
H.add(tmp,
col_id1,
col_id2);
}
else// if (col_id1!=col_id2)
{
H.add(tmp.T(),
col_id2,
col_id1);
}
}
}
}
}
double getResidual(const std::vector<Trans > & trans_vec,
const _ConsList & constraint_list,
const AbstractConFun<Trans,TransDoF> & con_fun,
TooN::Vector<> & residual_vec)
{
double sum = 0;
int constraint_id=0;
for (typename _ConsList::const_iterator it
= constraint_list.begin();
it!=constraint_list.end();
++it)
{
int id1 = it->trans_id1;
int id2 = it->trans_id2;
const Trans & mean = it->mean;
const TooN::Matrix<TransDoF,TransDoF> & inf
= it->fisher_information;
TooN::Vector<TransDoF> delta
= con_fun.diff(trans_vec[id1],
mean,
trans_vec[id2]);
residual_vec.slice(constraint_id*TransDoF,TransDoF) = delta;
sum += delta*inf*delta;
++constraint_id;
}
return sum;
}
void addTrans(const std::vector<Trans > & trans_vec,
const TooN::Vector<> &delta,
const AbstractConFun<Trans,TransDoF> & con_fun,
std::vector<Trans > & new_trans_vec,
int num_fix_trans)
{
for (int i=0; i<num_fix_trans; ++i)
new_trans_vec[i] = trans_vec[i];
for (uint i=num_fix_trans; i< trans_vec.size(); ++i)
{
new_trans_vec[i]
= con_fun.add(trans_vec[i],
delta.slice((i-num_fix_trans)*TransDoF, TransDoF));
}
}
};
}
#endif // RV_GRAPH_OPTIMIZER_H