Skip to content

Commit c4b86b0

Browse files
Zheng Qinalykhantejani
authored andcommitted
add tunable crop scale and aspect ratio in RandomSizedCrop (#343)
* add tunable crop scale and aspect ratio in
1 parent 64862b5 commit c4b86b0

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

torchvision/transforms/transforms.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,23 @@ def __call__(self, img):
323323
class RandomResizedCrop(object):
324324
"""Crop the given PIL Image to random size and aspect ratio.
325325
326-
A crop of random size of (0.08 to 1.0) of the original size and a random
327-
aspect ratio of 3/4 to 4/3 of the original aspect ratio is made. This crop
326+
A crop of random size (default: of 0.08 to 1.0) of the original size and a random
327+
aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop
328328
is finally resized to given size.
329329
This is popularly used to train the Inception networks.
330330
331331
Args:
332332
size: expected output size of each edge
333+
scale: range of size of the origin size cropped
334+
ratio: range of aspect ratio of the origin aspect ratio cropped
333335
interpolation: Default: PIL.Image.BILINEAR
334336
"""
335337

336-
def __init__(self, size, interpolation=Image.BILINEAR):
338+
def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), interpolation=Image.BILINEAR):
337339
self.size = (size, size)
338340
self.interpolation = interpolation
341+
self.scale = scale
342+
self.ratio = ratio
339343

340344
@staticmethod
341345
def get_params(img):
@@ -350,8 +354,8 @@ def get_params(img):
350354
"""
351355
for attempt in range(10):
352356
area = img.size[0] * img.size[1]
353-
target_area = random.uniform(0.08, 1.0) * area
354-
aspect_ratio = random.uniform(3. / 4, 4. / 3)
357+
target_area = random.uniform(*self.scale) * area
358+
aspect_ratio = random.uniform(*self.ratio)
355359

356360
w = int(round(math.sqrt(target_area * aspect_ratio)))
357361
h = int(round(math.sqrt(target_area / aspect_ratio)))

0 commit comments

Comments
 (0)