Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Make AdaLAM output match confidence (#1862)

Dmytro Mishkinffef7416845a
diff --git a/kornia/feature/adalam/adalam.py b/kornia/feature/adalam/adalam.py
index 1f9c4a8ef..fc5e5e211 100644
--- a/kornia/feature/adalam/adalam.py
+++ b/kornia/feature/adalam/adalam.py
@@ -2,7 +2,7 @@
# https://github.com/cavalli1234/AdaLAM
# Copyright (c) 2020, Luca Cavalli
-from typing import Dict, Optional, Tuple
+from typing import Dict, Optional, Tuple, Union
import torch
from torch import Tensor
@@ -72,7 +72,7 @@ def match_adalam(
else:
config_ = config
adalam_object = AdalamFilter(config_)
- idxs = adalam_object.match_and_filter(
+ idxs, quality = adalam_object.match_and_filter(
get_laf_center(lafs1).reshape(-1, 2),
get_laf_center(lafs2).reshape(-1, 2),
desc1,
@@ -83,8 +83,8 @@ def match_adalam(
get_laf_orientation(lafs2).reshape(-1),
get_laf_scale(lafs1).reshape(-1),
get_laf_scale(lafs2).reshape(-1),
+ return_dist=True,
)
- quality = torch.ones(len(idxs), 1, dtype=desc1.dtype, device=desc1.device)
return quality, idxs
@@ -123,7 +123,8 @@ class AdalamFilter:
o2: torch.Tensor = None,
s1: torch.Tensor = None,
s2: torch.Tensor = None,
- ):
+ return_dist: bool = False,
+ ) -> Union[Tuple[Tensor, Tensor], Tensor]:
"""Call the core functionality of AdaLAM, i.e. just outlier filtering. No sanity check is performed on the
inputs.
@@ -151,6 +152,7 @@ class AdalamFilter:
s1/s2: keypoint scales. They can be None if 'scale_rate_threshold' in config is set to None.
See documentation on 'scale_rate_threshold' in the DEFAULT_CONFIG.
Expected a float32 tensor with shape (num_keypoints_in_source/destination_image,)
+ return_dist: if True, inverse confidence value is also outputted.
Returns:
Filtered putative matches.
@@ -170,9 +172,23 @@ class AdalamFilter:
s1=s1,
s2=s2,
config=self.config,
+ return_dist=return_dist,
)
- def match_and_filter(self, k1, k2, d1, d2, im1shape=None, im2shape=None, o1=None, o2=None, s1=None, s2=None):
+ def match_and_filter(
+ self,
+ k1,
+ k2,
+ d1,
+ d2,
+ im1shape=None,
+ im2shape=None,
+ o1=None,
+ o2=None,
+ s1=None,
+ s2=None,
+ return_dist: bool = False,
+ ):
"""Standard matching and filtering with AdaLAM. This function:
- performs some elementary sanity check on the inputs;
@@ -200,6 +216,7 @@ class AdalamFilter:
s1/s2: keypoint scales. They can be None if 'scale_rate_threshold' in config is set to None.
See documentation on 'scale_rate_threshold' in the DEFAULT_CONFIG.
Expected an array with shape (num_keypoints_in_source/destination_image,)
+ return_dist: if True, inverse confidence value is also outputted.
Returns:
Filtered putative matches.
@@ -229,7 +246,9 @@ class AdalamFilter:
else:
mnn = None
- return self.filter_matches(k1, k2, putative_matches, scores, mnn, im1shape, im2shape, o1, o2, s1, s2)
+ return self.filter_matches(
+ k1, k2, putative_matches, scores, mnn, im1shape, im2shape, o1, o2, s1, s2, return_dist
+ )
def __to_torch(self, *args):
return (
diff --git a/kornia/feature/adalam/core.py b/kornia/feature/adalam/core.py
index 0bb929d17..7860bc387 100644
--- a/kornia/feature/adalam/core.py
+++ b/kornia/feature/adalam/core.py
@@ -203,7 +203,8 @@ def adalam_core(
o2: Optional[Tensor] = None,
s1: Optional[Tensor] = None,
s2: Optional[Tensor] = None,
-):
+ return_dist: bool = False,
+) -> Union[Tuple[Tensor, Tensor], Tensor]:
"""Call the core functionality of AdaLAM, i.e. just outlier filtering. No sanity check is performed on the
inputs.
@@ -231,10 +232,11 @@ def adalam_core(
s1/s2: keypoint scales. They can be None if 'scale_rate_threshold' in config is set to None.
See documentation on 'scale_rate_threshold' in the DEFAULT_CONFIG.
Expected a float32 tensor with shape (num_keypoints_in_source/destination_image,)
+ return_dist: if True, inverse confidence value is also outputed. Default is False
Returns:
- Filtered putative matches.
- A long tensor with shape (num_filtered_matches, 2) with indices of corresponding keypoints in k1 and k2.
+ idxs: A long tensor with shape (num_filtered_matches, 2) with indices of corresponding keypoints in k1 and k2.
+ dists: inverse confidence ratio.
""" # noqa: E501
AREA_RATIO = config['area_ratio']
SEARCH_EXP = config['search_expansion']
@@ -304,13 +306,23 @@ def adalam_core(
conf = inl_confidence[ransidx[inlier_idx]]
cnt = inlier_counts[ransidx[inlier_idx]].float()
- passed_inliers_mask = (conf >= MIN_CONF) & (cnt * (1 - 1 / conf) >= MIN_INLIERS)
+ dist_ratio = 1.0 / conf
+ passed_inliers_mask = (conf >= MIN_CONF) & (cnt * (1 - dist_ratio) >= MIN_INLIERS)
accepted_inliers = inlier_idx[passed_inliers_mask]
+ accepted_dist = dist_ratio[passed_inliers_mask]
absolute_im1idx = tokp1[accepted_inliers]
absolute_im2idx = tokp2[accepted_inliers]
final_matches = torch.stack([absolute_im1idx, absolute_im2idx], dim=1)
if final_matches.shape[0] > 1:
- return torch.unique(final_matches, dim=0)
+ # https://stackoverflow.com/a/72005790
+ final_matches, idxs, counts = torch.unique(final_matches, dim=0, return_inverse=True, return_counts=True)
+ _, ind_sorted = torch.sort(idxs)
+ cum_sum = counts.cumsum(0)
+ cum_sum = torch.cat((torch.tensor([0]), cum_sum[:-1]))
+ first_indicies = ind_sorted[cum_sum]
+ accepted_dist = accepted_dist[first_indicies]
+ if return_dist:
+ return final_matches, accepted_dist.reshape(-1, 1)
return final_matches