Skip to content

Commit f75b814

Browse files
surgan12fmassa
authored andcommitted
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: surgan12 <[email protected]> * tests changed * trvis * travis * fixes syntax * ... * flake fixes * flake_fixes * flake_fixes2
1 parent 5f54fd1 commit f75b814

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

test/test_transforms.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import division
12
import torch
23
import torchvision.transforms as transforms
34
import torchvision.transforms.functional as F
@@ -130,6 +131,25 @@ def test_ten_crop(self):
130131
assert len(results) == 10
131132
assert expected_output == results
132133

134+
def test_randomresized_params(self):
135+
height = random.randint(24, 32) * 2
136+
width = random.randint(24, 32) * 2
137+
img = torch.ones(3, height, width)
138+
to_pil_image = transforms.ToPILImage()
139+
img = to_pil_image(img)
140+
size = 100
141+
epsilon = 0.05
142+
for i in range(10):
143+
scale_min = round(random.random(), 2)
144+
scale_range = (scale_min, scale_min + round(random.random(), 2))
145+
aspect_min = round(random.random(), 2)
146+
aspect_ratio_range = (aspect_min, aspect_min + round(random.random(), 2))
147+
randresizecrop = transforms.RandomResizedCrop(size, scale_range, aspect_ratio_range)
148+
_, _, h, w = randresizecrop.get_params(img, scale_range, aspect_ratio_range)
149+
aspect_ratio_obtained = w / h
150+
assert (min(aspect_ratio_range) - epsilon <= aspect_ratio_obtained <= max(aspect_ratio_range) + epsilon or
151+
aspect_ratio_obtained == 1.0)
152+
133153
def test_resize(self):
134154
height = random.randint(24, 32) * 2
135155
width = random.randint(24, 32) * 2

torchvision/transforms/functional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR):
376376
377377
Args:
378378
img (PIL Image): Image to be cropped.
379-
i: Upper pixel coordinate.
380-
j: Left pixel coordinate.
379+
i: i in (i,j) i.e coordinates of the upper left corner
380+
j: j in (i,j) i.e coordinates of the upper left corner
381381
h: Height of the cropped image.
382382
w: Width of the cropped image.
383383
size (sequence or int): Desired output size. Same semantics as ``resize``.

torchvision/transforms/transforms.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,13 @@ class RandomResizedCrop(object):
543543
"""
544544

545545
def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), interpolation=Image.BILINEAR):
546-
self.size = (size, size)
546+
if isinstance(size, tuple):
547+
self.size = size
548+
else:
549+
self.size = (size, size)
550+
if (scale[0] > scale[1]) or (ratio[0] > ratio[1]):
551+
warnings.warn("range should be of kind (min, max)")
552+
547553
self.interpolation = interpolation
548554
self.scale = scale
549555
self.ratio = ratio
@@ -570,7 +576,7 @@ def get_params(img, scale, ratio):
570576
w = int(round(math.sqrt(target_area * aspect_ratio)))
571577
h = int(round(math.sqrt(target_area / aspect_ratio)))
572578

573-
if random.random() < 0.5:
579+
if random.random() < 0.5 and min(ratio) <= (h / w) <= max(ratio):
574580
w, h = h, w
575581

576582
if w <= img.size[0] and h <= img.size[1]:

0 commit comments

Comments
 (0)