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

Support resize data augmentation according to original image size #291

Merged
merged 9 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions mmseg/datasets/pipelines/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Resize(object):
(multi-scale). There are 3 multiscale modes:
Junjun2016 marked this conversation as resolved.
Show resolved Hide resolved

- ``ratio_range is not None``: randomly sample a ratio from the ratio range
and multiply it with the image scale.
and multiply it with the image scale. When img_scale is None, img_scale is
the shape of image in results (img_scale = results['img'].shape[:2]).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
and multiply it with the image scale. When img_scale is None, img_scale is
the shape of image in results (img_scale = results['img'].shape[:2]).
and multiply it with the image scale. When img_scale is None, the image is resized based on the original size.


- ``ratio_range is None and multiscale_mode == "range"``: randomly sample a
scale from the a range.
Expand Down Expand Up @@ -50,7 +51,7 @@ def __init__(self,

if ratio_range is not None:
# mode 1: given a scale and a range of image ratio
assert len(self.img_scale) == 1
assert self.img_scale is None or len(self.img_scale) == 1
else:
# mode 2: given multiple scales or a range of scales
assert multiscale_mode in ['value', 'range']
Expand Down Expand Up @@ -150,8 +151,12 @@ def _random_scale(self, results):
"""

if self.ratio_range is not None:
scale, scale_idx = self.random_sample_ratio(
self.img_scale[0], self.ratio_range)
if self.img_scale is None:
scale, scale_idx = self.random_sample_ratio(
results['img'].shape[:2], self.ratio_range)
else:
scale, scale_idx = self.random_sample_ratio(
self.img_scale[0], self.ratio_range)
elif len(self.img_scale) == 1:
scale, scale_idx = self.img_scale[0], 0
elif self.multiscale_mode == 'range':
Expand Down
10 changes: 10 additions & 0 deletions tests/test_data/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_resize():
resize_module = build_from_cfg(transform, PIPELINES)

results = dict()
# (288, 512, 3)
img = mmcv.imread(
osp.join(osp.dirname(__file__), '../data/color.jpg'), 'color')
results['img'] = img
Expand Down Expand Up @@ -92,6 +93,15 @@ def test_resize():
resized_results = resize_module(results.copy())
assert max(resized_results['img_shape'][:2]) <= 1333 * 1.1

# test img_scale=None and ratio_range is tuple.
# img shape: (288, 512, 3)
transform = dict(
type='Resize', img_scale=None, ratio_range=(0.5, 2.0), keep_ratio=True)
resize_module = build_from_cfg(transform, PIPELINES)
resized_results = resize_module(results.copy())
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


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