Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[ENHANCE] removed deprecated codes for v6.0 (#1281)

Jian Shi832e7dd48593
diff --git a/kornia/__init__.py b/kornia/__init__.py
index b1baba653..b56172460 100644
--- a/kornia/__init__.py
+++ b/kornia/__init__.py
@@ -37,7 +37,7 @@ from kornia.color import (
yuv_to_rgb,
)
from kornia.constants import *
-from kornia.contrib import extract_tensor_patches, max_blur_pool2d
+from kornia.contrib import extract_tensor_patches
from kornia.enhance import (
adjust_brightness,
adjust_contrast,
diff --git a/kornia/contrib/__init__.py b/kornia/contrib/__init__.py
index 8f5bba2ff..6100d0eb3 100644
--- a/kornia/contrib/__init__.py
+++ b/kornia/contrib/__init__.py
@@ -1,5 +1,4 @@
from kornia.contrib.connected_components import connected_components
from kornia.contrib.extract_patches import extract_tensor_patches, ExtractTensorPatches
-from kornia.contrib.max_blur_pool import max_blur_pool2d, MaxBlurPool2d
-__all__ = ["connected_components", "extract_tensor_patches", "max_blur_pool2d", "ExtractTensorPatches", "MaxBlurPool2d"]
+__all__ = ["connected_components", "extract_tensor_patches", "ExtractTensorPatches"]
diff --git a/kornia/contrib/max_blur_pool.py b/kornia/contrib/max_blur_pool.py
deleted file mode 100644
index b15620ef1..000000000
--- a/kornia/contrib/max_blur_pool.py
+++ /dev/null
@@ -1,35 +0,0 @@
-import warnings
-
-import torch
-
-from kornia.filters.blur_pool import max_blur_pool2d as _max_blur_pool2d # For doctest
-from kornia.filters.blur_pool import MaxBlurPool2D
-
-__all__ = ["max_blur_pool2d", "MaxBlurPool2d"]
-
-
-class MaxBlurPool2d(MaxBlurPool2D):
- __doc__ = MaxBlurPool2D.__doc__
-
- def __init__(self, kernel_size: int, ceil_mode: bool = False) -> None:
- super().__init__(kernel_size, stride=2, max_pool_size=2, ceil_mode=ceil_mode)
- warnings.warn(
- "`MaxBlurPool2d` is deprecated and will be removed after > 0.6. "
- "Please use `kornia.filters.MaxBlurPool2D instead.`",
- DeprecationWarning,
- stacklevel=2,
- )
-
-
-def max_blur_pool2d(input: torch.Tensor, kernel_size: int, ceil_mode: bool = False) -> torch.Tensor:
- r"""Create a module that computes pools and blurs and downsample a given feature map.
-
- See :class:`~kornia.contrib.MaxBlurPool2d` for details.
- """
- warnings.warn(
- "`max_blur_pool2d` is deprecated and will be removed after > 0.6. "
- "Please use `kornia.filters.max_blur_pool2d instead.`",
- DeprecationWarning,
- stacklevel=2,
- )
- return _max_blur_pool2d(input, kernel_size, stride=2, max_pool_size=2, ceil_mode=ceil_mode)
diff --git a/kornia/geometry/bbox.py b/kornia/geometry/bbox.py
index f75de75d3..fcecaceb3 100644
--- a/kornia/geometry/bbox.py
+++ b/kornia/geometry/bbox.py
@@ -440,9 +440,8 @@ def transform_bbox(trans_mat: torch.Tensor, boxes: torch.Tensor, mode: str = "xy
``xmin, ymin, width, height``
Returns:
The set of transformed points in the specified mode
-
-
"""
+
if not isinstance(mode, str):
raise TypeError(f"Mode must be a string. Got {type(mode)}")
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.py
index e34853df3..176a4c057 100644
--- a/kornia/geometry/linalg.py
+++ b/kornia/geometry/linalg.py
@@ -1,9 +1,6 @@
-import warnings
-
import torch
import kornia
-from kornia.geometry.bbox import transform_bbox as _transform_bbox
from kornia.geometry.conversions import convert_points_from_homogeneous, convert_points_to_homogeneous
from kornia.testing import check_is_tensor
@@ -12,7 +9,6 @@ __all__ = [
"relative_transformation",
"inverse_transformation",
"transform_points",
- "transform_boxes",
"perspective_transform_lafs",
]
@@ -206,30 +202,6 @@ def transform_points(trans_01: torch.Tensor, points_1: torch.Tensor) -> torch.Te
return points_0
-def transform_boxes(trans_mat: torch.Tensor, boxes: torch.Tensor, mode: str = "xyxy") -> torch.Tensor:
- r"""Function that applies a transformation matrix to a box or batch of boxes. Boxes must
- be a tensor of the shape (N, 4) or a batch of boxes (B, N, 4) and trans_mat must be a (3, 3)
- transformation matrix or a batch of transformation matrices (B, 3, 3)
-
- Args:
- trans_mat: The transformation matrix to be applied.
- boxes: The boxes to be transformed.
- mode: The format in which the boxes are provided. If set to 'xyxy' the boxes
- are assumed to be in the format (xmin, ymin, xmax, ymax). If set to 'xywh'
- the boxes are assumed to be in the format (xmin, ymin, width, height).
-
- Returns:
- The set of transformed points in the specified mode.
- """
- warnings.warn(
- "`kornia.geometry.linalg.transform_boxes` is deprecated and will be removed > 0.6.0. "
- "Please use `kornia.geometry.bbox.transform_bbox instead.`",
- DeprecationWarning,
- stacklevel=2,
- )
- return _transform_bbox(trans_mat, boxes, mode)
-
-
def perspective_transform_lafs(trans_01: torch.Tensor, lafs_1: torch.Tensor) -> torch.Tensor:
r"""Function that applies perspective transformations to a set of local affine frames (LAFs).
diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.py
index e320c987d..e8e66d4db 100644
--- a/kornia/geometry/transform/affwarp.py
+++ b/kornia/geometry/transform/affwarp.py
@@ -122,7 +122,7 @@ def affine(
matrix: torch.Tensor,
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
r"""Apply an affine transformation to the image.
@@ -229,7 +229,7 @@ def rotate(
center: Union[None, torch.Tensor] = None,
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
r"""Rotate the tensor anti-clockwise about the center.
@@ -359,7 +359,7 @@ def translate(
translation: torch.Tensor,
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
r"""Translate the tensor in pixel units.
@@ -408,7 +408,7 @@ def scale(
center: Union[None, torch.Tensor] = None,
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
r"""Scale the tensor by a factor.
@@ -739,7 +739,7 @@ class Affine(nn.Module):
center: Optional[torch.Tensor] = None,
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> None:
batch_sizes = [arg.size()[0] for arg in (angle, translation, scale_factor, shear) if arg is not None]
if not batch_sizes:
@@ -819,7 +819,7 @@ class Rescale(nn.Module):
self,
factor: Union[float, Tuple[float, float]],
interpolation: str = "bilinear",
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
antialias: bool = False,
) -> None:
super().__init__()
@@ -866,14 +866,14 @@ class Rotate(nn.Module):
center: Union[None, torch.Tensor] = None,
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> None:
super().__init__()
self.angle: torch.Tensor = angle
self.center: Union[None, torch.Tensor] = center
self.mode: str = mode
self.padding_mode: str = padding_mode
- self.align_corners: Optional[bool] = align_corners
+ self.align_corners: bool = align_corners
def forward(self, input: torch.Tensor) -> torch.Tensor:
return rotate(input, self.angle, self.center, self.mode, self.padding_mode, self.align_corners)
@@ -908,13 +908,13 @@ class Translate(nn.Module):
translation: torch.Tensor,
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> None:
super().__init__()
self.translation: torch.Tensor = translation
self.mode: str = mode
self.padding_mode: str = padding_mode
- self.align_corners: Optional[bool] = align_corners
+ self.align_corners: bool = align_corners
def forward(self, input: torch.Tensor) -> torch.Tensor:
return translate(input, self.translation, self.mode, self.padding_mode, self.align_corners)
@@ -954,14 +954,14 @@ class Scale(nn.Module):
center: Union[None, torch.Tensor] = None,
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> None:
super().__init__()
self.scale_factor: torch.Tensor = scale_factor
self.center: Union[None, torch.Tensor] = center
self.mode: str = mode
self.padding_mode: str = padding_mode
- self.align_corners: Optional[bool] = align_corners
+ self.align_corners: bool = align_corners
def forward(self, input: torch.Tensor) -> torch.Tensor:
return scale(input, self.scale_factor, self.center, self.mode, self.padding_mode, self.align_corners)
@@ -992,7 +992,7 @@ class Shear(nn.Module):
"""
def __init__(
- self, shear: torch.Tensor, mode: str = 'bilinear', padding_mode: str = 'zeros', align_corners: bool = False
+ self, shear: torch.Tensor, mode: str = 'bilinear', padding_mode: str = 'zeros', align_corners: bool = True
) -> None:
super().__init__()
self.shear: torch.Tensor = shear
diff --git a/kornia/geometry/transform/crop/crop2d.py b/kornia/geometry/transform/crop/crop2d.py
index 8df499082..ddfe0c6d2 100644
--- a/kornia/geometry/transform/crop/crop2d.py
+++ b/kornia/geometry/transform/crop/crop2d.py
@@ -3,10 +3,7 @@ from typing import Optional, Tuple
import torch
-from kornia.geometry.bbox import bbox_generator as _bbox_generator
-from kornia.geometry.bbox import bbox_to_mask as _bbox_to_mask
-from kornia.geometry.bbox import infer_bbox_shape as _infer_bbox_shape
-from kornia.geometry.bbox import validate_bbox as _validate_bbox
+from kornia.geometry.bbox import infer_bbox_shape, validate_bbox
from kornia.geometry.transform.imgwarp import get_perspective_transform, warp_affine
__all__ = [
@@ -14,10 +11,6 @@ __all__ = [
"crop_by_boxes",
"crop_by_transform_mat",
"center_crop",
- "validate_bboxes",
- "infer_box_shape",
- "bbox_to_mask",
- "bbox_generator",
]
@@ -27,7 +20,7 @@ def crop_and_resize(
size: Tuple[int, int],
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
r"""Extract crops from 2D images (4D tensor) and resize given a bounding box.
@@ -98,7 +91,7 @@ def center_crop(
size: Tuple[int, int],
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
r"""Crop the 2D images (4D tensor) from the center.
@@ -174,7 +167,7 @@ def crop_by_boxes(
dst_box: torch.Tensor,
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
"""Perform crop transform on 2D images (4D tensor) given two bounding boxes.
@@ -223,8 +216,8 @@ def crop_by_boxes(
RuntimeError: solve_cpu: For batch 0: U(2,2) is zero, singular U.
"""
# TODO: improve this since might slow down the function
- _validate_bbox(src_box)
- _validate_bbox(dst_box)
+ validate_bbox(src_box)
+ validate_bbox(dst_box)
if len(tensor.shape) != 4:
raise AssertionError(f"Only tensor with shape (B, C, H, W) supported. Got {tensor.shape}.")
@@ -233,7 +226,7 @@ def crop_by_boxes(
# Note: Tensor.dtype must be float. "solve_cpu" not implemented for 'Long'
dst_trans_src: torch.Tensor = get_perspective_transform(src_box.to(tensor), dst_box.to(tensor))
- bbox: Tuple[torch.Tensor, torch.Tensor] = _infer_bbox_shape(dst_box)
+ bbox: Tuple[torch.Tensor, torch.Tensor] = infer_bbox_shape(dst_box)
if not ((bbox[0] == bbox[0][0]).all() and (bbox[1] == bbox[1][0]).all()):
raise AssertionError(
f"Cropping height, width and depth must be exact same in a batch. "
@@ -254,7 +247,7 @@ def crop_by_transform_mat(
out_size: Tuple[int, int],
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
"""Perform crop transform on 2D images (4D tensor) given a perspective transformation matrix.
@@ -279,145 +272,3 @@ def crop_by_transform_mat(
)
return patches
-
-
-def validate_bboxes(boxes: torch.Tensor) -> bool:
- """Validate if a 2D bounding box usable or not.
-
- This function checks if the boxes are rectangular or not.
- Args:
- boxes (torch.Tensor): a tensor containing the coordinates of the
- bounding boxes to be extracted. The tensor must have the shape
- of Bx4x2, where each box is defined in the following (clockwise)
- order: top-left, top-right, bottom-right, bottom-left. The
- coordinates must be in the x, y order.
- """
- warnings.warn(
- "`kornia.geometry.transforms.crop.crop2d.validate_bboxes` is deprecated and will be removed > 0.6.0. "
- "Please use `kornia.geometry.bbox.validate_bbox instead.`",
- DeprecationWarning,
- stacklevel=2,
- )
- return _validate_bbox(boxes)
-
-
-def infer_box_shape(boxes: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
- r"""Auto-infer the output sizes for the given 2D bounding boxes.
-
- Args:
- boxes (torch.Tensor): a tensor containing the coordinates of the
- bounding boxes to be extracted. The tensor must have the shape
- of Bx4x2, where each box is defined in the following (clockwise)
- order: top-left, top-right, bottom-right, bottom-left. The
- coordinates must be in the x, y order.
-
- Returns:
- Tuple[torch.Tensor, torch.Tensor]:
- - Bounding box heights, shape of :math:`(B,)`.
- - Boundingbox widths, shape of :math:`(B,)`.
-
- Example:
- >>> boxes = torch.tensor([[
- ... [1., 1.],
- ... [2., 1.],
- ... [2., 2.],
- ... [1., 2.],
- ... ], [
- ... [1., 1.],
- ... [3., 1.],
- ... [3., 2.],
- ... [1., 2.],
- ... ]]) # 2x4x2
- >>> infer_box_shape(boxes)
- (tensor([2., 2.]), tensor([2., 3.]))
- """
- warnings.warn(
- "`kornia.geometry.transforms.crop.crop2d.infer_box_shape` is deprecated and will be removed > 0.6.0. "
- "Please use `kornia.geometry.bbox.infer_bbox_shape instead.`",
- DeprecationWarning,
- stacklevel=2,
- )
- return _infer_bbox_shape(boxes)
-
-
-def bbox_to_mask(boxes: torch.Tensor, width: int, height: int) -> torch.Tensor:
- """Convert 2D bounding boxes to masks. Covered area is 1. and the remaining is 0.
-
- Args:
- boxes (torch.Tensor): a tensor containing the coordinates of the bounding boxes to be extracted.
- The tensor must have the shape of Bx4x2, where each box is defined in the following (clockwise)
- order: top-left, top-right, bottom-right and bottom-left. The coordinates must be in the x, y order.
- width (int): width of the masked image.
- height (int): height of the masked image.
-
- Returns:
- torch.Tensor: the output mask tensor.
-
- Note:
- It is currently non-differentiable.
-
- Examples:
- >>> boxes = torch.tensor([[
- ... [1., 1.],
- ... [3., 1.],
- ... [3., 2.],
- ... [1., 2.],
- ... ]]) # 1x4x2
- >>> bbox_to_mask(boxes, 5, 5)
- tensor([[[0., 0., 0., 0., 0.],
- [0., 1., 1., 1., 0.],
- [0., 1., 1., 1., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.]]])
- """
- warnings.warn(
- "`kornia.geometry.transforms.crop.crop2d.bbox_to_mask` is deprecated and will be removed > 0.6.0. "
- "Please use `kornia.geometry.bbox.bbox_to_mask instead.`",
- DeprecationWarning,
- stacklevel=2,
- )
- return _bbox_to_mask(boxes, width, height)
-
-
-def bbox_generator(
- x_start: torch.Tensor, y_start: torch.Tensor, width: torch.Tensor, height: torch.Tensor
-) -> torch.Tensor:
- """Generate 2D bounding boxes according to the provided start coords, width and height.
-
- Args:
- x_start (torch.Tensor): a tensor containing the x coordinates of the bounding boxes to be extracted.
- Shape must be a scalar tensor or :math:`(B,)`.
- y_start (torch.Tensor): a tensor containing the y coordinates of the bounding boxes to be extracted.
- Shape must be a scalar tensor or :math:`(B,)`.
- width (torch.Tensor): widths of the masked image.
- Shape must be a scalar tensor or :math:`(B,)`.
- height (torch.Tensor): heights of the masked image.
- Shape must be a scalar tensor or :math:`(B,)`.
-
- Returns:
- torch.Tensor: the bounding box tensor.
-
- Examples:
- >>> x_start = torch.tensor([0, 1])
- >>> y_start = torch.tensor([1, 0])
- >>> width = torch.tensor([5, 3])
- >>> height = torch.tensor([7, 4])
- >>> bbox_generator(x_start, y_start, width, height)
- tensor([[[0, 1],
- [4, 1],
- [4, 7],
- [0, 7]],
- <BLANKLINE>
- [[1, 0],
- [3, 0],
- [3, 3],
- [1, 3]]])
- """
- warnings.warn(
- "`kornia.geometry.transforms.crop.crop2d.bbox_generator` is deprecated and will be removed > 0.6.0. "
- "Please use `kornia.geometry.bbox.bbox_generator instead.`",
- DeprecationWarning,
- stacklevel=2,
- )
- return _bbox_generator(x_start, y_start, width, height)
diff --git a/kornia/geometry/transform/crop/crop3d.py b/kornia/geometry/transform/crop/crop3d.py
index fe416ca10..6187cacb8 100644
--- a/kornia/geometry/transform/crop/crop3d.py
+++ b/kornia/geometry/transform/crop/crop3d.py
@@ -1,12 +1,8 @@
-import warnings
from typing import Optional, Tuple
import torch
-from kornia.geometry.bbox import bbox_generator3d as _bbox_generator3d
-from kornia.geometry.bbox import bbox_to_mask3d as _bbox_to_mask3d
-from kornia.geometry.bbox import infer_bbox_shape3d as _infer_bbox_shape3d
-from kornia.geometry.bbox import validate_bbox3d as _validate_bbox3d
+from kornia.geometry.bbox import infer_bbox_shape3d, validate_bbox3d
from kornia.geometry.transform.projwarp import get_perspective_transform3d, warp_affine3d
__all__ = [
@@ -14,10 +10,6 @@ __all__ = [
"crop_by_boxes3d",
"crop_by_transform_mat3d",
"center_crop3d",
- "validate_bboxes3d",
- "infer_box_shape3d",
- "bbox_to_mask3d",
- "bbox_generator3d",
]
@@ -310,8 +302,8 @@ def crop_by_boxes3d(
[41., 42., 43.],
[45., 46., 47.]]]]])
"""
- _validate_bbox3d(src_box)
- _validate_bbox3d(dst_box)
+ validate_bbox3d(src_box)
+ validate_bbox3d(dst_box)
if len(tensor.shape) != 5:
raise AssertionError(f"Only tensor with shape (B, C, D, H, W) supported. Got {tensor.shape}.")
@@ -322,7 +314,7 @@ def crop_by_boxes3d(
# simulate broadcasting
dst_trans_src = dst_trans_src.expand(tensor.shape[0], -1, -1).type_as(tensor)
- bbox = _infer_bbox_shape3d(dst_box)
+ bbox = infer_bbox_shape3d(dst_box)
if not ((bbox[0] == bbox[0][0]).all() and (bbox[1] == bbox[1][0]).all() and (bbox[2] == bbox[2][0]).all()):
raise AssertionError(
"Cropping height, width and depth must be exact same in a batch."
@@ -346,7 +338,7 @@ def crop_by_transform_mat3d(
out_size: Tuple[int, int, int],
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
"""Perform crop transform on 3D volumes (5D tensor) given a perspective transformation matrix.
@@ -371,187 +363,3 @@ def crop_by_transform_mat3d(
)
return patches
-
-
-def validate_bboxes3d(boxes: torch.Tensor) -> bool:
- """Validate if a 3D bounding box usable or not.
-
- This function checks if the boxes are cube or not.
- Args:
- boxes (torch.Tensor): a tensor containing the coordinates of the bounding boxes to be extracted.
- The tensor must have the shape of Bx8x3, where each box is defined in the following (clockwise)
- order: front-top-left, front-top-right, front-bottom-right, front-bottom-left, back-top-left,
- back-top-right, back-bottom-right, back-bottom-left. The coordinates must be in the x, y, z order.
- """
- warnings.warn(
- "`kornia.geometry.transforms.crop.crop3d.validate_bboxes3d` is deprecated and will be removed > 0.6.0. "
- "Please use `kornia.geometry.bbox.validate_bbox3d instead.`",
- DeprecationWarning,
- stacklevel=2,
- )
- return _validate_bbox3d(boxes)
-
-
-def infer_box_shape3d(boxes: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
- r"""Auto-infer the output sizes for the given 3D bounding boxes.
-
- Args:
- boxes (torch.Tensor): a tensor containing the coordinates of the bounding boxes to be extracted.
- The tensor must have the shape of Bx8x3, where each box is defined in the following (clockwise)
- order: front-top-left, front-top-right, front-bottom-right, front-bottom-left, back-top-left,
- back-top-right, back-bottom-right, back-bottom-left. The coordinates must be in the x, y, z order.
-
- Returns:
- Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
- - Bounding box depths, shape of :math:`(B,)`.
- - Bounding box heights, shape of :math:`(B,)`.
- - Bounding box widths, shape of :math:`(B,)`.
-
- Example:
- >>> boxes = torch.tensor([[[ 0, 1, 2],
- ... [10, 1, 2],
- ... [10, 21, 2],
- ... [ 0, 21, 2],
- ... [ 0, 1, 32],
- ... [10, 1, 32],
- ... [10, 21, 32],
- ... [ 0, 21, 32]],
- ... [[ 3, 4, 5],
- ... [43, 4, 5],
- ... [43, 54, 5],
- ... [ 3, 54, 5],
- ... [ 3, 4, 65],
- ... [43, 4, 65],
- ... [43, 54, 65],
- ... [ 3, 54, 65]]]) # 2x8x3
- >>> infer_box_shape3d(boxes)
- (tensor([31, 61]), tensor([21, 51]), tensor([11, 41]))
- """
- warnings.warn(
- "`kornia.geometry.transforms.crop.crop2d.infer_box_shape3d` is deprecated and will be removed > 0.6.0. "
- "Please use `kornia.geometry.bbox.infer_bbox_shape3d instead.`",
- DeprecationWarning,
- stacklevel=2,
- )
- return _infer_bbox_shape3d(boxes)
-
-
-def bbox_to_mask3d(boxes: torch.Tensor, size: Tuple[int, int, int]) -> torch.Tensor:
- """Convert 3D bounding boxes to masks. Covered area is 1. and the remaining is 0.
-
- Args:
- boxes (torch.Tensor): a tensor containing the coordinates of the bounding boxes to be extracted.
- The tensor must have the shape of Bx8x3, where each box is defined in the following (clockwise)
- order: front-top-left, front-top-right, front-bottom-right, front-bottom-left, back-top-left,
- back-top-right, back-bottom-right, back-bottom-left. The coordinates must be in the x, y, z order.
- size (Tuple[int, int, int]): depth, height and width of the masked image.
-
- Returns:
- torch.Tensor: the output mask tensor.
-
- Examples:
- >>> boxes = torch.tensor([[
- ... [1., 1., 1.],
- ... [2., 1., 1.],
- ... [2., 2., 1.],
- ... [1., 2., 1.],
- ... [1., 1., 2.],
- ... [2., 1., 2.],
- ... [2., 2., 2.],
- ... [1., 2., 2.],
- ... ]]) # 1x8x3
- >>> bbox_to_mask3d(boxes, (4, 5, 5))
- tensor([[[[[0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.]],
- <BLANKLINE>
- [[0., 0., 0., 0., 0.],
- [0., 1., 1., 0., 0.],
- [0., 1., 1., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.]],
- <BLANKLINE>
- [[0., 0., 0., 0., 0.],
- [0., 1., 1., 0., 0.],
- [0., 1., 1., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.]],
- <BLANKLINE>
- [[0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.]]]]])
- """
- warnings.warn(
- "`kornia.geometry.transforms.crop.crop2d.bbox_to_mask3d` is deprecated and will be removed > 0.6.0. "
- "Please use `kornia.geometry.bbox.bbox_to_mask3d instead.`",
- DeprecationWarning,
- stacklevel=2,
- )
- return _bbox_to_mask3d(boxes, size)
-
-
-def bbox_generator3d(
- x_start: torch.Tensor,
- y_start: torch.Tensor,
- z_start: torch.Tensor,
- width: torch.Tensor,
- height: torch.Tensor,
- depth: torch.Tensor,
-) -> torch.Tensor:
- """Generate 3D bounding boxes according to the provided start coords, width, height and depth.
-
- Args:
- x_start (torch.Tensor): a tensor containing the x coordinates of the bounding boxes to be extracted.
- Shape must be a scalar tensor or :math:`(B,)`.
- y_start (torch.Tensor): a tensor containing the y coordinates of the bounding boxes to be extracted.
- Shape must be a scalar tensor or :math:`(B,)`.
- z_start (torch.Tensor): a tensor containing the z coordinates of the bounding boxes to be extracted.
- Shape must be a scalar tensor or :math:`(B,)`.
- width (torch.Tensor): widths of the masked image.
- Shape must be a scalar tensor or :math:`(B,)`.
- height (torch.Tensor): heights of the masked image.
- Shape must be a scalar tensor or :math:`(B,)`.
- depth (torch.Tensor): depths of the masked image.
- Shape must be a scalar tensor or :math:`(B,)`.
-
- Returns:
- torch.Tensor: the 3d bounding box tensor :math:`(B, 8, 3)`.
-
- Examples:
- >>> x_start = torch.tensor([0, 3])
- >>> y_start = torch.tensor([1, 4])
- >>> z_start = torch.tensor([2, 5])
- >>> width = torch.tensor([10, 40])
- >>> height = torch.tensor([20, 50])
- >>> depth = torch.tensor([30, 60])
- >>> bbox_generator3d(x_start, y_start, z_start, width, height, depth)
- tensor([[[ 0, 1, 2],
- [10, 1, 2],
- [10, 21, 2],
- [ 0, 21, 2],
- [ 0, 1, 32],
- [10, 1, 32],
- [10, 21, 32],
- [ 0, 21, 32]],
- <BLANKLINE>
- [[ 3, 4, 5],
- [43, 4, 5],
- [43, 54, 5],
- [ 3, 54, 5],
- [ 3, 4, 65],
- [43, 4, 65],
- [43, 54, 65],
- [ 3, 54, 65]]])
- """
- warnings.warn(
- "`kornia.geometry.transforms.crop.crop2d.bbox_generator3d` is deprecated and will be removed > 0.6.0. "
- "Please use `kornia.geometry.bbox.bbox_generator3d instead.`",
- DeprecationWarning,
- stacklevel=2,
- )
- return _bbox_generator3d(x_start, y_start, z_start, width, height, depth)
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index bc78c257b..c7f1d9abf 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -37,7 +37,7 @@ def warp_perspective(
dsize: Tuple[int, int],
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
r"""Apply a perspective transformation to an image.
@@ -89,17 +89,6 @@ def warp_perspective(
if not (len(M.shape) == 3 and M.shape[-2:] == (3, 3)):
raise ValueError(f"Input M must be a Bx3x3 tensor. Got {M.shape}")
- # TODO: remove the statement below in kornia v0.6
- if align_corners is None:
- message: str = (
- "The align_corners default value has been changed. By default now is set True "
- "in order to match cv2.warpPerspective. In case you want to keep your previous "
- "behaviour set it to False. This warning will disappear in kornia > v0.6."
- )
- warnings.warn(message)
- # set default value for align corners
- align_corners = True
-
B, _, H, W = src.size()
h_out, w_out = dsize
@@ -123,7 +112,7 @@ def warp_affine(
dsize: Tuple[int, int],
mode: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
r"""Apply an affine transformation to a tensor.
@@ -174,17 +163,6 @@ def warp_affine(
if not (len(M.shape) == 3 or M.shape[-2:] == (2, 3)):
raise ValueError(f"Input M must be a Bx2x3 tensor. Got {M.shape}")
- # TODO: remove the statement below in kornia v0.6
- if align_corners is None:
- message: str = (
- "The align_corners default value has been changed. By default now is set True "
- "in order to match cv2.warpAffine. In case you want to keep your previous "
- "behaviour set it to False. This warning will disappear in kornia > v0.6."
- )
- warnings.warn(message)
- # set default value for align corners
- align_corners = True
-
B, C, H, W = src.size()
# we generate a 3x3 transformation matrix from 2x3 affine
diff --git a/kornia/geometry/transform/projwarp.py b/kornia/geometry/transform/projwarp.py
index 756b8aa48..7158d3a6d 100644
--- a/kornia/geometry/transform/projwarp.py
+++ b/kornia/geometry/transform/projwarp.py
@@ -25,7 +25,7 @@ def warp_affine3d(
dsize: Tuple[int, int, int],
flags: str = 'bilinear',
padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None,
+ align_corners: bool = True,
) -> torch.Tensor:
r"""Apply a projective transformation a to 3d tensor.
@@ -56,17 +56,6 @@ def warp_affine3d(
raise AssertionError(dsize)
B, C, D, H, W = src.size()
- # TODO: remove the statement below in kornia v0.6
- if align_corners is None:
- message: str = (
- "The align_corners default value has been changed. By default now is set True "
- "in order to match cv2.warpAffine. In case you want to keep your previous "
- "behaviour set it to False. This warning will disappear in kornia > v0.6."
- )
- warnings.warn(message)
- # set default value for align corners
- align_corners = True
-
size_src: Tuple[int, int, int] = (D, H, W)
size_out: Tuple[int, int, int] = dsize
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
index 5484e8bff..274e42d3f 100644
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -2609,9 +2609,9 @@ class TestNormalize:
f = Normalize(mean=mean, std=std, p=1)
data = torch.ones(2, 3, 256, 313)
if isinstance(mean, float):
- expected = (data - torch.tensor(mean)) / torch.tensor(std)
+ expected = (data - torch.as_tensor(mean)) / torch.as_tensor(std)
else:
- expected = (data - torch.tensor(mean[0])) / torch.tensor(std[0])
+ expected = (data - torch.as_tensor(mean[0])) / torch.as_tensor(std[0])
assert_close(f(data), expected)
@staticmethod
diff --git a/test/enhance/test_normalize.py b/test/enhance/test_normalize.py
index c6a2edc38..3fc1a9a00 100644
--- a/test/enhance/test_normalize.py
+++ b/test/enhance/test_normalize.py
@@ -125,9 +125,9 @@ class TestNormalize:
f = kornia.enhance.Normalize(mean=mean, std=std)
data = torch.ones(2, 3, 256, 313)
if isinstance(mean, float):
- expected = (data - torch.tensor(mean)) / torch.tensor(std)
+ expected = (data - torch.as_tensor(mean)) / torch.as_tensor(std)
else:
- expected = (data - torch.tensor(mean[0])) / torch.tensor(std[0])
+ expected = (data - torch.as_tensor(mean[0])) / torch.as_tensor(std[0])
assert_close(f(data), expected)
@staticmethod
diff --git a/test/geometry/test_linalg.py b/test/geometry/test_linalg.py
index 827c6d96d..dac1ac243 100644
--- a/test/geometry/test_linalg.py
+++ b/test/geometry/test_linalg.py
@@ -123,122 +123,6 @@ class TestTransformPoints:
assert_close(actual, expected, atol=1e-4, rtol=1e-4)
-class TestTransformBoxes:
- def test_transform_boxes(self, device, dtype):
-
- boxes = torch.tensor([[139.2640, 103.0150, 397.3120, 410.5225]], device=device, dtype=dtype)
-
- expected = torch.tensor([[372.7360, 103.0150, 114.6880, 410.5225]], device=device, dtype=dtype)
-
- trans_mat = torch.tensor([[[-1.0, 0.0, 512.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
-
- out = kornia.transform_boxes(trans_mat, boxes)
- assert_close(out, expected, atol=1e-4, rtol=1e-4)
-
- def test_transform_multiple_boxes(self, device, dtype):
-
- boxes = torch.tensor(
- [
- [139.2640, 103.0150, 397.3120, 410.5225],
- [1.0240, 80.5547, 512.0000, 512.0000],
- [165.2053, 262.1440, 510.6347, 508.9280],
- [119.8080, 144.2067, 257.0240, 410.1292],
- ],
- device=device,
- dtype=dtype,
- )
-
- boxes = boxes.repeat(2, 1, 1) # 2 x 4 x 4 two images 4 boxes each
-
- expected = torch.tensor(
- [
- [
- [372.7360, 103.0150, 114.6880, 410.5225],
- [510.9760, 80.5547, 0.0000, 512.0000],
- [346.7947, 262.1440, 1.3653, 508.9280],
- [392.1920, 144.2067, 254.9760, 410.1292],
- ],
- [
- [139.2640, 103.0150, 397.3120, 410.5225],
- [1.0240, 80.5547, 512.0000, 512.0000],
- [165.2053, 262.1440, 510.6347, 508.9280],
- [119.8080, 144.2067, 257.0240, 410.1292],
- ],
- ],
- device=device,
- dtype=dtype,
- )
-
- trans_mat = torch.tensor(
- [
- [[-1.0, 0.0, 512.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
- [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
- ],
- device=device,
- dtype=dtype,
- )
-
- out = kornia.transform_boxes(trans_mat, boxes)
- assert_close(out, expected, atol=1e-4, rtol=1e-4)
-
- def test_transform_boxes_wh(self, device, dtype):
-
- boxes = torch.tensor(
- [
- [139.2640, 103.0150, 258.0480, 307.5075],
- [1.0240, 80.5547, 510.9760, 431.4453],
- [165.2053, 262.1440, 345.4293, 246.7840],
- [119.8080, 144.2067, 137.2160, 265.9225],
- ],
- device=device,
- dtype=dtype,
- )
-
- expected = torch.tensor(
- [
- [372.7360, 103.0150, -258.0480, 307.5075],
- [510.9760, 80.5547, -510.9760, 431.4453],
- [346.7947, 262.1440, -345.4293, 246.7840],
- [392.1920, 144.2067, -137.2160, 265.9225],
- ],
- device=device,
- dtype=dtype,
- )
-
- trans_mat = torch.tensor([[[-1.0, 0.0, 512.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
-
- out = kornia.transform_boxes(trans_mat, boxes, mode='xywh')
- assert_close(out, expected, atol=1e-4, rtol=1e-4)
-
- def test_gradcheck(self, device, dtype):
-
- boxes = torch.tensor(
- [
- [139.2640, 103.0150, 258.0480, 307.5075],
- [1.0240, 80.5547, 510.9760, 431.4453],
- [165.2053, 262.1440, 345.4293, 246.7840],
- [119.8080, 144.2067, 137.2160, 265.9225],
- ],
- device=device,
- dtype=dtype,
- )
-
- trans_mat = torch.tensor([[[-1.0, 0.0, 512.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
-
- trans_mat = utils.tensor_to_gradcheck_var(trans_mat)
- boxes = utils.tensor_to_gradcheck_var(boxes)
-
- assert gradcheck(kornia.transform_boxes, (trans_mat, boxes), raise_exception=True)
-
- def test_jit(self, device, dtype):
- boxes = torch.tensor([[139.2640, 103.0150, 258.0480, 307.5075]], device=device, dtype=dtype)
- trans_mat = torch.tensor([[[-1.0, 0.0, 512.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
- args = (boxes, trans_mat)
- op = kornia.geometry.transform_points
- op_jit = torch.jit.script(op)
- assert_close(op(*args), op_jit(*args))
-
-
class TestComposeTransforms:
def test_translation_4x4(self, device, dtype):
offset = 10
diff --git a/test/geometry/transform/test_affine.py b/test/geometry/transform/test_affine.py
index 1386e22b1..b0032b3bd 100644
--- a/test/geometry/transform/test_affine.py
+++ b/test/geometry/transform/test_affine.py
@@ -10,166 +10,166 @@ from kornia.testing import assert_close
class TestResize:
def test_smoke(self, device, dtype):
inp = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
- out = kornia.resize(inp, (3, 4))
+ out = kornia.resize(inp, (3, 4), align_corners=False)
assert_close(inp, out, atol=1e-4, rtol=1e-4)
# 2D
inp = torch.rand(3, 4, device=device, dtype=dtype)
- out = kornia.resize(inp, (3, 4))
+ out = kornia.resize(inp, (3, 4), align_corners=False)
assert_close(inp, out, atol=1e-4, rtol=1e-4)
# 3D
inp = torch.rand(3, 3, 4, device=device, dtype=dtype)
- out = kornia.resize(inp, (3, 4))
+ out = kornia.resize(inp, (3, 4), align_corners=False)
assert_close(inp, out, atol=1e-4, rtol=1e-4)
# arbitrary dim
inp = torch.rand(1, 2, 3, 2, 1, 3, 3, 4, device=device, dtype=dtype)
- out = kornia.resize(inp, (3, 4))
+ out = kornia.resize(inp, (3, 4), align_corners=False)
assert_close(inp, out, atol=1e-4, rtol=1e-4)
def test_upsize(self, device, dtype):
inp = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
- out = kornia.resize(inp, (6, 8))
+ out = kornia.resize(inp, (6, 8), align_corners=False)
assert out.shape == (1, 3, 6, 8)
# 2D
inp = torch.rand(3, 4, device=device, dtype=dtype)
- out = kornia.resize(inp, (6, 8))
+ out = kornia.resize(inp, (6, 8), align_corners=False)
assert out.shape == (6, 8)
# 3D
inp = torch.rand(3, 3, 4, device=device, dtype=dtype)
- out = kornia.resize(inp, (6, 8))
+ out = kornia.resize(inp, (6, 8), align_corners=False)
assert out.shape == (3, 6, 8)
# arbitrary dim
inp = torch.rand(1, 2, 3, 2, 1, 3, 3, 4, device=device, dtype=dtype)
- out = kornia.resize(inp, (6, 8))
+ out = kornia.resize(inp, (6, 8), align_corners=False)
assert out.shape == (1, 2, 3, 2, 1, 3, 6, 8)
def test_downsize(self, device, dtype):
inp = torch.rand(1, 3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, (3, 1))
+ out = kornia.resize(inp, (3, 1), align_corners=False)
assert out.shape == (1, 3, 3, 1)
# 2D
inp = torch.rand(5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, (3, 1))
+ out = kornia.resize(inp, (3, 1), align_corners=False)
assert out.shape == (3, 1)
# 3D
inp = torch.rand(3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, (3, 1))
+ out = kornia.resize(inp, (3, 1), align_corners=False)
assert out.shape == (3, 3, 1)
# arbitrary dim
inp = torch.rand(1, 2, 3, 2, 1, 3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, (3, 1))
+ out = kornia.resize(inp, (3, 1), align_corners=False)
assert out.shape == (1, 2, 3, 2, 1, 3, 3, 1)
def test_downsizeAA(self, device, dtype):
inp = torch.rand(1, 3, 10, 8, device=device, dtype=dtype)
- out = kornia.resize(inp, (5, 3), antialias=True)
+ out = kornia.resize(inp, (5, 3), align_corners=False, antialias=True)
assert out.shape == (1, 3, 5, 3)
inp = torch.rand(1, 1, 20, 10, device=device, dtype=dtype)
- out = kornia.resize(inp, (15, 8), antialias=True)
+ out = kornia.resize(inp, (15, 8), align_corners=False, antialias=True)
assert out.shape == (1, 1, 15, 8)
# 2D
inp = torch.rand(10, 8, device=device, dtype=dtype)
- out = kornia.resize(inp, (5, 3), antialias=True)
+ out = kornia.resize(inp, (5, 3), align_corners=False, antialias=True)
assert out.shape == (5, 3)
# 3D
inp = torch.rand(3, 10, 8, device=device, dtype=dtype)
- out = kornia.resize(inp, (5, 3), antialias=True)
+ out = kornia.resize(inp, (5, 3), align_corners=False, antialias=True)
assert out.shape == (3, 5, 3)
# arbitrary dim
inp = torch.rand(1, 2, 3, 2, 1, 3, 10, 8, device=device, dtype=dtype)
- out = kornia.resize(inp, (5, 3), antialias=True)
+ out = kornia.resize(inp, (5, 3), align_corners=False, antialias=True)
assert out.shape == (1, 2, 3, 2, 1, 3, 5, 3)
def test_one_param(self, device, dtype):
inp = torch.rand(1, 3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10)
+ out = kornia.resize(inp, 10, align_corners=False)
assert out.shape == (1, 3, 25, 10)
# 2D
inp = torch.rand(5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10)
+ out = kornia.resize(inp, 10, align_corners=False)
assert out.shape == (25, 10)
# 3D
inp = torch.rand(3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10)
+ out = kornia.resize(inp, 10, align_corners=False)
assert out.shape == (3, 25, 10)
# arbitrary dim
inp = torch.rand(1, 2, 3, 2, 1, 3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10)
+ out = kornia.resize(inp, 10, align_corners=False)
assert out.shape == (1, 2, 3, 2, 1, 3, 25, 10)
def test_one_param_long(self, device, dtype):
inp = torch.rand(1, 3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="long")
+ out = kornia.resize(inp, 10, align_corners=False, side="long")
assert out.shape == (1, 3, 10, 4)
# 2D
inp = torch.rand(5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="long")
+ out = kornia.resize(inp, 10, align_corners=False, side="long")
assert out.shape == (10, 4)
# 3D
inp = torch.rand(3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="long")
+ out = kornia.resize(inp, 10, align_corners=False, side="long")
assert out.shape == (3, 10, 4)
# arbitrary dim
inp = torch.rand(1, 2, 3, 2, 1, 3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="long")
+ out = kornia.resize(inp, 10, align_corners=False, side="long")
assert out.shape == (1, 2, 3, 2, 1, 3, 10, 4)
def test_one_param_vert(self, device, dtype):
inp = torch.rand(1, 3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="vert")
+ out = kornia.resize(inp, 10, align_corners=False, side="vert")
assert out.shape == (1, 3, 10, 4)
# 2D
inp = torch.rand(5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="vert")
+ out = kornia.resize(inp, 10, align_corners=False, side="vert")
assert out.shape == (10, 4)
# 3D
inp = torch.rand(3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="vert")
+ out = kornia.resize(inp, 10, align_corners=False, side="vert")
assert out.shape == (3, 10, 4)
# arbitrary dim
inp = torch.rand(1, 2, 3, 2, 1, 3, 5, 2, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="vert")
+ out = kornia.resize(inp, 10, align_corners=False, side="vert")
assert out.shape == (1, 2, 3, 2, 1, 3, 10, 4)
def test_one_param_horz(self, device, dtype):
inp = torch.rand(1, 3, 2, 5, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="horz")
+ out = kornia.resize(inp, 10, align_corners=False, side="horz")
assert out.shape == (1, 3, 4, 10)
# 2D
inp = torch.rand(1, 3, 2, 5, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="horz")
+ out = kornia.resize(inp, 10, align_corners=False, side="horz")
assert out.shape == (1, 3, 4, 10)
# 3D
inp = torch.rand(1, 3, 2, 5, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="horz")
+ out = kornia.resize(inp, 10, align_corners=False, side="horz")
assert out.shape == (1, 3, 4, 10)
# arbitrary dim
inp = torch.rand(1, 3, 2, 5, device=device, dtype=dtype)
- out = kornia.resize(inp, 10, side="horz")
+ out = kornia.resize(inp, 10, align_corners=False, side="horz")
assert out.shape == (1, 3, 4, 10)
def test_gradcheck(self, device, dtype):
@@ -177,30 +177,30 @@ class TestResize:
new_size = 4
input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
input = utils.tensor_to_gradcheck_var(input) # to var
- assert gradcheck(kornia.Resize(new_size), (input,), raise_exception=True)
+ assert gradcheck(kornia.Resize(new_size, align_corners=False), (input,), raise_exception=True)
class TestRescale:
def test_smoke(self, device, dtype):
input = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
- output = kornia.rescale(input, (1.0, 1.0))
+ output = kornia.rescale(input, (1.0, 1.0), align_corners=False)
assert_close(input, output, atol=1e-4, rtol=1e-4)
def test_upsize(self, device, dtype):
input = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
- output = kornia.rescale(input, (3.0, 2.0))
+ output = kornia.rescale(input, (3.0, 2.0), align_corners=False)
assert output.shape == (1, 3, 9, 8)
def test_downsize(self, device, dtype):
input = torch.rand(1, 3, 9, 8, device=device, dtype=dtype)
- output = kornia.rescale(input, (1.0 / 3.0, 1.0 / 2.0))
+ output = kornia.rescale(input, (1.0 / 3.0, 1.0 / 2.0), align_corners=False)
assert output.shape == (1, 3, 3, 4)
def test_downscale_values(self, device, dtype):
inp_x = torch.arange(20, device=device, dtype=dtype) / 20.0
inp = inp_x[None].T @ inp_x[None]
inp = inp[None, None]
- out = kornia.rescale(inp, (0.25, 0.25), antialias=False)
+ out = kornia.rescale(inp, (0.25, 0.25), antialias=False, align_corners=False)
expected = torch.tensor(
[
[
@@ -222,7 +222,7 @@ class TestRescale:
inp_x = torch.arange(20, device=device, dtype=dtype) / 20.0
inp = inp_x[None].T @ inp_x[None]
inp = inp[None, None]
- out = kornia.rescale(inp, (0.25, 0.25), antialias=True)
+ out = kornia.rescale(inp, (0.25, 0.25), antialias=True, align_corners=False)
expected = torch.tensor(
[
[
@@ -242,13 +242,13 @@ class TestRescale:
def test_one_param(self, device, dtype):
input = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
- output = kornia.rescale(input, 2.0)
+ output = kornia.rescale(input, 2.0, align_corners=False)
assert output.shape == (1, 3, 6, 8)
def test_gradcheck(self, device, dtype):
input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
input = utils.tensor_to_gradcheck_var(input)
- assert gradcheck(kornia.Rescale(2.0), (input,), raise_exception=True)
+ assert gradcheck(kornia.Rescale(2.0, align_corners=False), (input,), raise_exception=True)
class TestRotate:
@@ -474,7 +474,7 @@ class TestShear:
# prepare transformation
shear = torch.tensor([[0.5, 0.0]], device=device, dtype=dtype)
- transform = kornia.Shear(shear)
+ transform = kornia.Shear(shear, align_corners=False)
assert_close(transform(inp), expected, atol=1e-4, rtol=1e-4)
def test_shear_y(self, device, dtype):
@@ -492,7 +492,7 @@ class TestShear:
# prepare transformation
shear = torch.tensor([[0.0, 0.5]], device=device, dtype=dtype)
- transform = kornia.Shear(shear)
+ transform = kornia.Shear(shear, align_corners=False)
assert_close(transform(inp), expected, atol=1e-4, rtol=1e-4)
def test_shear_batch2(self, device, dtype):
@@ -514,7 +514,7 @@ class TestShear:
# prepare transformation
shear = torch.tensor([[0.5, 0.0], [0.0, 0.5]], device=device, dtype=dtype)
- transform = kornia.Shear(shear)
+ transform = kornia.Shear(shear, align_corners=False)
assert_close(transform(inp), expected, atol=1e-4, rtol=1e-4)
def test_shear_batch2_broadcast(self, device, dtype):
@@ -533,7 +533,7 @@ class TestShear:
# prepare transformation
shear = torch.tensor([[0.5, 0.0]], device=device, dtype=dtype)
- transform = kornia.Shear(shear)
+ transform = kornia.Shear(shear, align_corners=False)
assert_close(transform(inp), expected, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device, dtype):
@@ -552,7 +552,7 @@ class TestShear:
shear = torch.tensor([[0.5, 0.0]], device=device, dtype=dtype)
batch_size, channels, height, width = 2, 3, 64, 64
img = torch.ones(batch_size, channels, height, width, device=device, dtype=dtype)
- trans = kornia.Shear(shear)
+ trans = kornia.Shear(shear, align_corners=False)
trans_traced = torch.jit.trace(kornia.Shear(shear), img)
assert_close(trans(img), trans_traced(img), atol=1e-4, rtol=1e-4)
diff --git a/test/geometry/transform/test_bbox.py b/test/geometry/transform/test_bbox.py
new file mode 100644
index 000000000..9bbf04191
--- /dev/null
+++ b/test/geometry/transform/test_bbox.py
@@ -0,0 +1,122 @@
+import torch
+from torch.autograd import gradcheck
+
+import kornia
+import kornia.testing as utils # test utils
+from kornia.testing import assert_close
+
+
+class TestTransformBoxes:
+ def test_transform_bbox(self, device, dtype):
+
+ boxes = torch.tensor([[139.2640, 103.0150, 397.3120, 410.5225]], device=device, dtype=dtype)
+
+ expected = torch.tensor([[372.7360, 103.0150, 114.6880, 410.5225]], device=device, dtype=dtype)
+
+ trans_mat = torch.tensor([[[-1.0, 0.0, 512.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
+
+ out = kornia.transform_bbox(trans_mat, boxes)
+ assert_close(out, expected, atol=1e-4, rtol=1e-4)
+
+ def test_transform_multiple_boxes(self, device, dtype):
+
+ boxes = torch.tensor(
+ [
+ [139.2640, 103.0150, 397.3120, 410.5225],
+ [1.0240, 80.5547, 512.0000, 512.0000],
+ [165.2053, 262.1440, 510.6347, 508.9280],
+ [119.8080, 144.2067, 257.0240, 410.1292],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ boxes = boxes.repeat(2, 1, 1) # 2 x 4 x 4 two images 4 boxes each
+
+ expected = torch.tensor(
+ [
+ [
+ [372.7360, 103.0150, 114.6880, 410.5225],
+ [510.9760, 80.5547, 0.0000, 512.0000],
+ [346.7947, 262.1440, 1.3653, 508.9280],
+ [392.1920, 144.2067, 254.9760, 410.1292],
+ ],
+ [
+ [139.2640, 103.0150, 397.3120, 410.5225],
+ [1.0240, 80.5547, 512.0000, 512.0000],
+ [165.2053, 262.1440, 510.6347, 508.9280],
+ [119.8080, 144.2067, 257.0240, 410.1292],
+ ],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ trans_mat = torch.tensor(
+ [
+ [[-1.0, 0.0, 512.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
+ [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ out = kornia.transform_bbox(trans_mat, boxes)
+ assert_close(out, expected, atol=1e-4, rtol=1e-4)
+
+ def test_transform_bbox_wh(self, device, dtype):
+
+ boxes = torch.tensor(
+ [
+ [139.2640, 103.0150, 258.0480, 307.5075],
+ [1.0240, 80.5547, 510.9760, 431.4453],
+ [165.2053, 262.1440, 345.4293, 246.7840],
+ [119.8080, 144.2067, 137.2160, 265.9225],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ expected = torch.tensor(
+ [
+ [372.7360, 103.0150, -258.0480, 307.5075],
+ [510.9760, 80.5547, -510.9760, 431.4453],
+ [346.7947, 262.1440, -345.4293, 246.7840],
+ [392.1920, 144.2067, -137.2160, 265.9225],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ trans_mat = torch.tensor([[[-1.0, 0.0, 512.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
+
+ out = kornia.transform_bbox(trans_mat, boxes, mode='xywh')
+ assert_close(out, expected, atol=1e-4, rtol=1e-4)
+
+ def test_gradcheck(self, device, dtype):
+
+ boxes = torch.tensor(
+ [
+ [139.2640, 103.0150, 258.0480, 307.5075],
+ [1.0240, 80.5547, 510.9760, 431.4453],
+ [165.2053, 262.1440, 345.4293, 246.7840],
+ [119.8080, 144.2067, 137.2160, 265.9225],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ trans_mat = torch.tensor([[[-1.0, 0.0, 512.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
+
+ trans_mat = utils.tensor_to_gradcheck_var(trans_mat)
+ boxes = utils.tensor_to_gradcheck_var(boxes)
+
+ assert gradcheck(kornia.transform_bbox, (trans_mat, boxes), raise_exception=True)
+
+ def test_jit(self, device, dtype):
+ boxes = torch.tensor([[139.2640, 103.0150, 258.0480, 307.5075]], device=device, dtype=dtype)
+ trans_mat = torch.tensor([[[-1.0, 0.0, 512.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
+ args = (boxes, trans_mat)
+ op = kornia.geometry.transform_points
+ op_jit = torch.jit.script(op)
+ assert_close(op(*args), op_jit(*args))
diff --git a/test/losses/test_hd.py b/test/losses/test_hd.py
index 212f40748..fefe44051 100644
--- a/test/losses/test_hd.py
+++ b/test/losses/test_hd.py
@@ -32,7 +32,7 @@ class HausdorffERLossNumpy(nn.Module):
@torch.no_grad()
def perform_erosion(
- self, pred: np.ndarray, target: np.ndarray, debug
+ self, pred: np.ndarray, target: np.ndarray
) -> np.ndarray:
bound = (pred - target) ** 2
@@ -44,13 +44,9 @@ class HausdorffERLossNumpy(nn.Module):
raise ValueError(f"Dimension {bound.ndim} is nor supported.")
eroted = np.zeros_like(bound)
- erosions = []
for batch in range(len(bound)):
- # debug
- erosions.append(np.copy(bound[batch][0]))
-
for k in range(self.erosions):
# compute convolution with kernel
@@ -66,17 +62,11 @@ class HausdorffERLossNumpy(nn.Module):
# save erosion and add to loss
bound[batch] = erosion
eroted[batch] += erosion * (k + 1) ** self.alpha
- if debug:
- erosions.append(np.copy(erosion[0]))
- # image visualization in debug mode
- if debug:
- return eroted, erosions
- else:
- return eroted
+ return eroted
def forward_one(
- self, pred: torch.Tensor, target: torch.Tensor, debug=False
+ self, pred: torch.Tensor, target: torch.Tensor
) -> torch.Tensor:
"""
Uses one binary channel: 1 - fg, 0 - bg
@@ -86,23 +76,16 @@ class HausdorffERLossNumpy(nn.Module):
assert pred.size(1) == target.size(1) == 1
# pred = torch.sigmoid(pred)
- if debug:
- eroted, erosions = self.perform_erosion(
- pred.cpu().numpy(), target.cpu().numpy(), debug
- )
- return eroted.mean(), erosions
-
- else:
- eroted = torch.from_numpy(
- self.perform_erosion(pred.cpu().numpy(), target.cpu().numpy(), debug)
- ).to(dtype=pred.dtype, device=pred.device)
+ eroted = torch.from_numpy(
+ self.perform_erosion(pred.cpu().numpy(), target.cpu().numpy())
+ ).to(dtype=pred.dtype, device=pred.device)
- loss = eroted.mean()
+ loss = eroted.mean()
- return loss
+ return loss
def forward(
- self, pred: torch.Tensor, target: torch.Tensor, debug=False
+ self, pred: torch.Tensor, target: torch.Tensor
) -> torch.Tensor:
"""
Uses one binary channel: 1 - fg, 0 - bg
diff --git a/test/test_contrib.py b/test/test_contrib.py
index ba8c3e9c0..778ef85b6 100644
--- a/test/test_contrib.py
+++ b/test/test_contrib.py
@@ -91,34 +91,6 @@ class TestConnectedComponents:
assert_close(op(img), op_jit(img))
-class TestMaxBlurPool2d:
- def test_shape(self, device):
- input = torch.rand(1, 2, 4, 6).to(device)
- pool = kornia.contrib.MaxBlurPool2d(kernel_size=3)
- assert pool(input).shape == (1, 2, 2, 3)
-
- def test_shape_batch(self, device):
- input = torch.rand(3, 2, 6, 10).to(device)
- pool = kornia.contrib.MaxBlurPool2d(kernel_size=5)
- assert pool(input).shape == (3, 2, 3, 5)
-
- def test_gradcheck(self, device):
- input = torch.rand(2, 3, 4, 4).to(device)
- input = utils.tensor_to_gradcheck_var(input) # to var
- assert gradcheck(kornia.contrib.max_blur_pool2d, (input, 3), raise_exception=True)
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
- @torch.jit.script
- def op_script(input: torch.Tensor, kernel_size: int) -> torch.Tensor:
- return kornia.contrib.max_blur_pool2d(input, kernel_size)
-
- img = torch.rand(2, 3, 4, 5).to(device)
- actual = op_script(img, kernel_size=3)
- expected = kornia.contrib.max_blur_pool2d(img, kernel_size=3)
- assert_close(actual, expected)
-
-
class TestExtractTensorPatches:
def test_smoke(self, device):
input = torch.arange(16.0).view(1, 1, 4, 4).to(device)