Skip to content

Commit

Permalink
[Enhance]: Check dtype in transform unit tests. (open-mmlab#5969)
Browse files Browse the repository at this point in the history
* add dtype to bbox unit test

* add dtype to unit test

* add docstring

* add label and check dtype
  • Loading branch information
RangiLyu authored and ZwwWayne committed Jul 18, 2022
1 parent 1f559a6 commit 978aac9
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 221 deletions.
4 changes: 4 additions & 0 deletions tests/test_data/test_pipelines/test_transform/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from .utils import check_result_same, construct_toy_data, create_random_bboxes

__all__ = ['create_random_bboxes', 'construct_toy_data', 'check_result_same']
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,8 @@
from mmcv.utils import build_from_cfg
from numpy.testing import assert_array_equal

from mmdet.core.mask import BitmapMasks, PolygonMasks
from mmdet.datasets.builder import PIPELINES


def construct_toy_data(poly2mask=True):
img = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=np.uint8)
img = np.stack([img, img, img], axis=-1)
results = dict()
# image
results['img'] = img
results['img_shape'] = img.shape
results['img_fields'] = ['img']
# bboxes
results['bbox_fields'] = ['gt_bboxes', 'gt_bboxes_ignore']
results['gt_bboxes'] = np.array([[0., 0., 2., 1.]], dtype=np.float32)
results['gt_bboxes_ignore'] = np.array([[2., 0., 3., 1.]],
dtype=np.float32)
# labels
results['gt_labels'] = np.array([1], dtype=np.int64)
# masks
results['mask_fields'] = ['gt_masks']
if poly2mask:
gt_masks = np.array([[0, 1, 1, 0], [0, 1, 0, 0]],
dtype=np.uint8)[None, :, :]
results['gt_masks'] = BitmapMasks(gt_masks, 2, 4)
else:
raw_masks = [[np.array([1, 0, 2, 0, 2, 1, 1, 1], dtype=np.float)]]
results['gt_masks'] = PolygonMasks(raw_masks, 2, 4)
# segmentations
results['seg_fields'] = ['gt_semantic_seg']
results['gt_semantic_seg'] = img[..., 0]
return results
from .utils import construct_toy_data


def test_adjust_color():
Expand Down
69 changes: 8 additions & 61 deletions tests/test_data/test_pipelines/test_transform/test_rotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,7 @@

from mmdet.core.mask import BitmapMasks, PolygonMasks
from mmdet.datasets.builder import PIPELINES


def construct_toy_data(poly2mask=True):
img = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=np.uint8)
img = np.stack([img, img, img], axis=-1)
results = dict()
# image
results['img'] = img
results['img_shape'] = img.shape
results['img_fields'] = ['img']
# bboxes
results['bbox_fields'] = ['gt_bboxes', 'gt_bboxes_ignore']
results['gt_bboxes'] = np.array([[0., 0., 2., 1.]], dtype=np.float32)
results['gt_bboxes_ignore'] = np.array([[2., 0., 3., 1.]],
dtype=np.float32)
# labels
results['gt_labels'] = np.array([1], dtype=np.int64)
# masks
results['mask_fields'] = ['gt_masks']
if poly2mask:
gt_masks = np.array([[0, 1, 1, 0], [0, 1, 0, 0]],
dtype=np.uint8)[None, :, :]
results['gt_masks'] = BitmapMasks(gt_masks, 2, 4)
else:
raw_masks = [[np.array([0, 0, 2, 0, 2, 1, 0, 1], dtype=np.float)]]
results['gt_masks'] = PolygonMasks(raw_masks, 2, 4)
# segmentations
results['seg_fields'] = ['gt_semantic_seg']
results['gt_semantic_seg'] = img[..., 0]
return results


def _check_fields(results, results_rotated, keys):
for key in keys:
if isinstance(results[key], (BitmapMasks, PolygonMasks)):
assert np.equal(results[key].to_ndarray(),
results_rotated[key].to_ndarray()).all()
else:
assert np.equal(results[key], results_rotated[key]).all()


def check_rotate(results, results_rotated):
# check image
_check_fields(results, results_rotated, results.get('img_fields', ['img']))
# check bboxes
_check_fields(results, results_rotated, results.get('bbox_fields', []))
# check masks
_check_fields(results, results_rotated, results.get('mask_fields', []))
# check segmentations
_check_fields(results, results_rotated, results.get('seg_fields', []))
# _check gt_labels
if 'gt_labels' in results:
assert np.equal(results['gt_labels'],
results_rotated['gt_labels']).all()
from .utils import check_result_same, construct_toy_data


def test_rotate():
Expand Down Expand Up @@ -105,14 +52,14 @@ def test_rotate():
)
rotate_module = build_from_cfg(transform, PIPELINES)
results_wo_rotate = rotate_module(copy.deepcopy(results))
check_rotate(results, results_wo_rotate)
check_result_same(results, results_wo_rotate)

# test case when no rotate aug (prob<=0)
transform = dict(
type='Rotate', level=10, prob=0., img_fill_val=img_fill_val, scale=0.6)
rotate_module = build_from_cfg(transform, PIPELINES)
results_wo_rotate = rotate_module(copy.deepcopy(results))
check_rotate(results, results_wo_rotate)
check_result_same(results, results_wo_rotate)

# test clockwise rotation with angle 90
results = construct_toy_data()
Expand Down Expand Up @@ -140,14 +87,14 @@ def test_rotate():
results_gt['gt_semantic_seg'] = np.array(
[[255, 6, 2, 255], [255, 7, 3,
255]]).astype(results['gt_semantic_seg'].dtype)
check_rotate(results_gt, results_rotated)
check_result_same(results_gt, results_rotated)

# test clockwise rotation with angle 90, PolygonMasks
results = construct_toy_data(poly2mask=False)
results_rotated = rotate_module(copy.deepcopy(results))
gt_masks = [[np.array([2, 0, 2, 1, 1, 1, 1, 0], dtype=np.float)]]
results_gt['gt_masks'] = PolygonMasks(gt_masks, 2, 4)
check_rotate(results_gt, results_rotated)
check_result_same(results_gt, results_rotated)

# test counter-clockwise roatation with angle 90,
# and specify the ratation center
Expand Down Expand Up @@ -183,7 +130,7 @@ def test_rotate():
gt_seg = (np.ones((h, w)) * 255).astype(results['gt_semantic_seg'].dtype)
gt_seg[0, 0], gt_seg[0, 1] = 1, 5
results_gt['gt_semantic_seg'] = gt_seg
check_rotate(results_gt, results_rotated)
check_result_same(results_gt, results_rotated)

transform = dict(
type='Rotate',
Expand All @@ -195,15 +142,15 @@ def test_rotate():
prob=1.)
rotate_module = build_from_cfg(transform, PIPELINES)
results_rotated = rotate_module(copy.deepcopy(results))
check_rotate(results_gt, results_rotated)
check_result_same(results_gt, results_rotated)

# test counter-clockwise roatation with angle 90,
# and specify the ratation center, PolygonMasks
results = construct_toy_data(poly2mask=False)
results_rotated = rotate_module(copy.deepcopy(results))
gt_masks = [[np.array([0, 0, 0, 0, 1, 0, 1, 0], dtype=np.float)]]
results_gt['gt_masks'] = PolygonMasks(gt_masks, 2, 4)
check_rotate(results_gt, results_rotated)
check_result_same(results_gt, results_rotated)

# test AutoAugment equipped with Rotate
policies = [[dict(type='Rotate', level=10, prob=1.)]]
Expand Down
78 changes: 12 additions & 66 deletions tests/test_data/test_pipelines/test_transform/test_shear.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,7 @@

from mmdet.core.mask import BitmapMasks, PolygonMasks
from mmdet.datasets.builder import PIPELINES


def construct_toy_data(poly2mask=True):
img = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=np.uint8)
img = np.stack([img, img, img], axis=-1)
results = dict()
# image
results['img'] = img
results['img_shape'] = img.shape
results['img_fields'] = ['img']
# bboxes
results['bbox_fields'] = ['gt_bboxes', 'gt_bboxes_ignore']
results['gt_bboxes'] = np.array([[0., 0., 2., 1.]], dtype=np.float32)
results['gt_bboxes_ignore'] = np.array([[2., 0., 3., 1.]],
dtype=np.float32)
# labels
results['gt_labels'] = np.array([1], dtype=np.int64)
# masks
results['mask_fields'] = ['gt_masks']
if poly2mask:
gt_masks = np.array([[0, 1, 1, 0], [0, 1, 0, 0]],
dtype=np.uint8)[None, :, :]
results['gt_masks'] = BitmapMasks(gt_masks, 2, 4)
else:
raw_masks = [[np.array([1, 0, 2, 0, 2, 1, 1, 1], dtype=np.float)]]
results['gt_masks'] = PolygonMasks(raw_masks, 2, 4)

# segmentations
results['seg_fields'] = ['gt_semantic_seg']
results['gt_semantic_seg'] = img[..., 0]
return results


def _check_fields(results, results_sheared, keys):
for key in keys:
if isinstance(results[key], (BitmapMasks, PolygonMasks)):
assert np.equal(results[key].to_ndarray(),
results_sheared[key].to_ndarray()).all()
else:
assert np.equal(results[key], results_sheared[key]).all()


def check_shear(results, results_sheared):
# _check_keys(results, results_sheared)
# check image
_check_fields(results, results_sheared, results.get('img_fields', ['img']))
# check bboxes
_check_fields(results, results_sheared, results.get('bbox_fields', []))
# check masks
_check_fields(results, results_sheared, results.get('mask_fields', []))
# check segmentations
_check_fields(results, results_sheared, results.get('seg_fields', []))
# check gt_labels
if 'gt_labels' in results:
assert np.equal(results['gt_labels'],
results_sheared['gt_labels']).all()
from .utils import check_result_same, construct_toy_data


def test_shear():
Expand Down Expand Up @@ -94,7 +39,7 @@ def test_shear():
direction='horizontal')
shear_module = build_from_cfg(transform, PIPELINES)
results_wo_shear = shear_module(copy.deepcopy(results))
check_shear(results, results_wo_shear)
check_result_same(results, results_wo_shear)

# test case when no shear aug (level=0, direction='vertical')
transform = dict(
Expand All @@ -106,7 +51,7 @@ def test_shear():
direction='vertical')
shear_module = build_from_cfg(transform, PIPELINES)
results_wo_shear = shear_module(copy.deepcopy(results))
check_shear(results, results_wo_shear)
check_result_same(results, results_wo_shear)

# test case when no shear aug (prob<=0)
transform = dict(
Expand All @@ -117,7 +62,7 @@ def test_shear():
direction='vertical')
shear_module = build_from_cfg(transform, PIPELINES)
results_wo_shear = shear_module(copy.deepcopy(results))
check_shear(results, results_wo_shear)
check_result_same(results, results_wo_shear)

# test shear horizontally, magnitude=1
transform = dict(
Expand All @@ -143,14 +88,15 @@ def test_shear():
results_gt['gt_masks'] = BitmapMasks(gt_masks, 2, 4)
results_gt['gt_semantic_seg'] = np.array(
[[1, 2, 3, 4], [255, 5, 6, 7]], dtype=results['gt_semantic_seg'].dtype)
check_shear(results_gt, results_sheared)
check_result_same(results_gt, results_sheared)

# test PolygonMasks with shear horizontally, magnitude=1
results = construct_toy_data(poly2mask=False)
results_sheared = shear_module(copy.deepcopy(results))
gt_masks = [[np.array([1, 0, 2, 0, 3, 1, 2, 1], dtype=np.float)]]
print(results_sheared['gt_masks'])
gt_masks = [[np.array([0, 0, 2, 0, 3, 1, 1, 1], dtype=np.float)]]
results_gt['gt_masks'] = PolygonMasks(gt_masks, 2, 4)
check_shear(results_gt, results_sheared)
check_result_same(results_gt, results_sheared)

# test shear vertically, magnitude=-1
img_fill_val = 128
Expand Down Expand Up @@ -180,14 +126,14 @@ def test_shear():
results_gt['gt_semantic_seg'] = np.array(
[[1, 6, 255, 255], [5, 255, 255, 255]],
dtype=results['gt_semantic_seg'].dtype)
check_shear(results_gt, results_sheared)
check_result_same(results_gt, results_sheared)

# test PolygonMasks with shear vertically, magnitude=-1
results = construct_toy_data(poly2mask=False)
results_sheared = shear_module(copy.deepcopy(results))
gt_masks = [[np.array([1, 0, 2, 0, 2, 0, 1, 0], dtype=np.float)]]
gt_masks = [[np.array([0, 0, 2, 0, 2, 0, 0, 1], dtype=np.float)]]
results_gt['gt_masks'] = PolygonMasks(gt_masks, 2, 4)
check_shear(results_gt, results_sheared)
check_result_same(results_gt, results_sheared)

results = construct_toy_data()
# same mask for BitmapMasks and PolygonMasks
Expand All @@ -196,7 +142,7 @@ def test_shear():
4)
results['gt_bboxes'] = np.array([[1., 0., 2., 1.]], dtype=np.float32)
results_sheared_bitmap = shear_module(copy.deepcopy(results))
check_shear(results_sheared_bitmap, results_sheared)
check_result_same(results_sheared_bitmap, results_sheared)

# test AutoAugment equipped with Shear
policies = [[dict(type='Shear', level=10, prob=1.)]]
Expand Down

0 comments on commit 978aac9

Please sign in to comment.