diff --git a/test/test_transforms.py b/test/test_transforms.py index b3c82334d14..0562adb2a90 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={}) + + 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={}) + + 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={}) + + 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) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index bf847c8fc75..ff12a070571 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``. """ @@ -692,6 +692,12 @@ def __init__(self, distortion_scale=0.5, p=0.5, interpolation=InterpolationMode. self.interpolation = interpolation self.distortion_scale = distortion_scale + + 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 def forward(self, img): @@ -1175,8 +1181,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. @@ -1186,7 +1192,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: @@ -1212,6 +1218,12 @@ def __init__( self.resample = self.interpolation = interpolation self.expand = expand + + 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 @staticmethod @@ -1280,8 +1292,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. @@ -1339,6 +1351,12 @@ def __init__( self.shear = shear self.resample = self.interpolation = interpolation + + 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 @staticmethod