Commit f75b8148 authored by surgan12's avatar surgan12 Committed by Francisco Massa
Browse files

RandomResizedCrop modifications (#715)



* randomresizedmods

* lint checks

* test to randomrescrop added

* updates

* tests updated

* tests updated

* upd

* updates

* Update torchvision/transforms/transforms.py
Co-Authored-By: default avatarsurgan12 <33121121+surgan12@users.noreply.github.com>

* tests changed

* trvis

* travis

* fixes syntax

* ...

* flake fixes

* flake_fixes

* flake_fixes2
parent 5f54fd14
from __future__ import division
import torch import torch
import torchvision.transforms as transforms import torchvision.transforms as transforms
import torchvision.transforms.functional as F import torchvision.transforms.functional as F
...@@ -130,6 +131,25 @@ class Tester(unittest.TestCase): ...@@ -130,6 +131,25 @@ class Tester(unittest.TestCase):
assert len(results) == 10 assert len(results) == 10
assert expected_output == results assert expected_output == results
def test_randomresized_params(self):
height = random.randint(24, 32) * 2
width = random.randint(24, 32) * 2
img = torch.ones(3, height, width)
to_pil_image = transforms.ToPILImage()
img = to_pil_image(img)
size = 100
epsilon = 0.05
for i in range(10):
scale_min = round(random.random(), 2)
scale_range = (scale_min, scale_min + round(random.random(), 2))
aspect_min = round(random.random(), 2)
aspect_ratio_range = (aspect_min, aspect_min + round(random.random(), 2))
randresizecrop = transforms.RandomResizedCrop(size, scale_range, aspect_ratio_range)
_, _, h, w = randresizecrop.get_params(img, scale_range, aspect_ratio_range)
aspect_ratio_obtained = w / h
assert (min(aspect_ratio_range) - epsilon <= aspect_ratio_obtained <= max(aspect_ratio_range) + epsilon or
aspect_ratio_obtained == 1.0)
def test_resize(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
......
...@@ -376,8 +376,8 @@ def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR): ...@@ -376,8 +376,8 @@ def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR):
Args: Args:
img (PIL Image): Image to be cropped. img (PIL Image): Image to be cropped.
i: Upper pixel coordinate. i: i in (i,j) i.e coordinates of the upper left corner
j: Left pixel coordinate. j: j in (i,j) i.e coordinates of the upper left corner
h: Height of the cropped image. h: Height of the cropped image.
w: Width of the cropped image. w: Width of the cropped image.
size (sequence or int): Desired output size. Same semantics as ``resize``. size (sequence or int): Desired output size. Same semantics as ``resize``.
......
...@@ -543,7 +543,13 @@ class RandomResizedCrop(object): ...@@ -543,7 +543,13 @@ class RandomResizedCrop(object):
""" """
def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), interpolation=Image.BILINEAR): def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), interpolation=Image.BILINEAR):
self.size = (size, size) if isinstance(size, tuple):
self.size = size
else:
self.size = (size, size)
if (scale[0] > scale[1]) or (ratio[0] > ratio[1]):
warnings.warn("range should be of kind (min, max)")
self.interpolation = interpolation self.interpolation = interpolation
self.scale = scale self.scale = scale
self.ratio = ratio self.ratio = ratio
...@@ -570,7 +576,7 @@ class RandomResizedCrop(object): ...@@ -570,7 +576,7 @@ class RandomResizedCrop(object):
w = int(round(math.sqrt(target_area * aspect_ratio))) w = int(round(math.sqrt(target_area * aspect_ratio)))
h = int(round(math.sqrt(target_area / aspect_ratio))) h = int(round(math.sqrt(target_area / aspect_ratio)))
if random.random() < 0.5: if random.random() < 0.5 and min(ratio) <= (h / w) <= max(ratio):
w, h = h, w w, h = h, w
if w <= img.size[0] and h <= img.size[1]: if w <= img.size[0] and h <= img.size[1]:
......
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