π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix mypy errors for affine transforms
edgarriba6ed3490b53a3
diff --git a/mypy_files.txt b/mypy_files.txtindex 37b43dbfa..64d47bf8d 100644--- a/mypy_files.txt+++ b/mypy_files.txt@@ -1,6 +1,7 @@torchgeometry/core/homography_warper.pytorchgeometry/core/depth_warper.pytorchgeometry/core/pinhole.py+torchgeometry/core/imgwarp.pytorchgeometry/image/gaussian.pytorchgeometry/image/color.pytorchgeometry/image/laplacian.pydiff --git a/torchgeometry/core/affine.py b/torchgeometry/core/affine.pyindex 00c517095..33da2e4ab 100644--- a/torchgeometry/core/affine.py+++ b/torchgeometry/core/affine.py@@ -3,7 +3,7 @@ from typing import Unionimport torchimport torch.nn as nn-from torchgeometry.core import warp_affine, get_rotation_matrix2d+from torchgeometry.core.imgwarp import warp_affine, get_rotation_matrix2d# utilities to compute affine matrices@@ -82,7 +82,9 @@ def affine(tensor: torch.Tensor, matrix: torch.Tensor) -> torch.Tensor:matrix = matrix.expand(tensor.shape[0], -1, -1)# warp the input tensor- warped: torch.Tensor = warp_affine(tensor, matrix, tensor.shape[-2:])+ height: int = tensor.shape[-2]+ width: int = tensor.shape[-1]+ warped: torch.Tensor = warp_affine(tensor, matrix, (height, width))# return in the original shapeif is_unbatched:@@ -115,7 +117,7 @@ def rotate(tensor: torch.Tensor, angle: torch.Tensor,# compute the rotation centerif center is None:- center: torch.Tensor = _compute_tensor_center(tensor)+ center = _compute_tensor_center(tensor)# compute the rotation matrix# TODO: add broadcasting to get_rotation_matrix2d for center@@ -164,7 +166,7 @@ def scale(tensor: torch.Tensor, scale_factor: torch.Tensor,# compute the tensor centerif center is None:- center: torch.Tensor = _compute_tensor_center(tensor)+ center = _compute_tensor_center(tensor)# compute the rotation matrix# TODO: add broadcasting to get_rotation_matrix2d for center@@ -217,7 +219,7 @@ class Rotate(nn.Module):self.angle: torch.Tensor = angleself.center: Union[None, torch.Tensor] = center- def forward(self, input: torch.Tensor) -> torch.Tensor:+ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignorereturn rotate(input, self.angle, self.center)def __repr__(self):@@ -242,7 +244,7 @@ class Translate(nn.Module):super(Translate, self).__init__()self.translation: torch.Tensor = translation- def forward(self, input: torch.Tensor) -> torch.Tensor:+ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignorereturn translate(input, self.translation)def __repr__(self):@@ -268,9 +270,9 @@ class Scale(nn.Module):center: Union[None, torch.Tensor] = None) -> None:super(Scale, self).__init__()self.scale_factor: torch.Tensor = scale_factor- self.center: torch.Tensor = center+ self.center: Union[None, torch.Tensor] = center- def forward(self, input: torch.Tensor) -> torch.Tensor:+ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignorereturn scale(input, self.scale_factor, self.center)def __repr__(self):@@ -296,7 +298,7 @@ class Shear(nn.Module):super(Shear, self).__init__()self.shear: torch.Tensor = shear- def forward(self, input: torch.Tensor) -> torch.Tensor:+ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignorereturn shear(input, self.shear)def __repr__(self):diff --git a/torchgeometry/core/imgwarp.py b/torchgeometry/core/imgwarp.pyindex 136633708..a1b65a75a 100644--- a/torchgeometry/core/imgwarp.py+++ b/torchgeometry/core/imgwarp.py@@ -264,7 +264,10 @@ def get_perspective_transform(src, dst):return M.view(-1, 3, 3) # Bx3x3-def get_rotation_matrix2d(center, angle, scale):+def get_rotation_matrix2d(+ center: torch.Tensor,+ angle: torch.Tensor,+ scale: torch.Tensor) -> torch.Tensor:r"""Calculates an affine matrix of 2D rotation.The function calculates the following matrix:@@ -330,20 +333,22 @@ def get_rotation_matrix2d(center, angle, scale):raise ValueError("Inputs must have same batch size dimension. Got {}".format(center.shape, angle.shape, scale.shape))# convert angle and apply scale- angle_rad = deg2rad(angle)- alpha = torch.cos(angle_rad) * scale- beta = torch.sin(angle_rad) * scale+ angle_rad: torch.Tensor = deg2rad(angle)+ alpha: torch.Tensor = torch.cos(angle_rad) * scale+ beta: torch.Tensor = torch.sin(angle_rad) * scale# unpack the center to x, y coordinates- x, y = center[..., 0], center[..., 1]+ x: torch.Tensor = center[..., 0]+ y: torch.Tensor = center[..., 1]# create output tensor- batch_size, _ = center.shape- M = torch.zeros(batch_size, 2, 3, device=center.device, dtype=center.dtype)+ batch_size: int = center.shape[0]+ M: torch.Tensor = torch.zeros(+ batch_size, 2, 3, device=center.device, dtype=center.dtype)M[..., 0, 0] = alphaM[..., 0, 1] = beta- M[..., 0, 2] = (1. - alpha) * x - beta * y+ M[..., 0, 2] = (torch.tensor(1.) - alpha) * x - beta * yM[..., 1, 0] = -betaM[..., 1, 1] = alpha- M[..., 1, 2] = beta * x + (1. - alpha) * y+ M[..., 1, 2] = beta * x + (torch.tensor(1.) - alpha) * yreturn M