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 Cutout transform #1022

Merged
merged 17 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion mmseg/datasets/pipelines/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,8 @@ def __init__(self,
n_holes,
lkm2835 marked this conversation as resolved.
Show resolved Hide resolved
cutout_shape=None,
cutout_ratio=None,
fill_in=(0, 0, 0)):
fill_in=(0, 0, 0),
ignore_index=255):
lkm2835 marked this conversation as resolved.
Show resolved Hide resolved

assert (cutout_shape is None) ^ (cutout_ratio is None), \
'Either cutout_shape or cutout_ratio should be specified.'
Expand All @@ -989,6 +990,7 @@ def __init__(self,
n_holes = (n_holes, n_holes)
self.n_holes = n_holes
self.fill_in = fill_in
self.ignore_index = ignore_index
self.with_ratio = cutout_ratio is not None
self.candidates = cutout_ratio if self.with_ratio else cutout_shape
if not isinstance(self.candidates, list):
Expand All @@ -1012,6 +1014,9 @@ def __call__(self, results):
y2 = np.clip(y1 + cutout_h, 0, h)
results['img'][y1:y2, x1:x2, :] = self.fill_in
lkm2835 marked this conversation as resolved.
Show resolved Hide resolved

for key in results.get('seg_fields', []):
results[key][y1:y2, x1:x2] = self.ignore_index
lkm2835 marked this conversation as resolved.
Show resolved Hide resolved

return results

def __repr__(self):
Expand Down
13 changes: 11 additions & 2 deletions tests/test_data/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,12 @@ def test_cutout():
img = mmcv.imread(
osp.join(osp.dirname(__file__), '../data/color.jpg'), 'color')

seg = np.array(
Image.open(osp.join(osp.dirname(__file__), '../data/seg.png')))

results['img'] = img
results['gt_semantic_seg'] = seg
results['seg_fields'] = ['gt_semantic_seg']
results['img_shape'] = img.shape
results['ori_shape'] = img.shape
results['pad_shape'] = img.shape
Expand All @@ -552,16 +557,20 @@ def test_cutout():
type='CutOut',
n_holes=(2, 4),
cutout_shape=[(10, 10), (15, 15)],
fill_in=(255, 255, 255))
fill_in=(255, 255, 255),
ignore_index=255)
cutout_module = build_from_cfg(transform, PIPELINES)
cutout_result = cutout_module(copy.deepcopy(results))
assert cutout_result['img'].sum() > img.sum()
assert cutout_result['gt_semantic_seg'].sum() > seg.sum()

transform = dict(
type='CutOut',
n_holes=1,
cutout_ratio=(0.8, 0.8),
fill_in=(255, 255, 255))
fill_in=(255, 255, 255),
ignore_index=255)
cutout_module = build_from_cfg(transform, PIPELINES)
cutout_result = cutout_module(copy.deepcopy(results))
assert cutout_result['img'].sum() > img.sum()
assert cutout_result['gt_semantic_seg'].sum() > seg.sum()