testSmartStereoFactor_iSAM2.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
3  * Atlanta, Georgia 30332-0415
4  * All Rights Reserved
5  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
6  * See LICENSE for the license information
7  * -------------------------------------------------------------------------- */
8 
19 
20 #include <gtsam/base/debug.h>
21 #include <gtsam/nonlinear/ISAM2.h>
24 
25 #include <array>
26 #include <fstream>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include <vector>
31 
32 // Set to 1 to enable verbose output of intermediary results
33 #define TEST_VERBOSE_OUTPUT 0
34 
35 #if TEST_VERBOSE_OUTPUT
36 #define TEST_COUT(ARGS_) std::cout << ARGS_
37 #else
38 #define TEST_COUT(ARGS_) void(0)
39 #endif
40 
41 // Tolerance for ground-truth pose comparison:
42 static const double tol = 1e-3;
43 
44 // Synthetic dataset generated with rwt
45 // (https://github.com/jlblancoc/recursive-world-toolkit)
46 // Camera parameters
47 const double fx = 200.0;
48 const double fy = 150.0;
49 const double cx = 512.0;
50 const double cy = 384.0;
51 const double baseline = 0.2; // meters
52 
54 using lm_id_t = int;
55 
56 struct stereo_meas_t {
57  stereo_meas_t(lm_id_t id, double lu, double ru, double v_lr)
58  : lm_id{id}, left_u{lu}, right_u{ru}, v{v_lr} {}
59 
60  lm_id_t lm_id{-1}; // landmark id
61  double left_u{0}, right_u{0}, v{0};
62 };
63 
64 static std::map<timestep_t, std::vector<stereo_meas_t>> dataset = {
65  {0,
66  {{0, 911.99993896, 712.00000000, 384.0},
67  {159, 311.99996948, 211.99996948, 384.0},
68  {3, 378.66665649, 312.00000000, 384.0},
69  {2, 645.33331299, 578.66662598, 384.0},
70  {157, 111.99994659, 11.99993896, 384.0},
71  {4, 578.66662598, 545.33331299, 384.0},
72  {5, 445.33331299, 412.00000000, 384.0},
73  {6, 562.00000000, 537.00000000, 384.0}}},
74  {1,
75  {{0, 1022.06353760, 762.57519531, 384.0},
76  {159, 288.30487061, 177.80273438, 384.0},
77  {2, 655.30645752, 583.12127686, 384.0},
78  {3, 368.60937500, 297.43176270, 384.0},
79  {4, 581.82666016, 547.16766357, 384.0},
80  {5, 443.66183472, 409.23681641, 384.0},
81  {6, 564.35980225, 538.62115479, 384.0},
82  {7, 461.66418457, 436.05477905, 384.0},
83  {8, 550.32220459, 531.75256348, 384.0},
84  {9, 476.17767334, 457.67541504, 384.0}}},
85  {2,
86  {{159, 257.97128296, 134.26287842, 384.0},
87  {2, 666.87255859, 588.07275391, 384.0},
88  {3, 356.53823853, 280.10061646, 384.0},
89  {4, 585.10949707, 548.99212646, 384.0},
90  {5, 441.66403198, 406.05108643, 384.0},
91  {6, 566.75402832, 540.21868896, 384.0},
92  {7, 461.16207886, 434.90002441, 384.0},
93  {8, 552.28387451, 533.30230713, 384.0},
94  {9, 476.63549805, 457.79418945, 384.0},
95  {10, 546.48394775, 530.53009033, 384.0}}},
96  {3,
97  {{159, 218.10592651, 77.30914307, 384.0},
98  {2, 680.54644775, 593.68103027, 384.0},
99  {3, 341.92507935, 259.28231812, 384.0},
100  {4, 588.53289795, 550.80499268, 384.0},
101  {5, 439.29989624, 402.39105225, 384.0},
102  {6, 569.18627930, 541.78991699, 384.0},
103  {7, 460.47863770, 433.51678467, 384.0},
104  {8, 554.24902344, 534.82952881, 384.0},
105  {9, 477.00451660, 457.80438232, 384.0},
106  {10, 548.33770752, 532.07501221, 384.0},
107  {11, 483.58688354, 467.47830200, 384.0},
108  {12, 542.36785889, 529.29321289, 384.0}}},
109  {4,
110  {{2, 697.09454346, 600.18432617, 384.0},
111  {3, 324.03643799, 233.97094727, 384.0},
112  {4, 592.11877441, 552.60449219, 384.0},
113  {5, 436.52197266, 398.19531250, 384.0},
114  {6, 571.66101074, 543.33209229, 384.0},
115  {7, 459.59658813, 431.88333130, 384.0},
116  {8, 556.21801758, 536.33258057, 384.0},
117  {9, 477.27893066, 457.69882202, 384.0},
118  {10, 550.18920898, 533.60003662, 384.0},
119  {11, 484.24472046, 467.86862183, 384.0},
120  {12, 544.14727783, 530.86157227, 384.0},
121  {13, 491.26141357, 478.11267090, 384.0},
122  {14, 541.29949951, 529.57086182, 384.0},
123  {15, 494.58111572, 482.95935059, 384.0}}}};
124 
125 // clang-format off
126 /*
127 % Ground truth path of the SENSOR, and the ROBOT
128 % STEP X Y Z QR QX QY QZ | X Y Z QR QX QY QZ
129  ----------------------------------------------------------------------------------------------------------------------------------------
130  0 0.000000 0.000000 0.000000 0.500000 -0.500000 0.500000 -0.500000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000
131  1 0.042019 -0.008403 0.000000 0.502446 -0.502446 0.497542 -0.497542 0.042019 -0.008403 0.000000 0.999988 0.000000 0.000000 0.004905
132  2 0.084783 -0.016953 0.000000 0.504879 -0.504879 0.495073 -0.495073 0.084783 -0.016953 0.000000 0.999952 0.000000 0.000000 0.009806
133  3 0.128305 -0.025648 0.000000 0.507299 -0.507299 0.492592 -0.492592 0.128305 -0.025648 0.000000 0.999892 0.000000 0.000000 0.014707
134  4 0.172605 -0.034490 0.000000 0.509709 -0.509709 0.490098 -0.490098 0.172605 -0.034490 0.000000 0.999808 0.000000 0.000000 0.019611
135 */
136 // clang-format on
137 
138 // Ground truth using camera pose = vehicle frame
139 // The table above uses:
140 // camera +x = vehicle -y
141 // camera +y = vehicle -z
142 // camera +z = vehicle +x
143 static const std::map<timestep_t, gtsam::Point3> gt_positions = {
144  {0, {0.000000, 0.000000, 0.0}},
145  {1, {0.042019, -0.008403, 0.0}},
146  {2, {0.084783, -0.016953, 0.0}},
147  {3, {0.128305, -0.025648, 0.0}},
148  {4, {0.172605, -0.034490, 0.0}}};
149 
150 // Batch version, to compare against iSAM2 solution.
151 TEST(testISAM2SmartFactor, Stereo_Batch) {
152  TEST_COUT("============ Running: Batch ============\n");
153 
154  using namespace gtsam;
155  using symbol_shorthand::V;
156  using symbol_shorthand::X;
157 
158  const auto K =
159  boost::make_shared<Cal3_S2Stereo>(fx, fy, .0, cx, cy, baseline);
160 
161  // Pose prior - at identity
162  auto priorPoseNoise = noiseModel::Diagonal::Sigmas(
163  (Vector(6) << Vector3::Constant(0.2), Vector3::Constant(0.2)).finished());
164 
165  // Map: landmark_id => smart_factor_index inside iSAM2
166  std::map<lm_id_t, FactorIndex> lm2factor;
167 
168  // Storage of smart factors:
169  std::map<lm_id_t, SmartStereoProjectionPoseFactor::shared_ptr> smartFactors;
170 
171  NonlinearFactorGraph batch_graph;
172  Values batch_values;
173 
174  // Run one timestep at once:
175  for (const auto &entries : dataset) {
176  // 1) Process new observations:
177  // ------------------------------
178  const auto kf_id = entries.first;
179  const std::vector<stereo_meas_t> &obs = entries.second;
180 
181  for (const stereo_meas_t &stObs : obs) {
182  if (smartFactors.count(stObs.lm_id) == 0) {
183  auto noise = noiseModel::Isotropic::Sigma(3, 0.1);
185 
186  smartFactors[stObs.lm_id] =
187  boost::make_shared<SmartStereoProjectionPoseFactor>(noise, parm);
188 
189  batch_graph.push_back(smartFactors[stObs.lm_id]);
190  }
191 
192  TEST_COUT("Adding stereo observation from KF #" << kf_id << " for LM #"
193  << stObs.lm_id << "\n");
194 
195  smartFactors[stObs.lm_id]->add(
196  StereoPoint2(stObs.left_u, stObs.right_u, stObs.v), X(kf_id), K);
197  }
198 
199  // prior, for the first keyframe:
200  if (kf_id == 0) {
201  batch_graph.addPrior(X(kf_id), Pose3::identity(), priorPoseNoise);
202  }
203 
204  batch_values.insert(X(kf_id), Pose3::identity());
205  }
206 
208 #if TEST_VERBOSE_OUTPUT
209  parameters.verbosity = NonlinearOptimizerParams::LINEAR;
210  parameters.verbosityLM = LevenbergMarquardtParams::TRYDELTA;
211 #endif
212 
213  LevenbergMarquardtOptimizer lm(batch_graph, batch_values, parameters);
214 
215  Values finalEstimate = lm.optimize();
216 #if TEST_VERBOSE_OUTPUT
217  finalEstimate.print("LevMarq estimate:");
218 #endif
219 
220  // GT:
221  // camera +x = vehicle -y
222  // camera +y = vehicle -z
223  // camera +z = vehicle +x
224  for (const auto &gt : gt_positions) {
225  const Pose3 p = finalEstimate.at<Pose3>(X(gt.first));
226  EXPECT(assert_equal(p.x(), -gt.second.y(), tol));
227  EXPECT(assert_equal(p.y(), -gt.second.z(), tol));
228  EXPECT(assert_equal(p.z(), gt.second.x(), tol));
229  }
230 }
231 
232 TEST(testISAM2SmartFactor, Stereo_iSAM2) {
233  TEST_COUT("======= Running: iSAM2 ==========\n");
234 
235 #if TEST_VERBOSE_OUTPUT
236  SETDEBUG("ISAM2 update", true);
237  // SETDEBUG("ISAM2 update verbose",true);
238 #endif
239 
240  using namespace gtsam;
241  using symbol_shorthand::V;
242  using symbol_shorthand::X;
243 
244  const auto K =
245  boost::make_shared<Cal3_S2Stereo>(fx, fy, .0, cx, cy, baseline);
246 
248  parameters.relinearizeThreshold = 0.01;
249  parameters.evaluateNonlinearError = true;
250 
251  // Do not cache smart factors:
252  parameters.cacheLinearizedFactors = false;
253 
254  // Important: must set relinearizeSkip=1 to additional calls to update() to
255  // have a real effect.
256  parameters.relinearizeSkip = 1;
257 
258  ISAM2 isam(parameters);
259 
260  // Pose prior - at identity
261  auto priorPoseNoise = noiseModel::Diagonal::Sigmas(
262  (Vector(6) << Vector3::Constant(0.2), Vector3::Constant(0.2)).finished());
263 
264  // Map: landmark_id => smart_factor_index inside iSAM2
265  std::map<lm_id_t, FactorIndex> lm2factor;
266 
267  // Storage of smart factors:
268  std::map<lm_id_t, SmartStereoProjectionPoseFactor::shared_ptr> smartFactors;
269 
270  Pose3 lastKeyframePose = Pose3::identity();
271 
272  // Run one timestep at once:
273  for (const auto &entries : dataset) {
274  // 1) Process new observations:
275  // ------------------------------
276  const auto kf_id = entries.first;
277  const std::vector<stereo_meas_t> &obs = entries.second;
278 
279  // Special instructions for using iSAM2 + smart factors:
280  // Must fill factorNewAffectedKeys:
281  FastMap<FactorIndex, KeySet> factorNewAffectedKeys;
282  NonlinearFactorGraph newFactors;
283 
284  // Map: factor index in the internal graph of iSAM2 => landmark_id
285  std::map<FactorIndex, lm_id_t> newFactor2lm;
286 
287  for (const stereo_meas_t &stObs : obs) {
288  if (smartFactors.count(stObs.lm_id) == 0) {
289  auto noise = noiseModel::Isotropic::Sigma(3, 0.1);
291 
292  smartFactors[stObs.lm_id] =
293  boost::make_shared<SmartStereoProjectionPoseFactor>(noise, params);
294  newFactor2lm[newFactors.size()] = stObs.lm_id;
295  newFactors.push_back(smartFactors[stObs.lm_id]);
296  } else {
297  // Only if the factor *already* existed:
298  factorNewAffectedKeys[lm2factor.at(stObs.lm_id)].insert(X(kf_id));
299  }
300 
301  TEST_COUT("Adding stereo observation from KF #" << kf_id << " for LM #"
302  << stObs.lm_id << "\n");
303 
304  smartFactors[stObs.lm_id]->add(
305  StereoPoint2(stObs.left_u, stObs.right_u, stObs.v), X(kf_id), K);
306  }
307 
308  // prior, for the first keyframe:
309  if (kf_id == 0) {
310  newFactors.addPrior(X(kf_id), Pose3::identity(), priorPoseNoise);
311  }
312 
313  // 2) Run iSAM2:
314  // ------------------------------
315  Values newValues;
316  newValues.insert(X(kf_id), lastKeyframePose);
317 
318  TEST_COUT("Running iSAM2 for frame: " << kf_id << "\n");
319 
320  ISAM2UpdateParams updateParams;
321  updateParams.newAffectedKeys = std::move(factorNewAffectedKeys);
322 
323  ISAM2Result res = isam.update(newFactors, newValues, updateParams);
324 
325  for (const auto &f2l : newFactor2lm)
326  lm2factor[f2l.second] = res.newFactorsIndices.at(f2l.first);
327 
328  TEST_COUT("error before1: " << res.errorBefore.value() << "\n");
329  TEST_COUT("error after1 : " << res.errorAfter.value() << "\n");
330 
331  // Additional refining steps:
332  ISAM2Result res2 = isam.update();
333  TEST_COUT("error before2: " << res2.errorBefore.value() << "\n");
334  TEST_COUT("error after2 : " << res2.errorAfter.value() << "\n");
335 
336  Values currentEstimate = isam.calculateEstimate();
337 #if TEST_VERBOSE_OUTPUT
338  currentEstimate.print("currentEstimate:");
339 #endif
340 
341  // Keep last KF pose as initial pose of the next one, to reduce the need
342  // to run more non-linear iterations:
343  lastKeyframePose = currentEstimate.at(X(kf_id)).cast<Pose3>();
344 
345  } // end for each timestep
346 
347  Values finalEstimate = isam.calculateEstimate();
348 
349  // GT:
350  // camera +x = vehicle -y
351  // camera +y = vehicle -z
352  // camera +z = vehicle +x
353  for (const auto &gt : gt_positions) {
354  const Pose3 p = finalEstimate.at<Pose3>(X(gt.first));
355  EXPECT(assert_equal(p.x(), -gt.second.y(), tol));
356  EXPECT(assert_equal(p.y(), -gt.second.z(), tol));
357  EXPECT(assert_equal(p.z(), gt.second.x(), tol));
358  }
359 }
360 
361 /* *************************************************************************
362  */
363 int main() {
364  TestResult tr;
365  return TestRegistry::runAllTests(tr);
366 }
size_t size() const
Definition: FactorGraph.h:306
#define SETDEBUG(S, V)
Definition: debug.h:61
virtual const Values & optimize()
return int(ret)+1
static int runAllTests(TestResult &result)
Global debugging flags.
void insert(Key j, const Value &val)
Definition: Values.cpp:140
const double baseline
const double cx
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
Verbosity verbosity
The printing verbosity during optimization (default SILENT)
static const double tol
IsDerived< DERIVEDFACTOR > push_back(boost::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:166
Smart stereo factor on poses, assuming camera calibration is fixed.
double x() const
get x
Definition: Pose3.h:269
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
FactorIndices newFactorsIndices
Definition: ISAM2Result.h:97
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Values calculateEstimate() const
Definition: ISAM2.cpp:763
static std::map< timestep_t, std::vector< stereo_meas_t > > dataset
const double fy
const ValueType at(Key j) const
Definition: Values-inl.h:342
Incremental update functionality (ISAM2) for BayesTree, with fluid relinearization.
#define EXPECT(condition)
Definition: Test.h:151
stereo_meas_t(lm_id_t id, double lu, double ru, double v_lr)
static const std::map< timestep_t, gtsam::Point3 > gt_positions
double y() const
get y
Definition: Pose3.h:274
boost::optional< double > errorAfter
Definition: ISAM2Result.h:66
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
NonlinearISAM isam(relinearizeInterval)
static SmartStereoProjectionParams params
cout<< "Here is the matrix m:"<< endl<< m<< endl;Eigen::FullPivLU< Matrix5x3 > lu(m)
static ConjugateGradientParameters parameters
traits
Definition: chartTesting.h:28
TEST(testISAM2SmartFactor, Stereo_Batch)
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: Values.cpp:77
#define HESSIAN
const double cy
#define TEST_COUT(ARGS_)
boost::optional< double > errorBefore
Definition: ISAM2Result.h:54
float * p
virtual ISAM2Result update(const NonlinearFactorGraph &newFactors=NonlinearFactorGraph(), const Values &newTheta=Values(), const FactorIndices &removeFactorIndices=FactorIndices(), const boost::optional< FastMap< Key, int > > &constrainedKeys=boost::none, const boost::optional< FastList< Key > > &noRelinKeys=boost::none, const boost::optional< FastList< Key > > &extraReelimKeys=boost::none, bool force_relinearize=false)
Definition: ISAM2.cpp:396
const double fx
VerbosityLM verbosityLM
The verbosity level for Levenberg-Marquardt (default: SILENT), see also NonlinearOptimizerParams::ver...
#define X
Definition: icosphere.cpp:20
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
boost::optional< FastMap< FactorIndex, KeySet > > newAffectedKeys
std::size_t timestep_t
double z() const
get z
Definition: Pose3.h:279


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:49:48