Skip to content

Commit c63348a

Browse files
q.yaolvhan028
authored andcommitted
[Enhancement] MMRotate 1.x support (#1401)
* wip * update twostage detector support * fix unit test * sdk wip * comment * refactor export info * fix * support roi trans * update rotate.yml * clear model.py, support torch1.13
1 parent 555712b commit c63348a

File tree

66 files changed

+2115
-3525
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2115
-3525
lines changed

demo/python/rotated_object_detection.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ def main():
2222
args = parse_args()
2323

2424
img = cv2.imread(args.image_path)
25-
detector = RotatedDetector(
26-
model_path=args.model_path, device_name=args.device_name, device_id=0)
25+
detector = RotatedDetector(args.model_path, args.device_name, 0)
2726
rbboxes, labels = detector(img)
2827

2928
indices = [i for i in range(len(rbboxes))]

docs/en/03-benchmark/benchmark.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,7 +1775,7 @@ Users can directly test the performance through [how_to_evaluate_a_model.md](../
17751775
<td align="center">fp32</td>
17761776
</tr>
17771777
<tr>
1778-
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/tree/main/configs/rotated_retinanet/rotated_retinanet_obb_r50_fpn_1x_dota_le135.py">RotatedRetinaNet</a></td>
1778+
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/tree/1.x/configs/rotated_retinanet/rotated-retinanet-hbox-oc_r50_fpn_1x_dota.py">RotatedRetinaNet</a></td>
17791779
<td align="center">Rotated Detection</td>
17801780
<td align="center">DOTA-v1.0</td>
17811781
<td align="center">mAP</td>
@@ -1787,7 +1787,7 @@ Users can directly test the performance through [how_to_evaluate_a_model.md](../
17871787
<td align="center">-</td>
17881788
</tr>
17891789
<tr>
1790-
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/tree/main/configs/oriented_rcnn/oriented_rcnn_r50_fpn_1x_dota_le90.py">Oriented RCNN</a></td>
1790+
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/tree/1.x/configs/oriented_rcnn/oriented-rcnn-le90_r50_fpn_1x_dota.py">Oriented RCNN</a></td>
17911791
<td align="center">Rotated Detection</td>
17921792
<td align="center">DOTA-v1.0</td>
17931793
<td align="center">mAP</td>
@@ -1799,7 +1799,7 @@ Users can directly test the performance through [how_to_evaluate_a_model.md](../
17991799
<td align="center">-</td>
18001800
</tr>
18011801
<tr>
1802-
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/blob/main/configs/gliding_vertex/gliding_vertex_r50_fpn_1x_dota_le90.py">GlidingVertex</a></td>
1802+
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/blob/1.x/configs/gliding_vertex/gliding-vertex-rbox_r50_fpn_1x_dota.py">GlidingVertex</a></td>
18031803
<td align="center">Rotated Detection</td>
18041804
<td align="center">DOTA-v1.0</td>
18051805
<td align="center">mAP</td>
@@ -1811,7 +1811,7 @@ Users can directly test the performance through [how_to_evaluate_a_model.md](../
18111811
<td align="center">-</td>
18121812
</tr>
18131813
<tr>
1814-
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/blob/main/configs/roi_trans/roi_trans_r50_fpn_1x_dota_le90.py">RoI Transformer</a></td>
1814+
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/blob/1.x/configs/roi_trans/roi-trans-le90_r50_fpn_1x_dota.py">RoI Transformer</a></td>
18151815
<td align="center">Rotated Detection</td>
18161816
<td align="center">DOTA-v1.0</td>
18171817
<td align="center">mAP</td>
Lines changed: 160 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# MMRotate Deployment
22

3-
- [Installation](#installation)
4-
- [Install mmrotate](#install-mmrotate)
5-
- [Install mmdeploy](#install-mmdeploy)
6-
- [Convert model](#convert-model)
7-
- [Model specification](#model-specification)
8-
- [Model inference](#model-inference)
9-
- [Backend model inference](#backend-model-inference)
10-
- [SDK model inference](#sdk-model-inference)
11-
- [Supported models](#supported-models)
3+
- [MMRotate Deployment](#mmrotate-deployment)
4+
- [Installation](#installation)
5+
- [Install mmrotate](#install-mmrotate)
6+
- [Install mmdeploy](#install-mmdeploy)
7+
- [Convert model](#convert-model)
8+
- [Model specification](#model-specification)
9+
- [Model inference](#model-inference)
10+
- [Backend model inference](#backend-model-inference)
11+
- [SDK model inference](#sdk-model-inference)
12+
- [Supported models](#supported-models)
1213

1314
______________________________________________________________________
1415

@@ -18,16 +19,166 @@ ______________________________________________________________________
1819

1920
### Install mmrotate
2021

22+
Please follow the [installation guide](https://mmrotate.readthedocs.io/en/1.x/get_started.html) to install mmrotate.
23+
2124
### Install mmdeploy
2225

26+
There are several methods to install mmdeploy, among which you can choose an appropriate one according to your target platform and device.
27+
28+
**Method I:** Install precompiled package
29+
30+
> **TODO**. MMDeploy hasn't released based on dev-1.x branch.
31+
32+
**Method II:** Build using scripts
33+
34+
If your target platform is **Ubuntu 18.04 or later version**, we encourage you to run
35+
[scripts](../01-how-to-build/build_from_script.md). For example, the following commands install mmdeploy as well as inference engine - `ONNX Runtime`.
36+
37+
```shell
38+
git clone --recursive -b dev-1.x https://github.com/open-mmlab/mmdeploy.git
39+
cd mmdeploy
40+
python3 tools/scripts/build_ubuntu_x64_ort.py $(nproc)
41+
export PYTHONPATH=$(pwd)/build/lib:$PYTHONPATH
42+
export LD_LIBRARY_PATH=$(pwd)/../mmdeploy-dep/onnxruntime-linux-x64-1.8.1/lib/:$LD_LIBRARY_PATH
43+
```
44+
45+
**NOTE**:
46+
47+
- Adding `$(pwd)/build/lib` to `PYTHONPATH` is for importing mmdeploy SDK python module - `mmdeploy_python`, which will be presented in chapter [SDK model inference](#sdk-model-inference).
48+
- When [inference onnx model by ONNX Runtime](#backend-model-inference), it requests ONNX Runtime library be found. Thus, we add it to `LD_LIBRARY_PATH`.
49+
50+
**Method III:** Build from source
51+
52+
If neither **I** nor **II** meets your requirements, [building mmdeploy from source](../01-how-to-build/build_from_source.md) is the last option.
53+
2354
## Convert model
2455

56+
You can use [tools/deploy.py](https://github.com/open-mmlab/mmdeploy/blob/dev-1.x/tools/deploy.py) to convert mmrotate models to the specified backend models. Its detailed usage can be learned from [here](https://github.com/open-mmlab/mmdeploy/blob/master/docs/en/02-how-to-run/convert_model.md#usage).
57+
58+
The command below shows an example about converting `rotated-faster-rcnn` model to onnx model that can be inferred by ONNX Runtime.
59+
60+
```shell
61+
cd mmdeploy
62+
63+
# download rotated-faster-rcnn model from mmrotate model zoo
64+
mim download mmrotate --config rotated-faster-rcnn-le90_r50_fpn_1x_dota --dest .
65+
wget https://github.com/open-mmlab/mmrotate/raw/main/demo/dota_demo.jpg
66+
67+
# convert mmrotate model to onnxruntime model with dynamic shape
68+
python tools/deploy.py \
69+
configs/mmrotate/rotated-detection_onnxruntime_dynamic.py \
70+
rotated-faster-rcnn-le90_r50_fpn_1x_dota.py \
71+
rotated_faster_rcnn_r50_fpn_1x_dota_le90-0393aa5c.pth \
72+
dota_demo.jpg \
73+
--work-dir mmdeploy_models/mmrotate/ort \
74+
--device cpu \
75+
--show \
76+
--dump-info
77+
```
78+
79+
It is crucial to specify the correct deployment config during model conversion. We've already provided builtin deployment config [files](https://github.com/open-mmlab/mmdeploy/tree/dev-1.x/configs/mmrotate) of all supported backends for mmrotate. The config filename pattern is:
80+
81+
```
82+
rotated_detection-{backend}-{precision}_{static | dynamic}_{shape}.py
83+
```
84+
85+
- **{backend}:** inference backend, such as onnxruntime, tensorrt, pplnn, ncnn, openvino, coreml etc.
86+
- **{precision}:** fp16, int8. When it's empty, it means fp32
87+
- **{static | dynamic}:** static shape or dynamic shape
88+
- **{shape}:** input shape or shape range of a model
89+
90+
Therefore, in the above example, you can also convert `rotated-faster-rcnn` to other backend models by changing the deployment config file `rotated-detection_onnxruntime_dynamic` to [others](https://github.com/open-mmlab/mmdeploy/tree/dev-1.x/configs/mmrotate), e.g., converting to tensorrt-fp16 model by `rotated-detection_tensorrt-fp16_dynamic-320x320-1024x1024.py`.
91+
92+
```{tip}
93+
When converting mmrotate models to tensorrt models, --device should be set to "cuda"
94+
```
95+
2596
## Model specification
2697

98+
Before moving on to model inference chapter, let's know more about the converted model structure which is very important for model inference.
99+
100+
The converted model locates in the working directory like `mmdeploy_models/mmrotate/ort` in the previous example. It includes:
101+
102+
```
103+
mmdeploy_models/mmrotate/ort
104+
├── deploy.json
105+
├── detail.json
106+
├── end2end.onnx
107+
└── pipeline.json
108+
```
109+
110+
in which,
111+
112+
- **end2end.onnx**: backend model which can be inferred by ONNX Runtime
113+
- \***.json**: the necessary information for mmdeploy SDK
114+
115+
The whole package **mmdeploy_models/mmrotate/ort** is defined as **mmdeploy SDK model**, i.e., **mmdeploy SDK model** includes both backend model and inference meta information.
116+
27117
## Model inference
28118

29119
### Backend model inference
30120

121+
Take the previous converted `end2end.onnx` model as an example, you can use the following code to inference the model and visualize the results.
122+
123+
```python
124+
from mmdeploy.apis.utils import build_task_processor
125+
from mmdeploy.utils import get_input_shape, load_config
126+
import torch
127+
128+
deploy_cfg = 'configs/mmrotate/rotated-detection_onnxruntime_dynamic.py'
129+
model_cfg = './rotated-faster-rcnn-le90_r50_fpn_1x_dota.py'
130+
device = 'cpu'
131+
backend_model = ['./mmdeploy_models/mmrotate/ort/end2end.onnx']
132+
image = './dota_demo.jpg'
133+
134+
# read deploy_cfg and model_cfg
135+
deploy_cfg, model_cfg = load_config(deploy_cfg, model_cfg)
136+
137+
# build task and backend model
138+
task_processor = build_task_processor(model_cfg, deploy_cfg, device)
139+
model = task_processor.build_backend_model(backend_model)
140+
141+
# process input image
142+
input_shape = get_input_shape(deploy_cfg)
143+
model_inputs, _ = task_processor.create_input(image, input_shape)
144+
145+
# do model inference
146+
with torch.no_grad():
147+
result = model.test_step(model_inputs)
148+
149+
# visualize results
150+
task_processor.visualize(
151+
image=image,
152+
model=model,
153+
result=result[0],
154+
window_name='visualize',
155+
output_file='./output.png')
156+
```
157+
31158
### SDK model inference
32159

160+
You can also perform SDK model inference like following,
161+
162+
```python
163+
from mmdeploy_python import RotatedDetector
164+
import cv2
165+
import numpy as np
166+
167+
img = cv2.imread('./dota_demo.jpg')
168+
169+
# create a detector
170+
detector = RotatedDetector(model_path='./mmdeploy_models/mmrotate/ort', device_name='cpu', device_id=0)
171+
# perform inference
172+
det = detector(img)
173+
```
174+
175+
Besides python API, mmdeploy SDK also provides other FFI (Foreign Function Interface), such as C, C++, C#, Java and so on. You can learn their usage from [demos](https://github.com/open-mmlab/mmdeploy/tree/dev-1.x/demo).
176+
33177
## Supported models
178+
179+
| Model | OnnxRuntime | TensorRT |
180+
| :------------------------------------------------------------------------------------------------ | :---------: | :------: |
181+
| [Rotated RetinaNet](https://github.com/open-mmlab/mmrotate/blob/1.x/configs/rotated_retinanet) | Y | Y |
182+
| [Rotated FasterRCNN](https://github.com/open-mmlab/mmrotate/blob/1.x/configs/rotated_faster_rcnn) | Y | Y |
183+
| [Oriented R-CNN](https://github.com/open-mmlab/mmrotate/blob/1.x/configs/oriented_rcnn) | Y | Y |
184+
| [Gliding Vertex](https://github.com/open-mmlab/mmrotate/blob/1.x/configs/gliding_vertex) | Y | Y |

docs/zh_cn/03-benchmark/benchmark.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,7 @@ GPU: ncnn, TensorRT, PPLNN
17701770
<td align="center">fp32</td>
17711771
</tr>
17721772
<tr>
1773-
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/tree/main/configs/rotated_retinanet/rotated_retinanet_obb_r50_fpn_1x_dota_le135.py">RotatedRetinaNet</a></td>
1773+
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/tree/1.x/configs/rotated_retinanet/rotated-retinanet-hbox-oc_r50_fpn_1x_dota.py">RotatedRetinaNet</a></td>
17741774
<td align="center">Rotated Detection</td>
17751775
<td align="center">DOTA-v1.0</td>
17761776
<td align="center">mAP</td>
@@ -1782,7 +1782,7 @@ GPU: ncnn, TensorRT, PPLNN
17821782
<td align="center">-</td>
17831783
</tr>
17841784
<tr>
1785-
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/tree/main/configs/oriented_rcnn/oriented_rcnn_r50_fpn_1x_dota_le90.py">Oriented RCNN</a></td>
1785+
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/tree/1.x/configs/oriented_rcnn/oriented-rcnn-le90_r50_fpn_1x_dota.py">Oriented RCNN</a></td>
17861786
<td align="center">Rotated Detection</td>
17871787
<td align="center">DOTA-v1.0</td>
17881788
<td align="center">mAP</td>
@@ -1794,7 +1794,7 @@ GPU: ncnn, TensorRT, PPLNN
17941794
<td align="center">-</td>
17951795
</tr>
17961796
<tr>
1797-
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/blob/main/configs/gliding_vertex/gliding_vertex_r50_fpn_1x_dota_le90.py">GlidingVertex</a></td>
1797+
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/blob/1.x/configs/gliding_vertex/gliding-vertex-rbox_r50_fpn_1x_dota.py">GlidingVertex</a></td>
17981798
<td align="center">Rotated Detection</td>
17991799
<td align="center">DOTA-v1.0</td>
18001800
<td align="center">mAP</td>
@@ -1806,7 +1806,7 @@ GPU: ncnn, TensorRT, PPLNN
18061806
<td align="center">-</td>
18071807
</tr>
18081808
<tr>
1809-
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/blob/main/configs/roi_trans/roi_trans_r50_fpn_1x_dota_le90.py">RoI Transformer</a></td>
1809+
<td align="center"><a href="https://github.com/open-mmlab/mmrotate/blob/1.x/configs/roi_trans/roi-trans-le90_r50_fpn_1x_dota.py">RoI Transformer</a></td>
18101810
<td align="center">Rotated Detection</td>
18111811
<td align="center">DOTA-v1.0</td>
18121812
<td align="center">mAP</td>

0 commit comments

Comments
 (0)