Skip to content

Commit

Permalink
Support mmocr:dev-1.x (open-mmlab#904)
Browse files Browse the repository at this point in the history
* init

* update UT

* fix UT except SAR

* update to latest 2.0

* fix ncnn UT

* export info
  • Loading branch information
AllentDan authored Sep 1, 2022
1 parent 308e28f commit a2f8287
Show file tree
Hide file tree
Showing 83 changed files with 1,101 additions and 1,535 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ mmdeploy/backend/ncnn/onnx2ncnn

/mmdeploy-*

# OCR dicts
dicts
# snpe
grpc-cpp-plugin
service/snpe/grpc_cpp_plugin
2 changes: 1 addition & 1 deletion configs/mmocr/text-detection/text-detection_sdk_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

backend_config = dict(pipeline=[
dict(type='LoadImageFromFile'),
dict(type='Collect', keys=['img'], meta_keys=['filename', 'ori_shape'])
dict(type='PackTextDetInputs', meta_keys=['img_path', 'ori_shape'])
])
2 changes: 1 addition & 1 deletion mmdeploy/apis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) OpenMMLab. All rights reserved.

# mmcv dependency
# mmcv & mmengine dependency
try:
from .calibration import create_calib_input_data
from .extract_model import extract_model
Expand Down
10 changes: 5 additions & 5 deletions mmdeploy/apis/inference.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Copyright (c) OpenMMLab. All rights reserved.
from typing import Any, Sequence, Union

import mmcv
import mmengine
import numpy as np
import torch

from mmdeploy.utils import get_input_shape, load_config


def inference_model(model_cfg: Union[str, mmcv.Config],
deploy_cfg: Union[str, mmcv.Config],
def inference_model(model_cfg: Union[str, mmengine.Config],
deploy_cfg: Union[str, mmengine.Config],
backend_files: Sequence[str], img: Union[str, np.ndarray],
device: str) -> Any:
"""Run inference with PyTorch or backend model and show results.
Expand All @@ -27,8 +27,8 @@ def inference_model(model_cfg: Union[str, mmcv.Config],
backend_files, img, device)
Args:
model_cfg (str | mmcv.Config): Model config file or Config object.
deploy_cfg (str | mmcv.Config): Deployment config file or Config
model_cfg (str | mmengine.Config): Model config file or Config object.
deploy_cfg (str | mmengine.Config): Deployment config file or Config
object.
backend_files (Sequence[str]): Input backend model file(s).
img (str | np.ndarray): Input image file or numpy array for inference.
Expand Down
11 changes: 6 additions & 5 deletions mmdeploy/apis/openvino/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) OpenMMLab. All rights reserved.
from typing import Dict, List

import mmcv
import mmengine

from mmdeploy.backend.openvino import ModelOptimizerOptions
from mmdeploy.utils import get_model_inputs
Expand Down Expand Up @@ -30,12 +30,12 @@ def update_input_names(input_info: Dict[str, List],
return input_info


def get_input_info_from_cfg(deploy_cfg: mmcv.Config) -> Dict[str, List]:
def get_input_info_from_cfg(deploy_cfg: mmengine.Config) -> Dict[str, List]:
"""Get the input names and shapes from the configs for OpenVINO Model
Optimizer.
Args:
deploy_cfg (mmcv.Config): Deployment config.
deploy_cfg (mmengine.Config): Deployment config.
Returns:
Dict[str, List]: A dict that stores the names and shapes of input.
Expand All @@ -53,12 +53,13 @@ def get_input_info_from_cfg(deploy_cfg: mmcv.Config) -> Dict[str, List]:
return input_info


def get_mo_options_from_cfg(deploy_cfg: mmcv.Config) -> ModelOptimizerOptions:
def get_mo_options_from_cfg(
deploy_cfg: mmengine.Config) -> ModelOptimizerOptions:
"""Get additional parameters for the Model Optimizer from the deploy
config.
Args:
deploy_cfg (mmcv.Config): Deployment config.
deploy_cfg (mmengine.Config): Deployment config.
Returns:
ModelOptimizerOptions: A class that will contain additional arguments.
Expand Down
14 changes: 7 additions & 7 deletions mmdeploy/apis/pytorch2onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os.path as osp
from typing import Any, Optional, Union

import mmcv
import mmengine
import torch

from mmdeploy.apis.core.pipeline_manager import no_mp
Expand All @@ -16,8 +16,8 @@
def torch2onnx(img: Any,
work_dir: str,
save_file: str,
deploy_cfg: Union[str, mmcv.Config],
model_cfg: Union[str, mmcv.Config],
deploy_cfg: Union[str, mmengine.Config],
model_cfg: Union[str, mmengine.Config],
model_checkpoint: Optional[str] = None,
device: str = 'cuda:0'):
"""Convert PyTorch model to ONNX model.
Expand All @@ -42,16 +42,16 @@ def torch2onnx(img: Any,
converting model.
work_dir (str): A working directory to save files.
save_file (str): Filename to save onnx model.
deploy_cfg (str | mmcv.Config): Deployment config file or
deploy_cfg (str | mmengine.Config): Deployment config file or
Config object.
model_cfg (str | mmcv.Config): Model config file or Config object.
model_cfg (str | mmengine.Config): Model config file or Config object.
model_checkpoint (str): A checkpoint path of PyTorch model,
defaults to `None`.
device (str): A string specifying device type, defaults to 'cuda:0'.
"""
# load deploy_cfg if necessary
deploy_cfg, model_cfg = load_config(deploy_cfg, model_cfg)
mmcv.mkdir_or_exist(osp.abspath(work_dir))
mmengine.mkdir_or_exist(osp.abspath(work_dir))

input_shape = get_input_shape(deploy_cfg)

Expand All @@ -66,7 +66,7 @@ def torch2onnx(img: Any,
data_preprocessor=getattr(torch_model, 'data_preprocessor', None))
if not isinstance(model_inputs, torch.Tensor) and len(model_inputs) == 1:
model_inputs = model_inputs[0]
data_samples = data[1]
data_samples = data['data_samples']
patch_metas = {'data_samples': data_samples}
input_metas = {'data_samples': data_samples, 'mode': 'predict'}

Expand Down
12 changes: 6 additions & 6 deletions mmdeploy/apis/pytorch2torchscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os.path as osp
from typing import Any, Optional, Union

import mmcv
import mmengine
import torch

from mmdeploy.apis.core.pipeline_manager import PIPELINE_MANAGER, no_mp
Expand All @@ -14,8 +14,8 @@
def torch2torchscript(img: Any,
work_dir: str,
save_file: str,
deploy_cfg: Union[str, mmcv.Config],
model_cfg: Union[str, mmcv.Config],
deploy_cfg: Union[str, mmengine.Config],
model_cfg: Union[str, mmengine.Config],
model_checkpoint: Optional[str] = None,
device: str = 'cuda:0'):
"""Convert PyTorch model to torchscript model.
Expand All @@ -25,16 +25,16 @@ def torch2torchscript(img: Any,
converting model.
work_dir (str): A working directory to save files.
save_file (str): Filename to save torchscript model.
deploy_cfg (str | mmcv.Config): Deployment config file or
deploy_cfg (str | mmengine.Config): Deployment config file or
Config object.
model_cfg (str | mmcv.Config): Model config file or Config object.
model_cfg (str | mmengine.Config): Model config file or Config object.
model_checkpoint (str): A checkpoint path of PyTorch model,
defaults to `None`.
device (str): A string specifying device type, defaults to 'cuda:0'.
"""
# load deploy_cfg if necessary
deploy_cfg, model_cfg = load_config(deploy_cfg, model_cfg)
mmcv.mkdir_or_exist(osp.abspath(work_dir))
mmengine.mkdir_or_exist(osp.abspath(work_dir))

input_shape = get_input_shape(deploy_cfg)

Expand Down
6 changes: 3 additions & 3 deletions mmdeploy/apis/utils/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def create_calib_input_data(calib_file: str,
Args:
calib_file (str): Input calibration file.
deploy_cfg (str | mmcv.Config): Deployment config.
model_cfg (str | mmcv.Config): The model config.
deploy_cfg (str | mmengine.Config): Deployment config.
model_cfg (str | mmengine.Config): The model config.
model_checkpoint (str): PyTorch model checkpoint, defaults to `None`.
dataset_cfg (str | mmcv.Config): Dataset config, defaults to `None`
dataset_cfg (str | mmengine.Config): Dataset config, defaults to `None`
dataset_type (str): A string specifying dataset type, e.g.: 'test',
'val', defaults to 'val'.
device (str): Specifying the device to run on, defaults to 'cpu'.
Expand Down
15 changes: 8 additions & 7 deletions mmdeploy/apis/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Copyright (c) OpenMMLab. All rights reserved.
import mmcv
import mmengine

from mmdeploy.codebase import BaseTask, get_codebase_class, import_codebase
from mmdeploy.utils import get_codebase, get_task_type


def build_task_processor(model_cfg: mmcv.Config, deploy_cfg: mmcv.Config,
device: str) -> BaseTask:
def build_task_processor(model_cfg: mmengine.Config,
deploy_cfg: mmengine.Config, device: str) -> BaseTask:
"""Build a task processor to manage the deployment pipeline.
Args:
model_cfg (str | mmcv.Config): Model config file.
deploy_cfg (str | mmcv.Config): Deployment config file.
model_cfg (str | mmengine.Config): Model config file.
deploy_cfg (str | mmengine.Config): Deployment config file.
device (str): A string specifying device type.
Returns:
Expand All @@ -23,14 +23,15 @@ def build_task_processor(model_cfg: mmcv.Config, deploy_cfg: mmcv.Config,
return codebase.build_task_processor(model_cfg, deploy_cfg, device)


def get_predefined_partition_cfg(deploy_cfg: mmcv.Config, partition_type: str):
def get_predefined_partition_cfg(deploy_cfg: mmengine.Config,
partition_type: str):
"""Get the predefined partition config.
Notes:
Currently only support mmdet codebase.
Args:
deploy_cfg (mmcv.Config): use deploy config to get the codebase and
deploy_cfg (mmengine.Config): use deploy config to get the codebase and
task type.
partition_type (str): A string specifying partition type.
Expand Down
12 changes: 6 additions & 6 deletions mmdeploy/apis/visualize.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Copyright (c) OpenMMLab. All rights reserved.
from typing import Optional, Sequence, Union

import mmcv
import mmengine
import numpy as np
import torch

from mmdeploy.codebase import BaseTask
from mmdeploy.utils import Backend, get_backend, get_input_shape, load_config


def visualize_model(model_cfg: Union[str, mmcv.Config],
deploy_cfg: Union[str, mmcv.Config],
def visualize_model(model_cfg: Union[str, mmengine.Config],
deploy_cfg: Union[str, mmengine.Config],
model: Union[str, Sequence[str], BaseTask],
img: Union[str, np.ndarray],
device: str,
Expand All @@ -33,8 +33,8 @@ def visualize_model(model_cfg: Union[str, mmcv.Config],
img, device, show_result=True)
Args:
model_cfg (str | mmcv.Config): Model config file or Config object.
deploy_cfg (str | mmcv.Config): Deployment config file or Config
model_cfg (str | mmengine.Config): Model config file or Config object.
deploy_cfg (str | mmengine.Config): Deployment config file or Config
object.
model (str | list[str], BaseSubtask): Input model or file(s).
img (str | np.ndarray): Input image file or numpy array for inference.
Expand Down Expand Up @@ -69,7 +69,7 @@ def visualize_model(model_cfg: Union[str, mmcv.Config],

model_inputs, _ = task_processor.create_input(img, input_shape)
with torch.no_grad():
result = model.test_step([model_inputs])[0]
result = model.test_step(model_inputs)[0]

task_processor.visualize(
image=img,
Expand Down
2 changes: 1 addition & 1 deletion mmdeploy/backend/base/backend_wrapper_registry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
from mmcv.utils import Registry
from mmengine import Registry

from mmdeploy.utils.config_utils import Backend

Expand Down
4 changes: 2 additions & 2 deletions mmdeploy/backend/ncnn/quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from subprocess import call
from typing import List

import mmcv
import mmengine

from .init_plugins import get_ncnn2int8_path

Expand All @@ -19,7 +19,7 @@ def get_quant_model_file(onnx_path: str, work_dir: str) -> List[str]:
List[str]: The path to the files where the export result will be
located.
"""
mmcv.mkdir_or_exist(osp.abspath(work_dir))
mmengine.mkdir_or_exist(osp.abspath(work_dir))
base_name = osp.splitext(osp.split(onnx_path)[1])[0]
quant_onnx = osp.join(work_dir, base_name + '_quant.onnx')
quant_table = osp.join(work_dir, base_name + '.table')
Expand Down
4 changes: 2 additions & 2 deletions mmdeploy/backend/openvino/onnx2openvino.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from subprocess import PIPE, CalledProcessError, run
from typing import Dict, Optional, Sequence, Union

import mmcv
import mmengine
import onnx

from mmdeploy.utils import get_root_logger
Expand Down Expand Up @@ -50,7 +50,7 @@ def get_output_model_file(onnx_path: str, work_dir: str) -> str:
Returns:
str: The path to the file where the export result will be located.
"""
mmcv.mkdir_or_exist(osp.abspath(work_dir))
mmengine.mkdir_or_exist(osp.abspath(work_dir))
file_name = osp.splitext(osp.split(onnx_path)[1])[0]
model_xml = osp.join(work_dir, file_name + '.xml')
return model_xml
Expand Down
1 change: 1 addition & 0 deletions mmdeploy/backend/pplnn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def register_engines(device_id: int,
export_algo_file: str = None,
import_algo_file: str = None) -> List[pplnn.Engine]:
"""Register engines for pplnn runtime.
Args:
device_id (int): Specifying device index. `-1` for cpu.
disable_avx512 (bool): Whether to disable avx512 for x86.
Expand Down
1 change: 1 addition & 0 deletions mmdeploy/backend/pplnn/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self,
def forward(self, inputs: Dict[str,
torch.Tensor]) -> Dict[str, torch.Tensor]:
"""Run forward inference.
Args:
inputs (Dict[str, torch.Tensor]): Input name and tensor pairs.
Return:
Expand Down
Loading

0 comments on commit a2f8287

Please sign in to comment.