-
Notifications
You must be signed in to change notification settings - Fork 665
[Enhancement] Add a visualize script for inference result comparation. #2012
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
Draft
IRONICBo
wants to merge
7
commits into
open-mmlab:main
Choose a base branch
from
IRONICBo:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fbae550
Add visualize scripts.
IRONICBo 0a8e585
Fix visualize script.
IRONICBo bece673
Fix default save_dir.
IRONICBo 9514b47
Update visualize tool's doc.
IRONICBo 7ff3290
Merge branch 'main' of https://github.com/IRONICBo/mmdeploy
IRONICBo f8e7fbe
Update batch visualize.
IRONICBo 0e15917
reconstruct
RunningLeon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
import argparse | ||
import logging | ||
import os.path as osp | ||
|
||
import mmcv | ||
import mmengine | ||
import numpy as np | ||
|
||
from mmdeploy.apis.utils import build_task_processor | ||
from mmdeploy.utils import get_input_shape, 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( | ||
'--model', | ||
type=str, | ||
nargs='+', | ||
required=True, | ||
help='deploy model path') | ||
parser.add_argument( | ||
'--checkpoint', default=None, help='model checkpoint path') | ||
parser.add_argument( | ||
'--device', help='device type for inference', default='cpu') | ||
parser.add_argument( | ||
'--test-img', | ||
type=str, | ||
nargs='+', | ||
required=True, | ||
help='image used to test model') | ||
parser.add_argument( | ||
'--batch', | ||
type=int, | ||
choices=[1, 2], | ||
help='batch size for inference, accepts only 1 or 2') | ||
parser.add_argument( | ||
'--save-dir', | ||
default='workdir', | ||
help='the dir to save inference results') | ||
parser.add_argument( | ||
'--show', action='store_true', help='Show detection outputs') | ||
parser.add_argument( | ||
'--log-level', | ||
help='set log level', | ||
default='INFO', | ||
choices=list(logging._nameToLevel.keys())) | ||
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) | ||
|
||
# load cfgs | ||
deploy_cfg, model_cfg = load_config(args.deploy_cfg, args.model_cfg) | ||
task_processor = build_task_processor(model_cfg, deploy_cfg, args.device) | ||
input_shape = get_input_shape(deploy_cfg) | ||
backend_model = task_processor.build_backend_model( | ||
args.model, | ||
data_preprocessor_updater=task_processor.update_data_preprocessor) | ||
torch_model = None | ||
if args.checkpoint is not None: | ||
torch_model = task_processor.build_pytorch_model(args.checkpoint) | ||
|
||
mmengine.mkdir_or_exist(args.save_dir) | ||
# get visualizer | ||
visualizer = task_processor.get_visualizer('mmdeploy', args.save_dir) | ||
|
||
for i in range(0, len(args.test_img), args.batch): | ||
imgs = args.test_img[i:(i + args.batch)] | ||
model_inputs, _ = task_processor.create_input( | ||
imgs, | ||
input_shape, | ||
data_preprocessor=getattr(backend_model, 'data_preprocessor', | ||
None)) | ||
backend_results = backend_model.test_step(model_inputs) | ||
torch_results = [None] * len(imgs) | ||
if torch_model is not None: | ||
torch_results = torch_model.test_step(model_inputs) | ||
|
||
# get visualized results | ||
for img_path, torch_res, backend_res in zip(imgs, torch_results, | ||
backend_results): | ||
_, filename = osp.split(img_path) | ||
output_file = osp.join(args.save_dir, filename) | ||
image = mmcv.imread(img_path, channel_order='rgb') | ||
visualizer.add_datasample( | ||
filename, | ||
image, | ||
data_sample=backend_res, | ||
draw_gt=False, | ||
show=False, | ||
out_file=None) | ||
drawn_img = visualizer.get_image() | ||
if torch_res: | ||
visualizer.add_datasample( | ||
filename, | ||
image, | ||
data_sample=torch_res, | ||
draw_gt=False, | ||
show=False, | ||
out_file=None) | ||
drawn_img_torch = visualizer.get_image() | ||
shape = drawn_img.shape | ||
dummy_img = np.full((shape[0], 20, shape[2]), | ||
255, | ||
dtype=np.uint8) | ||
drawn_img = np.concatenate( | ||
(drawn_img, dummy_img, drawn_img_torch), axis=1) | ||
if args.show: | ||
visualizer.show(drawn_img, win_name=filename, wait_time=0) | ||
drawn_img = mmcv.image.rgb2bgr(drawn_img) | ||
mmcv.imwrite(drawn_img, output_file) | ||
logger.info(f'Saved to {output_file}') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.