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):
assert len(results) == 10
assert expected_output == results
def test_scale(self):
def test_resize(self):
height = random.randint(24, 32) * 2
width = random.randint(24, 32) * 2
osize = random.randint(5, 12) * 2
......@@ -131,7 +131,7 @@ class Tester(unittest.TestCase):
img = torch.ones(3, height, width)
result = transforms.Compose([
transforms.ToPILImage(),
transforms.Scale(osize),
transforms.Resize(osize),
transforms.ToTensor(),
])(img)
assert osize in result.size()
......@@ -142,7 +142,7 @@ class Tester(unittest.TestCase):
result = transforms.Compose([
transforms.ToPILImage(),
transforms.Scale([osize, osize]),
transforms.Resize([osize, osize]),
transforms.ToTensor(),
])(img)
assert osize in result.size()
......@@ -153,7 +153,7 @@ class Tester(unittest.TestCase):
owidth = random.randint(5, 12) * 2
result = transforms.Compose([
transforms.ToPILImage(),
transforms.Scale((oheight, owidth)),
transforms.Resize((oheight, owidth)),
transforms.ToTensor(),
])(img)
assert result.size(1) == oheight
......@@ -161,7 +161,7 @@ class Tester(unittest.TestCase):
result = transforms.Compose([
transforms.ToPILImage(),
transforms.Scale([oheight, owidth]),
transforms.Resize([oheight, owidth]),
transforms.ToTensor(),
])(img)
assert result.size(1) == oheight
......@@ -265,7 +265,7 @@ class Tester(unittest.TestCase):
@unittest.skipIf(accimage is None, 'accimage not available')
def test_accimage_resize(self):
trans = transforms.Compose([
transforms.Scale(256, interpolation=Image.LINEAR),
transforms.Resize(256, interpolation=Image.LINEAR),
transforms.ToTensor(),
])
......
......@@ -11,6 +11,7 @@ import numpy as np
import numbers
import types
import collections
import warnings
def _is_pil_image(img):
......@@ -141,21 +142,21 @@ def normalize(tensor, mean, std):
return tensor
def scale(img, size, interpolation=Image.BILINEAR):
"""Rescale the input PIL.Image to the given size.
def resize(img, size, interpolation=Image.BILINEAR):
"""Resize the input PIL.Image to the given size.
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
(h, w), output size will be matched to this. If size is an int,
smaller edge of the image will be matched to this number.
i.e, if height > width, then image will be rescaled to
(h, w), the output size will be matched to this. If size is an int,
the smaller edge of the image will be matched to this number maintaing
the aspect ratio. i.e, if height > width, then image will be rescaled to
(size * height / width, size)
interpolation (int, optional): Desired interpolation. Default is
``PIL.Image.BILINEAR``
Returns:
PIL.Image: Rescaled image.
PIL.Image: Resized image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
......@@ -178,6 +179,12 @@ def scale(img, size, interpolation=Image.BILINEAR):
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):
"""Pad the given PIL.Image on all sides with the given "pad" value.
......@@ -228,10 +235,10 @@ def crop(img, i, j, h, w):
return img.crop((j, i, j + w, i + h))
def scaled_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR):
"""Crop the given PIL.Image and scale it to desired size.
def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR):
"""Crop the given PIL.Image and resize it to desired size.
Notably used in RandomSizedCrop.
Notably used in RandomResizedCrop.
Args:
img (PIL.Image): Image to be cropped.
......@@ -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'
img = crop(img, i, j, h, w)
img = scale(img, size, interpolation)
img = resize(img, size, interpolation)
return img
......@@ -435,8 +442,8 @@ class Normalize(object):
return normalize(tensor, self.mean, self.std)
class Scale(object):
"""Rescale the input PIL.Image to the given size.
class Resize(object):
"""Resize the input PIL.Image to the given size.
Args:
size (sequence or int): Desired output size. If size is a sequence like
......@@ -461,7 +468,14 @@ class Scale(object):
Returns:
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):
......@@ -645,7 +659,7 @@ class RandomVerticalFlip(object):
return img
class RandomSizedCrop(object):
class RandomResizedCrop(object):
"""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
......@@ -701,10 +715,17 @@ class RandomSizedCrop(object):
img (PIL.Image): Image to be flipped.
Returns:
PIL.Image: Randomly cropped and scaled image.
PIL.Image: Randomly cropped and resize image.
"""
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):
......
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