Skip to content

Commit

Permalink
[Enhancement]: Update configs for OpenVINO (open-mmlab#306)
Browse files Browse the repository at this point in the history
* Update config for OpenVINO

* Update test

* Update better test

* Update instance-seg config of OpenVINO
  • Loading branch information
SingleZombie authored Dec 20, 2021
1 parent abdf64a commit 0f9f0a6
Show file tree
Hide file tree
Showing 22 changed files with 77 additions and 99 deletions.
6 changes: 6 additions & 0 deletions configs/mmcls/classification_openvino_dynamic-224x224.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_base_ = ['./classification_dynamic.py', '../_base_/backends/openvino.py']

onnx_config = dict(input_shape=None)

backend_config = dict(
model_inputs=[dict(opt_shapes=dict(input=[1, 3, 224, 224]))])
3 changes: 0 additions & 3 deletions configs/mmcls/classification_openvino_dynamic.py

This file was deleted.

3 changes: 0 additions & 3 deletions configs/mmcls/classification_openvino_static-224x224.py

This file was deleted.

6 changes: 6 additions & 0 deletions configs/mmdet/_base_/base_openvino_dynamic-800x1344.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_base_ = ['./base_dynamic.py', '../../_base_/backends/openvino.py']

onnx_config = dict(input_shape=None)

backend_config = dict(
model_inputs=[dict(opt_shapes=dict(input=[1, 3, 800, 1344]))])
3 changes: 0 additions & 3 deletions configs/mmdet/_base_/base_openvino_static-800x1344.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_base_ = ['../_base_/base_openvino_dynamic-800x1344.py']
3 changes: 0 additions & 3 deletions configs/mmdet/detection/detection_openvino_dynamic.py

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
'../../_base_/backends/openvino.py'
]

onnx_config = dict(input_shape=None)
backend_config = dict(
model_inputs=[dict(opt_shapes=dict(input=[1, 3, 800, 1344]))])
codebase_config = dict(post_processing=dict(export_postprocess_mask=False))
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
_base_ = ['./super-resolution_dynamic.py', '../../_base_/backends/openvino.py']

backend_config = dict(
model_inputs=[dict(opt_shapes=dict(input=[1, 3, 256, 256]))])

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_base_ = ['./text-detection_dynamic.py', '../../_base_/backends/openvino.py']

onnx_config = dict(input_shape=None)

backend_config = dict(
model_inputs=[dict(opt_shapes=dict(input=[1, 3, 640, 640]))])

This file was deleted.

This file was deleted.

4 changes: 4 additions & 0 deletions configs/mmseg/segmentation_openvino_dynamic-1024x2048.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
_base_ = ['./segmentation_dynamic.py', '../_base_/backends/openvino.py']

backend_config = dict(
model_inputs=[dict(opt_shapes=dict(input=[1, 3, 1024, 2048]))])
3 changes: 0 additions & 3 deletions configs/mmseg/segmentation_openvino_dynamic.py

This file was deleted.

3 changes: 0 additions & 3 deletions configs/mmseg/segmentation_openvino_static-1024x2048.py

This file was deleted.

26 changes: 13 additions & 13 deletions docs/codebases/mmedit.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ Please refer to [official installation guide](https://mmediting.readthedocs.io/e
## MMEditing models support

| Model | Task | ONNX Runtime | TensorRT | NCNN | PPLNN | OpenVINO | Model Config File |
| :---------- | :--------------- | :----------: | :------: | :---: | :-----: | :------: | :------------------------------------------------------------------------------------------- |
| SRCNN | super-resolution | Y | Y | Y | Y | N | $MMEDIT_DIR/configs/restorers/srcnn/srcnn_x4k915_g1_1000k_div2k.py |
| ESRGAN | super-resolution | Y | Y | Y | Y | N | $MMEDIT_DIR/configs/restorers/esrgan/esrgan_x4c64b23g32_g1_400k_div2k.py |
| ESRGAN | super-resolution | Y | Y | Y | Y | N | $MMEDIT_DIR/configs/restorers/esrgan/esrgan_psnr_x4c64b23g32_g1_1000k_div2k.py |
| SRGAN | super-resolution | Y | Y | Y | Y | N | $MMEDIT_DIR/configs/restorers/srresnet_srgan/srgan_x4c64b16_g1_1000k_div2k.py |
| SRResNet | super-resolution | Y | Y | Y | Y | N | $MMEDIT_DIR/configs/restorers/srresnet_srgan/srgan_x4c64b16_g1_1000k_div2k.py |
| Real-ESRGAN | super-resolution | Y | Y | Y | Y | N | $MMEDIT_DIR/configs/restorers/real_esrgan/realesrnet_c64b23g32_12x4_lr2e-4_1000k_df2k_ost.py |
| EDSR | super-resolution | Y | Y | Y | N | N | $MMEDIT_DIR/configs/restorers/edsr/edsr_x2c64b16_g1_300k_div2k.py |
| EDSR | super-resolution | Y | Y | Y | N | N | $MMEDIT_DIR/configs/restorers/edsr/edsr_x3c64b16_g1_300k_div2k.py |
| EDSR | super-resolution | Y | Y | Y | N | N | $MMEDIT_DIR/configs/restorers/edsr/edsr_x4c64b16_g1_300k_div2k.py |
| RDN | super-resolution | Y | Y | Y | Y | N | $MMEDIT_DIR/configs/restorers/rdn/rdn_x2c64b16_g1_1000k_div2k.py |
| RDN | super-resolution | Y | Y | Y | Y | N | $MMEDIT_DIR/configs/restorers/rdn/rdn_x3c64b16_g1_1000k_div2k.py |
| RDN | super-resolution | Y | Y | Y | Y | N | $MMEDIT_DIR/configs/restorers/rdn/rdn_x4c64b16_g1_1000k_div2k.py |
| :---------- | :--------------- | :----------: | :------: | :---: | :---: | :------: | :------------------------------------------------------------------------------------------- |
| SRCNN | super-resolution | Y | Y | Y | Y | Y | $MMEDIT_DIR/configs/restorers/srcnn/srcnn_x4k915_g1_1000k_div2k.py |
| ESRGAN | super-resolution | Y | Y | Y | Y | Y | $MMEDIT_DIR/configs/restorers/esrgan/esrgan_x4c64b23g32_g1_400k_div2k.py |
| ESRGAN | super-resolution | Y | Y | Y | Y | Y | $MMEDIT_DIR/configs/restorers/esrgan/esrgan_psnr_x4c64b23g32_g1_1000k_div2k.py |
| SRGAN | super-resolution | Y | Y | Y | Y | Y | $MMEDIT_DIR/configs/restorers/srresnet_srgan/srgan_x4c64b16_g1_1000k_div2k.py |
| SRResNet | super-resolution | Y | Y | Y | Y | Y | $MMEDIT_DIR/configs/restorers/srresnet_srgan/srgan_x4c64b16_g1_1000k_div2k.py |
| Real-ESRGAN | super-resolution | Y | Y | Y | Y | Y | $MMEDIT_DIR/configs/restorers/real_esrgan/realesrnet_c64b23g32_12x4_lr2e-4_1000k_df2k_ost.py |
| EDSR | super-resolution | Y | Y | Y | N | Y | $MMEDIT_DIR/configs/restorers/edsr/edsr_x2c64b16_g1_300k_div2k.py |
| EDSR | super-resolution | Y | Y | Y | N | Y | $MMEDIT_DIR/configs/restorers/edsr/edsr_x3c64b16_g1_300k_div2k.py |
| EDSR | super-resolution | Y | Y | Y | N | Y | $MMEDIT_DIR/configs/restorers/edsr/edsr_x4c64b16_g1_300k_div2k.py |
| RDN | super-resolution | Y | Y | Y | Y | Y | $MMEDIT_DIR/configs/restorers/rdn/rdn_x2c64b16_g1_1000k_div2k.py |
| RDN | super-resolution | Y | Y | Y | Y | Y | $MMEDIT_DIR/configs/restorers/rdn/rdn_x3c64b16_g1_1000k_div2k.py |
| RDN | super-resolution | Y | Y | Y | Y | Y | $MMEDIT_DIR/configs/restorers/rdn/rdn_x4c64b16_g1_1000k_div2k.py |

## Reminder

Expand Down
4 changes: 2 additions & 2 deletions mmdeploy/apis/openvino/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
if is_available():
from mmdeploy.backend.openvino.onnx2openvino \
import onnx2openvino, get_output_model_file
from .utils import get_input_shape_from_cfg
from .utils import get_input_info_from_cfg
__all__ += [
'onnx2openvino', 'get_output_model_file', 'get_input_shape_from_cfg'
'onnx2openvino', 'get_output_model_file', 'get_input_info_from_cfg'
]
40 changes: 11 additions & 29 deletions mmdeploy/apis/openvino/utils.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
# Copyright (c) OpenMMLab. All rights reserved.
from typing import List
from typing import Dict, List

import mmcv

from mmdeploy.utils import get_input_shape
from mmdeploy.utils import get_model_inputs


def get_input_shape_from_cfg(deploy_cfg: mmcv.Config,
model_cfg: mmcv.Config) -> List[int]:
"""Get the input shape from the configs for OpenVINO Model Optimizer. The
value from config 'deploy_cfg' has the highest priority, then 'model_cfg'.
If there is no input shape in configs, then the default value will be used.
def get_input_info_from_cfg(deploy_cfg: mmcv.Config) -> Dict[str, List]:
"""Get the input names and shapes from the configs for OpenVINO Model
Optimizer.
Args:
deploy_cfg (mmcv.Config): Deployment config.
model_cfg (mmcv.Config): Model config.
Returns:
List[int]: The input shape in [1, 3, H, W] format from config
or [1, 3, 800, 1344].
Dict[str, List]: A dict that stores the names and shapes of input.
"""
shape = [1, 3]
is_use_deploy_cfg = False
try:
input_shape = get_input_shape(deploy_cfg)
if input_shape is not None:
is_use_deploy_cfg = True
except KeyError:
is_use_deploy_cfg = False

if is_use_deploy_cfg:
shape += [input_shape[1], input_shape[0]]
else:
test_pipeline = model_cfg.get('test_pipeline', None)
if test_pipeline is not None:
img_scale = test_pipeline[1]['img_scale']
shape += [img_scale[1], img_scale[0]]
else:
shape += [800, 1344]
return shape
# The partition is not supported now. Set the id of model to 0.
model_inputs = get_model_inputs(deploy_cfg)[0]
input_info = model_inputs['opt_shapes']
return input_info
40 changes: 17 additions & 23 deletions tests/test_apis/test_onnx2openvino.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,23 @@ def test_can_not_run_onnx2openvino_without_mo():


@backend_checker(Backend.OPENVINO)
def test_get_input_shape_from_cfg():
from mmdeploy.apis.openvino import get_input_shape_from_cfg
def test_get_input_info_from_cfg():
from mmdeploy.apis.openvino import get_input_info_from_cfg

# Test with default value
deploy_cfg = mmcv.Config()
model_cfg = mmcv.Config()
input_shape = get_input_shape_from_cfg(deploy_cfg, model_cfg)
assert input_shape == [1, 3, 800, 1344], \
'The function returned a different default shape.'

# Test with model_cfg that contains the required data.
height, width = 800, 1200
model_cfg = mmcv.Config(
{'test_pipeline': [{}, {
'img_scale': (width, height)
}]})
input_shape = get_input_shape_from_cfg(deploy_cfg, model_cfg)
assert input_shape == [1, 3, height, width], \
'The shape in the model_cfg does not match the output shape.'

# Test with deploy_cfg that contains the required data.
with pytest.raises(KeyError):
get_input_info_from_cfg(deploy_cfg)

input_name = 'input'
height, width = 600, 1000
deploy_cfg = mmcv.Config({'onnx_config': {'input_shape': (width, height)}})
input_shape = get_input_shape_from_cfg(deploy_cfg, model_cfg)
assert input_shape == [1, 3, height, width], \
'The shape in the deploy_cfg does not match the output shape.'
expected_input_info = {input_name: [1, 3, height, width]}
deploy_cfg = mmcv.Config({
'backend_config': {
'model_inputs': [{
'opt_shapes': expected_input_info
}]
}
})
input_info = get_input_info_from_cfg(deploy_cfg)
assert input_info == expected_input_info, \
'The expected value of \'input_info\' does not match the received one.'
10 changes: 4 additions & 6 deletions tools/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,11 @@ def main():

from mmdeploy.apis.openvino import (onnx2openvino,
get_output_model_file,
get_input_shape_from_cfg)
get_input_info_from_cfg)
openvino_files = []
for onnx_path in onnx_files:
model_xml_path = get_output_model_file(onnx_path, args.work_dir)
input_name = deploy_cfg.onnx_config.input_names
input_shape = [get_input_shape_from_cfg(deploy_cfg, model_cfg)]
input_info = dict(zip(input_name, input_shape))
input_info = get_input_info_from_cfg(deploy_cfg)
output_names = deploy_cfg.onnx_config.output_names
create_process(
f'onnx2openvino with {onnx_path}',
Expand All @@ -257,8 +255,8 @@ def main():
for onnx_path in onnx_files:
algo_file = onnx_path.replace('.onnx', '.json')
model_inputs = get_model_inputs(deploy_cfg)
assert 'opt_shape' in model_inputs, 'expect opt_shape '
'in deploy config for pplnn'
assert 'opt_shape' in model_inputs, 'Expect opt_shape ' \
'in deploy config for PPLNN'
# PPLNN accepts only 1 input shape for optimization,
# may get changed in the future
input_shapes = [model_inputs.opt_shape]
Expand Down

0 comments on commit 0f9f0a6

Please sign in to comment.