Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(ransac): keep the failure inlier mask one-dimensional (#4885)

Keshav Chhabrad7f276b56dc5
diff --git a/changelog.d/4885.breaking.md b/changelog.d/4885.breaking.md
new file mode 100644
index 000000000..6f0fc55f3
--- /dev/null
+++ b/changelog.d/4885.breaking.md
@@ -0,0 +1,2 @@
+`RANSAC` now returns a one-dimensional `(N,)` boolean inlier mask when no model is found, matching successful
+calls. Previously, the no-model mask had shape `(N, 1)` and could not be used to index the input keypoints.
diff --git a/kornia/geometry/ransac.py b/kornia/geometry/ransac.py
index 739f8bb1f..2ed1f307f 100644
--- a/kornia/geometry/ransac.py
+++ b/kornia/geometry/ransac.py
@@ -357,15 +357,15 @@ class RANSAC(nn.Module):
weights: optional correspondences weights. Not used now.
Returns:
- - Estimated model, shape of :math:`(1, 3, 3)`.
- - The inlier/outlier mask, shape of :math:`(1, N)`, where N is number of input correspondences.
+ - Estimated model, shape of :math:`(3, 3)`.
+ - The inlier/outlier mask, shape of :math:`(N,)`, where N is number of input correspondences.
"""
self.validate_inputs(kp1, kp2, weights)
best_score_total: float = float(self.minimal_sample_size)
num_tc: int = len(kp1)
best_model_total = torch.zeros(3, 3, dtype=kp1.dtype, device=kp1.device)
- inliers_best_total: torch.Tensor = torch.zeros(num_tc, 1, device=kp1.device, dtype=torch.bool)
+ inliers_best_total: torch.Tensor = torch.zeros(num_tc, device=kp1.device, dtype=torch.bool)
for i in range(self.max_iter):
# Sample minimal samples in batch to estimate models
idxs = self.sample(self.minimal_sample_size, num_tc, self.batch_size, i, kp1.device)
diff --git a/tests/geometry/test_ransac.py b/tests/geometry/test_ransac.py
index fe93d0efc..72a618226 100644
--- a/tests/geometry/test_ransac.py
+++ b/tests/geometry/test_ransac.py
@@ -45,6 +45,25 @@ class TestRANSACHomography(BaseTester):
H, _ = ransac(points1, points2)
assert H.shape == (3, 3)
+ @pytest.mark.parametrize("model_found", [False, True])
+ def test_forward_output_shapes_are_stable(self, device, dtype, model_found):
+ generator = torch.Generator().manual_seed(123)
+ # A 10 px extent keeps bfloat16 rounding of the transformed points below inl_th; at 100 px it drops inliers.
+ points1 = 10.0 * torch.rand(16, 2, generator=generator).to(device=device, dtype=dtype)
+ unrelated_points = 10.0 * torch.rand(16, 2, generator=generator).to(device=device, dtype=dtype)
+ homography = torch.tensor([[1.0, 0.1, 2.0], [0.05, 1.0, -1.0], [0.001, 0.002, 1.0]], device=device, dtype=dtype)
+ points2 = transform_points(homography[None], points1[None])[0] if model_found else unrelated_points
+ ransac = RANSAC("homography", inl_th=0.5, batch_size=4, max_iter=1, max_lo_iters=0, seed=0)
+
+ model, inliers = ransac(points1, points2)
+
+ assert model.shape == (3, 3)
+ assert inliers.shape == (16,)
+ assert inliers.dtype == torch.bool
+ assert inliers.device == points1.device
+ selected_points = points1[inliers]
+ assert selected_points.shape == ((16 if model_found else 0), 2)
+
@pytest.mark.xfail(reason="might slightly and randomly imprecise due to RANSAC randomness")
def test_dirty_points(self, device, dtype):
# generate input data