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 support for mask diagonal flip in TTA #5403

Merged
merged 4 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions mmdet/core/post_processing/merge_augs.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def merge_aug_masks(aug_masks, img_metas, rcnn_test_cfg, weights=None):
mask = mask[:, :, :, ::-1]
elif flip_direction == 'vertical':
mask = mask[:, :, ::-1, :]
elif flip_direction == 'diagonal':
mask = mask[:, :, :, ::-1]
mask = mask[:, :, ::-1, :]
else:
raise ValueError(
f"Invalid flipping direction '{flip_direction}'")
Expand Down
7 changes: 4 additions & 3 deletions mmdet/datasets/pipelines/test_time_aug.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ class MultiScaleFlipAug:
scale_factor (float | list[float] | None): Scale factors for resizing.
flip (bool): Whether apply flip augmentation. Default: False.
flip_direction (str | list[str]): Flip augmentation directions,
options are "horizontal" and "vertical". If flip_direction is list,
multiple flip augmentations will be applied.
It has no effect when flip == False. Default: "horizontal".
options are "horizontal", "vertical" and "diagonal". If
flip_direction is a list, multiple flip augmentations will be
applied. It has no effect when flip == False. Default:
"horizontal".
"""

def __init__(self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def model_aug_test_template(cfg_file):
# init test pipeline and set aug test
load_cfg, multi_scale_cfg = cfg.test_pipeline
multi_scale_cfg['flip'] = True
multi_scale_cfg['flip_direction'] = ['horizontal', 'vertical', 'diagonal']
multi_scale_cfg['img_scale'] = [(1333, 800), (800, 600), (640, 480)]

load = build_from_cfg(load_cfg, PIPELINES)
Expand All @@ -29,8 +30,8 @@ def model_aug_test_template(cfg_file):
img_prefix=osp.join(osp.dirname(__file__), '../../../data'),
img_info=dict(filename='color.jpg'))
results = transform(load(results))
assert len(results['img']) == 6
assert len(results['img_metas']) == 6
assert len(results['img']) == 12
assert len(results['img_metas']) == 12

results['img'] = [collate([x]) for x in results['img']]
results['img_metas'] = [collate([x]).data[0] for x in results['img_metas']]
Expand All @@ -56,14 +57,14 @@ def test_aug_test_size():
transforms=[],
img_scale=[(1333, 800), (800, 600), (640, 480)],
flip=True,
flip_direction=['horizontal', 'vertical'])
flip_direction=['horizontal', 'vertical', 'diagonal'])
multi_aug_test_module = build_from_cfg(transform, PIPELINES)

results = load(results)
results = multi_aug_test_module(load(results))
# len(["original", "horizontal", "vertical"]) *
# len(["original", "horizontal", "vertical", "diagonal"]) *
# len([(1333, 800), (800, 600), (640, 480)])
assert len(results['img']) == 9
assert len(results['img']) == 12


def test_cascade_rcnn_aug_test():
Expand Down Expand Up @@ -107,6 +108,7 @@ def test_cornernet_aug_test():
# init test pipeline and set aug test
load_cfg, multi_scale_cfg = cfg.test_pipeline
multi_scale_cfg['flip'] = True
multi_scale_cfg['flip_direction'] = ['horizontal', 'vertical', 'diagonal']
multi_scale_cfg['scale_factor'] = [0.5, 1.0, 2.0]

load = build_from_cfg(load_cfg, PIPELINES)
Expand All @@ -116,8 +118,8 @@ def test_cornernet_aug_test():
img_prefix=osp.join(osp.dirname(__file__), '../../../data'),
img_info=dict(filename='color.jpg'))
results = transform(load(results))
assert len(results['img']) == 6
assert len(results['img_metas']) == 6
assert len(results['img']) == 12
assert len(results['img_metas']) == 12

results['img'] = [collate([x]) for x in results['img']]
results['img_metas'] = [collate([x]).data[0] for x in results['img_metas']]
Expand Down