Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix bug and add scale coef (#1808)

Dmytro Mishkinab944b8cf1c2
diff --git a/kornia/feature/integrated.py b/kornia/feature/integrated.py
index 4ea2cc675..0d38e1ce1 100644
--- a/kornia/feature/integrated.py
+++ b/kornia/feature/integrated.py
@@ -1,3 +1,4 @@
+import warnings
from typing import Dict, List, Optional, Tuple
import torch
@@ -10,7 +11,7 @@ from kornia.geometry.transform import ScalePyramid
from .affine_shape import LAFAffNetShapeEstimator
from .hardnet import HardNet
from .keynet import KeyNetDetector
-from .laf import extract_patches_from_pyramid, get_laf_center, raise_error_if_laf_is_not_valid
+from .laf import extract_patches_from_pyramid, get_laf_center, raise_error_if_laf_is_not_valid, scale_laf
from .orientation import LAFOrienter, OriNet, PassLAF
from .responses import BlobDoG, CornerGFTT
from .scale_space_detector import ScaleSpaceDetector
@@ -42,6 +43,9 @@ def get_laf_descriptors(
patch_descriptor.eval()
timg: torch.Tensor = img
+ if lafs.shape[1] == 0:
+ warnings.warn(f"LAF contains no keypoints {lafs.shape}, returning empty tensor")
+ return torch.empty(lafs.shape[0], lafs.shape[1], 128)
if grayscale_descriptor and img.size(1) == 3:
timg = rgb_to_grayscale(img)
@@ -114,12 +118,16 @@ class LocalFeature(nn.Module):
Args:
detector: the detection module.
descriptor: the descriptor module.
+ scaling_coef: multiplier for change default detector scale (e.g. it is too small for KeyNet by default)
"""
- def __init__(self, detector: nn.Module, descriptor: LAFDescriptor) -> None:
+ def __init__(self, detector: nn.Module, descriptor: LAFDescriptor, scaling_coef: float = 1.0) -> None:
super().__init__()
self.detector = detector
self.descriptor = descriptor
+ if scaling_coef <= 0:
+ raise ValueError(f"Scaling coef should be >= 0, got {scaling_coef}")
+ self.scaling_coef = scaling_coef
def forward(
self, img: torch.Tensor, mask: Optional[torch.Tensor] = None
@@ -136,6 +144,7 @@ class LocalFeature(nn.Module):
- Local descriptors of shape :math:`(B,N,D)` where :math:`D` is descriptor size.
"""
lafs, responses = self.detector(img, mask)
+ lafs = scale_laf(lafs, self.scaling_coef)
descs = self.descriptor(img, lafs)
return (lafs, responses, descs)
@@ -190,23 +199,35 @@ class GFTTAffNetHardNet(LocalFeature):
class KeyNetHardNet(LocalFeature):
"""Convenience module, which implements KeyNet detector + HardNet descriptor."""
- def __init__(self, num_features: int = 8000, upright: bool = False, device: torch.device = torch.device('cpu')):
+ def __init__(
+ self,
+ num_features: int = 8000,
+ upright: bool = False,
+ device: torch.device = torch.device('cpu'),
+ scale_laf: float = 1.0,
+ ):
ori_module = PassLAF() if upright else LAFOrienter(angle_detector=OriNet(True))
detector = KeyNetDetector(True, num_features=num_features, ori_module=ori_module).to(device)
descriptor = LAFDescriptor(None, patch_size=32, grayscale_descriptor=True).to(device)
- super().__init__(detector, descriptor)
+ super().__init__(detector, descriptor, scale_laf)
class KeyNetAffNetHardNet(LocalFeature):
"""Convenience module, which implements KeyNet detector + AffNet + HardNet descriptor."""
- def __init__(self, num_features: int = 8000, upright: bool = False, device: torch.device = torch.device('cpu')):
+ def __init__(
+ self,
+ num_features: int = 8000,
+ upright: bool = False,
+ device: torch.device = torch.device('cpu'),
+ scale_laf: float = 1.0,
+ ):
ori_module = PassLAF() if upright else LAFOrienter(angle_detector=OriNet(True))
detector = KeyNetDetector(
True, num_features=num_features, ori_module=ori_module, aff_module=LAFAffNetShapeEstimator(True).eval()
).to(device)
descriptor = LAFDescriptor(None, patch_size=32, grayscale_descriptor=True).to(device)
- super().__init__(detector, descriptor)
+ super().__init__(detector, descriptor, scale_laf)
class LocalFeatureMatcher(nn.Module):
diff --git a/test/feature/test_integrated.py b/test/feature/test_integrated.py
index c573538dd..5014a947e 100644
--- a/test/feature/test_integrated.py
+++ b/test/feature/test_integrated.py
@@ -17,7 +17,10 @@ from kornia.feature import (
SIFTDescriptor,
SIFTFeature,
extract_patches_from_pyramid,
+ get_laf_center,
get_laf_descriptors,
+ get_laf_orientation,
+ get_laf_scale,
)
from kornia.feature.integrated import LocalFeatureMatcher
from kornia.geometry import RANSAC, resize, transform_points
@@ -87,6 +90,16 @@ class TestLAFDescriptor:
descs_reference = sift(patches.view(B1 * N1, CH1, H1, W1)).view(B1, N1, -1)
assert_close(descs_test, descs_reference)
+ def test_empty(self, device):
+ B, C, H, W = 1, 1, 32, 32
+ PS = 16
+ img = torch.rand(B, C, H, W, device=device)
+ lafs = torch.zeros(B, 0, 2, 3, device=device)
+ sift = SIFTDescriptor(PS).to(device)
+ lafsift = LAFDescriptor(sift, PS)
+ descs_test = lafsift(img, lafs)
+ assert descs_test.shape == (B, 0, 128)
+
def test_gradcheck(self, device):
B, C, H, W = 1, 1, 32, 32
PS = 16
@@ -131,6 +144,20 @@ class TestLocalFeature:
descs1 = desc(patches.view(B1 * N1, CH1, H1, W1)).view(B1, N1, -1)
assert_close(descs, descs1)
+ def test_scale(self, device, dtype):
+ B, C, H, W = 1, 1, 64, 64
+ PS = 16
+ img = torch.rand(B, C, H, W, device=device, dtype=dtype)
+ det = ScaleSpaceDetector(10)
+ desc = SIFTDescriptor(PS)
+ local_feature = LocalFeature(det, LAFDescriptor(desc, PS), 1.0).to(device, dtype)
+ local_feature2 = LocalFeature(det, LAFDescriptor(desc, PS), 2.0).to(device, dtype)
+ lafs, responses, descs = local_feature(img)
+ lafs2, responses2, descs2 = local_feature2(img)
+ assert_close(get_laf_center(lafs), get_laf_center(lafs2))
+ assert_close(get_laf_orientation(lafs), get_laf_orientation(lafs2))
+ assert_close(2.0 * get_laf_scale(lafs), get_laf_scale(lafs2))
+
@pytest.mark.skip("Takes too long time (but works)")
def test_gradcheck(self, device):
B, C, H, W = 1, 1, 32, 32