Skip to content

Commit 00ca2a3

Browse files
SemyonBevzukRunningLeonhaofanwangVVssssskgrimoire
authored
[Enhancement] Additional arguments support for OpenVINO Model Optimizer (#178)
* Add mo args. * [Docs]: update docs and argument descriptions (#196) * bump version to v0.4.0 * update docs and argument descriptions * revert version change * fix unnecessary change of config for dynamic exportation (#199) * fix mmcls get classes (#215) * fix mmcls get classes * resolve comment * resolve comment * Add ModelOptimizerOptions. * Fix merge bugs. * Update mmpose.md (#224) * [Dostring]add example in apis docstring (#214) * add example in apis docstring * add backend example in docstring * rm blank line * Fixed get_mo_options_from_cfg args * fix l2norm test Co-authored-by: RunningLeon <[email protected]> Co-authored-by: Haofan Wang <[email protected]> Co-authored-by: VVsssssk <[email protected]> Co-authored-by: grimoire <[email protected]>
1 parent 987d48c commit 00ca2a3

File tree

18 files changed

+241
-15
lines changed

18 files changed

+241
-15
lines changed

docs/en/backends/openvino.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ Notes:
6363
the RoiAlign operation is replaced with the [ExperimentalDetectronROIFeatureExtractor](https://docs.openvinotoolkit.org/latest/openvino_docs_ops_detection_ExperimentalDetectronROIFeatureExtractor_6.html) operation in the ONNX graph.
6464
- Models "VFNet" and "Faster R-CNN + DCN" use the custom "DeformableConv2D" operation.
6565

66+
### Deployment config
67+
68+
With the deployment config, you can specify additional options for the Model Optimizer.
69+
To do this, add the necessary parameters to the `backend_config.mo_options` in the fields `args` (for parameters with values) and `flags` (for flags).
70+
71+
Example:
72+
```python
73+
backend_config = dict(
74+
mo_options=dict(
75+
args=dict({
76+
'--mean_values': [0, 0, 0],
77+
'--scale_values': [255, 255, 255],
78+
'--data_type': 'FP32',
79+
}),
80+
flags=['--disable_fusing'],
81+
)
82+
)
83+
```
84+
85+
Information about the possible parameters for the Model Optimizer can be found in the [documentation](https://docs.openvino.ai/latest/openvino_docs_MO_DG_prepare_model_convert_model_Converting_Model.html).
86+
87+
6688
### FAQs
6789

6890
- None

docs/en/codebases/mmpose.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Please refer to [official installation guide](https://mmpose.readthedocs.io/en/latest/install.html) to install the codebase.
88

9-
## MMEditing models support
9+
## MMPose models support
1010

1111
| Model | Task | ONNX Runtime | TensorRT | NCNN | PPLNN | OpenVINO | Model config |
1212
|:----------|:--------------|:------------:|:--------:|:----:|:-----:|:--------:|:-------------------------------------------------------------------------------------------:|

mmdeploy/apis/calibration.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ def create_calib_table(calib_file: str,
2121
**kwargs) -> None:
2222
"""Create calibration table.
2323
24+
Examples:
25+
>>> from mmdeploy.apis import create_calib_table
26+
>>> from mmdeploy.utils import get_calib_filename, load_config
27+
>>> deploy_cfg = 'configs/mmdet/detection/' \
28+
'detection_tensorrt-int8_dynamic-320x320-1344x1344.py'
29+
>>> deploy_cfg = load_config(deploy_cfg)[0]
30+
>>> calib_file = get_calib_filename(deploy_cfg)
31+
>>> model_cfg = 'mmdetection/configs/fcos/' \
32+
'fcos_r50_caffe_fpn_gn-head_1x_coco.py'
33+
>>> model_checkpoint = 'checkpoints/' \
34+
'fcos_r50_caffe_fpn_gn-head_1x_coco-821213aa.pth'
35+
>>> create_calib_table(calib_file, deploy_cfg, \
36+
model_cfg, model_checkpoint, device='cuda:0')
37+
2438
Args:
2539
calib_file (str): Input calibration file.
2640
deploy_cfg (str | mmcv.Config): Deployment config.

mmdeploy/apis/extract_model.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ def extract_model(model: Union[str, onnx.ModelProto],
2323
The sub-model is defined by the names of the input and output tensors
2424
exactly.
2525
26+
Examples:
27+
>>> from mmdeploy.apis import extract_model
28+
>>> model = 'work_dir/fastrcnn.onnx'
29+
>>> start = 'detector:input'
30+
>>> end = ['extract_feat:output', 'multiclass_nms[0]:input']
31+
>>> dynamic_axes = {
32+
'input': {
33+
0: 'batch',
34+
2: 'height',
35+
3: 'width'
36+
},
37+
'scores': {
38+
0: 'batch',
39+
1: 'num_boxes',
40+
},
41+
'boxes': {
42+
0: 'batch',
43+
1: 'num_boxes',
44+
}
45+
}
46+
>>> save_file = 'partition_model.onnx'
47+
>>> extract_model(model, start, end, dynamic_axes=dynamic_axes, \
48+
save_file=save_file)
49+
2650
Args:
2751
model (str | onnx.ModelProto): Input ONNX model to be extracted.
2852
start (str | Sequence[str]): Start marker(s) to extract.

mmdeploy/apis/inference.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ def inference_model(model_cfg: Union[str, mmcv.Config],
1414
device: str) -> Any:
1515
"""Run inference with PyTorch or backend model and show results.
1616
17+
Examples:
18+
>>> from mmdeploy.apis import inference_model
19+
>>> model_cfg = 'mmdetection/configs/fcos/' \
20+
'fcos_r50_caffe_fpn_gn-head_1x_coco.py'
21+
>>> deploy_cfg = 'configs/mmdet/detection/' \
22+
'detection_onnxruntime_dynamic.py'
23+
>>> backend_files = ['work_dir/fcos.onnx']
24+
>>> img = 'demo.jpg'
25+
>>> device = 'cpu'
26+
>>> model_output = inference_model(model_cfg, deploy_cfg, \
27+
backend_files, img, device)
28+
1729
Args:
1830
model_cfg (str | mmcv.Config): Model config file or Config object.
1931
deploy_cfg (str | mmcv.Config): Deployment config file or Config

mmdeploy/apis/openvino/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
if is_available():
77
from mmdeploy.backend.openvino.onnx2openvino import (get_output_model_file,
88
onnx2openvino)
9-
from .utils import get_input_info_from_cfg
9+
from .utils import get_input_info_from_cfg, get_mo_options_from_cfg
1010
__all__ += [
11-
'onnx2openvino', 'get_output_model_file', 'get_input_info_from_cfg'
11+
'onnx2openvino', 'get_output_model_file', 'get_input_info_from_cfg',
12+
'get_mo_options_from_cfg'
1213
]

mmdeploy/apis/openvino/utils.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
import mmcv
55

6+
from mmdeploy.backend.openvino import ModelOptimizerOptions
67
from mmdeploy.utils import get_model_inputs
7-
from mmdeploy.utils.config_utils import get_ir_config
8+
from mmdeploy.utils.config_utils import get_backend_config, get_ir_config
89

910

1011
def update_input_names(input_info: Dict[str, List],
@@ -50,3 +51,19 @@ def get_input_info_from_cfg(deploy_cfg: mmcv.Config) -> Dict[str, List]:
5051
input_info = dict(zip(input_names, input_info))
5152
input_info = update_input_names(input_info, input_names)
5253
return input_info
54+
55+
56+
def get_mo_options_from_cfg(deploy_cfg: mmcv.Config) -> ModelOptimizerOptions:
57+
"""Get additional parameters for the Model Optimizer from the deploy
58+
config.
59+
60+
Args:
61+
deploy_cfg (mmcv.Config): Deployment config.
62+
63+
Returns:
64+
ModelOptimizerOptions: A class that will contain additional arguments.
65+
"""
66+
backend_config = get_backend_config(deploy_cfg)
67+
mo_options = backend_config.get('mo_options', None)
68+
mo_options = ModelOptimizerOptions(mo_options)
69+
return mo_options

mmdeploy/apis/pytorch2onnx.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ def torch2onnx(img: Any,
6262
device: str = 'cuda:0'):
6363
"""Convert PyTorch model to ONNX model.
6464
65+
Examples:
66+
>>> from mmdeploy.apis import torch2onnx
67+
>>> img = 'demo.jpg'
68+
>>> work_dir = 'work_dir'
69+
>>> save_file = 'fcos.onnx'
70+
>>> deploy_cfg = 'configs/mmdet/detection/' \
71+
'detection_onnxruntime_dynamic.py'
72+
>>> model_cfg = 'mmdetection/configs/fcos/' \
73+
'fcos_r50_caffe_fpn_gn-head_1x_coco.py'
74+
>>> model_checkpoint = 'checkpoints/' \
75+
'fcos_r50_caffe_fpn_gn-head_1x_coco-821213aa.pth'
76+
>>> device = 'cpu'
77+
>>> torch2onnx(img, work_dir, save_file, deploy_cfg, \
78+
model_cfg, model_checkpoint, device)
79+
6580
Args:
6681
img (str | np.ndarray | torch.Tensor): Input image used to assist
6782
converting model.

mmdeploy/apis/visualize.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ def visualize_model(model_cfg: Union[str, mmcv.Config],
1919
show_result: bool = False):
2020
"""Run inference with PyTorch or backend model and show results.
2121
22+
Examples:
23+
>>> from mmdeploy.apis import visualize_model
24+
>>> model_cfg = 'mmdetection/configs/fcos/' \
25+
'fcos_r50_caffe_fpn_gn-head_1x_coco.py'
26+
>>> deploy_cfg = 'configs/mmdet/detection/' \
27+
'detection_onnxruntime_dynamic.py'
28+
>>> model = 'work_dir/fcos.onnx'
29+
>>> img = 'demo.jpg'
30+
>>> device = 'cpu'
31+
>>> visualize_model(model_cfg, deploy_cfg, model, \
32+
img, device, show_result=True)
33+
2234
Args:
2335
model_cfg (str | mmcv.Config): Model config file or Config object.
2436
deploy_cfg (str | mmcv.Config): Deployment config file or Config

mmdeploy/backend/ncnn/onnx2ncnn.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ def onnx2ncnn(onnx_path: str, save_param: str, save_bin: str):
3333
a executable program to convert the `.onnx` file to a `.param` file and
3434
a `.bin` file. The output files will save to work_dir.
3535
36+
Example:
37+
>>> from mmdeploy.backend.ncnn.onnx2ncnn import onnx2ncnn
38+
>>> onnx_path = 'work_dir/end2end.onnx'
39+
>>> save_param = 'work_dir/end2end.param'
40+
>>> save_bin = 'work_dir/end2end.bin'
41+
>>> onnx2ncnn(onnx_path, save_param, save_bin)
42+
3643
Args:
3744
onnx_path (str): The path of the onnx model.
3845
save_param (str): The path to save the output `.param` file.

0 commit comments

Comments
 (0)