Commit 87fe75b0 authored by Alykhan Tejani's avatar Alykhan Tejani Committed by GitHub
Browse files

Deprecate Scale in favour of Resize (#274)

rename scaled_crop -> resized_crop + rename RandomSizedCrop -> RandomResizedCrop
parent bd8581a5
...@@ -123,7 +123,7 @@ class Tester(unittest.TestCase): ...@@ -123,7 +123,7 @@ class Tester(unittest.TestCase):
assert len(results) == 10 assert len(results) == 10
assert expected_output == results assert expected_output == results
def test_scale(self): def test_resize(self):
height = random.randint(24, 32) * 2 height = random.randint(24, 32) * 2
width = random.randint(24, 32) * 2 width = random.randint(24, 32) * 2
osize = random.randint(5, 12) * 2 osize = random.randint(5, 12) * 2
...@@ -131,7 +131,7 @@ class Tester(unittest.TestCase): ...@@ -131,7 +131,7 @@ class Tester(unittest.TestCase):
img = torch.ones(3, height, width) img = torch.ones(3, height, width)
result = transforms.Compose([ result = transforms.Compose([
transforms.ToPILImage(), transforms.ToPILImage(),
transforms.Scale(osize), transforms.Resize(osize),
transforms.ToTensor(), transforms.ToTensor(),
])(img) ])(img)
assert osize in result.size() assert osize in result.size()
...@@ -142,7 +142,7 @@ class Tester(unittest.TestCase): ...@@ -142,7 +142,7 @@ class Tester(unittest.TestCase):
result = transforms.Compose([ result = transforms.Compose([
transforms.ToPILImage(), transforms.ToPILImage(),
transforms.Scale([osize, osize]), transforms.Resize([osize, osize]),
transforms.ToTensor(), transforms.ToTensor(),
])(img) ])(img)
assert osize in result.size() assert osize in result.size()
...@@ -153,7 +153,7 @@ class Tester(unittest.TestCase): ...@@ -153,7 +153,7 @@ class Tester(unittest.TestCase):
owidth = random.randint(5, 12) * 2 owidth = random.randint(5, 12) * 2
result = transforms.Compose([ result = transforms.Compose([
transforms.ToPILImage(), transforms.ToPILImage(),
transforms.Scale((oheight, owidth)), transforms.Resize((oheight, owidth)),
transforms.ToTensor(), transforms.ToTensor(),
])(img) ])(img)
assert result.size(1) == oheight assert result.size(1) == oheight
...@@ -161,7 +161,7 @@ class Tester(unittest.TestCase): ...@@ -161,7 +161,7 @@ class Tester(unittest.TestCase):
result = transforms.Compose([ result = transforms.Compose([
transforms.ToPILImage(), transforms.ToPILImage(),
transforms.Scale([oheight, owidth]), transforms.Resize([oheight, owidth]),
transforms.ToTensor(), transforms.ToTensor(),
])(img) ])(img)
assert result.size(1) == oheight assert result.size(1) == oheight
...@@ -265,7 +265,7 @@ class Tester(unittest.TestCase): ...@@ -265,7 +265,7 @@ class Tester(unittest.TestCase):
@unittest.skipIf(accimage is None, 'accimage not available') @unittest.skipIf(accimage is None, 'accimage not available')
def test_accimage_resize(self): def test_accimage_resize(self):
trans = transforms.Compose([ trans = transforms.Compose([
transforms.Scale(256, interpolation=Image.LINEAR), transforms.Resize(256, interpolation=Image.LINEAR),
transforms.ToTensor(), transforms.ToTensor(),
]) ])
......
...@@ -11,6 +11,7 @@ import numpy as np ...@@ -11,6 +11,7 @@ import numpy as np
import numbers import numbers
import types import types
import collections import collections
import warnings
def _is_pil_image(img): def _is_pil_image(img):
...@@ -141,21 +142,21 @@ def normalize(tensor, mean, std): ...@@ -141,21 +142,21 @@ def normalize(tensor, mean, std):
return tensor return tensor
def scale(img, size, interpolation=Image.BILINEAR): def resize(img, size, interpolation=Image.BILINEAR):
"""Rescale the input PIL.Image to the given size. """Resize the input PIL.Image to the given size.
Args: Args:
img (PIL.Image): Image to be scaled. img (PIL.Image): Image to be resized.
size (sequence or int): Desired output size. If size is a sequence like size (sequence or int): Desired output size. If size is a sequence like
(h, w), output size will be matched to this. If size is an int, (h, w), the output size will be matched to this. If size is an int,
smaller edge of the image will be matched to this number. the smaller edge of the image will be matched to this number maintaing
i.e, if height > width, then image will be rescaled to the aspect ratio. i.e, if height > width, then image will be rescaled to
(size * height / width, size) (size * height / width, size)
interpolation (int, optional): Desired interpolation. Default is interpolation (int, optional): Desired interpolation. Default is
``PIL.Image.BILINEAR`` ``PIL.Image.BILINEAR``
Returns: Returns:
PIL.Image: Rescaled image. PIL.Image: Resized image.
""" """
if not _is_pil_image(img): if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img))) raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
...@@ -178,6 +179,12 @@ def scale(img, size, interpolation=Image.BILINEAR): ...@@ -178,6 +179,12 @@ def scale(img, size, interpolation=Image.BILINEAR):
return img.resize(size[::-1], interpolation) return img.resize(size[::-1], interpolation)
def scale(*args, **kwargs):
warnings.warn("The use of the transforms.Scale transform is deprecated, " +
"please use transforms.Resize instead.")
return resize(*args, **kwargs)
def pad(img, padding, fill=0): def pad(img, padding, fill=0):
"""Pad the given PIL.Image on all sides with the given "pad" value. """Pad the given PIL.Image on all sides with the given "pad" value.
...@@ -228,10 +235,10 @@ def crop(img, i, j, h, w): ...@@ -228,10 +235,10 @@ def crop(img, i, j, h, w):
return img.crop((j, i, j + w, i + h)) return img.crop((j, i, j + w, i + h))
def scaled_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR): def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR):
"""Crop the given PIL.Image and scale it to desired size. """Crop the given PIL.Image and resize it to desired size.
Notably used in RandomSizedCrop. Notably used in RandomResizedCrop.
Args: Args:
img (PIL.Image): Image to be cropped. img (PIL.Image): Image to be cropped.
...@@ -247,7 +254,7 @@ def scaled_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR): ...@@ -247,7 +254,7 @@ def scaled_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR):
""" """
assert _is_pil_image(img), 'img should be PIL Image' assert _is_pil_image(img), 'img should be PIL Image'
img = crop(img, i, j, h, w) img = crop(img, i, j, h, w)
img = scale(img, size, interpolation) img = resize(img, size, interpolation)
return img return img
...@@ -435,8 +442,8 @@ class Normalize(object): ...@@ -435,8 +442,8 @@ class Normalize(object):
return normalize(tensor, self.mean, self.std) return normalize(tensor, self.mean, self.std)
class Scale(object): class Resize(object):
"""Rescale the input PIL.Image to the given size. """Resize the input PIL.Image to the given size.
Args: Args:
size (sequence or int): Desired output size. If size is a sequence like size (sequence or int): Desired output size. If size is a sequence like
...@@ -461,7 +468,14 @@ class Scale(object): ...@@ -461,7 +468,14 @@ class Scale(object):
Returns: Returns:
PIL.Image: Rescaled image. PIL.Image: Rescaled image.
""" """
return scale(img, self.size, self.interpolation) return resize(img, self.size, self.interpolation)
class Scale(Resize):
def __init__(self, *args, **kwargs):
warnings.warn("The use of the transforms.Scale transform is deprecated, " +
"please use transforms.Resize instead.")
super(Scale, self).__init__(*args, **kwargs)
class CenterCrop(object): class CenterCrop(object):
...@@ -645,7 +659,7 @@ class RandomVerticalFlip(object): ...@@ -645,7 +659,7 @@ class RandomVerticalFlip(object):
return img return img
class RandomSizedCrop(object): class RandomResizedCrop(object):
"""Crop the given PIL.Image to random size and aspect ratio. """Crop the given PIL.Image to random size and aspect ratio.
A crop of random size of (0.08 to 1.0) of the original size and a random A crop of random size of (0.08 to 1.0) of the original size and a random
...@@ -701,10 +715,17 @@ class RandomSizedCrop(object): ...@@ -701,10 +715,17 @@ class RandomSizedCrop(object):
img (PIL.Image): Image to be flipped. img (PIL.Image): Image to be flipped.
Returns: Returns:
PIL.Image: Randomly cropped and scaled image. PIL.Image: Randomly cropped and resize image.
""" """
i, j, h, w = self.get_params(img) i, j, h, w = self.get_params(img)
return scaled_crop(img, i, j, h, w, self.size, self.interpolation) return resized_crop(img, i, j, h, w, self.size, self.interpolation)
class RandomSizedCrop(RandomResizedCrop):
def __init__(self, *args, **kwargs):
warnings.warn("The use of the transforms.RandomSizedCrop transform is deprecated, " +
"please use transforms.RandomResizedCrop instead.")
super(RandomSizedCrop, self).__init__(*args, **kwargs)
class FiveCrop(object): class FiveCrop(object):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment