提交 03bd4e44 编写于 作者: Y Yang Nie 提交者: Tingquan Gao

add use_log_aspect for RandCropImage

上级 cc4333a3
...@@ -53,9 +53,11 @@ DataLoader: ...@@ -53,9 +53,11 @@ DataLoader:
- DecodeImage: - DecodeImage:
to_rgb: True to_rgb: True
channel_first: False channel_first: False
backend: pil
- RandCropImage: - RandCropImage:
size: 224 size: 224
interpolation: bilinear interpolation: bilinear
use_log_aspect: True
backend: pil backend: pil
- RandFlipImage: - RandFlipImage:
flip_code: 1 flip_code: 1
...@@ -83,6 +85,7 @@ DataLoader: ...@@ -83,6 +85,7 @@ DataLoader:
- DecodeImage: - DecodeImage:
to_rgb: True to_rgb: True
channel_first: False channel_first: False
backend: pil
- ResizeImage: - ResizeImage:
resize_short: 256 resize_short: 256
interpolation: bilinear interpolation: bilinear
...@@ -110,6 +113,7 @@ Infer: ...@@ -110,6 +113,7 @@ Infer:
- DecodeImage: - DecodeImage:
to_rgb: True to_rgb: True
channel_first: False channel_first: False
backend: pil
- ResizeImage: - ResizeImage:
resize_short: 256 resize_short: 256
interpolation: bilinear interpolation: bilinear
......
...@@ -445,6 +445,7 @@ class RandCropImage(object): ...@@ -445,6 +445,7 @@ class RandCropImage(object):
scale=None, scale=None,
ratio=None, ratio=None,
interpolation=None, interpolation=None,
use_log_aspect=False,
backend="cv2"): backend="cv2"):
if type(size) is int: if type(size) is int:
self.size = (size, size) # (h, w) self.size = (size, size) # (h, w)
...@@ -454,6 +455,7 @@ class RandCropImage(object): ...@@ -454,6 +455,7 @@ class RandCropImage(object):
self.progress_size = progress_size self.progress_size = progress_size
self.scale = [0.08, 1.0] if scale is None else scale self.scale = [0.08, 1.0] if scale is None else scale
self.ratio = [3. / 4., 4. / 3.] if ratio is None else ratio self.ratio = [3. / 4., 4. / 3.] if ratio is None else ratio
self.use_log_aspect = use_log_aspect
self._resize_func = UnifiedResize( self._resize_func = UnifiedResize(
interpolation=interpolation, backend=backend) interpolation=interpolation, backend=backend)
...@@ -464,21 +466,21 @@ class RandCropImage(object): ...@@ -464,21 +466,21 @@ class RandCropImage(object):
scale = self.scale scale = self.scale
ratio = self.ratio ratio = self.ratio
aspect_ratio = math.sqrt(random.uniform(*ratio)) if self.use_log_aspect:
w = 1. * aspect_ratio log_ratio = list(map(math.log, ratio))
h = 1. / aspect_ratio aspect_ratio = math.exp(random.uniform(*log_ratio))
else:
aspect_ratio = random.uniform(*ratio)
img_h, img_w = img.shape[:2] img_h, img_w = img.shape[:2]
bound = min((float(img_w) / img_h) / aspect_ratio,
bound = min((float(img_w) / img_h) / (w**2), (float(img_h) / img_w) * aspect_ratio)
(float(img_h) / img_w) / (h**2))
scale_max = min(scale[1], bound) scale_max = min(scale[1], bound)
scale_min = min(scale[0], bound) scale_min = min(scale[0], bound)
target_area = img_w * img_h * random.uniform(scale_min, scale_max) target_area = img_w * img_h * random.uniform(scale_min, scale_max)
target_size = math.sqrt(target_area) w = int(math.sqrt(target_area * aspect_ratio))
w = int(target_size * w) h = int(math.sqrt(target_area / aspect_ratio))
h = int(target_size * h)
i = random.randint(0, img_w - w) i = random.randint(0, img_w - w)
j = random.randint(0, img_h - h) j = random.randint(0, img_h - h)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册