Skip to content

feat: add optional random seed to the Python estimators - #48

Draft
f-dy wants to merge 1 commit into
danini:masterfrom
f-dy:feature/random-seed
Draft

feat: add optional random seed to the Python estimators#48
f-dy wants to merge 1 commit into
danini:masterfrom
f-dy:feature/random-seed

Conversation

@f-dy

@f-dy f-dy commented May 31, 2026

Copy link
Copy Markdown

feat: add optional random seed to the Python estimators

What

Adds a seed argument (default -1 = nondeterministic, unchanged behavior) to
the five Python estimators in bindings.cpp:

  • findHomography, findFundamentalMatrix, findEssentialMatrix,
    findRigidTransformation, findLine2D
H, mask = pymagsac.findHomography(corrs, w1, h1, w2, h2, seed=42)  # reproducible

Why

There is currently no way to make MAGSAC/MAGSAC++ runs reproducible, which makes
debugging non-deterministic results difficult. A fixed seed gives repeatable
sampling.

How

A small helper sets the seed before the estimator builds its sampler:

if (seed >= 0) {
    gcransac::utils::UniformRandomGenerator<size_t>::setGlobalSeed(seed); // all URG samplers
    std::srand(seed);                                                     // rand()/Eigen::Random
} else
    gcransac::utils::UniformRandomGenerator<size_t>::clearGlobalSeed();
  • setGlobalSeed makes all UniformRandomGenerator-based samplers
    (Uniform / PROSAC / Progressive-NAPSAC / Importance) deterministic.
  • std::srand(seed) covers the samplers/solvers that use std::rand() /
    Eigen::…::Random() (AdaptiveReorderingSampler, PnP bundle-adjustment,
    DLS/SIFT solvers).
  • seed < 0 restores the previous std::random_device behavior.

⚠️ Dependency

This PR depends on danini/graph-cut-ransac#51
("add a process-wide random seed to UniformRandomGenerator"), which introduces
UniformRandomGenerator::setGlobalSeed/clearGlobalSeed.

The submodule bump to the gcransac commit carrying #51 is intentionally not
included
in this PR (to keep the diff minimal and avoid pinning a fork commit).
It should be applied once #51 is merged into graph-cut-ransac master. Until
then this branch will not compile against the currently-pinned submodule.

Scope / follow-up

Covers the five estimators present on master. The additional estimators added
in #45 (plane, PnP, radial homography, affine/essential variants) get the same
seed parameter in a follow-up once #45 lands.

f-dy added a commit to f-dy/magsac that referenced this pull request May 31, 2026
Adds a 'seed' argument (default -1 = nondeterministic) to all 14 find*
estimators. When seed >= 0 the run is reproducible: a shared helper
(applyMagsacSeed) sets UniformRandomGenerator::setGlobalSeed(seed) for all
URG-based samplers and std::srand(seed) for the std::rand()/Eigen::Random
solver paths. Bumps the graph-cut-ransac submodule to include
setGlobalSeed/clearGlobalSeed.

Combines danini#48 (seed for the pre-existing estimators) and
danini/graph-cut-ransac#51 (the seed mechanism), extended to the new
estimators added in danini#45 (incl. findPlane3D).
@f-dy
f-dy marked this pull request as ready for review May 31, 2026 22:22
@f-dy
f-dy marked this pull request as draft May 31, 2026 22:41
Adds a 'seed' argument (default -1 = nondeterministic) to findHomography,
findFundamentalMatrix, findEssentialMatrix, findRigidTransformation and
findLine2D. When seed >= 0 the run is reproducible:
UniformRandomGenerator::setGlobalSeed(seed) makes all URG-based samplers
(Uniform/PROSAC/P-NAPSAC/Importance) deterministic, and std::srand(seed) covers
the samplers/solvers that use std::rand()/Eigen::Random. seed < 0 restores the
previous std::random_device behavior.

Depends on graph-cut-ransac#51 (adds UniformRandomGenerator::setGlobalSeed).
The submodule bump to that commit is intentionally not included here; it should
be applied once #51 is merged.
@f-dy
f-dy force-pushed the feature/random-seed branch from eec1652 to 2636b55 Compare May 31, 2026 23:11
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.

1 participant