Skip to content

Commit

Permalink
RandomResizedCrop modifications (#715)
Browse files Browse the repository at this point in the history
* randomresizedmods

* lint checks

* test to randomrescrop added

* updates

* tests updated

* tests updated

* upd

* updates

* Update torchvision/transforms/transforms.py

Co-Authored-By: surgan12 <33121121+surgan12@users.noreply.github.com>

* tests changed

* trvis

* travis

* fixes syntax

* ...

* flake fixes

* flake_fixes

* flake_fixes2
  • Loading branch information
surgan12 authored and fmassa committed Feb 18, 2019
1 parent 5f54fd1 commit f75b814
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
20 changes: 20 additions & 0 deletions test/test_transforms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import division
import torch
import torchvision.transforms as transforms
import torchvision.transforms.functional as F
Expand Down Expand Up @@ -130,6 +131,25 @@ def test_ten_crop(self):
assert len(results) == 10
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):
height = random.randint(24, 32) * 2
width = random.randint(24, 32) * 2
Expand Down
4 changes: 2 additions & 2 deletions torchvision/transforms/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR):
Args:
img (PIL Image): Image to be cropped.
i: Upper pixel coordinate.
j: Left pixel coordinate.
i: i in (i,j) i.e coordinates of the upper left corner
j: j in (i,j) i.e coordinates of the upper left corner
h: Height of the cropped image.
w: Width of the cropped image.
size (sequence or int): Desired output size. Same semantics as ``resize``.
Expand Down
10 changes: 8 additions & 2 deletions torchvision/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,13 @@ class RandomResizedCrop(object):
"""

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.scale = scale
self.ratio = ratio
Expand All @@ -570,7 +576,7 @@ def get_params(img, scale, ratio):
w = 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

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

0 comments on commit f75b814

Please sign in to comment.