Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sfm] Performance improvements: optimize RemoveOutliers_AngleError #841

Merged
merged 5 commits into from
Sep 2, 2020

Conversation

0xfaded
Copy link

@0xfaded 0xfaded commented Jul 27, 2020

  • Use greedy hueristic to avoid O(N^2) code in vast majority of cases
  • Use Eigen matrix operations to speed up O(N^2) operations
  • Add omp parallel

Description

This diff removes an N^2 loop which becomes relevant when processing large numbers of images. On my setup, when processing 8000 images 50% of the time is spent in this routine.

Features list

Implementation remarks

The loop in question searches for the largest angle between view directions. In my implementation, I first attempt a simple greedy algorithm which in almost all cases exits in sub-linear time if the angle exists. If the greedy algorithm fails, I revert to a n^2 implemented in Eigen for speed..

I haven't done a thorough analysis, but I suspect the overall expected runtime is << linear.

 - Use greedy hueristic to avoid O(N^2) code in vast majority of cases
 - Use Eigen matrix operations to speed up O(N^2) operations
 - Add omp parallel
@fabiencastan
Copy link
Member

Hi @0xfaded,
Thanks for the PR. I haven't looked at your implementation yet, but it sounds like a great idea.
Could you provide some figures on the impact of the proposed changes on performance?

@0xfaded
Copy link
Author

0xfaded commented Jul 29, 2020

Hi Fabien.

For large datasets, in my case over 8000 images, this reduces the SFM time from 1h40 to 50 minutes on my machine.

This check is run on every feature after every bundle adjustment, so for datasets with large tracks this routine actually becomes the bottleneck.

In terms of the actual implementation, every effort is made to exit the loop as early as possible. In most cases, this reduces the expexted complexity from O(N^2) to near O(1).

For small datasets, this will not be noticeable. Is there a particular benchmark you want me to run?

@fabiencastan
Copy link
Member

Wow that sounds amazing!
Yes, we are limiting the number of cameras that we are considering in the BA with the "local BA" strategy, but at the same time we are doing this test on all landmarks at each step...

@fabiencastan
Copy link
Member

When we use the local BA, we may retrieve the cameras that have been optimized in the BA (local BA state per pose) and for each camera we have a pre-computed list of tracks id visible in this camera (_map_tracksPerView).
So we could be able to test only the landmarks that could have change in the BA.

src/aliceVision/camera/IntrinsicBase.hpp Outdated Show resolved Hide resolved
src/aliceVision/sfm/sfmFilters.cpp Outdated Show resolved Hide resolved
@0xfaded
Copy link
Author

0xfaded commented Jul 30, 2020

When we use the local BA, we may retrieve the cameras that have been optimized in the BA (local BA state per pose) and for each camera we have a pre-computed list of tracks id visible in this camera (_map_tracksPerView).
So we could be able to test only the landmarks that could have change in the BA.

Thanks @fabiencastan for the suggestion. However I'd prefer to implement this as a separate pull request.

Copy link
Member

@simogasp simogasp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the nice contribution!
I just left some minor syntactical fixes to be more uniform to the code style

src/aliceVision/sfm/sfmFilters.cpp Outdated Show resolved Hide resolved
src/aliceVision/sfm/sfmFilters.cpp Outdated Show resolved Hide resolved
src/aliceVision/sfm/sfmFilters.cpp Outdated Show resolved Hide resolved
src/aliceVision/sfm/sfmFilters.cpp Outdated Show resolved Hide resolved
src/aliceVision/sfm/sfmFilters.cpp Outdated Show resolved Hide resolved
src/aliceVision/sfm/sfmFilters.cpp Outdated Show resolved Hide resolved
src/aliceVision/sfm/sfmFilters.cpp Outdated Show resolved Hide resolved
Co-authored-by: Simone Gasparini <[email protected]>
@fabiencastan
Copy link
Member

CI error on windows:

C:\projects\alicevision\src\aliceVision\sfm\sfmFilters.cpp(77,1): error C3015: initialization in OpenMP 'for' statement has improper form [

@fabiencastan fabiencastan changed the title [sfm] Optimize RemoveOutliers_AngleError [sfm] Performance improvements: optimize RemoveOutliers_AngleError Sep 2, 2020
@fabiencastan fabiencastan added this to the 2020.1.0 milestone Sep 2, 2020
@fabiencastan fabiencastan merged commit 287c71a into alicevision:develop Sep 2, 2020
@fabiencastan
Copy link
Member

@0xfaded Thanks for the nice work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants