From df029ab70566a2feccb6fb36f1e0322ebe376f43 Mon Sep 17 00:00:00 2001 From: clavichord93 Date: Thu, 23 Nov 2017 17:04:21 +0800 Subject: [PATCH 1/2] add tunable crop scale and aspect ratio in --- torchvision/transforms/transforms.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index 903e2b6cf5b..bfd2a50085f 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -323,19 +323,23 @@ def __call__(self, img): 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 - aspect ratio of 3/4 to 4/3 of the original aspect ratio is made. This crop + A crop of random size (default: of 0.08 to 1.0) of the original size and a random + aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop is finally resized to given size. This is popularly used to train the Inception networks. Args: size: expected output size of each edge + scale: range of size of the origin size cropped + ratio: range of aspect ratio of the origin aspect ratio cropped interpolation: Default: PIL.Image.BILINEAR """ - def __init__(self, size, interpolation=Image.BILINEAR): + def __init__(self, size, scale=(0.08, 1.0), ratio=(3./4., 4./3.), interpolation=Image.BILINEAR): self.size = (size, size) self.interpolation = interpolation + self.scale = scale + self.ratio = ratio @staticmethod def get_params(img): @@ -350,8 +354,8 @@ def get_params(img): """ for attempt in range(10): area = img.size[0] * img.size[1] - target_area = random.uniform(0.08, 1.0) * area - aspect_ratio = random.uniform(3. / 4, 4. / 3) + target_area = random.uniform(*self.scale) * area + aspect_ratio = random.uniform(*self.ratio) w = int(round(math.sqrt(target_area * aspect_ratio))) h = int(round(math.sqrt(target_area / aspect_ratio))) From b575f91ba6ca021f94b9d405f09d8bbfb90a7071 Mon Sep 17 00:00:00 2001 From: clavichord93 Date: Thu, 23 Nov 2017 17:32:30 +0800 Subject: [PATCH 2/2] fix whitespace around arithmetic operators --- torchvision/transforms/transforms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index bfd2a50085f..dad79180eaa 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -335,7 +335,7 @@ class RandomResizedCrop(object): interpolation: Default: PIL.Image.BILINEAR """ - 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) self.interpolation = interpolation self.scale = scale