Skip to content

RandomRotation and fill #3303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 28, 2021
22 changes: 22 additions & 0 deletions test/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
32 changes: 25 additions & 7 deletions torchvision/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
"""

Expand All @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down