π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(feature): draw k distinct couples in AdaLAM's RANSAC sampler (#6365)
Saksham Sharma763352c04e1c
diff --git a/changelog.d/6365.fixed.md b/changelog.d/6365.fixed.mdnew file mode 100644index 000000000..83d2cf2f0--- /dev/null+++ b/changelog.d/6365.fixed.md@@ -0,0 +1 @@+`kornia.feature.match_adalam` and `kornia.feature.adalam.AdalamFilter` no longer repeat sample couples in their local RANSAC. The couple sampler used the size of the last, incomplete row of couples as its first index, so that row repeated couples already drawn instead of continuing with new ones: with the default `ransac_iters=128`, 8 of the 128 hypotheses in each region were duplicates and the 17th-ranked sample was never paired with the 8 best. A region with too few samples for `ransac_iters` distinct couples (16 or fewer at the default) still wraps its sample indices and reuses couples, as before. Matches and their confidences can therefore differ slightly from earlier releases.diff --git a/kornia/feature/adalam/utils.py b/kornia/feature/adalam/utils.pyindex 23d39d723..9d46c99d0 100644--- a/kornia/feature/adalam/utils.py+++ b/kornia/feature/adalam/utils.py@@ -154,6 +154,13 @@ def draw_first_k_couples(k: int, rdims: torch.Tensor, dv: torch.device) -> torchMax n for which we can exhaustively sample with k couples:* n2/2 + n/2 = k* n = sqrt(1/4 + 2k)-1/2 = (sqrt(8k+1)-1)/2++ The couples are ``(1, 0), (2, 0), (2, 1), (3, 0), ...``: the ``k`` distinct pairs ``(i, j)`` with ``j < i`` in+ that order, so the residual couples after the complete rows continue with ``(n + 1, 0), (n + 1, 1), ...``. The+ reference AdaLAM implementation used the residual count as that first index, which repeated couples already+ drawn (8 of the default 128) instead of trying new ones. Each region then takes the indices modulo its number of+ samples in ``rdims``, so the couples stay distinct only in a region with enough samples for ``k`` distinct+ couples, at least 17 for ``k = 128``; in a smaller region they wrap, and can repeat or pair a sample with itself."""max_exhaustive_search = int(math.sqrt(2 * k + 0.25) - 0.5)residual_search = int(k - max_exhaustive_search * (max_exhaustive_search + 1) / 2)@@ -164,7 +171,10 @@ def draw_first_k_couples(k: int, rdims: torch.Tensor, dv: torch.device) -> torchtorch.tensor([residual_search], dtype=torch.long, device=dv),])- idx_sequence = torch.stack([repeats.repeat_interleave(repeats), arange_sequence(repeats)], dim=-1)+ # Row ``i`` holds the couples ``(i, 0), ..., (i, i - 1)``; its first index is ``i``, not its length, which differs+ # for the residual row.+ firsts = torch.arange(1, max_exhaustive_search + 2, dtype=torch.long, device=dv)+ idx_sequence = torch.stack([firsts.repeat_interleave(repeats), arange_sequence(repeats)], dim=-1)return torch.remainder(idx_sequence.unsqueeze(-1), rdims)diff --git a/tests/feature/test_adalam.py b/tests/feature/test_adalam.pyindex 2dc499b49..44cbacfd4 100644--- a/tests/feature/test_adalam.py+++ b/tests/feature/test_adalam.py@@ -28,6 +28,7 @@ from kornia.feature.adalam.ransac import (ransac,stable_sort_residuals,)+from kornia.feature.adalam.utils import draw_first_k_couplesfrom testing.base import BaseTester@@ -217,3 +218,21 @@ class TestAdalamExact(BaseTester):)self.assert_close(result[1], torch.arange(29, device=device))self.assert_close(result[2], dims)+++class TestDrawFirstKCouples(BaseTester):+ @pytest.mark.parametrize("k", [1, 2, 3, 4, 10, 100, 128, 129])+ def test_couples_are_the_first_k_distinct_pairs(self, k, device):+ # The k couples are the pairs (i, j), j < i, in row order. The residual row after the complete ones used+ # its length as its first index, so it repeated couples already drawn: 8 of the default 128.+ couples = draw_first_k_couples(k, torch.tensor([1000], device=device), device)+ expected = [(i, j) for i in range(1, k + 2) for j in range(i)][:k]+ assert couples.shape == (k, 2, 1)+ assert [tuple(c) for c in couples[:, :, 0].tolist()] == expected++ def test_couples_wrap_per_region(self, device):+ # Each region's couples are reduced modulo its own number of samples, so a region smaller than the couples+ # need wraps: here (3, 0) becomes (0, 0), a rank-deficient pair that the minimal fit marks as NaN.+ couples = draw_first_k_couples(4, torch.tensor([1000, 3], device=device), device)+ self.assert_close(couples[:, :, 0], torch.tensor([[1, 0], [2, 0], [2, 1], [3, 0]], device=device))+ self.assert_close(couples[:, :, 1], torch.tensor([[1, 0], [2, 0], [2, 1], [0, 0]], device=device))