Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix AdaLAM crash (#1881)

Dmytro Mishkin6f5e98734d8d
diff --git a/kornia/feature/adalam/adalam.py b/kornia/feature/adalam/adalam.py
index fc5e5e211..6fd618313 100644
--- a/kornia/feature/adalam/adalam.py
+++ b/kornia/feature/adalam/adalam.py
@@ -15,6 +15,18 @@ from .core import adalam_core
from .utils import dist_matrix
+def _no_match(dm: Tensor):
+ """Helper function, which output empty tensors.
+
+ Returns:
+ - Descriptor distance of matching descriptors, shape of :math:`(0, 1)`.
+ - Long tensor indexes of matching descriptors in desc1 and desc2, shape of :math:`(0, 2)`.
+ """
+ dists = torch.empty(0, 1, device=dm.device, dtype=dm.dtype)
+ idxs = torch.empty(0, 2, device=dm.device, dtype=torch.long)
+ return dists, idxs
+
+
def get_adalam_default_config():
DEFAULT_CONFIG = {
'area_ratio': 100, # Ratio between seed circle area and image area. Higher values produce more seeds with smaller neighborhoods. # noqa: E501
@@ -235,6 +247,8 @@ class AdalamFilter:
"Please either provide orientations or set 'orientation_difference_threshold' to None to disable orientations filtering" # noqa: E501
)
k1, k2, d1, d2, o1, o2, s1, s2 = self.__to_torch(k1, k2, d1, d2, o1, o2, s1, s2)
+ if len(d2) <= 1:
+ return _no_match(d1)
distmat = dist_matrix(d1, d2, is_normalized=False)
dd12, nn12 = torch.topk(distmat, k=2, dim=1, largest=False) # (n1, 2)
diff --git a/test/feature/test_matching.py b/test/feature/test_matching.py
index 52ddba58d..db018a90d 100644
--- a/test/feature/test_matching.py
+++ b/test/feature/test_matching.py
@@ -338,6 +338,16 @@ class TestAdalam:
expected_idxs = data_dev['expected_idxs'].long()
assert_close(idxs, expected_idxs, rtol=1e-4, atol=1e-4)
+ @pytest.mark.parametrize("data", ["adalam_idxs"], indirect=True)
+ def test_single_nocrash(self, device, dtype, data):
+ torch.random.manual_seed(0)
+ # This is not unit test, but that is quite good integration test
+ data_dev = utils.dict_to(data, device, dtype)
+ with torch.no_grad():
+ dists, idxs = match_adalam(
+ data_dev['descs1'], data_dev['descs2'][:1], data_dev['lafs1'], data_dev['lafs2'][:, :1]
+ )
+
@pytest.mark.parametrize("data", ["adalam_idxs"], indirect=True)
def test_module(self, device, dtype, data):
torch.random.manual_seed(0)