π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
review fix
Dmytro Mishkine9235bed0451
diff --git a/kornia/feature/__init__.py b/kornia/feature/__init__.pyindex dbc719a20..ec4dba8ab 100644--- a/kornia/feature/__init__.py+++ b/kornia/feature/__init__.py@@ -1,15 +1,14 @@+from kornia.geometry import angle_to_rotation_matrixfrom .harris import CornerHarris, corner_harrisfrom .nms import NonMaximaSuppression2d, non_maxima_suppression2dfrom .laf import (extract_patches_from_pyramid,extract_patches_simple,- visualize_laf,- normalize_LAF,- denormalize_LAF,+ normalize_laf,+ denormalize_laf,laf_to_boundary_points,ellipse_to_laf,make_upright,get_laf_scale)-from kornia.geometry.transform.imgwarp import angle_to_rotation_matrixfrom .siftdesc import SIFTDescriptor__all__ = ["non_maxima_suppression2d",@@ -18,13 +17,11 @@ __all__ = ["CornerHarris","extract_patches_from_pyramid","extract_patches_simple",- "visualize_laf",- "normalize_LAF",- "denormalize_LAF",+ "normalize_laf",+ "denormalize_laf","laf_to_boundary_points","ellipse_to_laf","make_upright","get_laf_scale",- "angle_to_rotation_matrix""SIFTDescriptor"]diff --git a/kornia/feature/laf.py b/kornia/feature/laf.pyindex 4263df450..71f68345b 100644--- a/kornia/feature/laf.py+++ b/kornia/feature/laf.py@@ -20,7 +20,6 @@ def get_laf_scale(LAF: torch.Tensor) -> torch.Tensor:Args:LAF: (torch.Tensor): tensor [BxNx2x3] or [BxNx2x2].-Returns:torch.Tensor: tensor BxNx1x1 .@@ -40,15 +39,16 @@ def get_laf_scale(LAF: torch.Tensor) -> torch.Tensor:"Got {}".format(LAF.size()))out = LAF[..., 0:1, 0:1] * LAF[..., 1:2, 1:2] -\- LAF[..., 1:2, 0:1] * LAF[..., 0:1, 1:2] + eps # noqa: E127+ LAF[..., 1:2, 0:1] * LAF[..., 0:1, 1:2] + epsreturn out.abs().sqrt()-def make_upright(LAF: torch.Tensor) -> torch.Tensor: # noqa: E128+def make_upright(LAF: torch.Tensor, eps: float = 1e-9) -> torch.Tensor:"""Rectifies the affine matrix, so that it becomes uprightArgs:LAF: (torch.Tensor): tensor of LAFs.+ eps (float): for safe division, (default 1e-9)Returns:torch.Tensor: tensor of same shape.@@ -69,7 +69,6 @@ def make_upright(LAF: torch.Tensor) -> torch.Tensor: # noqa: E128det = get_laf_scale(LAF)# The function is equivalent to doing 2x2 SVD and reseting rotation# matrix to an identity: U, S, V = svd(LAF); LAF_upright = U * S.- eps = 1e-10b2a2 = torch.sqrt(LAF[..., 0:1, 1:2]**2 + LAF[..., 0:1, 0:1]**2) + epsLAF1_ell = torch.cat([(b2a2 / det).contiguous(), # type: ignoretorch.zeros_like(det)], dim=3) # type: ignore@@ -184,38 +183,9 @@ def get_laf_pts_to_draw(LAF: torch.Tensor,return (pts_np[..., 0], pts_np[..., 1])-def visualize_laf(img: torch.Tensor, # pragma: no cover- LAF: torch.Tensor,- img_idx: int = 0,- color = 'r'):- """- Args:- img: (torch.Tensor)- LAF: (torch.Tensor)- img_idx (int): image index in the batch to draw- color (string): line color in matplotlib format-- Returns:- Nothing, shows matplotlib imshow.-- Shape:- - img: :math:`(B, CH, H, W)`- - LAF: :math:`(B, N, 2, 3)`-- """- if MATPLOTLIB:- x, y = get_laf_pts_to_draw(LAF, img_idx)- plt.figure()- plt.imshow(kornia.utils.tensor_to_image(img[img_idx]))- plt.plot(x, y, color)- plt.show()- return---def denormalize_LAF(LAF: torch.Tensor, images: torch.Tensor) -> torch.Tensor:+def denormalize_laf(LAF: torch.Tensor, images: torch.Tensor) -> torch.Tensor:"""De-normalizes LAFs from scale to image scale.-B,N,H,W = images.size()MIN_SIZE = min(H,W)[a11 a21 x]@@ -252,7 +222,7 @@ def denormalize_LAF(LAF: torch.Tensor, images: torch.Tensor) -> torch.Tensor:return coef.expand_as(LAF) * LAF-def normalize_LAF(LAF: torch.Tensor, images: torch.Tensor) -> torch.Tensor:+def normalize_laf(LAF: torch.Tensor, images: torch.Tensor) -> torch.Tensor:"""Normalizes LAFs to [0,1] scale from pixel scale.See below:@@ -318,7 +288,7 @@ def generate_patch_grid_from_normalized_LAF(img: torch.Tensor,# norm, then renorm is needed for allowing detection on one resolution# and extraction at arbitrary other- LAF_renorm = denormalize_LAF(LAF, img)+ LAF_renorm = denormalize_laf(LAF, img)grid = F.affine_grid(LAF_renorm.view(B * N, 2, 3),[B * N, ch, PS, PS])@@ -372,7 +342,7 @@ def extract_patches_from_pyramid(img: torch.Tensor,"""B, N, _, _ = LAF.size()num, ch, h, w = img.size()- scale = 2.0 * get_laf_scale(denormalize_LAF(LAF, img)) / float(PS)+ scale = 2.0 * get_laf_scale(denormalize_laf(LAF, img)) / float(PS)pyr_idx = (scale.log2() + 0.5).relu().long()cur_img = imgcur_pyr_level = int(0)diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.pyindex 210672472..37f60c380 100644--- a/kornia/geometry/transform/imgwarp.py+++ b/kornia/geometry/transform/imgwarp.py@@ -18,6 +18,7 @@ __all__ = ["normal_transform_pixel","remap","invert_affine_transform",+ "angle_to_rotation_matrix"]@@ -269,16 +270,14 @@ def get_perspective_transform(src, dst):return M.view(-1, 3, 3) # Bx3x3-def angle_to_rotation_matrix(angle: torch.Tensor,- do_deg2rad: bool = False) -> torch.Tensor:+def angle_to_rotation_matrix(angle: torch.Tensor) -> torch.Tensor:"""- Creates a rotation matrix out of angles+ Creates a rotation matrix out of angles in degreesArgs:- angle: (torch.Tensor): tensor of angles, any shape.- do_deg2rad: (bool): if we should convert to radians first+ angle: (torch.Tensor): tensor of angles in degrees, any shape.Returns:- torch.Tensor: tensor of Nx2x2 rotation matrices.+ torch.Tensor: tensor of *x2x2 rotation matrices.Shape:- Input: :math:`(*)`@@ -288,16 +287,10 @@ def angle_to_rotation_matrix(angle: torch.Tensor,>>> input = torch.rand(1, 3) # Nx3>>> output = kornia.angle_to_rotation_matrix(input) # Nx3x2x2"""- if do_deg2rad:- ang = kornia.deg2rad(angle)- else:- ang = angle- n_dims = len(ang.size())- cos_a = torch.cos(ang).unsqueeze(-1).unsqueeze(-1)- sin_a = torch.sin(ang).unsqueeze(-1).unsqueeze(-1)- A1_ang = torch.cat([cos_a, sin_a], dim=n_dims + 1)- A2_ang = torch.cat([-sin_a, cos_a], dim=n_dims + 1)- return torch.cat([A1_ang, A2_ang], dim=n_dims)+ ang_rad = kornia.deg2rad(angle)+ cos_a: torch.Tensor = torch.cos(ang_rad)+ sin_a: torch.Tensor = torch.sin(ang_rad)+ return torch.stack([cos_a, sin_a, -sin_a, cos_a], dim=-1).view(*angle.shape, 2, 2)def get_rotation_matrix2d(@@ -369,9 +362,7 @@ def get_rotation_matrix2d(raise ValueError("Inputs must have same batch size dimension. Got {}".format(center.shape, angle.shape, scale.shape))# convert angle and apply scale- angle_rad: torch.Tensor = deg2rad(angle)-- scaled_rotation: torch.Tensor = angle_to_rotation_matrix(angle_rad) * scale.view(-1, 1, 1)+ scaled_rotation: torch.Tensor = angle_to_rotation_matrix(angle) * scale.view(-1, 1, 1)alpha: torch.Tensor = scaled_rotation[:, 0, 0]beta: torch.Tensor = scaled_rotation[:, 0, 1]diff --git a/test/feature/test_laf.py b/test/feature/test_laf.pyindex 918b69b7b..296cc657d 100644--- a/test/feature/test_laf.py+++ b/test/feature/test_laf.py@@ -17,11 +17,10 @@ class TestAngleToRotationMatrix:assert rotmat.shape == (1, 3, 4, 4, 2, 2)def test_angles(self):- inp = torch.tensor([0, math.pi / 2.0])-+ ang_deg = torch.tensor([0, 90.])expected = torch.tensor([[[1.0, 0.], [0., 1.0]],[[0, 1.0], [-1.0, 0]]])- rotmat = kornia.geometry.transform.imgwarp.angle_to_rotation_matrix(inp)+ rotmat = kornia.geometry.transform.imgwarp.angle_to_rotation_matrix(ang_deg)assert_allclose(rotmat, expected)def test_gradcheck(self):@@ -114,7 +113,7 @@ class TestNormalizeLAF:def test_shape(self):inp = torch.rand(5, 3, 2, 3)img = torch.rand(5, 3, 10, 10)- assert inp.shape == kornia.feature.normalize_LAF(inp, img).shape+ assert inp.shape == kornia.feature.normalize_laf(inp, img).shapedef test_conversion(self):w, h = 10, 5@@ -122,7 +121,7 @@ class TestNormalizeLAF:laf = laf.view(1, 1, 2, 3)img = torch.rand(1, 3, h, w)expected = torch.tensor([[0.2, 0, 0.1], [0, 0.2, 0.2]]).float()- lafn = kornia.feature.normalize_LAF(laf, img)+ lafn = kornia.feature.normalize_laf(laf, img)assert_allclose(lafn, expected)def test_gradcheck(self):@@ -132,7 +131,7 @@ class TestNormalizeLAF:img = torch.rand(batch_size, 3, 10, 32)img = utils.tensor_to_gradcheck_var(img) # to varlaf = utils.tensor_to_gradcheck_var(laf) # to var- assert gradcheck(kornia.feature.normalize_LAF,+ assert gradcheck(kornia.feature.normalize_laf,(laf, img,),raise_exception=True)@@ -169,7 +168,7 @@ class TestDenormalizeLAF:def test_shape(self):inp = torch.rand(5, 3, 2, 3)img = torch.rand(5, 3, 10, 10)- assert inp.shape == kornia.feature.denormalize_LAF(inp, img).shape+ assert inp.shape == kornia.feature.denormalize_laf(inp, img).shapedef test_conversion(self):w, h = 10, 5@@ -177,7 +176,7 @@ class TestDenormalizeLAF:expected = expected.view(1, 1, 2, 3)img = torch.rand(1, 3, h, w)lafn = torch.tensor([[0.2, 0, 0.1], [0, 0.2, 0.2]]).float()- laf = kornia.feature.denormalize_LAF(lafn.view(1, 1, 2, 3), img)+ laf = kornia.feature.denormalize_laf(lafn.view(1, 1, 2, 3), img)assert_allclose(laf, expected)def test_gradcheck(self):@@ -187,7 +186,7 @@ class TestDenormalizeLAF:img = torch.rand(batch_size, 3, 10, 32)img = utils.tensor_to_gradcheck_var(img) # to varlaf = utils.tensor_to_gradcheck_var(laf) # to var- assert gradcheck(kornia.feature.denormalize_LAF,+ assert gradcheck(kornia.feature.denormalize_laf,(laf, img,),raise_exception=True)