Skip to content

Commit

Permalink
Add get_mo_command. (open-mmlab#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
SemyonBevzuk authored Dec 6, 2021
1 parent cc72c00 commit 2f6f6f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
5 changes: 3 additions & 2 deletions docs/backends/openvino.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ This tutorial is based on Linux systems like Ubuntu-18.04.

### Installation
It is recommended to create a virtual environment for the project.
1. Install [OpenVINO](https://docs.openvino.ai/2021.4/get_started.html). For example, you can install OpenVINO with [pip](https://pypi.org/project/openvino-dev/).
1. Install [OpenVINO](https://docs.openvino.ai/2021.4/get_started.html). It is recommended to use the installer or install using pip.
Installation example using [pip](https://pypi.org/project/openvino-dev/):
```bash
pip install openvino-dev
```
Expand Down Expand Up @@ -39,7 +40,7 @@ python tools/deploy.py \
tests/data/tiger.jpeg \
--work-dir ../deploy_result \
--device cpu \
--log-level INFO \
--log-level INFO
```

### List of supported models exportable to OpenVINO from MMDetection
Expand Down
44 changes: 28 additions & 16 deletions mmdeploy/backend/openvino/onnx2openvino.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,32 @@
import torch


def is_mo_available() -> bool:
"""Checking if OpenVINO Model Optimizer is available.
def get_mo_command() -> str:
"""Checks for possible commands to run Model Optimizer. The following
commands will be tested:
'mo.py' - if you installed OpenVINO using the installer.
'mo' - if you installed OpenVINO with pip.
Returns:
bool: True, if Model Optimizer is available, else - False.
str: Command to run Model Optimizer. If it is not available,
the empty string "" will be returned.
"""
is_available = True
try:
run('mo -h',
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
shell=True,
check=True)
except CalledProcessError:
is_available = False
return is_available
mo_command = ''
mo_commands = ['mo.py', 'mo']
for command in mo_commands:
is_available = True
try:
run(f'{command} -h',
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
shell=True,
check=True)
except CalledProcessError:
is_available = False
if is_available:
mo_command = command
return mo_command


def get_output_model_file(onnx_path: str, work_dir: str) -> str:
Expand Down Expand Up @@ -59,7 +69,9 @@ def onnx2openvino(input_info: Dict[str, Union[List[int], torch.Size]],
input_shapes = ','.join(str(list(elem)) for elem in input_info.values())
output = ','.join(output_names)

if not is_mo_available():
mo_command = get_mo_command()
is_mo_available = bool(mo_command)
if not is_mo_available:
raise RuntimeError(
'OpenVINO Model Optimizer is not found or configured improperly')

Expand All @@ -69,8 +81,8 @@ def onnx2openvino(input_info: Dict[str, Union[List[int], torch.Size]],
f'--input="{input_names}" ' \
f'--input_shape="{input_shapes}" ' \
f'--disable_fusing '
command = f'mo {mo_args}'
logging.info(f'Args for mo: {command}')
command = f'{mo_command} {mo_args}'
logging.info(f'Args for Model Optimizer: {command}')
mo_output = run(command, capture_output=True, shell=True, check=True)
logging.info(mo_output.stdout.decode())
logging.debug(mo_output.stderr.decode())
Expand Down

0 comments on commit 2f6f6f8

Please sign in to comment.