Skip to content

Commit

Permalink
[Enhancement] Remove batch inference assertion (#3210)
Browse files Browse the repository at this point in the history
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

#3181
#2965
#2644
#1645
#1444
#1370
#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.
  • Loading branch information
xiexinch committed Jul 20, 2023
1 parent 20fa129 commit 857f854
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 3 additions & 3 deletions mmseg/models/data_preprocessor.py
Expand Up @@ -132,9 +132,9 @@ def forward(self, data: dict, training: bool = False) -> Dict[str, Any]:
inputs, data_samples = self.batch_augments(
inputs, data_samples)
else:
assert len(inputs) == 1, (
'Batch inference is not support currently, '
'as the image size might be different in a batch')
img_size = inputs[0].shape[1:]
assert all(input_.shape[1:] == img_size for input_ in inputs), \
'The image size in a batch should be the same.'
# pad images when testing
if self.test_cfg:
inputs, padded_samples = stack_batch(
Expand Down
8 changes: 7 additions & 1 deletion mmseg/models/segmentors/encoder_decoder.py
@@ -1,8 +1,10 @@
# Copyright (c) OpenMMLab. All rights reserved.
import logging
from typing import List, Optional

import torch.nn as nn
import torch.nn.functional as F
from mmengine.logging import print_log
from torch import Tensor

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

0 comments on commit 857f854

Please sign in to comment.