Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[Feat] Add rescale functional and transformation (#631)

Philip Meier7c1907912c98
diff --git a/docs/source/geometry.transform.rst b/docs/source/geometry.transform.rst
index 0c05265ad..09e564722 100644
--- a/docs/source/geometry.transform.rst
+++ b/docs/source/geometry.transform.rst
@@ -33,6 +33,7 @@ The functions in this section perform various geometrical transformations of 2D
.. autofunction:: vflip
.. autofunction:: rot180
.. autofunction:: resize
+.. autofunction:: rescale
.. autoclass:: Rotate
.. autoclass:: Translate
@@ -45,4 +46,5 @@ The functions in this section perform various geometrical transformations of 2D
.. autoclass:: Vflip
.. autoclass:: Rot180
.. autoclass:: Resize
-.. autoclass:: Affine
+.. autoclass:: Rescale
+.. autoclass:: Affine
\ No newline at end of file
diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.py
index ac3351591..68b33211c 100644
--- a/kornia/geometry/transform/affwarp.py
+++ b/kornia/geometry/transform/affwarp.py
@@ -18,11 +18,13 @@ __all__ = [
"translate",
"shear",
"resize",
+ "rescale",
"Scale",
"Rotate",
"Translate",
"Shear",
"Resize",
+ "Rescale",
"Affine",
]
@@ -488,6 +490,52 @@ class Affine(nn.Module):
return affine(input, matrix[..., :2, :3], align_corners=self.align_corners)
+def rescale(
+ input: torch.Tensor,
+ factor: Union[float, Tuple[float, float]],
+ interpolation: str = "bilinear",
+ align_corners: bool = False,
+) -> torch.Tensor:
+ r"""Rescale the input torch.Tensor with the given factor.
+
+ See :class:`~kornia.Rescale` for details.
+ """
+ if isinstance(factor, float):
+ factor_vert = factor_horz = factor
+ else:
+ factor_vert, factor_horz = factor
+
+ height, width = input.size()[-2:]
+ size = (int(height * factor_vert), int(width * factor_horz))
+ return resize(input, size, interpolation=interpolation, align_corners=align_corners)
+
+
+class Rescale(nn.Module):
+ r"""Rescale the input torch.Tensor with the given factor.
+
+ Args:
+ factor (float, tuple(float, float)): Desired scaling factor in each direction. If scalar, the value is used
+ for both the x- and y-direction.
+ interpolation (str): Algorithm used for upsampling. Can be one of ``"nearest"``, ``"linear"``, ``"bilinear"``,
+ ``"bicubic"``, ``"trilinear"``, or ``"area"``. Default: ``"bilinear"``.
+ align_corners(bool): Interpolation flag. Default: False. See :func:`~torch.nn.functional.interpolate` for
+ details.
+
+ Returns:
+ torch.Tensor: The rescaled tensor.
+ """
+ def __init__(
+ self, factor: Union[float, Tuple[float, float]], interpolation: str = "bilinear", align_corners: bool = False
+ ) -> None:
+ super().__init__()
+ self.factor: Union[float, Tuple[float, float]] = factor
+ self.interpolation: str = interpolation
+ self.align_corners: bool = align_corners
+
+ def forward(self, input: torch.Tensor) -> torch.Tensor:
+ return rescale(input, self.factor, self.interpolation, align_corners=self.align_corners)
+
+
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 a1b43b1e8..54b9228bb 100644
--- a/test/geometry/transform/test_affine.py
+++ b/test/geometry/transform/test_affine.py
@@ -51,6 +51,33 @@ class TestResize:
assert gradcheck(kornia.Resize(new_size), (input, ), raise_exception=True)
+class TestRescale:
+ def test_smoke(self, device):
+ input = torch.rand(1, 3, 3, 4, device=device)
+ output = kornia.rescale(input, (1.0, 1.0))
+ assert_allclose(input, output)
+
+ def test_upsize(self, device):
+ input = torch.rand(1, 3, 3, 4, device=device)
+ output = kornia.rescale(input, (3.0, 2.0))
+ assert output.shape == (1, 3, 9, 8)
+
+ def test_downsize(self, device):
+ input = torch.rand(1, 3, 9, 8, device=device)
+ output = kornia.rescale(input, (1.0 / 3.0, 1.0 / 2.0))
+ assert output.shape == (1, 3, 3, 4)
+
+ def test_one_param(self, device):
+ input = torch.rand(1, 3, 3, 4, device=device)
+ output = kornia.rescale(input, 2.0)
+ assert output.shape == (1, 3, 6, 8)
+
+ def test_gradcheck(self, device):
+ input = torch.rand(1, 2, 3, 4).to(device)
+ input = utils.tensor_to_gradcheck_var(input)
+ assert gradcheck(kornia.Rescale(2.0), (input, ), raise_exception=True)
+
+
class TestRotate:
def test_angle90(self, device):
# prepare input data