Skip to content

Commit 71a1e6c

Browse files
authored
fix torch2onnx for mmdet3d (#2425)
* fix mmdet3d * fix * resolve comments * fix rtmdet ncnn * update docs
1 parent ec35b40 commit 71a1e6c

File tree

12 files changed

+64
-86
lines changed

12 files changed

+64
-86
lines changed

.github/md-link-config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
},
1616
{
1717
"pattern": "^http://localhost"
18+
},
19+
{
20+
"pattern": "^https://openmmlab.com"
1821
}
1922
],
2023
"httpHeaders": [

.github/workflows/regression-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ on:
1717
required: true
1818
description: 'Whether to start regression test on Linux x86_64'
1919
type: boolean
20-
default: false
20+
default: true
2121
test_windows:
2222
required: true
2323
description: 'Whether to start regression test on Windows'
@@ -52,7 +52,7 @@ on:
5252
required: false
5353
description: 'Do not change it unless you know what you are doing!'
5454
type: string
55-
default: 'https://e2e1-14-136-99-158.ngrok-free.app'
55+
default: 'https://4ec1-14-136-99-158.ngrok-free.app'
5656

5757
concurrency:
5858
group: ${{ github.workflow }}-${{ github.ref }}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
_base_ = ['../_base_/base_static.py', '../../_base_/backends/ncnn.py']
2+
3+
codebase_config = dict(model_type='ncnn_end2end')
4+
onnx_config = dict(output_names=['detection_output'], input_shape=[640, 640])

docs/en/04-supported-codebases/mmdet3d.md

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,28 @@ ______________________________________________________________________
1212

1313
## Install mmdet3d
1414

15-
These branches are required for mmdet3d deployment
16-
17-
| codebase | commit |
18-
| :------: | :-------: |
19-
| mmdet3d | v1.1.0rc1 |
20-
| mmcv | v2.0.0rc1 |
21-
| mmdet | v3.0.0rc1 |
22-
| mmseg | v1.0.0rc0 |
23-
24-
First checkout and install mmcv/mmdet/mmseg/mmdet3d
25-
26-
```bash
27-
python3 -m pip install openmim --user
28-
python3 -m mim install mmcv==2.0.0rc1 mmdet==3.0.0rc1 mmseg==1.0.0rc0 --user
29-
30-
git clone https://github.com/open-mmlab/mmdetection3d --branch v1.1.0rc1
31-
cd mmdetection3d
32-
python3 -m pip install .
33-
cd -
34-
```
35-
36-
After installation, `tools/check_env.py` should display mmdet3d version normally
15+
We could install mmdet3d through [mim](https://github.com/open-mmlab/mim).
16+
For other ways of installation, please refer to [here](https://mmdetection3d.readthedocs.io/en/latest/get_started.html#installation)
3717

3818
```bash
39-
python3 tools/check_env.py
40-
..
41-
11/11 13:56:19 - mmengine - INFO - **********Codebase information**********
42-
11/11 13:56:19 - mmengine - INFO - mmdet: 3.0.0rc1
43-
11/11 13:56:19 - mmengine - INFO - mmseg: 1.0.0rc0
44-
..
45-
11/11 13:56:19 - mmengine - INFO - mmdet3d: 1.1.0rc1
19+
python3 -m pip install -U openmim
20+
python3 -m mim install "mmdet3d>=1.1.0"
4621
```
4722

4823
## Convert model
4924

5025
For example, use `tools/deploy.py` to convert centerpoint to onnxruntime format
5126

5227
```bash
53-
export MODEL_CONFIG=/path/to/mmdetection3d/configs/centerpoint/centerpoint_pillar02_second_secfpn_head-circlenms_8xb4-cyclic-20e_nus-3d.py
28+
# cd to mmdeploy root directory
29+
# download config and model
30+
mim download mmdet3d --config centerpoint_pillar02_second_secfpn_head-circlenms_8xb4-cyclic-20e_nus-3d --dest .
31+
32+
export MODEL_CONFIG=centerpoint_pillar02_second_secfpn_head-circlenms_8xb4-cyclic-20e_nus-3d.py
5433

55-
export MODEL_PATH=https://download.openmmlab.com/mmdetection3d/v1.0.0_models/centerpoint/centerpoint_02pillar_second_secfpn_circlenms_4x8_cyclic_20e_nus/centerpoint_02pillar_second_secfpn_circlenms_4x8_cyclic_20e_nus_20210816_064624-0f3299c0.pth
34+
export MODEL_PATH=centerpoint_02pillar_second_secfpn_circlenms_4x8_cyclic_20e_nus_20220811_031844-191a3822.pth
5635

57-
export TEST_DATA=/path/to/mmdetection3d/tests/data/nuscenes/sweeps/LIDAR_TOP/n008-2018-09-18-12-07-26-0400__LIDAR_TOP__1537287083900561.pcd.bin
36+
export TEST_DATA=tests/data/n008-2018-08-01-15-16-36-0400__LIDAR_TOP__1533151612397179.pcd.bin
5837

5938
python3 tools/deploy.py configs/mmdet3d/voxel-detection/voxel-detection_onnxruntime_dynamic.py $MODEL_CONFIG $MODEL_PATH $TEST_DATA --work-dir centerpoint
6039
```
@@ -82,4 +61,4 @@ The caller needs to refer to the corresponding [python implementation](../../../
8261
| [pointpillars](https://github.com/open-mmlab/mmdetection3d/blob/main/configs/pointpillars/pointpillars_hv_secfpn_8xb6-160e_kitti-3d-3class.py) | voxel detection | KITTI | ✔️ | ✔️ | ✔️ |
8362
| [smoke](https://github.com/open-mmlab/mmdetection3d/blob/main/configs/smoke/smoke_dla34_dlaneck_gn-all_4xb8-6x_kitti-mono3d.py) | monocular detection | KITTI | ✔️ | x | ✔️ |
8463

85-
- Make sure trt >= 8.4 for some bug fixed, such as ScatterND, dynamic shape crash and so on.
64+
- Make sure trt >= 8.6 for some bug fixed, such as ScatterND, dynamic shape crash and so on.

docs/zh_cn/04-supported-codebases/mmdet3d.md

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,28 @@ ______________________________________________________________________
1212

1313
## 安装 mmdet3d
1414

15-
因为依赖的 codebase 不在 master 分支,所以要切到相应分支:
16-
17-
| codebase | commit |
18-
| :------: | :-------: |
19-
| mmdet3d | v1.1.0rc1 |
20-
| mmcv | v2.0.0rc1 |
21-
| mmdet | v3.0.0rc1 |
22-
| mmseg | v1.0.0rc0 |
23-
24-
先安装前置依赖 mmcv/mmdet/mmseg,再安装 mmdet3d
25-
26-
```bash
27-
python3 -m pip install openmim --user
28-
python3 -m mim install mmcv==2.0.0rc1 mmdet==3.0.0rc1 mmseg==1.0.0rc0 --user
29-
30-
git clone https://github.com/open-mmlab/mmdetection3d --branch v1.1.0rc1
31-
cd mmdetection3d
32-
python3 -m pip install .
33-
cd -
34-
```
35-
36-
成功后 `tools/check_env.py` 应能正常显示 mmdet3d 版本号。
15+
我们可以通过 [mim](https://github.com/open-mmlab/mim) 来安装 mmdet3d.
16+
更多安装方式可参考该[文档](https://mmdetection3d.readthedocs.io/en/latest/get_started.html#installation)
3717

3818
```bash
39-
python3 tools/check_env.py
40-
..
41-
11/11 13:56:19 - mmengine - INFO - **********Codebase information**********
42-
11/11 13:56:19 - mmengine - INFO - mmdet: 3.0.0rc1
43-
11/11 13:56:19 - mmengine - INFO - mmseg: 1.0.0rc0
44-
..
45-
11/11 13:56:19 - mmengine - INFO - mmdet3d: 1.1.0rc1
19+
python3 -m pip install -U openmim
20+
python3 -m mim install "mmdet3d>=1.1.0"
4621
```
4722

4823
## 模型转换
4924

5025
使用 `tools/deploy.py` 把 mmdet3d 转到相应后端,以 centerpoint onnxruntime 为例:
5126

5227
```bash
53-
export MODEL_CONFIG=/path/to/mmdetection3d/configs/centerpoint/centerpoint_pillar02_second_secfpn_head-circlenms_8xb4-cyclic-20e_nus-3d.py
28+
# 切换到 mmdeploy 根目录
29+
# 通过mim下载centerpoint模型
30+
mim download mmdet3d --config centerpoint_pillar02_second_secfpn_head-circlenms_8xb4-cyclic-20e_nus-3d --dest .
31+
32+
export MODEL_CONFIG=centerpoint_pillar02_second_secfpn_head-circlenms_8xb4-cyclic-20e_nus-3d.py
5433

55-
export MODEL_PATH=https://download.openmmlab.com/mmdetection3d/v1.0.0_models/centerpoint/centerpoint_02pillar_second_secfpn_circlenms_4x8_cyclic_20e_nus/centerpoint_02pillar_second_secfpn_circlenms_4x8_cyclic_20e_nus_20210816_064624-0f3299c0.pth
34+
export MODEL_PATH=centerpoint_02pillar_second_secfpn_circlenms_4x8_cyclic_20e_nus_20220811_031844-191a3822.pth
5635

57-
export TEST_DATA=/path/to/mmdetection3d/tests/data/nuscenes/sweeps/LIDAR_TOP/n008-2018-09-18-12-07-26-0400__LIDAR_TOP__1537287083900561.pcd.bin
36+
export TEST_DATA=tests/data/n008-2018-08-01-15-16-36-0400__LIDAR_TOP__1533151612397179.pcd.bin
5837

5938
python3 tools/deploy.py configs/mmdet3d/voxel-detection/voxel-detection_onnxruntime_dynamic.py $MODEL_CONFIG $MODEL_PATH $TEST_DATA --work-dir centerpoint
6039
```
@@ -80,4 +59,4 @@ ls -lah centerpoint
8059
| [pointpillars](https://github.com/open-mmlab/mmdetection3d/blob/main/configs/pointpillars/pointpillars_hv_secfpn_8xb6-160e_kitti-3d-3class.py) | voxel detection | KITTI | ✔️ | ✔️ | ✔️ |
8160
| [smoke](https://github.com/open-mmlab/mmdetection3d/blob/main/configs/smoke/smoke_dla34_dlaneck_gn-all_4xb8-6x_kitti-mono3d.py) | monocular detection | KITTI | ✔️ | x | ✔️ |
8261

83-
- 考虑到 ScatterND、动态 shape 等已知问题,请确保 trt >= 8.4
62+
- 考虑到 ScatterND、动态 shape 等已知问题,请确保 trt >= 8.6

mmdeploy/codebase/mmdet/deploy/object_detection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ def process_model_config(model_cfg: Config,
7070
if transform.type == 'Resize':
7171
pipeline[i].keep_ratio = False
7272
pipeline[i].scale = tuple(input_shape)
73-
if transform.type in ('YOLOv5KeepRatioResize', 'LetterResize'):
73+
elif transform.type in ('YOLOv5KeepRatioResize', 'LetterResize'):
7474
pipeline[i].scale = tuple(input_shape)
75+
elif transform.type == 'Pad' and 'size' in transform:
76+
pipeline[i].size = tuple(input_shape)
7577

7678
pipeline = [
7779
transform for transform in pipeline

mmdeploy/codebase/mmdet3d/deploy/mono_detection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def create_input(
124124

125125
if data_preprocessor is not None:
126126
collate_data = data_preprocessor(collate_data, False)
127-
inputs = collate_data['inputs']
127+
assert 'inputs' in collate_data
128+
inputs = collate_data['inputs']['imgs']
128129
else:
129130
inputs = collate_data['inputs']
130131
return collate_data, inputs

mmdeploy/codebase/mmdet3d/models/base.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
'mmdet3d.models.detectors.Base3DDetector.forward' # noqa: E501
1111
)
1212
def basedetector__forward(self,
13-
inputs: list,
13+
voxels: torch.Tensor,
14+
num_points: torch.Tensor,
15+
coors: torch.Tensor,
1416
data_samples=None,
1517
**kwargs) -> Tuple[List[torch.Tensor]]:
1618
"""Extract features of images."""
1719

1820
batch_inputs_dict = {
1921
'voxels': {
20-
'voxels': inputs[0],
21-
'num_points': inputs[1],
22-
'coors': inputs[2]
22+
'voxels': voxels,
23+
'num_points': num_points,
24+
'coors': coors
2325
}
2426
}
2527
return self._forward(batch_inputs_dict, data_samples, **kwargs)

mmdeploy/codebase/mmdet3d/models/mvx_two_stage.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ def mvxtwostagedetector__extract_feat(self, batch_inputs_dict: dict) -> tuple:
4646

4747
@FUNCTION_REWRITER.register_rewriter(
4848
'mmdet3d.models.detectors.mvx_two_stage.MVXTwoStageDetector.forward')
49-
def mvxtwostagedetector__forward(self, inputs: list, **kwargs):
49+
def mvxtwostagedetector__forward(self, voxels: torch.Tensor,
50+
num_points: torch.Tensor, coors: torch.Tensor,
51+
**kwargs):
5052
"""Rewrite this func to remove voxelize op.
5153
5254
Args:
53-
inputs (list): input list comprises voxels, num_points and coors
55+
voxels (Tensor): input voxels
56+
num_points (Tensor): input num_points
57+
coors (Tensor): input coors
5458
5559
Returns:
5660
tuple: A tuple of classification scores, bbox and direction
@@ -70,9 +74,9 @@ def mvxtwostagedetector__forward(self, inputs: list, **kwargs):
7074
deploy_cfg = ctx.cfg
7175
batch_inputs_dict = {
7276
'voxels': {
73-
'voxels': inputs[0],
74-
'num_points': inputs[1],
75-
'coors': inputs[2]
77+
'voxels': voxels,
78+
'num_points': num_points,
79+
'coors': coors
7680
}
7781
}
7882

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# Copyright (c) OpenMMLab. All rights reserved.
2+
from torch import Tensor
3+
24
from mmdeploy.core import FUNCTION_REWRITER
35

46

57
@FUNCTION_REWRITER.register_rewriter(
68
'mmdet3d.models.detectors.single_stage_mono3d.'
79
'SingleStageMono3DDetector.forward')
8-
def singlestagemono3ddetector__forward(self, inputs: list, **kwargs):
9-
"""Rewrite this func to r.
10+
def singlestagemono3ddetector__forward(self, inputs: Tensor, **kwargs):
11+
"""Rewrite to support feed inputs of Tensor type.
1012
1113
Args:
12-
inputs (dict): Input dict comprises `imgs`
14+
inputs (Tensor): Input image
1315
1416
Returns:
1517
list: two torch.Tensor
1618
"""
17-
x = self.extract_feat(inputs)
19+
20+
x = self.extract_feat({'imgs': inputs})
1821
results = self.bbox_head.forward(x)
1922
return results[0], results[1]

0 commit comments

Comments
 (0)