Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Adalam fix2 (#1888)

Dmytro Mishkin2b4052cfe61c
diff --git a/kornia/feature/adalam/adalam.py b/kornia/feature/adalam/adalam.py
index 6fd618313..9e30a84d7 100644
--- a/kornia/feature/adalam/adalam.py
+++ b/kornia/feature/adalam/adalam.py
@@ -11,22 +11,10 @@ from kornia.feature.laf import get_laf_center, get_laf_orientation, get_laf_scal
from kornia.testing import KORNIA_CHECK_LAF, KORNIA_CHECK_SHAPE
from kornia.utils.helpers import get_cuda_device_if_available
-from .core import adalam_core
+from .core import _no_match, 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
@@ -248,7 +236,10 @@ class AdalamFilter:
)
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)
+ idxs, dists = _no_match(d1)
+ if return_dist:
+ return idxs, dists
+ return idxs
distmat = dist_matrix(d1, d2, is_normalized=False)
dd12, nn12 = torch.topk(distmat, k=2, dim=1, largest=False) # (n1, 2)
diff --git a/kornia/feature/adalam/core.py b/kornia/feature/adalam/core.py
index e351080e6..3f66d264b 100644
--- a/kornia/feature/adalam/core.py
+++ b/kornia/feature/adalam/core.py
@@ -8,6 +8,18 @@ from .ransac import ransac
from .utils import dist_matrix, orientation_diff
+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 select_seeds(dist1: Tensor, R1: Union[float, Tensor], scores1: Tensor, fnn12: Tensor, mnn: Optional[Tensor]):
"""Select seed correspondences among the set of available matches.
@@ -288,9 +300,18 @@ def adalam_core(
if rdims.shape[0] == 0:
# No seed point survived. Just output ratio-test matches. This should happen very rarely.
- absolute_im1idx = torch.where(scores1 < 0.8**2)[0]
- absolute_im2idx = fnn12[absolute_im1idx]
- return torch.stack([absolute_im1idx, absolute_im2idx], dim=1)
+ score_mask = scores1 <= 0.95
+ absolute_im1idx = torch.where(score_mask)[0]
+ if len(absolute_im1idx) > 0:
+ absolute_im2idx = fnn12[absolute_im1idx]
+ out_scores = scores1[score_mask].reshape(-1, 1)
+ idxs = torch.stack([absolute_im1idx, absolute_im2idx], dim=1)
+ else:
+ idxs, out_scores = _no_match(scores1)
+ if return_dist:
+ return idxs, out_scores
+ else:
+ return idxs
# Format neighborhoods for parallel RANSACs
im1loc, im2loc, ransidx, tokp1, tokp2 = extract_local_patterns(
diff --git a/test/feature/test_matching.py b/test/feature/test_matching.py
index db018a90d..1c06483a1 100644
--- a/test/feature/test_matching.py
+++ b/test/feature/test_matching.py
@@ -348,6 +348,19 @@ class TestAdalam:
data_dev['descs1'], data_dev['descs2'][:1], data_dev['lafs1'], data_dev['lafs2'][:, :1]
)
+ @pytest.mark.parametrize("data", ["adalam_idxs"], indirect=True)
+ def test_seeds_fail(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'][:100],
+ data_dev['descs2'][:100],
+ data_dev['lafs1'][:, :100],
+ data_dev['lafs2'][:, :100],
+ )
+
@pytest.mark.parametrize("data", ["adalam_idxs"], indirect=True)
def test_module(self, device, dtype, data):
torch.random.manual_seed(0)