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

[Fix] Fix bitmap mask translate when out_shape is different. #8993

Merged
merged 3 commits into from
Oct 12, 2022
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
11 changes: 9 additions & 2 deletions mmdet/structures/mask/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ def translate(self,
>>> border_value = 0
>>> interpolation = 'bilinear'
>>> # Note, There seem to be issues when:
>>> # * out_shape is different than self's shape
>>> # * the mask dtype is not supported by cv2.AffineWarp
>>> new = self.translate(out_shape, offset, direction,
>>> border_value, interpolation)
Expand All @@ -432,8 +431,16 @@ def translate(self,
if len(self.masks) == 0:
translated_masks = np.empty((0, *out_shape), dtype=np.uint8)
else:
masks = self.masks
if masks.shape[-2:] != out_shape:
empty_masks = np.zeros((masks.shape[0], *out_shape),
dtype=masks.dtype)
min_h = min(out_shape[0], masks.shape[1])
min_w = min(out_shape[1], masks.shape[2])
empty_masks[:, :min_h, :min_w] = masks[:, :min_h, :min_w]
masks = empty_masks
translated_masks = mmcv.imtranslate(
self.masks.transpose((1, 2, 0)),
masks.transpose((1, 2, 0)),
offset,
direction,
border_value=border_value,
Expand Down
43 changes: 43 additions & 0 deletions tests/test_structures/test_mask/test_mask_structures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from unittest import TestCase

import numpy as np
from mmengine.testing import assert_allclose

from mmdet.structures.mask import BitmapMasks


class TestMaskStructures(TestCase):

def test_bitmap_translate_same_size(self):
mask_array = np.zeros((5, 10, 10), dtype=np.uint8)
mask_array[:, 0:5, 0:5] = 1
mask_target = np.zeros((5, 10, 10), dtype=np.uint8)
mask_target[:, 0:5, 5:10] = 1

mask = BitmapMasks(mask_array, 10, 10)
mask = mask.translate((10, 10), 5)
assert mask.masks.shape == (5, 10, 10)
assert_allclose(mask_target, mask.masks)

def test_bitmap_translate_diff_size(self):
# test out shape larger
mask_array = np.zeros((5, 10, 10), dtype=np.uint8)
mask_array[:, 0:5, 0:5] = 1

mask_target = np.zeros((5, 20, 20), dtype=np.uint8)
mask_target[:, 0:5, 5:10] = 1
mask = BitmapMasks(mask_array, 10, 10)
mask = mask.translate((20, 20), 5)
assert mask.masks.shape == (5, 20, 20)
assert_allclose(mask_target, mask.masks)

# test out shape smaller
mask_array = np.zeros((5, 10, 10), dtype=np.uint8)
mask_array[:, 0:5, 0:5] = 1

mask_target = np.zeros((5, 20, 8), dtype=np.uint8)
mask_target[:, 0:5, 5:] = 1
mask = BitmapMasks(mask_array, 10, 10)
mask = mask.translate((20, 8), 5)
assert mask.masks.shape == (5, 20, 8)
assert_allclose(mask_target, mask.masks)