From 540dec521f10ee1d3a22ee78419d681d558dfffc Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Wed, 30 Aug 2023 14:41:15 +0200 Subject: [PATCH 1/3] downgrade warnign for F.rotate with center and expand=True --- test/test_transforms_v2_refactored.py | 1 - torchvision/transforms/v2/_geometry.py | 5 +++++ torchvision/transforms/v2/functional/_geometry.py | 9 --------- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/test/test_transforms_v2_refactored.py b/test/test_transforms_v2_refactored.py index 9de1ed6d78a..fd8f23f3428 100644 --- a/test/test_transforms_v2_refactored.py +++ b/test/test_transforms_v2_refactored.py @@ -1375,7 +1375,6 @@ def test_transform_noop(self, make_input, device): assert_equal(output, input) -@pytest.mark.filterwarnings("ignore:The provided center argument has no effect") class TestRotate: _EXHAUSTIVE_TYPE_AFFINE_KWARGS = dict( # float, int diff --git a/torchvision/transforms/v2/_geometry.py b/torchvision/transforms/v2/_geometry.py index ba3e690dd4d..76679690c4e 100644 --- a/torchvision/transforms/v2/_geometry.py +++ b/torchvision/transforms/v2/_geometry.py @@ -593,6 +593,11 @@ class RandomRotation(Transform): Note that the expand flag assumes rotation around the center and no translation. center (sequence, optional): Optional center of rotation, (x, y). Origin is the upper left corner. Default is the center of the image. + + ..note:: + + In theory, ``center`` has no effect together with ``expand=True``. In practice however, this can lead to + off-by-one differences of the resulting image size compared to ``center=None``. fill (number or tuple or dict, optional): Pixel fill value used when the ``padding_mode`` is constant. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively. Fill value can be also a dictionary mapping data type to the fill value, e.g. diff --git a/torchvision/transforms/v2/functional/_geometry.py b/torchvision/transforms/v2/functional/_geometry.py index 7838d7e3eae..5ca5c875ee4 100644 --- a/torchvision/transforms/v2/functional/_geometry.py +++ b/torchvision/transforms/v2/functional/_geometry.py @@ -977,9 +977,6 @@ def rotate_image( center_f = [0.0, 0.0] if center is not None: - if expand: - # TODO: Do we actually want to warn, or just document this? - warnings.warn("The provided center argument has no effect on the result if expand is True") # Center values should be in pixel coordinates but translated such that (0, 0) corresponds to image center. center_f = [(c - s * 0.5) for c, s in zip(center, [width, height])] @@ -1017,9 +1014,6 @@ def _rotate_image_pil( ) -> PIL.Image.Image: interpolation = _check_interpolation(interpolation) - if center is not None and expand: - warnings.warn("The provided center argument has no effect on the result if expand is True") - return _FP.rotate( image, angle, interpolation=pil_modes_mapping[interpolation], expand=expand, fill=fill, center=center ) @@ -1033,9 +1027,6 @@ def rotate_bounding_boxes( expand: bool = False, center: Optional[List[float]] = None, ) -> Tuple[torch.Tensor, Tuple[int, int]]: - if center is not None and expand: - warnings.warn("The provided center argument has no effect on the result if expand is True") - return _affine_bounding_boxes_with_expand( bounding_boxes, format=format, From eb451e3f029c06e04c53759f3daa57d09cdff5bf Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Thu, 31 Aug 2023 16:14:27 +0200 Subject: [PATCH 2/3] address comments --- torchvision/transforms/v2/_geometry.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/torchvision/transforms/v2/_geometry.py b/torchvision/transforms/v2/_geometry.py index 76679690c4e..1eb0550e76e 100644 --- a/torchvision/transforms/v2/_geometry.py +++ b/torchvision/transforms/v2/_geometry.py @@ -590,14 +590,16 @@ class RandomRotation(Transform): expand (bool, optional): Optional expansion flag. If true, expands the output to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image. - Note that the expand flag assumes rotation around the center and no translation. + Note that the expand flag assumes rotation around the center (see note below) and no translation. center (sequence, optional): Optional center of rotation, (x, y). Origin is the upper left corner. Default is the center of the image. ..note:: - In theory, ``center`` has no effect together with ``expand=True``. In practice however, this can lead to - off-by-one differences of the resulting image size compared to ``center=None``. + In theory, setting ``center`` has no effect if ``expand=True``, since the image center will become the + center of rotation. In practice however, due to numerical precision, this can lead to off-by-one + differences of the resulting image size compared to using the image center in the first place. Thus, when + setting ``expand=True``, it's best to leave ``center=None`` (default). fill (number or tuple or dict, optional): Pixel fill value used when the ``padding_mode`` is constant. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively. Fill value can be also a dictionary mapping data type to the fill value, e.g. From d32bfebef39f5932e5fc943c8db2f5ccd686de54 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Thu, 31 Aug 2023 16:05:59 +0100 Subject: [PATCH 3/3] Update torchvision/transforms/v2/_geometry.py --- torchvision/transforms/v2/_geometry.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/torchvision/transforms/v2/_geometry.py b/torchvision/transforms/v2/_geometry.py index 5c1ecc1a789..8d79fb6fd24 100644 --- a/torchvision/transforms/v2/_geometry.py +++ b/torchvision/transforms/v2/_geometry.py @@ -594,12 +594,12 @@ class RandomRotation(Transform): center (sequence, optional): Optional center of rotation, (x, y). Origin is the upper left corner. Default is the center of the image. - ..note:: + .. note:: - In theory, setting ``center`` has no effect if ``expand=True``, since the image center will become the - center of rotation. In practice however, due to numerical precision, this can lead to off-by-one - differences of the resulting image size compared to using the image center in the first place. Thus, when - setting ``expand=True``, it's best to leave ``center=None`` (default). + In theory, setting ``center`` has no effect if ``expand=True``, since the image center will become the + center of rotation. In practice however, due to numerical precision, this can lead to off-by-one + differences of the resulting image size compared to using the image center in the first place. Thus, when + setting ``expand=True``, it's best to leave ``center=None`` (default). fill (number or tuple or dict, optional): Pixel fill value used when the ``padding_mode`` is constant. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively. Fill value can be also a dictionary mapping data type to the fill value, e.g.