From 483beb71a9c9ea895abd785ece10242f1668f060 Mon Sep 17 00:00:00 2001 From: Jon Crall Date: Sat, 22 Feb 2020 23:59:39 -0500 Subject: [PATCH] Fix IOU assigners when ignore_of_thr > 0 and no pred boxes (#2135) * Fix IOU assigners when ignore_of_thr > 0 and no pred boxes * remove extra brakets * fix test linter --- .../bbox/assigners/approx_max_iou_assigner.py | 4 +-- mmdet/core/bbox/assigners/max_iou_assigner.py | 4 +-- tests/test_assigner.py | 36 +++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/mmdet/core/bbox/assigners/approx_max_iou_assigner.py b/mmdet/core/bbox/assigners/approx_max_iou_assigner.py index e7d3510a0ba..f157a6b4575 100644 --- a/mmdet/core/bbox/assigners/approx_max_iou_assigner.py +++ b/mmdet/core/bbox/assigners/approx_max_iou_assigner.py @@ -118,8 +118,8 @@ def assign(self, bboxes = squares[:, :4] - if (self.ignore_iof_thr > 0) and (gt_bboxes_ignore is not None) and ( - gt_bboxes_ignore.numel() > 0): + if (self.ignore_iof_thr > 0 and gt_bboxes_ignore is not None + and gt_bboxes_ignore.numel() > 0 and bboxes.numel() > 0): if self.ignore_wrt_candidates: ignore_overlaps = bbox_overlaps( bboxes, gt_bboxes_ignore, mode='iof') diff --git a/mmdet/core/bbox/assigners/max_iou_assigner.py b/mmdet/core/bbox/assigners/max_iou_assigner.py index 93ffc42cae1..87f9f7252bb 100644 --- a/mmdet/core/bbox/assigners/max_iou_assigner.py +++ b/mmdet/core/bbox/assigners/max_iou_assigner.py @@ -98,8 +98,8 @@ def assign(self, bboxes, gt_bboxes, gt_bboxes_ignore=None, gt_labels=None): bboxes = bboxes[:, :4] overlaps = bbox_overlaps(gt_bboxes, bboxes) - if (self.ignore_iof_thr > 0) and (gt_bboxes_ignore is not None) and ( - gt_bboxes_ignore.numel() > 0): + if (self.ignore_iof_thr > 0 and gt_bboxes_ignore is not None + and gt_bboxes_ignore.numel() > 0 and bboxes.numel() > 0): if self.ignore_wrt_candidates: ignore_overlaps = bbox_overlaps( bboxes, gt_bboxes_ignore, mode='iof') diff --git a/tests/test_assigner.py b/tests/test_assigner.py index 5348eaba3a3..cb783e83c00 100644 --- a/tests/test_assigner.py +++ b/tests/test_assigner.py @@ -112,6 +112,42 @@ def test_max_iou_assigner_with_empty_boxes(): assert assign_result.labels is None +def test_max_iou_assigner_with_empty_boxes_and_ignore(): + """ + Test corner case where an network might predict no boxes and ignore_iof_thr + is on + """ + self = MaxIoUAssigner( + pos_iou_thr=0.5, + neg_iou_thr=0.5, + ignore_iof_thr=0.5, + ) + bboxes = torch.empty((0, 4)) + gt_bboxes = torch.FloatTensor([ + [0, 0, 10, 9], + [0, 10, 10, 19], + ]) + gt_bboxes_ignore = torch.Tensor([ + [30, 30, 40, 40], + ]) + gt_labels = torch.LongTensor([2, 3]) + + # Test with gt_labels + assign_result = self.assign( + bboxes, + gt_bboxes, + gt_labels=gt_labels, + gt_bboxes_ignore=gt_bboxes_ignore) + assert len(assign_result.gt_inds) == 0 + assert tuple(assign_result.labels.shape) == (0, ) + + # Test without gt_labels + assign_result = self.assign( + bboxes, gt_bboxes, gt_labels=None, gt_bboxes_ignore=gt_bboxes_ignore) + assert len(assign_result.gt_inds) == 0 + assert assign_result.labels is None + + def test_max_iou_assigner_with_empty_boxes_and_gt(): """ Test corner case where an network might predict no boxes and no gt