From c01ab3b1cb850eb6757447678888b0a089e4f4ff Mon Sep 17 00:00:00 2001 From: Wenwei Zhang <40779233+ZwwWayne@users.noreply.github.com> Date: Fri, 29 Nov 2019 11:22:12 +0800 Subject: [PATCH] fix scale factor bug (#1730) * fix scale factor bug * reformat * clean code --- mmdet/models/bbox_heads/bbox_head.py | 4 +++- mmdet/models/detectors/test_mixins.py | 5 +++++ mmdet/models/mask_heads/fcn_mask_head.py | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/mmdet/models/bbox_heads/bbox_head.py b/mmdet/models/bbox_heads/bbox_head.py index 6188fb8..2e983ff 100644 --- a/mmdet/models/bbox_heads/bbox_head.py +++ b/mmdet/models/bbox_heads/bbox_head.py @@ -155,7 +155,9 @@ class BBoxHead(nn.Module): if isinstance(scale_factor, float): bboxes /= scale_factor else: - bboxes /= torch.from_numpy(scale_factor).to(bboxes.device) + scale_factor = torch.from_numpy(scale_factor).to(bboxes.device) + bboxes = (bboxes.view(bboxes.size(0), -1, 4) / + scale_factor).view(bboxes.size()[0], -1) if cfg is None: return bboxes, scores diff --git a/mmdet/models/detectors/test_mixins.py b/mmdet/models/detectors/test_mixins.py index 1abd609..05b2081 100644 --- a/mmdet/models/detectors/test_mixins.py +++ b/mmdet/models/detectors/test_mixins.py @@ -1,3 +1,5 @@ +import torch + from mmdet.core import (bbox2roi, bbox_mapping, merge_aug_bboxes, merge_aug_masks, merge_aug_proposals, multiclass_nms) @@ -114,6 +116,9 @@ class MaskTestMixin(object): else: # if det_bboxes is rescaled to the original image size, we need to # rescale it back to the testing scale to obtain RoIs. + if rescale and not isinstance(scale_factor, float): + scale_factor = torch.from_numpy(scale_factor).to( + det_bboxes.device) _bboxes = ( det_bboxes[:, :4] * scale_factor if rescale else det_bboxes) mask_rois = bbox2roi([_bboxes]) diff --git a/mmdet/models/mask_heads/fcn_mask_head.py b/mmdet/models/mask_heads/fcn_mask_head.py index f25aa17..f9dafcd 100644 --- a/mmdet/models/mask_heads/fcn_mask_head.py +++ b/mmdet/models/mask_heads/fcn_mask_head.py @@ -159,6 +159,8 @@ class FCNMaskHead(nn.Module): scale_factor = 1.0 for i in range(bboxes.shape[0]): + if not isinstance(scale_factor, (float, np.ndarray)): + scale_factor = scale_factor.cpu().numpy() bbox = (bboxes[i, :] / scale_factor).astype(np.int32) label = labels[i] w = max(bbox[2] - bbox[0] + 1, 1) -- GitLab