From b8f6edf454fe9f26db1301928a241a5877915494 Mon Sep 17 00:00:00 2001 From: tonystratum Date: Tue, 26 Jan 2021 22:53:17 +0200 Subject: [PATCH 1/9] initial fix --- torchvision/transforms/transforms.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index bf847c8fc75..64e580b27e8 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -1234,7 +1234,9 @@ def forward(self, img): """ fill = self.fill if isinstance(img, Tensor): - if isinstance(fill, (int, float)): + if fill is None: + fill = [.0] * F._get_image_num_channels(img) + elif isinstance(fill, (int, float)): fill = [float(fill)] * F._get_image_num_channels(img) else: fill = [float(f) for f in fill] From ec44069110e0a95cc824aa70820b09a3df7fe678 Mon Sep 17 00:00:00 2001 From: tonystratum Date: Wed, 27 Jan 2021 17:48:37 +0200 Subject: [PATCH 2/9] fill=0 --- torchvision/transforms/transforms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index 64e580b27e8..f93212e96fa 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -1186,7 +1186,7 @@ class RandomRotation(torch.nn.Module): """ def __init__( - self, degrees, interpolation=InterpolationMode.NEAREST, expand=False, center=None, fill=None, resample=None + self, degrees, interpolation=InterpolationMode.NEAREST, expand=False, center=None, fill=0, resample=None ): super().__init__() if resample is not None: From 8c3f7f592818cfe51c46e5b728d1fb432f6e39a8 Mon Sep 17 00:00:00 2001 From: tonystratum Date: Wed, 27 Jan 2021 18:02:24 +0200 Subject: [PATCH 3/9] docstrings --- torchvision/transforms/transforms.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index f93212e96fa..ada4b83dd09 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -673,8 +673,8 @@ class RandomPerspective(torch.nn.Module): :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. - fill (sequence or number, optional): Pixel fill value for the area outside the transformed - image. If given a number, the value is used for all bands respectively. + fill (sequence or number): Pixel fill value for the area outside the transformed + image. Default is ``0``. If given a number, the value is used for all bands respectively. If input is PIL Image, the options is only available for ``Pillow>=5.0.0``. """ @@ -1175,8 +1175,8 @@ class RandomRotation(torch.nn.Module): 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. - fill (sequence or number, optional): Pixel fill value for the area outside the rotated - image. If given a number, the value is used for all bands respectively. + fill (sequence or number): Pixel fill value for the area outside the rotated + image. Default is ``0``. If given a number, the value is used for all bands respectively. If input is PIL Image, the options is only available for ``Pillow>=5.2.0``. resample (int, optional): deprecated argument and will be removed since v0.10.0. Please use the ``interpolation`` parameter instead. @@ -1282,8 +1282,8 @@ class RandomAffine(torch.nn.Module): :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable. - fill (sequence or number, optional): Pixel fill value for the area outside the transformed - image. If given a number, the value is used for all bands respectively. + fill (sequence or number): Pixel fill value for the area outside the transformed + image. Default is ``0``. If given a number, the value is used for all bands respectively. If input is PIL Image, the options is only available for ``Pillow>=5.0.0``. fillcolor (sequence or number, optional): deprecated argument and will be removed since v0.10.0. Please use the ``fill`` parameter instead. From a05259c9a2454419b1d1c5fd92ef1719af526666 Mon Sep 17 00:00:00 2001 From: tonystratum Date: Wed, 27 Jan 2021 18:32:27 +0200 Subject: [PATCH 4/9] fill type check --- torchvision/transforms/transforms.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index ada4b83dd09..dc37198d02e 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -692,6 +692,10 @@ def __init__(self, distortion_scale=0.5, p=0.5, interpolation=InterpolationMode. self.interpolation = interpolation self.distortion_scale = distortion_scale + + if not isinstance(fill, Sequence) and not isinstance(fill, numbers.Number): + raise TypeError("Fill should be either a sequence or a number.") + self.fill = fill def forward(self, img): @@ -1212,6 +1216,10 @@ def __init__( self.resample = self.interpolation = interpolation self.expand = expand + + if not isinstance(fill, Sequence) and not isinstance(fill, numbers.Number): + raise TypeError("Fill should be either a sequence or a number.") + self.fill = fill @staticmethod @@ -1341,6 +1349,10 @@ def __init__( self.shear = shear self.resample = self.interpolation = interpolation + + if not isinstance(fill, Sequence) and not isinstance(fill, numbers.Number): + raise TypeError("Fill should be either a sequence or a number.") + self.fillcolor = self.fill = fill @staticmethod From 0fcb13975bb3e8ff199ba65060051ae7000df44a Mon Sep 17 00:00:00 2001 From: tonystratum Date: Wed, 27 Jan 2021 18:53:09 +0200 Subject: [PATCH 5/9] fill type check --- torchvision/transforms/transforms.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index dc37198d02e..70d485567a6 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -693,7 +693,7 @@ def __init__(self, distortion_scale=0.5, p=0.5, interpolation=InterpolationMode. self.interpolation = interpolation self.distortion_scale = distortion_scale - if not isinstance(fill, Sequence) and not isinstance(fill, numbers.Number): + if not isinstance(fill, (Sequence, numbers.Number)): raise TypeError("Fill should be either a sequence or a number.") self.fill = fill @@ -1217,7 +1217,7 @@ def __init__( self.resample = self.interpolation = interpolation self.expand = expand - if not isinstance(fill, Sequence) and not isinstance(fill, numbers.Number): + if not isinstance(fill, (Sequence, numbers.Number)): raise TypeError("Fill should be either a sequence or a number.") self.fill = fill @@ -1350,7 +1350,7 @@ def __init__( self.resample = self.interpolation = interpolation - if not isinstance(fill, Sequence) and not isinstance(fill, numbers.Number): + if not isinstance(fill, (Sequence, numbers.Number)): raise TypeError("Fill should be either a sequence or a number.") self.fillcolor = self.fill = fill From 93da8d3515446bd73472ee700d6c0f5e04aca3ce Mon Sep 17 00:00:00 2001 From: tonystratum Date: Wed, 27 Jan 2021 19:59:49 +0200 Subject: [PATCH 6/9] set None to zero --- torchvision/transforms/transforms.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index 70d485567a6..ff12a070571 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -693,7 +693,9 @@ def __init__(self, distortion_scale=0.5, p=0.5, interpolation=InterpolationMode. self.interpolation = interpolation self.distortion_scale = distortion_scale - if not isinstance(fill, (Sequence, numbers.Number)): + if fill is None: + fill = 0 + elif not isinstance(fill, (Sequence, numbers.Number)): raise TypeError("Fill should be either a sequence or a number.") self.fill = fill @@ -1217,7 +1219,9 @@ def __init__( self.resample = self.interpolation = interpolation self.expand = expand - if not isinstance(fill, (Sequence, numbers.Number)): + if fill is None: + fill = 0 + elif not isinstance(fill, (Sequence, numbers.Number)): raise TypeError("Fill should be either a sequence or a number.") self.fill = fill @@ -1242,9 +1246,7 @@ def forward(self, img): """ fill = self.fill if isinstance(img, Tensor): - if fill is None: - fill = [.0] * F._get_image_num_channels(img) - elif isinstance(fill, (int, float)): + if isinstance(fill, (int, float)): fill = [float(fill)] * F._get_image_num_channels(img) else: fill = [float(f) for f in fill] @@ -1350,7 +1352,9 @@ def __init__( self.resample = self.interpolation = interpolation - if not isinstance(fill, (Sequence, numbers.Number)): + if fill is None: + fill = 0 + elif not isinstance(fill, (Sequence, numbers.Number)): raise TypeError("Fill should be either a sequence or a number.") self.fillcolor = self.fill = fill From 9557f0ad5a5744c5b374fc4363e15fe75521663e Mon Sep 17 00:00:00 2001 From: tonystratum Date: Thu, 28 Jan 2021 14:43:03 +0200 Subject: [PATCH 7/9] unit tests --- test/test_transforms.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/test_transforms.py b/test/test_transforms.py index b3c82334d14..19b63e16f93 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -180,6 +180,14 @@ def test_randomperspective(self): torch.nn.functional.mse_loss(tr_img2, F.to_tensor(img))) def test_randomperspective_fill(self): + + # assert fill being either a Sequence or a Number + with self.assertRaises(TypeError): + transforms.RandomPerspective(fill=NotImplemented) + + t = transforms.RandomPerspective(fill=None) + self.assertTrue(t.fill == 0) + height = 100 width = 100 img = torch.ones(3, height, width) @@ -1531,6 +1539,13 @@ def test_random_rotation(self): transforms.RandomRotation([-0.7]) transforms.RandomRotation([-0.7, 0, 0.7]) + # assert fill being either a Sequence or a Number + with self.assertRaises(TypeError): + transforms.RandomRotation(0, fill=NotImplemented) + + t = transforms.RandomRotation(0, fill=None) + self.assertTrue(t.fill == 0) + t = transforms.RandomRotation(10) angle = t.get_params(t.degrees) self.assertTrue(angle > -10 and angle < 10) @@ -1573,6 +1588,13 @@ def test_random_affine(self): transforms.RandomAffine([-90, 90], translate=[0.2, 0.2], scale=[0.5, 0.5], shear=[-10, 0, 10]) transforms.RandomAffine([-90, 90], translate=[0.2, 0.2], scale=[0.5, 0.5], shear=[-10, 0, 10, 0, 10]) + # assert fill being either a Sequence or a Number + with self.assertRaises(TypeError): + transforms.RandomAffine(0, fill=NotImplemented) + + t = transforms.RandomAffine(0, fill=None) + self.assertTrue(t.fill == 0) + x = np.zeros((100, 100, 3), dtype=np.uint8) img = F.to_pil_image(x) From 7fb99dc39607347b3ca62f076321b83c66bf8c3e Mon Sep 17 00:00:00 2001 From: tonystratum Date: Thu, 28 Jan 2021 15:25:57 +0200 Subject: [PATCH 8/9] set instead of NotImplemented --- test/test_transforms.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index 19b63e16f93..19bb8e24b85 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -183,7 +183,7 @@ def test_randomperspective_fill(self): # assert fill being either a Sequence or a Number with self.assertRaises(TypeError): - transforms.RandomPerspective(fill=NotImplemented) + transforms.RandomPerspective(fill={}) t = transforms.RandomPerspective(fill=None) self.assertTrue(t.fill == 0) @@ -1541,7 +1541,7 @@ def test_random_rotation(self): # assert fill being either a Sequence or a Number with self.assertRaises(TypeError): - transforms.RandomRotation(0, fill=NotImplemented) + transforms.RandomRotation(0, fill={}) t = transforms.RandomRotation(0, fill=None) self.assertTrue(t.fill == 0) @@ -1590,7 +1590,7 @@ def test_random_affine(self): # assert fill being either a Sequence or a Number with self.assertRaises(TypeError): - transforms.RandomAffine(0, fill=NotImplemented) + transforms.RandomAffine(0, fill={}) t = transforms.RandomAffine(0, fill=None) self.assertTrue(t.fill == 0) From 649fe856d26c57f0bbbcc98cb46d7d96c2e2b76e Mon Sep 17 00:00:00 2001 From: tonystratum Date: Thu, 28 Jan 2021 16:40:54 +0200 Subject: [PATCH 9/9] fix W293 --- test/test_transforms.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index 19bb8e24b85..0562adb2a90 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -184,7 +184,7 @@ def test_randomperspective_fill(self): # assert fill being either a Sequence or a Number with self.assertRaises(TypeError): transforms.RandomPerspective(fill={}) - + t = transforms.RandomPerspective(fill=None) self.assertTrue(t.fill == 0) @@ -1542,7 +1542,7 @@ def test_random_rotation(self): # assert fill being either a Sequence or a Number with self.assertRaises(TypeError): transforms.RandomRotation(0, fill={}) - + t = transforms.RandomRotation(0, fill=None) self.assertTrue(t.fill == 0) @@ -1591,7 +1591,7 @@ def test_random_affine(self): # assert fill being either a Sequence or a Number with self.assertRaises(TypeError): transforms.RandomAffine(0, fill={}) - + t = transforms.RandomAffine(0, fill=None) self.assertTrue(t.fill == 0)