Skip to content

Commit 857f854

Browse files
authored
[Enhancement] Remove batch inference assertion (open-mmlab#3210)
Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers. ## Motivation open-mmlab#3181 open-mmlab#2965 open-mmlab#2644 open-mmlab#1645 open-mmlab#1444 open-mmlab#1370 open-mmlab#125 ## Modification Remove the assertion at data_preprocessor ## BC-breaking (Optional) Does the modification introduce changes that break the backward-compatibility of the downstream repos? If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR. ## Use cases (Optional) If this PR introduces a new feature, it is better to list some use cases here, and update the documentation. ## 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 20fa129 commit 857f854

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

mmseg/models/data_preprocessor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ def forward(self, data: dict, training: bool = False) -> Dict[str, Any]:
132132
inputs, data_samples = self.batch_augments(
133133
inputs, data_samples)
134134
else:
135-
assert len(inputs) == 1, (
136-
'Batch inference is not support currently, '
137-
'as the image size might be different in a batch')
135+
img_size = inputs[0].shape[1:]
136+
assert all(input_.shape[1:] == img_size for input_ in inputs), \
137+
'The image size in a batch should be the same.'
138138
# pad images when testing
139139
if self.test_cfg:
140140
inputs, padded_samples = stack_batch(

mmseg/models/segmentors/encoder_decoder.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Copyright (c) OpenMMLab. All rights reserved.
2+
import logging
23
from typing import List, Optional
34

45
import torch.nn as nn
56
import torch.nn.functional as F
7+
from mmengine.logging import print_log
68
from torch import Tensor
79

810
from mmseg.registry import MODELS
@@ -330,7 +332,11 @@ def inference(self, inputs: Tensor, batch_img_metas: List[dict]) -> Tensor:
330332
f'Only "slide" or "whole" test mode are supported, but got ' \
331333
f'{self.test_cfg["mode"]}.'
332334
ori_shape = batch_img_metas[0]['ori_shape']
333-
assert all(_['ori_shape'] == ori_shape for _ in batch_img_metas)
335+
if not all(_['ori_shape'] == ori_shape for _ in batch_img_metas):
336+
print_log(
337+
'Image shapes are different in the batch.',
338+
logger='current',
339+
level=logging.WARN)
334340
if self.test_cfg.mode == 'slide':
335341
seg_logit = self.slide_inference(inputs, batch_img_metas)
336342
else:

0 commit comments

Comments
 (0)