|
@@ -212,13 +212,15 @@ class Resize(Transform):
|
|
|
Otherwise, target_size represents [target height, target width].
|
|
Otherwise, target_size represents [target height, target width].
|
|
|
interp ({'NEAREST', 'LINEAR', 'CUBIC', 'AREA', 'LANCZOS4', 'RANDOM'}, optional):
|
|
interp ({'NEAREST', 'LINEAR', 'CUBIC', 'AREA', 'LANCZOS4', 'RANDOM'}, optional):
|
|
|
Interpolation method of resize. Defaults to 'LINEAR'.
|
|
Interpolation method of resize. Defaults to 'LINEAR'.
|
|
|
|
|
+ keep_ratio (bool): the resize scale of width/height is same and width/height after resized is not greater
|
|
|
|
|
+ than target width/height. Defaults to False.
|
|
|
|
|
|
|
|
Raises:
|
|
Raises:
|
|
|
TypeError: Invalid type of target_size.
|
|
TypeError: Invalid type of target_size.
|
|
|
ValueError: Invalid interpolation method.
|
|
ValueError: Invalid interpolation method.
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
- def __init__(self, target_size, interp='LINEAR'):
|
|
|
|
|
|
|
+ def __init__(self, target_size, interp='LINEAR', keep_ratio=False):
|
|
|
super(Resize, self).__init__()
|
|
super(Resize, self).__init__()
|
|
|
if not (interp == "RANDOM" or interp in interp_dict):
|
|
if not (interp == "RANDOM" or interp in interp_dict):
|
|
|
raise ValueError("interp should be one of {}".format(
|
|
raise ValueError("interp should be one of {}".format(
|
|
@@ -234,25 +236,22 @@ class Resize(Transform):
|
|
|
# (height, width)
|
|
# (height, width)
|
|
|
self.target_size = target_size
|
|
self.target_size = target_size
|
|
|
self.interp = interp
|
|
self.interp = interp
|
|
|
|
|
+ self.keep_ratio = keep_ratio
|
|
|
|
|
|
|
|
- def apply_im(self, image, interp):
|
|
|
|
|
- image = cv2.resize(
|
|
|
|
|
- image, (self.target_size[1], self.target_size[0]),
|
|
|
|
|
- interpolation=interp)
|
|
|
|
|
|
|
+ def apply_im(self, image, interp, target_size):
|
|
|
|
|
+ image = cv2.resize(image, target_size, interpolation=interp)
|
|
|
return image
|
|
return image
|
|
|
|
|
|
|
|
- def apply_mask(self, mask):
|
|
|
|
|
- mask = cv2.resize(
|
|
|
|
|
- mask, (self.target_size[1], self.target_size[0]),
|
|
|
|
|
- interpolation=cv2.INTER_NEAREST)
|
|
|
|
|
|
|
+ def apply_mask(self, mask, target_size):
|
|
|
|
|
+ mask = cv2.resize(mask, target_size, interpolation=cv2.INTER_NEAREST)
|
|
|
return mask
|
|
return mask
|
|
|
|
|
|
|
|
- def apply_bbox(self, bbox, scale):
|
|
|
|
|
|
|
+ def apply_bbox(self, bbox, scale, target_size):
|
|
|
im_scale_x, im_scale_y = scale
|
|
im_scale_x, im_scale_y = scale
|
|
|
bbox[:, 0::2] *= im_scale_x
|
|
bbox[:, 0::2] *= im_scale_x
|
|
|
bbox[:, 1::2] *= im_scale_y
|
|
bbox[:, 1::2] *= im_scale_y
|
|
|
- bbox[:, 0::2] = np.clip(bbox[:, 0::2], 0, self.target_size[1])
|
|
|
|
|
- bbox[:, 1::2] = np.clip(bbox[:, 1::2], 0, self.target_size[0])
|
|
|
|
|
|
|
+ bbox[:, 0::2] = np.clip(bbox[:, 0::2], 0, target_size[0])
|
|
|
|
|
+ bbox[:, 1::2] = np.clip(bbox[:, 1::2], 0, target_size[1])
|
|
|
return bbox
|
|
return bbox
|
|
|
|
|
|
|
|
def apply_segm(self, segms, im_size, scale):
|
|
def apply_segm(self, segms, im_size, scale):
|
|
@@ -281,14 +280,22 @@ class Resize(Transform):
|
|
|
|
|
|
|
|
im_scale_y = self.target_size[0] / im_h
|
|
im_scale_y = self.target_size[0] / im_h
|
|
|
im_scale_x = self.target_size[1] / im_w
|
|
im_scale_x = self.target_size[1] / im_w
|
|
|
|
|
+ target_size = (self.target_size[1], self.target_size[0])
|
|
|
|
|
+ if self.keep_ratio:
|
|
|
|
|
+ scale = min(im_scale_y, im_scale_x)
|
|
|
|
|
+ target_w = int(round(im_w * scale))
|
|
|
|
|
+ target_h = int(round(im_h * scale))
|
|
|
|
|
+ target_size = (target_w, target_h)
|
|
|
|
|
+ im_scale_y = target_h / im_h
|
|
|
|
|
+ im_scale_x = target_w / im_w
|
|
|
|
|
|
|
|
- sample['image'] = self.apply_im(sample['image'], interp)
|
|
|
|
|
|
|
+ sample['image'] = self.apply_im(sample['image'], interp, target_size)
|
|
|
|
|
|
|
|
if 'mask' in sample:
|
|
if 'mask' in sample:
|
|
|
- sample['mask'] = self.apply_mask(sample['mask'])
|
|
|
|
|
|
|
+ sample['mask'] = self.apply_mask(sample['mask'], target_size)
|
|
|
if 'gt_bbox' in sample and len(sample['gt_bbox']) > 0:
|
|
if 'gt_bbox' in sample and len(sample['gt_bbox']) > 0:
|
|
|
- sample['gt_bbox'] = self.apply_bbox(sample['gt_bbox'],
|
|
|
|
|
- [im_scale_x, im_scale_y])
|
|
|
|
|
|
|
+ sample['gt_bbox'] = self.apply_bbox(
|
|
|
|
|
+ sample['gt_bbox'], [im_scale_x, im_scale_y], target_size)
|
|
|
if 'gt_poly' in sample and len(sample['gt_poly']) > 0:
|
|
if 'gt_poly' in sample and len(sample['gt_poly']) > 0:
|
|
|
sample['gt_poly'] = self.apply_segm(
|
|
sample['gt_poly'] = self.apply_segm(
|
|
|
sample['gt_poly'], [im_h, im_w], [im_scale_x, im_scale_y])
|
|
sample['gt_poly'], [im_h, im_w], [im_scale_x, im_scale_y])
|
|
@@ -913,7 +920,8 @@ class Padding(Transform):
|
|
|
pad_mode=0,
|
|
pad_mode=0,
|
|
|
offsets=None,
|
|
offsets=None,
|
|
|
im_padding_value=(127.5, 127.5, 127.5),
|
|
im_padding_value=(127.5, 127.5, 127.5),
|
|
|
- label_padding_value=255):
|
|
|
|
|
|
|
+ label_padding_value=255,
|
|
|
|
|
+ size_divisor=32):
|
|
|
"""
|
|
"""
|
|
|
Pad image to a specified size or multiple of size_divisor.
|
|
Pad image to a specified size or multiple of size_divisor.
|
|
|
|
|
|
|
@@ -923,6 +931,7 @@ class Padding(Transform):
|
|
|
if 0, only pad to right and bottom. If 1, pad according to center. If 2, only pad left and top. Defaults to 0.
|
|
if 0, only pad to right and bottom. If 1, pad according to center. If 2, only pad left and top. Defaults to 0.
|
|
|
im_padding_value(Sequence[float]): RGB value of pad area. Defaults to (127.5, 127.5, 127.5).
|
|
im_padding_value(Sequence[float]): RGB value of pad area. Defaults to (127.5, 127.5, 127.5).
|
|
|
label_padding_value(int, optional): Filling value for the mask. Defaults to 255.
|
|
label_padding_value(int, optional): Filling value for the mask. Defaults to 255.
|
|
|
|
|
+ size_divisor(int): Image width and height after padding is a multiple of size_divisor
|
|
|
"""
|
|
"""
|
|
|
super(Padding, self).__init__()
|
|
super(Padding, self).__init__()
|
|
|
if isinstance(target_size, (list, tuple)):
|
|
if isinstance(target_size, (list, tuple)):
|
|
@@ -940,7 +949,7 @@ class Padding(Transform):
|
|
|
assert offsets, 'if pad_mode is -1, offsets should not be None'
|
|
assert offsets, 'if pad_mode is -1, offsets should not be None'
|
|
|
|
|
|
|
|
self.target_size = target_size
|
|
self.target_size = target_size
|
|
|
- self.size_divisor = 32
|
|
|
|
|
|
|
+ self.size_divisor = size_divisor
|
|
|
self.pad_mode = pad_mode
|
|
self.pad_mode = pad_mode
|
|
|
self.offsets = offsets
|
|
self.offsets = offsets
|
|
|
self.im_padding_value = im_padding_value
|
|
self.im_padding_value = im_padding_value
|