Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(feature): match (1, N, D) LightGlue descriptors like (N, D) (#6529)

小卑呔克507be3d51744
diff --git a/changelog.d/6529.fixed.md b/changelog.d/6529.fixed.md
new file mode 100644
index 000000000..b341b2350
--- /dev/null
+++ b/changelog.d/6529.fixed.md
@@ -0,0 +1 @@
+`LightGlueMatcher` accepts descriptors with a batch dimension of one, `(1, N, D)`, and returns the same matches as the unbatched `(N, D)` layout. The fewer-than-two-descriptors early exit read `shape[0]` as the descriptor count, so for a 3-D tensor it saw the batch size and silently returned no matches.
diff --git a/kornia/feature/integrated.py b/kornia/feature/integrated.py
index b5cf5bccd..d070a829d 100644
--- a/kornia/feature/integrated.py
+++ b/kornia/feature/integrated.py
@@ -1057,8 +1057,8 @@ class LightGlueMatcher(GeometryAwareDescriptorMatcher):
"""Run forward.
Args:
- desc1: Batch of descriptors of a shape ``(B1, D)``.
- desc2: Batch of descriptors of a shape ``(B2, D)``.
+ desc1: Batch of descriptors of a shape ``(B1, D)``, or ``(1, B1, D)`` for a batch of one.
+ desc2: Batch of descriptors of a shape ``(B2, D)``, or ``(1, B2, D)`` for a batch of one.
lafs1: LAFs of a shape ``(1, B1, 2, 3)``.
lafs2: LAFs of a shape ``(1, B2, 2, 3)``.
hw1: ``(H, W)`` of the first image; see the Convention block.
@@ -1070,7 +1070,7 @@ class LightGlueMatcher(GeometryAwareDescriptorMatcher):
shape of ``(B3, 2)`` where :math:`0 <= B3 <= B1`.
"""
- if (desc1.shape[0] < 2) or (desc2.shape[0] < 2):
+ if (desc1.shape[-2] < 2) or (desc2.shape[-2] < 2):
return _no_match(desc1)
keypoints1 = get_laf_center(lafs1)
keypoints2 = get_laf_center(lafs2)
diff --git a/tests/feature/test_integrated.py b/tests/feature/test_integrated.py
index c947fcfae..a4a9a0402 100644
--- a/tests/feature/test_integrated.py
+++ b/tests/feature/test_integrated.py
@@ -896,6 +896,39 @@ class TestDoGHardNet(BaseTester):
self.assert_close(seen["image0"]["scales"] * scale_coef, expected, low_tolerance=preset == "DoGAffNetHardNet")
+class TestLightGlueMatcherBatch(BaseTester):
+ def test_batch_of_one_descriptors_match_like_unbatched(self, device, dtype, monkeypatch):
+ from kornia.feature import LightGlueMatcher
+
+ class _RecordingLightGlue(nn.Module):
+ # Stands in for the network, whose construction downloads weights. It pairs descriptors row by row, so
+ # both layouts return the same matches if they reach it.
+ def __init__(self, *args, **kwargs):
+ super().__init__()
+
+ def forward(self, data):
+ desc0 = data["image0"]["descriptors"]
+ desc1 = data["image1"]["descriptors"]
+ count = min(desc0.shape[1], desc1.shape[1])
+ matches0 = torch.full((desc0.shape[0], desc0.shape[1]), -1, device=desc0.device)
+ matches0[:, :count] = torch.arange(count, device=desc0.device)
+ return {"matches0": matches0, "matching_scores0": torch.ones_like(matches0, dtype=torch.float32)}
+
+ monkeypatch.setattr("kornia.feature.integrated.LightGlue", _RecordingLightGlue)
+ matcher = LightGlueMatcher("disk")
+ centers = torch.rand(1, 5, 2, device=device, dtype=dtype)
+ lafs = kornia.feature.laf_from_center_scale_ori(centers)
+ descriptors = torch.rand(5, 8, device=device, dtype=dtype)
+ unbatched_scores, unbatched_matches = matcher(descriptors, descriptors, lafs, lafs, (10, 20), (10, 20))
+ batched_scores, batched_matches = matcher(descriptors[None], descriptors[None], lafs, lafs, (10, 20), (10, 20))
+ # (1, N, D) used to take the fewer-than-two-descriptors early exit, which read the batch size (1) as the
+ # descriptor count and silently returned no matches.
+ assert unbatched_matches.shape == (5, 2)
+ assert batched_matches.shape == unbatched_matches.shape
+ self.assert_close(batched_scores, unbatched_scores)
+ assert torch.equal(batched_matches, unbatched_matches)
+
+
class TestLocalFeatureMatcher(BaseTester):
def test_smoke(self, device):
matcher = LocalFeatureMatcher(SIFTFeature(5), DescriptorMatcher("snn", 0.8)).to(device)