Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add min_size arg in Resize to keep the shape after resize bigger than slide window #1318

Merged
merged 15 commits into from
Mar 1, 2022
22 changes: 21 additions & 1 deletion mmseg/datasets/pipelines/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,18 @@ class Resize(object):
Default: None
keep_ratio (bool): Whether to keep the aspect ratio when resizing the
image. Default: True
crop_size (int, optional): The minimum size for input and the shape
linfangjian01 marked this conversation as resolved.
Show resolved Hide resolved
of the image and seg map will not be less than ``crop_szie``.
It always be used in ``SETR`` algorithm to keep the shape.
Default: None
linfangjian01 marked this conversation as resolved.
Show resolved Hide resolved
"""

def __init__(self,
img_scale=None,
multiscale_mode='range',
ratio_range=None,
keep_ratio=True):
keep_ratio=True,
crop_size=None):
if img_scale is None:
self.img_scale = None
else:
Expand All @@ -126,6 +131,7 @@ def __init__(self,
self.multiscale_mode = multiscale_mode
self.ratio_range = ratio_range
self.keep_ratio = keep_ratio
self.crop_size = crop_size

@staticmethod
def random_select(img_scales):
Expand Down Expand Up @@ -240,6 +246,20 @@ def _random_scale(self, results):
def _resize_img(self, results):
"""Resize images with ``results['scale']``."""
if self.keep_ratio:
if self.crop_size is not None:
# keep the shape is not less than crop_size
if min(results['scale']) < self.crop_size:
new_short = self.crop_size
else:
new_short = min(results['scale'])

h, w = results['img'].shape[:2]
if h > w:
new_h, new_w = new_short * h / w, new_short
else:
new_h, new_w = new_short, new_short * w / h
results['scale'] = (new_h, new_w)

img, scale_factor = mmcv.imrescale(
results['img'], results['scale'], return_scale=True)
# the w_scale and h_scale has minor difference
Expand Down
6 changes: 6 additions & 0 deletions tests/test_data/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ def test_resize():
assert int(288 * 0.5) <= resized_results['img_shape'][0] <= 288 * 2.0
assert int(512 * 0.5) <= resized_results['img_shape'][1] <= 512 * 2.0

# test crop_size=640
transform = dict(type='Resize', img_scale=(2560, 640), crop_size=640)
resize_module = build_from_cfg(transform, PIPELINES)
resized_results = resize_module(results.copy())
assert resized_results['img_shape'] == (640, 1138, 3)


def test_flip():
# test assertion for invalid prob
Expand Down