Skip to content

Commit 7451459

Browse files
authored
[Bugfix] test resize with pad_shape (open-mmlab#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.
1 parent 83bff18 commit 7451459

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

mmseg/models/decode_heads/decode_head.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,17 @@ def predict_by_feat(self, seg_logits: Tensor,
350350
Tensor: Outputs segmentation logits map.
351351
"""
352352

353+
if isinstance(batch_img_metas[0]['img_shape'], torch.Size):
354+
# slide inference
355+
size = batch_img_metas[0]['img_shape']
356+
elif 'pad_shape' in batch_img_metas[0]:
357+
size = batch_img_metas[0]['pad_shape'][:2]
358+
else:
359+
size = batch_img_metas[0]['img_shape']
360+
353361
seg_logits = resize(
354362
input=seg_logits,
355-
size=batch_img_metas[0]['img_shape'],
363+
size=size,
356364
mode='bilinear',
357365
align_corners=self.align_corners)
358366
return seg_logits

mmseg/models/decode_heads/san_head.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,11 @@ def predict_by_feat(self, seg_logits: List[Tensor],
586586
"""
587587
mask_pred = seg_logits[0]
588588
cls_score = seg_logits[1]
589-
if 'pad_shape' in batch_img_metas[0]:
590-
size = batch_img_metas[0]['pad_shape']
589+
if isinstance(batch_img_metas[0]['img_shape'], torch.Size):
590+
# slide inference
591+
size = batch_img_metas[0]['img_shape']
592+
elif 'pad_shape' in batch_img_metas[0]:
593+
size = batch_img_metas[0]['pad_shape'][:2]
591594
else:
592595
size = batch_img_metas[0]['img_shape']
593596
# upsample mask

projects/hssn/decode_head/sep_aspp_contrast_head.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,17 @@ def predict_by_feat(self, seg_logits: Tuple[Tensor],
127127
# elif seg_logit.size(1) == 144 # For Mapillary dataset, 124+16+4
128128
# unofficial repository not release mapillary until 2023/2/6
129129

130+
if isinstance(batch_img_metas[0]['img_shape'], torch.Size):
131+
# slide inference
132+
size = batch_img_metas[0]['img_shape']
133+
elif 'pad_shape' in batch_img_metas[0]:
134+
size = batch_img_metas[0]['pad_shape'][:2]
135+
else:
136+
size = batch_img_metas[0]['img_shape']
130137
seg_logit = seg_logit[:, :-hiera_num_classes]
131138
seg_logit = resize(
132139
input=seg_logit,
133-
size=batch_img_metas[0]['img_shape'],
140+
size=size,
134141
mode='bilinear',
135142
align_corners=self.align_corners)
136143

0 commit comments

Comments
 (0)