Skip to content

Commit

Permalink
[Fix] FilterAnnotations bug (#8136)
Browse files Browse the repository at this point in the history
* Bug fix: #8131

* The better solution from chhluo

* #8136

since we changed the filtering type from mask to indexes, we can no longer use ```keep.any()``` to check, because it does not work at index zero.
But we can use

```
if keep.size == 0:
             if self.keep_empty:
                 return None
```

* add a unit test for filtering PolygonMasks

* W293 blank line contains whitespace

Co-authored-by: Wenwei Zhang <40779233+ZwwWayne@users.noreply.github.com>
Co-authored-by: m.stepanov <m.stepanov@redmadrobot.com>
  • Loading branch information
3 people committed Jun 14, 2022
1 parent ee79605 commit 2b05497
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
18 changes: 10 additions & 8 deletions mmdet/datasets/pipelines/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def __repr__(self):
repr_str += f'with_mask={self.with_mask}, '
repr_str += f'with_seg={self.with_seg}, '
repr_str += f'poly2mask={self.poly2mask}, '
repr_str += f'poly2mask={self.file_client_args})'
repr_str += f'file_client_args={self.file_client_args})'
return repr_str


Expand Down Expand Up @@ -564,7 +564,7 @@ def __call__(self, results):

def __repr__(self):
return self.__class__.__name__ + \
f'(num_max_proposals={self.num_max_proposals})'
f'(num_max_proposals={self.num_max_proposals})'


@PIPELINES.register_module()
Expand Down Expand Up @@ -625,19 +625,21 @@ def __call__(self, results):
for t in tests[1:]:
keep = keep & t

keep = keep.nonzero()[0]

keys = ('gt_bboxes', 'gt_labels', 'gt_masks')
for key in keys:
if key in results:
results[key] = results[key][keep]
if not keep.any():
if keep.size == 0:
if self.keep_empty:
return None
return results

def __repr__(self):
return self.__class__.__name__ + \
f'(min_gt_bbox_wh={self.min_gt_bbox_wh},' \
f'(min_gt_mask_area={self.min_gt_mask_area},' \
f'(by_box={self.by_box},' \
f'(by_mask={self.by_mask},' \
f'always_keep={self.always_keep})'
f'(min_gt_bbox_wh={self.min_gt_bbox_wh},' \
f'min_gt_mask_area={self.min_gt_mask_area},' \
f'by_box={self.by_box},' \
f'by_mask={self.by_mask},' \
f'always_keep={self.always_keep})'
14 changes: 13 additions & 1 deletion tests/test_data/test_pipelines/test_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import pytest

from mmdet.core.mask import BitmapMasks
from mmdet.core.mask import BitmapMasks, PolygonMasks
from mmdet.datasets.pipelines import (FilterAnnotations, LoadImageFromFile,
LoadImageFromWebcam,
LoadMultiChannelImageFromFiles)
Expand Down Expand Up @@ -118,3 +118,15 @@ def test_filter_annotations(target, kwargs):
if results is not None:
results = results['gt_bboxes'].shape[0]
assert results == target

polygons = [[np.array([2.0, 10.0, 4.0, 10.0, 4.0, 14.0, 2.0, 14.0])],
[np.array([2.0, 10.0, 2.1, 10.0, 2.1, 10.1, 2.0, 10.1])]]
polygon_masks = PolygonMasks(polygons, 24, 24)

results = dict(gt_bboxes=bboxes, gt_masks=polygon_masks)
results = filter_ann(results)

if results is not None:
results = len(results.get('gt_masks').masks)

assert results == target

0 comments on commit 2b05497

Please sign in to comment.