π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Refactored setting the LAF orientation into separate function (#869)
Dmytro Mishkin3a77a09c96fc
diff --git a/kornia/feature/__init__.py b/kornia/feature/__init__.pyindex 7c48effbc..fb7d53114 100644--- a/kornia/feature/__init__.py+++ b/kornia/feature/__init__.py@@ -26,6 +26,7 @@ from .laf import (extract_patches_from_pyramid,get_laf_scale,get_laf_center,get_laf_orientation,+ set_laf_orientation,raise_error_if_laf_is_not_valid,laf_from_center_scale_ori,laf_is_inside_image,@@ -67,6 +68,7 @@ __all__ = ["get_laf_scale","get_laf_center","get_laf_orientation",+ "set_laf_orientation","scale_laf","SIFTDescriptor","MKDDescriptor",diff --git a/kornia/feature/laf.py b/kornia/feature/laf.pyindex 09a69ddb7..53c44a3a2 100644--- a/kornia/feature/laf.py+++ b/kornia/feature/laf.py@@ -4,6 +4,8 @@ import mathimport torchimport torch.nn.functional as F+from kornia.geometry import angle_to_rotation_matrix+def raise_error_if_laf_is_not_valid(laf: torch.Tensor) -> None:"""Auxilary function, which verifies that input is a torch.tensor of [BxNx2x3] shape@@ -89,6 +91,30 @@ def get_laf_orientation(LAF: torch.Tensor) -> torch.Tensor:return kornia.rad2deg(angle_rad).unsqueeze(-1)+def set_laf_orientation(LAF: torch.Tensor,+ angles_degrees: torch.Tensor) -> torch.Tensor:+ """Changes the orientation of the LAFs.++ Args:+ LAF: (torch.Tensor): tensor [BxNx2x3].+ angles: (torch.Tensor): tensor BxNx1, in degrees .++ Returns:+ torch.Tensor: tensor [BxNx2x3] .++ Shape:+ - Input: :math: `(B, N, 2, 3)`, `(B, N, 1)`+ - Output: :math: `(B, N, 2, 3)`++ """+ raise_error_if_laf_is_not_valid(LAF)+ B, N = LAF.shape[:2]+ rotmat: torch.Tensor = angle_to_rotation_matrix(angles_degrees).view(B * N, 2, 2)+ laf_out: torch.Tensor = torch.cat([torch.bmm(make_upright(LAF).view(B * N, 2, 3)[:, :2, :2], rotmat),+ LAF.view(B * N, 2, 3)[:, :2, 2:]], dim=2).view(B, N, 2, 3)+ return laf_out++def laf_from_center_scale_ori(xy: torch.Tensor, scale: torch.Tensor, ori: torch.Tensor) -> torch.Tensor:"""Returns orientation of the LAFs, in radians. Useful to create kornia LAFs from OpenCV keypointsdiff --git a/kornia/feature/orientation.py b/kornia/feature/orientation.pyindex c07d30e14..0e21529b5 100644--- a/kornia/feature/orientation.py+++ b/kornia/feature/orientation.py@@ -8,8 +8,9 @@ from kornia.filters import get_gaussian_kernel2dfrom kornia.filters import SpatialGradientfrom kornia.constants import pifrom kornia.feature import (extract_patches_from_pyramid, make_upright,- normalize_laf, raise_error_if_laf_is_not_valid)-from kornia.geometry import rad2deg, angle_to_rotation_matrix+ normalize_laf, raise_error_if_laf_is_not_valid,+ set_laf_orientation)+from kornia.geometry import rad2degurls: Dict[str, str] = dict()urls["orinet"] = "https://github.com/ducha-aiki/affnet/raw/master/pretrained/OriNet.pth"@@ -237,8 +238,5 @@ class LAFOrienter(nn.Module):self.patch_size,self.patch_size)angles_radians: torch.Tensor = self.angle_detector(patches).view(B, N)- rotmat: torch.Tensor = angle_to_rotation_matrix(rad2deg(angles_radians)).view(B * N, 2, 2)-- laf_out: torch.Tensor = torch.cat([torch.bmm(make_upright(laf).view(B * N, 2, 3)[:, :2, :2], rotmat),- laf.view(B * N, 2, 3)[:, :2, 2:]], dim=2).view(B, N, 2, 3)+ laf_out: torch.Tensor = set_laf_orientation(laf, rad2deg(angles_radians))return laf_outdiff --git a/test/feature/test_laf.py b/test/feature/test_laf.pyindex ace6b3618..5ca7da882 100644--- a/test/feature/test_laf.py+++ b/test/feature/test_laf.py@@ -171,6 +171,41 @@ class TestScaleLAF:assert_allclose(model(laf, scale), model_jit(laf, scale))+class TestSetLAFOri:+ def test_shape_tensor(self, device):+ inp = torch.ones(7, 3, 2, 3, device=device).float()+ ori = torch.ones(7, 3, 1, 1, device=device).float()+ assert kornia.feature.set_laf_orientation(inp, ori).shape == inp.shape++ def test_ori(self, device):+ inp = torch.tensor([[0., 5., 0.8], [-5., 0, -4.]], device=device).float()+ inp = inp.view(1, 1, 2, 3)+ ori = torch.zeros(1, 1, 1, 1, device=device).float()+ out = kornia.feature.set_laf_orientation(inp, ori)+ expected = torch.tensor([[[[5., 0., 0.8], [0., 5., -4.]]]], device=device).float()+ assert_allclose(out, expected)++ def test_gradcheck(self, device):+ batch_size, channels, height, width = 1, 2, 2, 3+ laf = torch.rand(batch_size, channels, height, width, device=device)+ ori = torch.rand(batch_size, channels, 1, 1, device=device)+ ori = utils.tensor_to_gradcheck_var(ori) # to var+ laf = utils.tensor_to_gradcheck_var(laf) # to var+ assert gradcheck(kornia.feature.set_laf_orientation,+ (laf, ori),+ raise_exception=True, atol=1e-4)++ @pytest.mark.jit+ @pytest.mark.skip("Union")+ def test_jit(self, device, dtype):+ batch_size, channels, height, width = 1, 2, 2, 3+ laf = torch.rand(batch_size, channels, height, width, device=device)+ ori = torch.rand(batch_size, channels, 1, 1, device=device)+ model = kornia.feature.set_laf_orientation+ model_jit = torch.jit.script(kornia.feature.set_laf_orientation)+ assert_allclose(model(laf, ori), model_jit(laf, ori))++class TestMakeUpright:def test_shape(self, device):inp = torch.ones(5, 3, 2, 3, device=device)