Skip to content

Commit

Permalink
[Bugfix] test resize with pad_shape (open-mmlab#3421)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
okotaku authored Dec 7, 2023
1 parent 862a726 commit e4bfa6f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
10 changes: 9 additions & 1 deletion mmseg/models/decode_heads/decode_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions mmseg/models/decode_heads/san_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion projects/hssn/decode_head/sep_aspp_contrast_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit e4bfa6f

Please sign in to comment.