Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement resize

edgarribaef05d55d9944
diff --git a/docs/source/geometry.transform.rst b/docs/source/geometry.transform.rst
index 0025cbfbb..adc9d7288 100644
--- a/docs/source/geometry.transform.rst
+++ b/docs/source/geometry.transform.rst
@@ -25,6 +25,7 @@ The functions in this section perform various geometrical transformations of 2D
.. autofunction:: hflip
.. autofunction:: vflip
.. autofunction:: rot180
+.. autofunction:: resize
.. autoclass:: Rotate
.. autoclass:: Translate
@@ -36,4 +37,5 @@ The functions in this section perform various geometrical transformations of 2D
.. autoclass:: Hflip
.. autoclass:: Vflip
.. autoclass:: Rot180
+.. autoclass:: Resize
diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.py
index 8e51f7641..5f4ed2177 100644
--- a/kornia/geometry/transform/affwarp.py
+++ b/kornia/geometry/transform/affwarp.py
@@ -1,4 +1,4 @@
-from typing import Union
+from typing import Union, Tuple
import torch
import torch.nn as nn
@@ -13,10 +13,12 @@ __all__ = [
"rotate",
"translate",
"shear",
+ "resize",
"Scale",
"Rotate",
"Translate",
"Shear",
+ "Resize",
]
# utilities to compute affine matrices
@@ -213,6 +215,58 @@ def shear(tensor: torch.Tensor, shear: torch.Tensor) -> torch.Tensor:
return affine(tensor, shear_matrix[..., :2, :3])
+def resize(input: torch.Tensor, size: Union[int, Tuple[int, int]], interpolation: str = 'bilinear') -> torch.Tensor:
+ r"""Resize the input torch.Tensor to the given size.
+
+ See :class:`~kornia.Resize` for details.
+ """
+ if not torch.is_tensor(input):
+ raise TypeError("Input tensor type is not a torch.Tensor. Got {}"
+ .format(type(input)))
+
+ new_size: Tuple[int, int]
+
+ if isinstance(size, int):
+ w, h = input.shape[-2:]
+ if (w <= h and w == size) or (h <= w and h == size):
+ return input
+ if w < h:
+ ow = size
+ oh = int(size * h / w)
+ else:
+ oh = size
+ ow = int(size * w / h)
+ new_size = (ow, oh)
+ else:
+ new_size = size
+
+ return torch.nn.functional.interpolate(input, size=new_size, mode=interpolation)
+
+
+class Resize(nn.Module):
+ r"""Resize the input torch.Tensor to the given size.
+
+ Args:
+ size (int, tuple(int, int)): Desired output size. If size is a sequence like (h, w),
+ output size will be matched to this. If size is an int, smaller edge of the image will
+ be matched to this number. i.e, if height > width, then image will be rescaled
+ to (size * height / width, size)
+ interpolation (str): algorithm used for upsampling: 'nearest' | 'linear' | 'bilinear' |
+ 'bicubic' | 'trilinear' | 'area'. Default: 'bilinear'.
+
+ Returns:
+ torch.Tensor: The resized tensor.
+ """
+
+ def __init__(self, size: Union[int, Tuple[int, int]], interpolation: str = 'bilinear') -> None:
+ super(Resize, self).__init__()
+ self.size: Union[int, Tuple[int, int]] = size
+ self.interpolation: str = interpolation
+
+ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore
+ return resize(input, self.size, self.interpolation)
+
+
class Rotate(nn.Module):
r"""Rotate the tensor anti-clockwise about the centre.
diff --git a/test/geometry/transform/test_affine.py b/test/geometry/transform/test_affine.py
index cb99582ab..d24087445 100644
--- a/test/geometry/transform/test_affine.py
+++ b/test/geometry/transform/test_affine.py
@@ -9,6 +9,35 @@ from torch.autograd import gradcheck
from torch.testing import assert_allclose
+class TestResize:
+ def test_smoke(self, device):
+ inp = torch.rand(1, 3, 3, 4).to(device)
+ out = kornia.resize(inp, (3, 4))
+ assert_allclose(inp, out)
+
+ def test_upsize(self, device):
+ inp = torch.rand(1, 3, 3, 4).to(device)
+ out = kornia.resize(inp, (6, 8))
+ assert out.shape == (1, 3, 6, 8)
+
+ def test_upsize(self, device):
+ inp = torch.rand(1, 3, 5, 2).to(device)
+ out = kornia.resize(inp, (3, 1))
+ assert out.shape == (1, 3, 3, 1)
+
+ def test_one_param(self, device):
+ inp = torch.rand(1, 3, 5, 2).to(device)
+ out = kornia.resize(inp, 10)
+ assert out.shape == (1, 3, 25, 10)
+
+ def test_gradcheck(self, device):
+ # test parameters
+ new_size = 4
+ input = torch.rand(1, 2, 3, 4).to(device)
+ input = utils.tensor_to_gradcheck_var(input) # to var
+ assert gradcheck(kornia.Resize(new_size), (input, ), raise_exception=True)
+
+
class TestRotate:
def test_angle90(self, device):
# prepare input data