Skip to content

Commit

Permalink
support test with multi batch (#829)
Browse files Browse the repository at this point in the history
* support test with multi batch

* resolve comment
  • Loading branch information
RunningLeon authored Aug 1, 2022
1 parent 1a3841f commit 2cddae7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
23 changes: 23 additions & 0 deletions configs/mmpose/pose-detection_tensorrt_dynamic-256x192.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
_base_ = ['./pose-detection_static.py', '../_base_/backends/tensorrt.py']

onnx_config = dict(
input_shape=[192, 256],
dynamic_axes={
'input': {
0: 'batch',
},
'output': {
0: 'batch'
}
})

backend_config = dict(
common_config=dict(max_workspace_size=1 << 30),
model_inputs=[
dict(
input_shapes=dict(
input=dict(
min_shape=[1, 3, 256, 192],
opt_shape=[2, 3, 256, 192],
max_shape=[4, 3, 256, 192])))
])
2 changes: 2 additions & 0 deletions docs/en/02-how-to-run/how_to_evaluate_a_model.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ${MODEL_CFG} \
[--cfg-options ${CFG_OPTIONS}] \
[--metric-options ${METRIC_OPTIONS}]
[--log2file work_dirs/output.txt]
[--batch-size ${BATCH_SIZE}]
```

## Description of all arguments
Expand All @@ -42,6 +43,7 @@ ${MODEL_CFG} \
- `--metric-options`: Custom options for evaluation. The key-value pair in xxx=yyy
format will be kwargs for dataset.evaluate() function.
- `--log2file`: log evaluation results (and speed) to file.
- `--batch-size`: the batch size for inference, which would override `samples_per_gpu` in data config. Default is `1`. Note that not all models support `batch_size>1`.

\* Other arguments in `tools/test.py` are used for speed test. They have no concern with evaluation.

Expand Down
20 changes: 5 additions & 15 deletions mmdeploy/codebase/mmdet/deploy/mmdetection.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,12 @@ def build_dataset(dataset_cfg: Union[str, mmcv.Config],
from mmdet.datasets import build_dataset as build_dataset_mmdet
from mmdet.datasets import replace_ImageToTensor
assert dataset_type in dataset_cfg.data

data_cfg = dataset_cfg.data[dataset_type]
# in case the dataset is concatenated
if isinstance(data_cfg, dict):
data_cfg.test_mode = True
samples_per_gpu = data_cfg.get('samples_per_gpu', 1)
if samples_per_gpu > 1:
# Replace 'ImageToTensor' to 'DefaultFormatBundle'
data_cfg.pipeline = replace_ImageToTensor(data_cfg.pipeline)
elif isinstance(data_cfg, list):
for ds_cfg in data_cfg:
ds_cfg.test_mode = True
samples_per_gpu = max(
[ds_cfg.get('samples_per_gpu', 1) for ds_cfg in data_cfg])
if samples_per_gpu > 1:
for ds_cfg in data_cfg:
ds_cfg.pipeline = replace_ImageToTensor(ds_cfg.pipeline)
samples_per_gpu = dataset_cfg.data.get('samples_per_gpu', 1)
if samples_per_gpu > 1:
# Replace 'ImageToTensor' to 'DefaultFormatBundle'
data_cfg.pipeline = replace_ImageToTensor(data_cfg.pipeline)
dataset = build_dataset_mmdet(data_cfg)

return dataset
Expand Down
10 changes: 9 additions & 1 deletion tools/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def parse_args():
help='the interval between each log, require setting '
'speed-test first',
default=100)
parser.add_argument(
'--batch-size',
type=int,
default=1,
help='the batch size for test, would override `samples_per_gpu`'
'in data config.')
parser.add_argument(
'--uri',
action='store_true',
Expand Down Expand Up @@ -102,9 +108,11 @@ def main():
# prepare the dataset loader
dataset_type = 'test'
dataset = task_processor.build_dataset(model_cfg, dataset_type)
# override samples_per_gpu that used for training
model_cfg.data['samples_per_gpu'] = args.batch_size
data_loader = task_processor.build_dataloader(
dataset,
samples_per_gpu=1,
samples_per_gpu=model_cfg.data.samples_per_gpu,
workers_per_gpu=model_cfg.data.workers_per_gpu)

# load the model of the backend
Expand Down

0 comments on commit 2cddae7

Please sign in to comment.