Repository navigation
Add FlannIndex abstract interface and implement NanoFlannIndex subclass - #1744
Conversation
|
I see that the Docker CI build failed because nanoflann is missing on the test server. I will update the CMakeLists.txt and add I'll push these CMake fixes along with the Raspberry Pi 5 benchmark results as soon as I finish the speed tests! |
|
I can do the plumbing for the conditional build/parameters with nanoflann support. I'll have more time in a couple days to look deeper at this. |
|
Thanks, that would be awesome if you could handle the CMake and conditional build plumbing! Compiling on the Raspberry Pi 5 is taking quite a while, so I haven't finished those benchmarks yet. However, I have gathered the results on my x86 machine using the Here are the numbers: 0.23.7-jazzy branch (updated 2 months ago):
Latest master branch:
Command used for testing: rtabmap-rgbd_dataset --camera_info /root/data/rgbd_dataset_freiburg1_desk/rtabmap_calib.yaml \
--output /root/data/rgbd_dataset_freiburg1_desk/rtabmap_rpi5_test.db --Odom/Strategy 1 --Vis/MinInliers 12 \
--Vis/MaxFeatures 800 --Vis/FeatureType 6 --Kp/DetectorStrategy 6 --Rtabmap/DetectionRate 2.0 \
--Vis/DepthAsMask false --Mem/DepthAsMask false --RGBD/OptimizeMaxError 10.0 /root/data/rgbd_dataset_freiburg1_deskFor my RPi5, I currently have a year-old Noetic build set up. Because of this, I can only share the benchmark results for that specific older build on ARM once I have some time to run it. Let me know if those numbers would still be useful to you! |
|
I profiled the fresh The tests were performed on the TUM RGB-D Freiburg1 Desk dataset using the following configuration: rtabmap-rgbd_dataset \
--camera_info /root/data/rgbd_dataset_freiburg1_desk/rtabmap_calib.yaml \
--output /root/data/rgbd_dataset_freiburg1_desk/rtabmap_rpi5_test.db \
--Odom/Strategy 1 \
--Vis/MinInliers 12 \
--Vis/MaxFeatures 800 \
--Vis/FeatureType 6 \
--Kp/DetectorStrategy 6 \
--Rtabmap/DetectionRate 2.0 \
--Vis/DepthAsMask false \
--Mem/DepthAsMask false \
--RGBD/OptimizeMaxError 10.0 \
/root/data/rgbd_dataset_freiburg1_deskExecution time: 3 min 📊 gprof: BEFORE (with rtflann) — (click arrow to expand)Execution time: 3 min 🚀 gprof: AFTER (with nanoflann) — (click arrow to expand)Summary of Changes & Profiling Analysis:
This optimization flattens a substantial computational bottleneck in the visual odometry radius search stage, reclaiming critical CPU cycles for edge computing and low-power robotic hardware.
|
…ndex. Added tests. Vendoring nanoflann.h directly in the repo. RegistrationVis now use NANOFLANN_INDEX_KDTREE_SINGLE (instead of FLANN_INDEX_KDTREE_SINGLE) flann index for 2d points matching.
|
While looking at what nanoflann is good at, it cannot be a full replacement of rtflann, in particular it is very slow on high dimension descriptors. It is however faster for 2d points matching for visual odometry. I added performance tests to compare all flann strategies: Basically, what it shows is that for 2D/3D point clouds, nanoflann is faster to build and also faster to make queries. However, for high dimensional descriptors (visual dictionary), it is significantly worst for queries: Stereo 20 Hz integration benchmark
What it shows
At the level of that call alone (1000 keypoints indexed, as many looked up in a
Caveats
|
|
Hi @matlabbe, Thanks for the feedback and for testing the high-dimensional descriptors! To clarify, I completely agree with your benchmarks. I only focused on the 2D/3D point matching stage because I knew Your change in commit 0a4d369 to separate Let me know if you need any further changes from my side |
|
@matlabbe, What do you think about testing this struct LaserScan3DAdapter {
const rtabmap::LaserScan& scan;
LaserScan3DAdapter(const rtabmap::LaserScan& s) : scan(s) {}
inline size_t kdtree_get_point_count() const { return scan.size(); }
inline float kdtree_get_pt(const size_t idx, const size_t dim) const {
if (!scan.data().empty() && scan.dataType() == cv::CV_32FC3) {
const cv::Vec3f* ptr = scan.data().ptr<cv::Vec3f>();
return (dim == 0) ? ptr[idx][0] : ((dim == 1) ? ptr[idx][1] : ptr[idx][2]);
}
return 0.0f;
}
template <class BBOX> bool kdtree_get_bbox(BBOX& /* bb */) const { return false; }
};Currently, inside the pcl::PointCloud<pcl::PointXYZINormal>::Ptr fromCloudNormals = util3d::laserScanToPointCloudINormal(fromScan, fromScan.localTransform());This forces deep copying and heap allocations on every single frame, even if With Let me know if you need any further adjustments for the current PR |
|
Just saw your two comments after merging. Anyway, what is discussed could be done in another PR. Internally, PCL is using original flann library and it expects input format to be a rtabmap/corelib/src/RegistrationIcp.cpp Lines 707 to 713 in f647014 rtabmap/corelib/src/RegistrationIcp.cpp Lines 796 to 801 in f647014 Looking at pcl::PointCloud, they have these fields which could remove the need to transform the points so that an adapter over the LaserScan data "may" be possible: /** \brief Sensor acquisition pose (origin/translation). */
Eigen::Vector4f sensor_origin_ = Eigen::Vector4f::Zero ();
/** \brief Sensor acquisition pose (rotation). */
Eigen::Quaternionf sensor_orientation_ = Eigen::Quaternionf::Identity ();However, all ICP functions from PCL require a Here are some possible optimizations that could be done though. We could spin off these two in separate PRs. Saving copiesChecking up with Claude:
Using nanoflann in PCL's ICP
rtabmap/corelib/src/util3d_registration.cpp Lines 444 to 463 in f647014 |
|
@matlabbe, Thanks for the detailed breakdown! Your ideas about folding the NaN removal into the conversion and creating a custom KdTreeNanoFLANN wrapper sound great.I would love to work on these tasks! I'll spin them off into separate PRs as soon as I have some free time.Thanks again for the great collaboration! |
Hi @matlabbe,
The architectural refactoring is complete! I have moved
FlannIndexinto an abstract interface and implementedRtFlannIndexandNanoFlannIndexas subclasses according to your suggestions.I updated
RegistrationVis.cppandVWDictionary.cppto use the new factory pattern. All 16 tests intest_corelib(includingRegistrationVisTest) are now passing perfectly.In the meantime, I am going to test the matching performance and speed on a Raspberry Pi 5 to see the actual benchmark results on an ARM platform, and I will share the numbers here as soon as I finish.
ML Edits: Two changes for the default behavior of rtabmap:
RegistrationVisnow uses nanoflann for 2D reprojected points matching, when tracking features from one frame to another (when a guess is provided with feature matching approach). It gives a slight speed boost for that.test_flann_index_perfhas been added to compare performance of all flann strategiesVis/CorNNType=9andKp/NNStrategy=6can select nanoflann. Based on performance tests, it is not recommended to use withKp/NNStrategyfor large high dimensional dictionary. One place that may be beneficial is to set it forVis/CorNNType, thoughVis/CorNNType=8(flann kdtree single) may already be a good contender.Kp/FlannRebalancingFactor: The meaning of this parameter has slightly changed (description updated), but the value should be backward compatible with old default. Atfactor=1, it is still disabling auto-rebuilding of the index. However, whenfactor>1, the index is rebuilt when the ratio of words removed from the index over the total size of the index (including removed words) is over1 / factor. Before the factor meant that the index is rebuilt when the index doubles in size, independently if words have been removed or not. Based on performance testing (see newtest_flann_index_perftest), we can see that it doesn't worth it to rebuild the index when we don't remove words (the cost of rebalancing the index is far more expensive than the decrease in recall caused by not rebalancing the index). However, rebalancing when many words have been removed (when memory management is enabled) is beneficial in terms of memory and computation time.