Skip to content

Commit 753efef

Browse files
caglarpirmeta-codesync[bot]
authored andcommitted
Make the linear solver of bundle_shot_poses configurable
Summary: **Behavior before:** - `BAHelpers::BundleShotPoses` always solves with Ceres `DENSE_QR`. - Points and cameras are fixed, so the only unknowns are the 6 pose values of each shot, but the dense solver still builds the whole Jacobian: 2 rows per observation by 6 columns per shot. Its memory grows with the square of the number of shots. - OpenSfM's incremental reconstruction calls it with the one or two shots it just added, where that is cheap. Cluster augment calls it with every image it resected into a chunk, 400+ for some chunks, which OOM-kills the augment operator (T298706237). **Behavior after:** - New config key `bundle_shot_poses_linear_solver_type`, default `DENSE_QR`, so every current caller is unchanged. - `SPARSE_NORMAL_CHOLESKY` solves the same problem. With fixed points the poses are independent, so the normal equations are block-diagonal and memory grows linearly with the number of shots. **Why:** measured on synthetic poses-only problems with 1000 observations per shot (scratch benchmark, not in this diff); both solvers took 4 iterations to the same final cost: | shots | `SPARSE_NORMAL_CHOLESKY` | `DENSE_QR` | | --- | --- | --- | | 25 | 0.1 s, 0.03 GB | 0.8 s, 0.14 GB | | 50 | 0.2 s, 0.05 GB | 3.7 s, 0.49 GB | | 100 | 0.4 s, 0.09 GB | 28 s, 1.9 GB | | 200 | 1.6 s, 0.19 GB | 149 s, 7.3 GB | | 400 | 1.6 s, 0.36 GB | 1,100 s, 29.1 GB | Memory is the peak RSS added by the solve. Dense memory quadruples each time the shot count doubles; at 400 shots it is about two dense copies of the 800k x 2400 Jacobian. **Gating/Safety:** - The default keeps `DENSE_QR`; nothing changes until a caller sets the key. The child diff uses the sparse solver in cluster augment. - An unsupported value fails in `BundleAdjuster::Run` ("Linear solver type ... is not supported"). ___ landed-with-radar-review Differential Revision: D132259326 fbshipit-source-id: 86608f19e38514424715e1f7544a0d2cc609bf52
1 parent caf3e8f commit 753efef

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎opensfm/config.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ class OpenSfMConfig:
283283
optimize_camera_parameters: bool = True
284284
# Maximum optimizer iterations.
285285
bundle_max_iterations: int = 100
286+
# Ceres linear solver of the poses-only bundle (points fixed). DENSE_QR
287+
# memory grows with the square of the number of shots, SPARSE_NORMAL_CHOLESKY
288+
# linearly.
289+
bundle_shot_poses_linear_solver_type: str = "DENSE_QR"
286290

287291
# Retriangulate all points from time to time
288292
retriangulation: bool = True

‎opensfm/src/sfm/src/ba_helpers.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,8 @@ py::dict BAHelpers::BundleShotPoses(
685685
SetLossAndPriorsFromConfig(ba, config);
686686
ba.SetNumThreads(config["processes"].cast<int>());
687687
ba.SetMaxNumIterations(10);
688-
ba.SetLinearSolverType("DENSE_QR");
688+
ba.SetLinearSolverType(
689+
config["bundle_shot_poses_linear_solver_type"].cast<std::string>());
689690
const auto timer_setup = Clock::now();
690691

691692
RunWithoutGIL(ba);

‎opensfm/test/test_bundle.py‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,72 @@ def test_bundle_shot_poses_rig_gps_average() -> None:
780780

781781
origin = r.rig_instances["instance"].pose.get_origin()
782782
assert np.allclose(origin, np.mean(gps_positions, axis=0), atol=1e-6)
783+
784+
785+
def _poses_only_problem(
786+
num_shots: int, num_points: int
787+
) -> tuple[types.Reconstruction, dict[str, NDArray]]:
788+
"""Shots in their own rig instances, with perturbed poses, observing fixed
789+
points, as in the resection step of cluster augment. Returns the problem and
790+
the true shot origins."""
791+
rng = np.random.default_rng(42)
792+
camera = pygeometry.Camera.create_perspective(1.0, 0.0, 0.0)
793+
camera.id = "camera"
794+
r = types.Reconstruction()
795+
r.add_camera(camera)
796+
797+
points = rng.uniform([-10.0, -10.0, -10.0], [10.0, 10.0, 10.0], (num_points, 3))
798+
for point_id, coordinates in enumerate(points):
799+
r.create_point(str(point_id), coordinates)
800+
801+
true_origins = {}
802+
for i in range(num_shots):
803+
shot_id = f"shot{i}"
804+
origin = np.array([*rng.uniform(-3.0, 3.0, 2), -20.0])
805+
rotation = rng.normal(0.0, 0.05, 3)
806+
pose = pygeometry.Pose(rotation)
807+
pose.set_origin(origin)
808+
shot = r.create_shot(shot_id, camera.id, pose)
809+
for point_id, coordinates in enumerate(points):
810+
x, y = shot.project(coordinates) + rng.normal(0.0, 1e-4, 2)
811+
obs = pymap.Observation(x, y, 0.004, 0, 0, 0, point_id)
812+
r.add_observation(shot_id, str(point_id), obs)
813+
true_origins[shot_id] = origin
814+
815+
perturbed = pygeometry.Pose(rotation + rng.normal(0.0, 0.01, 3))
816+
perturbed.set_origin(origin + rng.normal(0.0, 0.2, 3))
817+
shot.pose = perturbed
818+
return r, true_origins
819+
820+
821+
def test_bundle_shot_poses_sparse_solver_matches_dense() -> None:
822+
"""The sparse solver finds the same poses as the default dense one."""
823+
poses = {}
824+
for solver in ("DENSE_QR", "SPARSE_NORMAL_CHOLESKY"):
825+
r, true_origins = _poses_only_problem(num_shots=20, num_points=100)
826+
custom_config = config.default_config()
827+
custom_config["bundle_shot_poses_linear_solver_type"] = solver
828+
829+
reconstruction.bundle_shot_poses(
830+
r,
831+
set(r.shots),
832+
dict(r.cameras.items()),
833+
dict(r.rig_cameras.items()),
834+
custom_config,
835+
)
836+
837+
for shot_id, origin in true_origins.items():
838+
assert np.allclose(r.shots[shot_id].pose.get_origin(), origin, atol=0.01), (
839+
solver,
840+
shot_id,
841+
r.shots[shot_id].pose.get_origin() - origin,
842+
)
843+
poses[solver] = {
844+
shot_id: np.concatenate([shot.pose.rotation, shot.pose.get_origin()])
845+
for shot_id, shot in r.shots.items()
846+
}
847+
848+
for shot_id, dense_pose in poses["DENSE_QR"].items():
849+
assert np.allclose(
850+
poses["SPARSE_NORMAL_CHOLESKY"][shot_id], dense_pose, atol=1e-6
851+
)

0 commit comments

Comments
 (0)