Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(feature): match_adalam no-match output order and match_fginn second-neighbour masking (#5860)

Dmytro Mishkin02f44b4d1cce
diff --git a/changelog.d/5860.fixed.md b/changelog.d/5860.fixed.md
new file mode 100644
index 000000000..c5908ed4c
--- /dev/null
+++ b/changelog.d/5860.fixed.md
@@ -0,0 +1,3 @@
+`match_adalam`, `GeometryAwareDescriptorMatcher("adalam")`, `AdalamFilter.match_and_filter` and `AdalamFilter.filter_matches` now return their outputs in the usual order when nothing matches: with fewer than two descriptors on either side, or when no seed survives and no ratio-test match remains, `match_adalam` used to return the `(0, 2)` int64 indices first and the `(0, 1)` float32 distances second, and `AdalamFilter.match_and_filter`, and `AdalamFilter.filter_matches` when no seed survives, returned the float `(0, 1)` tensor as their indices.
+
+`match_fginn` now masks out the candidates within `spatial_th` of the nearest neighbour when it picks the second neighbour, so the ratio no longer depends on the scale of the distances. It used to add `1e6` to their distance, so with distances of that order, such as squared distances between unnormalised SIFT descriptors, one of them could still be the second neighbour. When every candidate is within `spatial_th`, the match is accepted with ratio `0`; it used to be the nearest distance over the second-nearest plus `1e6`, which is close to `0` only for small distances.
diff --git a/kornia/feature/adalam/adalam.py b/kornia/feature/adalam/adalam.py
index 6aaac1de3..fa5d872f4 100644
--- a/kornia/feature/adalam/adalam.py
+++ b/kornia/feature/adalam/adalam.py
@@ -309,7 +309,7 @@ class AdalamFilter:
)
if (len(_d2) <= 1) or (len(_d1) <= 1):
- idxs, dists = _no_match(_d1)
+ dists, idxs = _no_match(_d1)
if return_dist:
return idxs, dists
return idxs
diff --git a/kornia/feature/adalam/core.py b/kornia/feature/adalam/core.py
index 38dc97941..167790e53 100644
--- a/kornia/feature/adalam/core.py
+++ b/kornia/feature/adalam/core.py
@@ -384,7 +384,7 @@ def adalam_core(
out_scores = scores1[score_mask].reshape(-1, 1)
idxs = torch.stack([absolute_im1idx, absolute_im2idx], dim=1)
else:
- idxs, out_scores = _no_match(scores1)
+ out_scores, idxs = _no_match(scores1)
if return_dist:
return idxs, out_scores
return idxs
diff --git a/kornia/feature/matching.py b/kornia/feature/matching.py
index 22e9295f4..4ff4161a5 100644
--- a/kornia/feature/matching.py
+++ b/kornia/feature/matching.py
@@ -396,11 +396,11 @@ def match_fginn(
If the distance matrix dm is not provided, :py:func:`torch.cdist` is used.
.. note::
- The geometric check looks at the ``min(10, B2)`` nearest candidates and penalizes every one of them
+ The geometric check looks at the ``min(10, B2)`` nearest candidates and excludes every one of them
that lies within ``spatial_th`` pixels of the query's own 1st nearest neighbor. When *all* of them do --
- a dense cluster of detections on one structure -- the effective 2nd nearest neighbor distance saturates,
- the ratio collapses towards zero and the match is **accepted**. That is the intended reading: no distinct
- competing structure among the candidates means the 1st nearest neighbor is unambiguous.
+ a dense cluster of detections on one structure -- no candidate is left to be the 2nd nearest neighbor, the
+ ratio is 0 and the match is **accepted**. That is the intended reading: no distinct competing structure among
+ the candidates means the 1st nearest neighbor is unambiguous.
Args:
desc1: Batch of descriptors of a shape :math:`(B1, D)`.
@@ -423,10 +423,8 @@ def match_fginn(
"""
KORNIA_CHECK_SHAPE(desc1, ["B", "DIM"])
KORNIA_CHECK_SHAPE(desc2, ["B", "DIM"])
- BIG_NUMBER = 1000000.0
distance_matrix = _get_lazy_distance_matrix(desc1, desc2, dm)
- dtype = distance_matrix.dtype
if desc2.shape[0] < 2: # We cannot perform snn check, so output empty matches
return _no_match(distance_matrix)
@@ -439,7 +437,10 @@ def match_fginn(
# Distance from every candidate to the 1st nearest neighbour *of the same query*.
# Indexing dim 0 here would take query 0's candidate list and broadcast it over all queries.
kdist = torch.norm(candidates_xy - candidates_xy[:, 0:1], p=2, dim=2)
- fginn_vals = vals_cand[:, 1:] + (kdist[:, 1:] < spatial_th).to(dtype) * BIG_NUMBER
+ # Candidates closer than spatial_th to the 1st nearest neighbour are masked out, whatever the scale of the
+ # distances. When none is left, the 2nd nearest neighbour distance is inf and the ratio is 0. torch.where also
+ # promotes an integer dm to the default floating dtype, which can hold inf.
+ fginn_vals = torch.where(kdist[:, 1:] < spatial_th, float("inf"), vals_cand[:, 1:])
vals_2nd, _ = fginn_vals.min(dim=1)
idxs_in_2 = idxs_in_2[:, 0]
diff --git a/tests/feature/test_matching.py b/tests/feature/test_matching.py
index 94803b128..bcc500146 100644
--- a/tests/feature/test_matching.py
+++ b/tests/feature/test_matching.py
@@ -22,8 +22,9 @@ import torch
from kornia.core._compat import torch_version_le
from kornia.feature import matching
+from kornia.feature.adalam import AdalamFilter, get_adalam_default_config
from kornia.feature.integrated import LightGlueMatcher
-from kornia.feature.laf import laf_from_center_scale_ori
+from kornia.feature.laf import get_laf_center, get_laf_orientation, get_laf_scale, laf_from_center_scale_ori
from kornia.feature.lightglue import LightGlue
from kornia.feature.matching import (
DescriptorMatcher,
@@ -1196,3 +1197,173 @@ class TestMatchSMNNBatched(BaseTester):
a = torch.rand(2, 3, 4, device=device, dtype=torch.float64)
b = torch.rand(2, 4, 4, device=device, dtype=torch.float64)
self.gradcheck(matching.match_smnn_batched, (a, b, 1.0))
+
+
+class TestMatchAdalamNoMatch(BaseTester):
+ """The paths of AdaLAM that find nothing to match return ``(dists, idxs)`` like the others (#5848)."""
+
+ HW = (180, 320)
+
+ def _scene(self, device, dtype):
+ # One-hot descriptors: every distance between the two sets is sqrt(2), so every ratio is 1 > 0.95, no seed
+ # survives and no ratio-test match remains.
+ d1 = torch.eye(12, device=device, dtype=dtype)[:6]
+ d2 = torch.eye(12, device=device, dtype=dtype)[6:]
+ g = torch.Generator().manual_seed(4)
+ size = torch.tensor([self.HW[1] - 10.0, self.HW[0] - 10.0])
+ lafs1, lafs2 = (
+ laf_from_center_scale_ori(torch.rand(1, 6, 2, generator=g) * size + 5.0).to(device, dtype) for _ in range(2)
+ )
+ return d1, d2, lafs1, lafs2
+
+ def _case(self, case, device, dtype):
+ d1, d2, lafs1, lafs2 = self._scene(device, dtype)
+ if case == "empty desc1":
+ return d1[:0], d2, lafs1[:, :0], lafs2
+ if case == "empty desc2":
+ return d1, d2[:0], lafs1, lafs2[:, :0]
+ if case == "one row in desc1":
+ return d1[:1], d2, lafs1[:, :1], lafs2
+ if case == "one row in desc2":
+ return d1, d2[:1], lafs1, lafs2[:, :1]
+ return d1, d2, lafs1, lafs2
+
+ def _assert_no_match(self, dists, idxs, device):
+ # AdaLAM computes in float32 whatever the descriptor dtype, and so does its output when something matches
+ assert dists.shape == (0, 1)
+ assert dists.dtype == torch.float32
+ assert idxs.shape == (0, 2)
+ assert idxs.dtype == torch.long
+ assert dists.device.type == idxs.device.type == device.type
+
+ CASES = ("empty desc1", "empty desc2", "one row in desc1", "one row in desc2", "no seed and no ratio-test match")
+
+ @pytest.mark.parametrize("case", CASES)
+ def test_match_adalam(self, device, dtype, case):
+ d1, d2, lafs1, lafs2 = self._case(case, device, dtype)
+ dists, idxs = match_adalam(d1, d2, lafs1, lafs2, hw1=self.HW, hw2=self.HW)
+ self._assert_no_match(dists, idxs, device)
+
+ @pytest.mark.parametrize("case", CASES)
+ def test_geometry_aware_descriptor_matcher(self, device, dtype, case):
+ d1, d2, lafs1, lafs2 = self._case(case, device, dtype)
+ dists, idxs = GeometryAwareDescriptorMatcher("adalam")(d1, d2, lafs1, lafs2)
+ self._assert_no_match(dists, idxs, device)
+
+ @pytest.mark.parametrize("case", CASES)
+ def test_adalam_filter_match_and_filter(self, device, dtype, case):
+ d1, d2, lafs1, lafs2 = self._case(case, device, dtype)
+ config = get_adalam_default_config()
+ config["device"] = device
+ args = (
+ get_laf_center(lafs1).reshape(-1, 2),
+ get_laf_center(lafs2).reshape(-1, 2),
+ d1,
+ d2,
+ self.HW,
+ self.HW,
+ get_laf_orientation(lafs1).reshape(-1),
+ get_laf_orientation(lafs2).reshape(-1),
+ get_laf_scale(lafs1).reshape(-1),
+ get_laf_scale(lafs2).reshape(-1),
+ )
+ # AdalamFilter returns the indices first
+ idxs, dists = AdalamFilter(config).match_and_filter(*args, return_dist=True)
+ self._assert_no_match(dists, idxs, device)
+ idxs = AdalamFilter(config).match_and_filter(*args, return_dist=False)
+ assert idxs.shape == (0, 2)
+ assert idxs.dtype == torch.long
+
+ def test_no_match_follows_the_output_of_a_match(self, device, dtype):
+ # An exact copy of desc1[0] in desc2 gives one ratio-test match (ratio 0) on the no-seed path: the empty output
+ # has its dtypes and device.
+ d1, d2, lafs1, lafs2 = self._scene(device, dtype)
+ d2 = torch.cat([d1[:1], d2[1:]])
+ dists, idxs = match_adalam(d1, d2, lafs1, lafs2, hw1=self.HW, hw2=self.HW)
+ assert idxs.tolist() == [[0, 0]]
+ assert dists.tolist() == [[0.0]]
+ empty_dists, empty_idxs = match_adalam(d1[:1], d2, lafs1[:, :1], lafs2, hw1=self.HW, hw2=self.HW)
+ assert (empty_dists.dtype, empty_dists.device) == (dists.dtype, dists.device)
+ assert (empty_idxs.dtype, empty_idxs.device) == (idxs.dtype, idxs.device)
+
+
+class TestMatchFGINNSecondNeighbour(BaseTester):
+ """The second neighbour of FGINN is the nearest candidate at least ``spatial_th`` from the nearest one (#5849)."""
+
+ @staticmethod
+ def _lafs(xy, device, dtype):
+ centers = torch.tensor([xy], device=device, dtype=dtype)
+ return laf_from_center_scale_ori(centers, torch.full((1, len(xy), 1, 1), 4.0, device=device, dtype=dtype))
+
+ @staticmethod
+ def _scales(dtype):
+ # Powers of two keep dm exact in every dtype. Float16 cannot hold distances of order 1e6.
+ big = 2.0**20 if torch.finfo(dtype).max > 1e7 else 2.0**13
+ return (1.0 / big, 1.0, big)
+
+ def _match(self, dm, xy2, device, dtype, **kwargs):
+ lafs1 = self._lafs([[10.0, 10.0]], device, dtype)
+ lafs2 = self._lafs(xy2, device, dtype)
+ desc1 = torch.zeros(1, 2, device=device, dtype=dtype)
+ desc2 = torch.zeros(len(xy2), 2, device=device, dtype=dtype)
+ return match_fginn(desc1, desc2, lafs1, lafs2, th=0.8, spatial_th=10.0, dm=dm, **kwargs)
+
+ # desc2 index 0 is the nearest neighbour, index 1 lies 2 px from it, index 2 lies 90 px from it
+ NEAR_AND_FAR = [[10.0, 10.0], [12.0, 10.0], [100.0, 10.0]]
+ # every candidate lies within spatial_th = 10 of the nearest neighbour
+ ALL_NEAR = [[10.0, 10.0], [12.0, 10.0], [13.0, 11.0]]
+
+ def test_a_candidate_near_the_nearest_neighbour_is_never_the_second_at_any_scale(self, device, dtype):
+ for scale in self._scales(dtype):
+ dm = torch.tensor([[2.0, 2.5, 5.0]], device=device, dtype=dtype) * scale
+ dists, idxs = self._match(dm, self.NEAR_AND_FAR, device, dtype)
+ assert idxs.tolist() == [[0, 0]]
+ # 2 / 5, not 2 / 2.5: the ratio does not depend on the scale of dm
+ self.assert_close(dists, torch.tensor([[0.4]], device=device, dtype=dtype))
+
+ @pytest.mark.parametrize("mutual", [False, True])
+ def test_all_candidates_near_the_nearest_neighbour_give_ratio_zero_at_any_scale(self, device, dtype, mutual):
+ for scale in self._scales(dtype):
+ dm = torch.tensor([[0.5, 0.75, 1.0]], device=device, dtype=dtype) * scale
+ dists, idxs = self._match(dm, self.ALL_NEAR, device, dtype, mutual=mutual)
+ assert idxs.tolist() == [[0, 0]]
+ # there is no second neighbour: the match is accepted with ratio 0, never nan
+ assert dists.tolist() == [[0.0]]
+
+ @pytest.mark.parametrize("int_dtype", [torch.int32, torch.int64])
+ def test_integer_distance_matrix(self, device, int_dtype):
+ # an integer dm, such as Hamming distances between binary descriptors
+ for xy2, expected in ((self.NEAR_AND_FAR, 0.4), (self.ALL_NEAR, 0.0)):
+ dm = torch.tensor([[2, 3, 5]], device=device, dtype=int_dtype)
+ dists, idxs = self._match(dm, xy2, device, torch.float32)
+ assert idxs.tolist() == [[0, 0]]
+ # promoted to the default floating dtype
+ assert dists.dtype == torch.get_default_dtype()
+ self.assert_close(dists, torch.tensor([[expected]], device=device))
+
+ def test_gradients_are_finite(self, device, dtype):
+ for xy2, expected in ((self.NEAR_AND_FAR, [[0.2, 0.0, -0.08]]), (self.ALL_NEAR, [[0.0, 0.0, 0.0]])):
+ dm = torch.tensor([[2.0, 2.5, 5.0]], device=device, dtype=dtype, requires_grad=True)
+ dists, _ = self._match(dm, xy2, device, dtype)
+ dists.sum().backward()
+ # d(dm[0] / dm[2]) / d dm = (1 / dm[2], 0, -dm[0] / dm[2] ** 2)
+ self.assert_close(dm.grad, torch.tensor(expected, device=device, dtype=dtype))
+
+ def test_gradcheck(self, device):
+ lafs1 = self._lafs([[10.0, 10.0]], device, torch.float64)
+ lafs2 = self._lafs(self.NEAR_AND_FAR, device, torch.float64)
+ desc = torch.zeros(3, 2, device=device, dtype=torch.float64)
+ dm = torch.tensor([[2.0, 2.5, 5.0]], device=device, dtype=torch.float64)
+ self.gradcheck(lambda dm: match_fginn(desc[:1], desc, lafs1, lafs2, 0.8, 10.0, dm=dm)[0], (dm,))
+
+ def test_dynamo(self, device, dtype, torch_optimizer):
+ lafs1 = self._lafs([[10.0, 10.0]], device, dtype)
+ desc = torch.zeros(3, 2, device=device, dtype=dtype)
+ op = torch_optimizer(match_fginn)
+ for xy2 in (self.NEAR_AND_FAR, self.ALL_NEAR):
+ lafs2 = self._lafs(xy2, device, dtype)
+ dm = torch.tensor([[2.0, 2.5, 5.0], [2.5, 2.0, 5.0]], device=device, dtype=dtype)
+ expected = match_fginn(desc[:2], desc, lafs1.expand(1, 2, 2, 3), lafs2, 0.8, 10.0, dm=dm)
+ actual = op(desc[:2], desc, lafs1.expand(1, 2, 2, 3), lafs2, 0.8, 10.0, dm=dm)
+ self.assert_close(actual[0], expected[0])
+ self.assert_close(actual[1], expected[1])