Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix mypy errors

Pau Riba29d142ff91ff
diff --git a/kornia/feature/nms.py b/kornia/feature/nms.py
index 06c909595..cbabfe7dc 100644
--- a/kornia/feature/nms.py
+++ b/kornia/feature/nms.py
@@ -1,4 +1,4 @@
-from typing import Tuple
+from typing import Tuple, Union
import torch
import torch.nn as nn
@@ -31,7 +31,8 @@ class NonMaximaSuppression2d(nn.Module):
def forward(self, x: torch.Tensor) -> torch.Tensor: # type: ignore
assert len(x.shape) == 4, x.shape
# find local maximum values
- x_max: torch.Tensor = self.max_pool2d(x)
+ x_max: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]] = \
+ self.max_pool2d(x)
# create mask for maximums in the original map
x_mask: torch.Tensor = torch.where(
diff --git a/kornia/filters/filter.py b/kornia/filters/filter.py
index 450b75d89..00568a170 100644
--- a/kornia/filters/filter.py
+++ b/kornia/filters/filter.py
@@ -5,13 +5,13 @@ import torch.nn as nn
import torch.nn.functional as F
-def compute_padding(kernel_size: Tuple[int, int]) -> Tuple[int, int, int, int]:
+def compute_padding(kernel_size: Tuple[int, int]) -> List[int]:
"""Computes padding tuple."""
# 4 ints: (padding_left, padding_right,padding_top,padding_bottom)
# https://pytorch.org/docs/stable/nn.html#torch.nn.functional.pad
assert len(kernel_size) == 2, kernel_size
computed = [(k - 1) // 2 for k in kernel_size]
- return computed[1], computed[1], computed[0], computed[0]
+ return [computed[1], computed[1], computed[0], computed[0]]
def filter2D(input: torch.Tensor, kernel: torch.Tensor,
@@ -68,7 +68,7 @@ def filter2D(input: torch.Tensor, kernel: torch.Tensor,
# pad the input tensor
height, width = tmp_kernel.shape[-2:]
- padding_shape: Tuple[int, int, int, int] = compute_padding((height, width))
+ padding_shape: List[int] = compute_padding((height, width))
input_pad: torch.Tensor = F.pad(input, padding_shape, mode=border_type)
# convolve the tensor with the kernel
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 760232401..1df226c15 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -1,5 +1,3 @@
-from typing import Optional
-
import torch
import torch.nn as nn
import torch.nn.functional as F
@@ -117,7 +115,7 @@ def convert_points_to_homogeneous(points: torch.Tensor) -> torch.Tensor:
raise ValueError("Input must be at least a 2D tensor. Got {}".format(
points.shape))
- return torch.nn.functional.pad(points, (0, 1), "constant", 1.0)
+ return torch.nn.functional.pad(points, [0, 1], "constant", 1.0)
def angle_axis_to_rotation_matrix(angle_axis: torch.Tensor) -> torch.Tensor:
@@ -317,7 +315,7 @@ def rotation_matrix_to_quaternion(
def normalize_quaternion(quaternion: torch.Tensor,
- eps: Optional[float] = 1e-12) -> torch.Tensor:
+ eps: float = 1e-12) -> torch.Tensor:
r"""Normalizes a quaternion.
Args:
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index 2d49ae52b..ed2b7ed6a 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -155,7 +155,7 @@ def warp_affine(src: torch.Tensor,
raise ValueError("Input M must be a Bx2x3 tensor. Got {}"
.format(src.shape))
# we generate a 3x3 transformation matrix from 2x3 affine
- M_3x3: torch.Tensor = F.pad(M, (0, 0, 0, 1, 0, 0),
+ M_3x3: torch.Tensor = F.pad(M, [0, 0, 0, 1, 0, 0],
mode="constant", value=0)
M_3x3[:, 2, 2] += 1.0
@@ -441,7 +441,7 @@ def invert_affine_transform(matrix: torch.Tensor) -> torch.Tensor:
if not (len(matrix.shape) == 3 or matrix.shape[-2:] == (2, 3)):
raise ValueError("Input matrix must be a Bx2x3 tensor. Got {}"
.format(matrix.shape))
- matrix_tmp: torch.Tensor = F.pad(matrix, (0, 0, 0, 1), "constant", 0.0)
+ matrix_tmp: torch.Tensor = F.pad(matrix, [0, 0, 0, 1], "constant", 0.0)
matrix_tmp[..., 2, 2] += 1.0
matrix_inv: torch.Tensor = torch.inverse(matrix_tmp)
diff --git a/kornia/geometry/warp/depth_warper.py b/kornia/geometry/warp/depth_warper.py
index 2a2036a5a..d5619fcab 100644
--- a/kornia/geometry/warp/depth_warper.py
+++ b/kornia/geometry/warp/depth_warper.py
@@ -1,4 +1,4 @@
-from typing import Optional, Union
+from typing import Union
import torch
import torch.nn as nn
import torch.nn.functional as F
@@ -31,23 +31,23 @@ class DepthWarper(nn.Module):
frame.
height (int): the height of the image to warp.
width (int): the width of the image to warp.
- mode (Optional[str]): interpolation mode to calculate output values
+ mode (str): interpolation mode to calculate output values
'bilinear' | 'nearest'. Default: 'bilinear'.
- padding_mode (Optional[str]): padding mode for outside grid values
+ padding_mode (str): padding mode for outside grid values
'zeros' | 'border' | 'reflection'. Default: 'zeros'.
"""
def __init__(self,
pinhole_dst: PinholeCamera,
height: int, width: int,
- mode: Optional[str] = 'bilinear',
- padding_mode: Optional[str] = 'zeros'):
+ mode: str = 'bilinear',
+ padding_mode: str = 'zeros'):
super(DepthWarper, self).__init__()
# constructor members
self.width: int = width
self.height: int = height
- self.mode: Optional[str] = mode
- self.padding_mode: Optional[str] = padding_mode
+ self.mode: str = mode
+ self.padding_mode: str = padding_mode
self.eps = 1e-6
# state members
diff --git a/kornia/geometry/warp/homography_warper.py b/kornia/geometry/warp/homography_warper.py
index 19a365be0..b039e76a7 100644
--- a/kornia/geometry/warp/homography_warper.py
+++ b/kornia/geometry/warp/homography_warper.py
@@ -1,4 +1,4 @@
-from typing import Optional, Tuple
+from typing import Tuple
import torch
import torch.nn as nn
@@ -26,27 +26,27 @@ class HomographyWarper(nn.Module):
Args:
height (int): The height of the image to warp.
width (int): The width of the image to warp.
- mode (Optional[str]): interpolation mode to calculate output values
+ mode (str): interpolation mode to calculate output values
'bilinear' | 'nearest'. Default: 'bilinear'.
- padding_mode (Optional[str]): padding mode for outside grid values
+ padding_mode (str): padding mode for outside grid values
'zeros' | 'border' | 'reflection'. Default: 'zeros'.
- normalized_coordinates (Optional[bool]): wether to use a grid with
- normalized coordinates.
+ normalized_coordinates (bool): wether to use a grid with
+ normalized coordinates.
"""
def __init__(
self,
height: int,
width: int,
- mode: Optional[str] = 'bilinear',
- padding_mode: Optional[str] = 'zeros',
- normalized_coordinates: Optional[bool] = True) -> None:
+ mode: str = 'bilinear',
+ padding_mode: str = 'zeros',
+ normalized_coordinates: bool = True) -> None:
super(HomographyWarper, self).__init__()
self.width: int = width
self.height: int = height
- self.mode: Optional[str] = mode
- self.padding_mode: Optional[str] = padding_mode
- self.normalized_coordinates: Optional[bool] = normalized_coordinates
+ self.mode: str = mode
+ self.padding_mode: str = padding_mode
+ self.normalized_coordinates: bool = normalized_coordinates
# create base grid to compute the flow
self.grid: torch.Tensor = create_meshgrid(
@@ -116,8 +116,8 @@ class HomographyWarper(nn.Module):
def homography_warp(patch_src: torch.Tensor,
dst_homo_src: torch.Tensor,
dsize: Tuple[int, int],
- mode: Optional[str] = 'bilinear',
- padding_mode: Optional[str] = 'zeros') -> torch.Tensor:
+ mode: str = 'bilinear',
+ padding_mode: str = 'zeros') -> torch.Tensor:
r"""Function that warps image patchs or tensors by homographies.
See :class:`~kornia.geometry.warp.HomographyWarper` for details.
@@ -129,9 +129,9 @@ def homography_warp(patch_src: torch.Tensor,
from source to destination of shape
:math:`(N, 3, 3)`.
dsize (Tuple[int, int]): The height and width of the image to warp.
- mode (Optional[str]): interpolation mode to calculate output values
+ mode (str): interpolation mode to calculate output values
'bilinear' | 'nearest'. Default: 'bilinear'.
- padding_mode (Optional[str]): padding mode for outside grid values
+ padding_mode (str): padding mode for outside grid values
'zeros' | 'border' | 'reflection'. Default: 'zeros'.
Return:
diff --git a/test/geometry/transform/test_imgwarp.py b/test/geometry/transform/test_imgwarp.py
index 8edc6936d..55da4c3dc 100644
--- a/test/geometry/transform/test_imgwarp.py
+++ b/test/geometry/transform/test_imgwarp.py
@@ -40,7 +40,7 @@ def test_warp_perspective_rotation(batch_shape, device_type):
dsize=(height, width))
assert_allclose(mask_warped_inv * patch,
- mask_warped_inv * patch_warped_inv)
+ mask_warped_inv * patch_warped_inv)
# evaluate function gradient
patch = utils.tensor_to_gradcheck_var(patch) # to var
diff --git a/test/utils/test_grid.py b/test/utils/test_grid.py
index fe3540fe9..8706e2b00 100644
--- a/test/utils/test_grid.py
+++ b/test/utils/test_grid.py
@@ -6,6 +6,7 @@ import kornia.testing as utils # test utils
from torch.testing import assert_allclose
+
def test_create_meshgrid():
height, width = 4, 6
normalized_coordinates = False