Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Add a visualize script for inference result comparation. #2012

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions docs/en/02-how-to-run/useful_tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ And the output look like this:
+--------+------------+---------+
```

## visualize

This tool can be used to visualize model inference results in different backend.

### Usage

```bash
python tools/visualize.py \
--deploy-cfg {DEPLOY_CFG} \
--model-cfg {MODEL_CFG} \
--deploy-path {DEPLOY_PATH} \
--test-img {TEST_IMGS} \
RunningLeon marked this conversation as resolved.
Show resolved Hide resolved
--checkpoint {CHECKPOINTS} \
--save-dir {SAVE_DIR} \
--device {DEVICE}
```

## generate_md_table

This tool can be used to generate supported-backends markdown table.
Expand Down Expand Up @@ -244,3 +261,30 @@ And the output look like this:
| [SAR](https://github.com/open-mmlab/mmocr/tree/main/configs/textrecog/sar) | TextRecognition | Y | N | Y | N | N | N |
| [SATRN](https://github.com/open-mmlab/mmocr/tree/main/configs/textrecog/satrn) | TextRecognition | Y | Y | Y | N | N | N |
| [ABINet](https://github.com/open-mmlab/mmocr/tree/main/configs/textrecog/abinet) | TextRecognition | Y | Y | Y | N | N | N |

## visualize

This tool can be used to visualize model inference results in different backend.

### Usage

```bash
python tools/visualize.py \
--deploy-cfg {DEPLOY_CFG} \
--model-cfg {MODEL_CFG} \
--deploy-path {DEPLOY_PATH} \
--test-img {TEST_IMGS} \
--checkpoint {CHECKPOINTS} \
--save-dir {SAVE_DIR} \
--device {DEVICE}
```

### Description of all arguments

- `deploy-cfg` : The path of the deploy config file in MMDeploy codebase.
- `model-cfg` : The path of model config file in OpenMMLab codebase.
- `deploy-path` : The path of the model to be tested, if the backend contains multiple files, you can use it multiple times.
- `test-img` : The path of the images to be tested, you can use it multiple times.
- `checkpoint` : The path of the checkpoint to be tested, if it is used, the result will be cancated to right part.
- `save-dir` : The path to save the visualization results, if it not specified, it will be set to '.'.
- `device` : The device type. If not specified, it will be set to `cpu`.
27 changes: 27 additions & 0 deletions docs/zh_cn/02-how-to-run/useful_tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,30 @@ python tools/generate_md_table.py tests/regression/mmocr.yml tests/regression/mm
| [SAR](https://github.com/open-mmlab/mmocr/tree/main/configs/textrecog/sar) | TextRecognition | Y | N | Y | N | N | N |
| [SATRN](https://github.com/open-mmlab/mmocr/tree/main/configs/textrecog/satrn) | TextRecognition | Y | Y | Y | N | N | N |
| [ABINet](https://github.com/open-mmlab/mmocr/tree/main/configs/textrecog/abinet) | TextRecognition | Y | Y | Y | N | N | N |

## visualize

这个工具可以用来可视化不同推理引擎的推理结果。

### 用法

```bash
python tools/visualize.py \
--deploy-cfg {DEPLOY_CFG} \
--model-cfg {MODEL_CFG} \
--deploy-path {DEPLOY_PATH} \
--test-img {TEST_IMGS} \
--checkpoint {CHECKPOINTS} \
--save-dir {SAVE_DIR} \
--device {DEVICE}
```

### 参数说明

- `deploy-cfg` : 输入的配置文件路径
- `model-cfg` : 输入的模型配置文件路径
- `deploy-path` : 测试的模型文件路径,如果部分模型含有多个文件,请多次使用该参数
- `test-img` : 测试的图片路径,可以多次使用测试测试多张图片
- `checkpoint` : PyTorch的权重文件,如果使用这个参数,推理的结果会被拼接到图像的右侧
- `save-dir` : 保存可视化结果,如果没有指定则会被设为当前目录
- `device` : 运行的设备类型,如果没有指定则会默认设置为`cpu`
103 changes: 103 additions & 0 deletions tools/visualize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import logging
import os
import os.path as osp
import time

import mmcv
import mmengine
import numpy as np
from tqdm import tqdm

from mmdeploy.apis import visualize_model
from mmdeploy.utils import Backend, get_backend, get_root_logger, load_config


def parse_args():
parser = argparse.ArgumentParser(
description='Model inference visualization.')
parser.add_argument('--deploy-cfg', help='deploy config path')
parser.add_argument('--model-cfg', help='model config path')
parser.add_argument(
'--deploy-path', type=str, nargs='+', help='deploy model path')
parser.add_argument(
'--checkpoint', default=None, help='model checkpoint path')
parser.add_argument(
'--test-img',
default=None,
type=str,
nargs='+',
help='image used to test model')
parser.add_argument(
'--save-dir',
default=os.getcwd(),
help='the dir to save inference results')
parser.add_argument('--device', help='device to run model', default='cpu')
parser.add_argument(
'--log-level',
help='set log level',
default='INFO',
choices=list(logging._nameToLevel.keys()))
parser.add_argument(
'--uri',
default='192.168.1.1:60000',
help='Remote ipv4:port or ipv6:port for inference on edge device.')
args = parser.parse_args()
return args


def main():
args = parse_args()
logger = get_root_logger()
log_level = logging.getLevelName(args.log_level)
logger.setLevel(log_level)

deploy_cfg_path = args.deploy_cfg
model_cfg_path = args.model_cfg
checkpoint_path = args.checkpoint
deploy_model_path = args.deploy_path
if not isinstance(deploy_model_path, list):
deploy_model_path = [deploy_model_path]

# load deploy_cfg
deploy_cfg = load_config(deploy_cfg_path)[0]

# create save_dir or generate default save_dir
current_time = time.localtime()
save_dir = osp.join(os.getcwd(),
time.strftime('%Y_%m_%d_%H_%M_%S', current_time))
mmengine.mkdir_or_exist(save_dir)

# get backend info
backend = get_backend(deploy_cfg)
extra = dict()
if backend == Backend.SNPE:
extra['uri'] = args.uri

# iterate single_img
for single_img in tqdm(args.test_img):
filename = osp.basename(single_img)
output_file = osp.join(save_dir, filename)
visualize_model(model_cfg_path, deploy_cfg_path, deploy_model_path,
single_img, args.device, backend, output_file, False,
**extra)

if checkpoint_path:
pytorch_output_file = osp.join(save_dir, 'pytorch_out.jpg')
visualize_model(model_cfg_path, deploy_cfg_path, [checkpoint_path],
single_img, args.device, Backend.PYTORCH,
pytorch_output_file, False)

# concat pytorch result and backend result
backend_result = mmcv.imread(output_file)
pytorch_result = mmcv.imread(pytorch_output_file)
result = np.concatenate((backend_result, pytorch_result), axis=1)
mmcv.imwrite(result, output_file)

# remove temp pytorch result
os.remove(osp.join(save_dir, pytorch_output_file))


if __name__ == '__main__':
main()