π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): let Rescale use the nearest and area modes (#6010)
Saksham Sharma4989702bd641
diff --git a/changelog.d/6010.fixed.md b/changelog.d/6010.fixed.mdnew file mode 100644index 000000000..c9089be25--- /dev/null+++ b/changelog.d/6010.fixed.md@@ -0,0 +1 @@+`Rescale` works with `interpolation="nearest"`, `"nearest-exact"` and `"area"`. Previously its default `align_corners=True` was passed to `torch.nn.functional.interpolate`, which rejects any value but `None` for these modes, so every call that did not pass `align_corners=None` raised `ValueError`. `align_corners` now applies to the `bilinear` and `bicubic` modes only, whose results are unchanged; the other modes ignore it, an explicit `align_corners=True` included.diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.pyindex 6c241a160..60410578f 100644--- a/kornia/geometry/transform/affwarp.py+++ b/kornia/geometry/transform/affwarp.py@@ -812,8 +812,8 @@ def rescale(input: The image tensor to be scale with shape of :math:`(B, C, H, W)`.factor: Desired scaling factor as ``(factor_h, factor_w)`` β height first. If a singlefloat, the value is used for both height and width (an ``int`` raises ``TypeError``).- interpolation: algorithm used for upsampling: ``'nearest'`` | ``'linear'`` | ``'bilinear'`` |- ``'bicubic'`` | ``'trilinear'`` | ``'area'``.+ interpolation: algorithm used for upsampling: ``'nearest'`` | ``'nearest-exact'`` | ``'linear'`` |+ ``'bilinear'`` | ``'bicubic'`` | ``'trilinear'`` | ``'area'``.align_corners: interpolation flag.antialias: if True, then image will be filtered with Gaussian before downscaling.No effect for upscaling.@@ -1056,14 +1056,17 @@ class Rescale(nn.Module):Convention:- align_corners: ``True`` by default (differs from function :func:`rescale`, whose default- is ``None``)+ is ``None``). It applies to the ``'bilinear'`` and ``'bicubic'`` modes only. ``'nearest'``,+ ``'nearest-exact'`` and ``'area'`` ignore it, an explicit ``align_corners=True`` included, because+ ``torch.nn.functional.interpolate`` accepts no value but ``None`` for them; the function+ :func:`rescale` raises ``ValueError`` for such a call.- See the convention block of :func:`rescale`.Args:factor: Desired scaling factor as ``(factor_h, factor_w)`` β height first. If a singlefloat, the value is used for both height and width (an ``int`` raises ``TypeError``).- interpolation: algorithm used for upsampling: ``'nearest'`` | ``'linear'`` | ``'bilinear'`` |- ``'bicubic'`` | ``'trilinear'`` | ``'area'``.+ interpolation: algorithm used for upsampling: ``'nearest'`` | ``'nearest-exact'`` | ``'linear'`` |+ ``'bilinear'`` | ``'bicubic'`` | ``'trilinear'`` | ``'area'``.align_corners: interpolation flag.antialias: if True, then image will be filtered with Gaussian before downscaling.No effect for upscaling.@@ -1104,9 +1107,12 @@ class Rescale(nn.Module):Tensor with the same leading and channel dimensions as ``input``.The output height and width are computed from ``self.factor``."""- return rescale(- input, self.factor, self.interpolation, align_corners=self.align_corners, antialias=self.antialias+ # ``align_corners`` only applies to the interpolating modes: ``F.interpolate`` rejects any value but ``None``+ # for ``nearest``, ``nearest-exact`` and ``area``, which the default ``True`` would otherwise make unusable.+ align_corners = (+ self.align_corners if self.interpolation in ("linear", "bilinear", "bicubic", "trilinear") else None)+ return rescale(input, self.factor, self.interpolation, align_corners=align_corners, antialias=self.antialias)class Rotate(nn.Module):diff --git a/tests/geometry/transform/test_affine.py b/tests/geometry/transform/test_affine.pyindex 23f9c6816..554e98d85 100644--- a/tests/geometry/transform/test_affine.py+++ b/tests/geometry/transform/test_affine.py@@ -498,6 +498,29 @@ class TestRescale(BaseTester):input = torch.rand(1, 2, 3, 4, device=device, dtype=torch.float64)self.gradcheck(kornia.geometry.transform.Rescale(2.0, align_corners=False), (input,), nondet_tol=1e-8)+ @pytest.mark.parametrize("interpolation", ["nearest", "nearest-exact", "area"])+ @pytest.mark.parametrize("factor", [0.5, 2.0])+ def test_non_interpolating_modes_ignore_align_corners_5998(self, interpolation, factor, device, dtype):+ # The module's default align_corners=True used to reach F.interpolate, which rejects it for these modes.+ input = torch.rand(2, 3, 4, 6, device=device, dtype=dtype)++ out = kornia.geometry.transform.Rescale(factor, interpolation=interpolation)(input)++ expected = kornia.geometry.transform.rescale(input, factor, interpolation=interpolation)+ self.assert_close(out, expected, rtol=0.0, atol=0.0)+ explicit = kornia.geometry.transform.Rescale(factor, interpolation=interpolation, align_corners=False)(input)+ self.assert_close(explicit, expected, rtol=0.0, atol=0.0)++ @pytest.mark.parametrize("interpolation", ["bilinear", "bicubic"])+ def test_interpolating_modes_keep_align_corners_default_5998(self, interpolation, device, dtype):+ # The interpolating modes still use the module's default align_corners=True.+ input = torch.rand(2, 3, 4, 6, device=device, dtype=dtype)++ out = kornia.geometry.transform.Rescale(0.5, interpolation=interpolation)(input)++ expected = kornia.geometry.transform.rescale(input, 0.5, interpolation=interpolation, align_corners=True)+ self.assert_close(out, expected, rtol=0.0, atol=0.0)+class TestRotate(BaseTester):@pytest.mark.parametrize("align_corners", [False, True])