Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Issue: LightGlue crashes when adaptive width pruning removes all keypoints (#3564). (#3565)

Kiran Dharmar9e6db647ce77
diff --git a/kornia/feature/lightglue.py b/kornia/feature/lightglue.py
index a682536dd..5cd3e3e4e 100644
--- a/kornia/feature/lightglue.py
+++ b/kornia/feature/lightglue.py
@@ -624,6 +624,26 @@ class LightGlue(nn.Module):
desc0 = data0["descriptors"].detach().contiguous()
desc1 = data1["descriptors"].detach().contiguous()
+ # Guard against empty-descriptors after pruning
+ if desc0.shape[1] == 0 or desc1.shape[1] == 0:
+ empty_m0 = torch.full((b, m), -1, device=device, dtype=torch.long)
+ empty_m1 = torch.full((b, n), -1, device=device, dtype=torch.long)
+ empty_s0 = torch.zeros((b, m), device=device)
+ empty_s1 = torch.zeros((b, n), device=device)
+
+ return {
+ "log_assignment": torch.empty((b, 1, 1), device=device),
+ "matches0": empty_m0,
+ "matches1": empty_m1,
+ "matching_scores0": empty_s0,
+ "matching_scores1": empty_s1,
+ "stop": 0,
+ "matches": [torch.empty((0, 2), device=device, dtype=torch.long) for _ in range(b)],
+ "scores": [torch.empty((0,), device=device) for _ in range(b)],
+ "prune0": torch.ones_like(empty_s0) * self.conf.n_layers,
+ "prune1": torch.ones_like(empty_s1) * self.conf.n_layers,
+ }
+
KORNIA_CHECK(desc0.shape[-1] == self.conf.input_dim, "Descriptor dimension does not match input dim in config")
KORNIA_CHECK(desc1.shape[-1] == self.conf.input_dim, "Descriptor dimension does not match input dim in config")
@@ -683,9 +703,11 @@ class LightGlue(nn.Module):
encoding1 = encoding1.index_select(-2, keep1)
prune1[:, ind1] += 1
- desc0, desc1 = desc0[..., :m, :], desc1[..., :n, :]
+ desc0, desc1 = desc0[..., :m, :], desc1[..., :n, :]
+
scores, _ = self.log_assignment[i](desc0, desc1)
m0, m1, mscores0, mscores1 = filter_matches(scores, self.conf.filter_threshold)
+
matches, mscores = [], []
for k in range(b):
valid = m0[k] > -1
diff --git a/tests/feature/test_lightglue.py b/tests/feature/test_lightglue.py
index 57454c2e3..31c960046 100644
--- a/tests/feature/test_lightglue.py
+++ b/tests/feature/test_lightglue.py
@@ -35,6 +35,60 @@ from testing.base import BaseTester
# ---------------------------------------------------------------------------
+def test_lightglue_empty_after_pruning():
+ model = LightGlue(features="superpoint", width_confidence=0.99)
+ model.eval()
+
+ data = {
+ "image0": {
+ "keypoints": torch.empty(1, 0, 2),
+ "descriptors": torch.empty(1, 0, 256),
+ "image_size": torch.tensor([[640, 480]]),
+ },
+ "image1": {
+ "keypoints": torch.empty(1, 0, 2),
+ "descriptors": torch.empty(1, 0, 256),
+ "image_size": torch.tensor([[640, 480]]),
+ },
+ }
+
+ with torch.no_grad():
+ out = model(data)
+
+ assert out["matches0"].shape == (1, 0)
+ assert out["matches1"].shape == (1, 0)
+ assert out["matching_scores0"].shape == (1, 0)
+ assert out["matching_scores1"].shape == (1, 0)
+
+
+def test_lightglue_pruning_removes_all():
+ model = LightGlue(features="superpoint", width_confidence=0.0)
+ model.eval()
+
+ B, M, D = 1, 8, 256
+
+ data = {
+ "image0": {
+ "keypoints": torch.rand(B, M, 2),
+ "descriptors": torch.rand(B, M, D),
+ "image_size": torch.tensor([[640, 480]]),
+ },
+ "image1": {
+ "keypoints": torch.rand(B, M, 2),
+ "descriptors": torch.rand(B, M, D),
+ "image_size": torch.tensor([[640, 480]]),
+ },
+ }
+
+ with torch.no_grad():
+ out = model(data)
+
+ assert "matches0" in out
+ assert "matches1" in out
+ assert out["matches0"].shape == (B, M)
+ assert out["matches1"].shape == (B, M)
+
+
def test_normalize_keypoints_smoke():
kpts = torch.zeros(1, 5, 2)
size = torch.tensor([[100, 200]])