From 7451459bb86338bb3e4bcb9254cba48537db1712 Mon Sep 17 00:00:00 2001 From: takuoko Date: Thu, 7 Dec 2023 15:04:11 +0900 Subject: [PATCH] [Bugfix] test resize with pad_shape (#3421) ## Motivation When using `test_cfg` for `data_preprocessor`, `predict_by_feat` resizes to the original size, not the padded size. ``` data_preprocessor = dict( type="SegDataPreProcessor", #type="SegDataPreProcessorWithPad", mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], bgr_to_rgb=True, pad_val=0, seg_pad_val=255, test_cfg=dict(size=(128, 128))) ``` Refar to: https://github.com/open-mmlab/mmsegmentation/blob/main/mmseg/models/decode_heads/san_head.py#L589-L592 ## Checklist 1. Pre-commit or other linting tools are used to fix the potential lint issues. 2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness. 3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMDet3D. 4. The documentation has been modified accordingly, like docstring or example tutorials. --- mmseg/models/decode_heads/decode_head.py | 10 +++++++++- mmseg/models/decode_heads/san_head.py | 7 +++++-- projects/hssn/decode_head/sep_aspp_contrast_head.py | 9 ++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/mmseg/models/decode_heads/decode_head.py b/mmseg/models/decode_heads/decode_head.py index 4faf54559d..179d871fd1 100644 --- a/mmseg/models/decode_heads/decode_head.py +++ b/mmseg/models/decode_heads/decode_head.py @@ -350,9 +350,17 @@ def predict_by_feat(self, seg_logits: Tensor, Tensor: Outputs segmentation logits map. """ + if isinstance(batch_img_metas[0]['img_shape'], torch.Size): + # slide inference + size = batch_img_metas[0]['img_shape'] + elif 'pad_shape' in batch_img_metas[0]: + size = batch_img_metas[0]['pad_shape'][:2] + else: + size = batch_img_metas[0]['img_shape'] + seg_logits = resize( input=seg_logits, - size=batch_img_metas[0]['img_shape'], + size=size, mode='bilinear', align_corners=self.align_corners) return seg_logits diff --git a/mmseg/models/decode_heads/san_head.py b/mmseg/models/decode_heads/san_head.py index 03dedf2e49..d20da80192 100644 --- a/mmseg/models/decode_heads/san_head.py +++ b/mmseg/models/decode_heads/san_head.py @@ -586,8 +586,11 @@ def predict_by_feat(self, seg_logits: List[Tensor], """ mask_pred = seg_logits[0] cls_score = seg_logits[1] - if 'pad_shape' in batch_img_metas[0]: - size = batch_img_metas[0]['pad_shape'] + if isinstance(batch_img_metas[0]['img_shape'], torch.Size): + # slide inference + size = batch_img_metas[0]['img_shape'] + elif 'pad_shape' in batch_img_metas[0]: + size = batch_img_metas[0]['pad_shape'][:2] else: size = batch_img_metas[0]['img_shape'] # upsample mask diff --git a/projects/hssn/decode_head/sep_aspp_contrast_head.py b/projects/hssn/decode_head/sep_aspp_contrast_head.py index d1d087362c..331af30de4 100644 --- a/projects/hssn/decode_head/sep_aspp_contrast_head.py +++ b/projects/hssn/decode_head/sep_aspp_contrast_head.py @@ -127,10 +127,17 @@ def predict_by_feat(self, seg_logits: Tuple[Tensor], # elif seg_logit.size(1) == 144 # For Mapillary dataset, 124+16+4 # unofficial repository not release mapillary until 2023/2/6 + if isinstance(batch_img_metas[0]['img_shape'], torch.Size): + # slide inference + size = batch_img_metas[0]['img_shape'] + elif 'pad_shape' in batch_img_metas[0]: + size = batch_img_metas[0]['pad_shape'][:2] + else: + size = batch_img_metas[0]['img_shape'] seg_logit = seg_logit[:, :-hiera_num_classes] seg_logit = resize( input=seg_logit, - size=batch_img_metas[0]['img_shape'], + size=size, mode='bilinear', align_corners=self.align_corners)