2021-11-30 15:00:37 +08:00
|
|
|
# Copyright (c) OpenMMLab. All rights reserved.
|
2021-08-03 17:12:44 +08:00
|
|
|
import argparse
|
2021-09-22 19:42:16 +08:00
|
|
|
import sys
|
2021-08-03 17:12:44 +08:00
|
|
|
|
|
|
|
from mmcv import DictAction
|
|
|
|
from mmcv.parallel import MMDataParallel
|
|
|
|
|
2021-11-25 09:57:05 +08:00
|
|
|
from mmdeploy.apis import build_task_processor
|
|
|
|
from mmdeploy.utils.config_utils import load_config
|
|
|
|
from mmdeploy.utils.device import parse_device_id
|
2021-09-22 19:42:16 +08:00
|
|
|
from mmdeploy.utils.timer import TimeCounter
|
2021-08-03 17:12:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='MMDeploy test (and eval) a backend.')
|
|
|
|
parser.add_argument('deploy_cfg', help='Deploy config path')
|
|
|
|
parser.add_argument('model_cfg', help='Model config path')
|
2021-08-12 17:38:38 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--model', type=str, nargs='+', help='Input model files.')
|
2021-08-03 17:12:44 +08:00
|
|
|
parser.add_argument('--out', help='output result file in pickle format')
|
|
|
|
parser.add_argument(
|
|
|
|
'--format-only',
|
|
|
|
action='store_true',
|
|
|
|
help='Format the output results without perform evaluation. It is'
|
|
|
|
'useful when you want to format the result to a specific format and '
|
|
|
|
'submit it to the test server')
|
|
|
|
parser.add_argument(
|
|
|
|
'--metrics',
|
|
|
|
type=str,
|
|
|
|
nargs='+',
|
|
|
|
help='evaluation metrics, which depends on the codebase and the '
|
|
|
|
'dataset, e.g., "bbox", "segm", "proposal" for COCO, and "mAP", '
|
|
|
|
'"recall" for PASCAL VOC in mmdet; "accuracy", "precision", "recall", '
|
|
|
|
'"f1_score", "support" for single label dataset, and "mAP", "CP", "CR"'
|
|
|
|
', "CF1", "OP", "OR", "OF1" for multi-label dataset in mmcls')
|
|
|
|
parser.add_argument('--show', action='store_true', help='show results')
|
|
|
|
parser.add_argument(
|
|
|
|
'--show-dir', help='directory where painted images will be saved')
|
|
|
|
parser.add_argument(
|
|
|
|
'--device', help='device used for conversion', default='cpu')
|
|
|
|
parser.add_argument(
|
|
|
|
'--cfg-options',
|
|
|
|
nargs='+',
|
|
|
|
action=DictAction,
|
|
|
|
help='override some settings in the used config, the key-value pair '
|
|
|
|
'in xxx=yyy format will be merged into config file. If the value to '
|
|
|
|
'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
|
|
|
|
'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
|
|
|
|
'Note that the quotation marks are necessary and that no white space '
|
|
|
|
'is allowed.')
|
|
|
|
parser.add_argument(
|
|
|
|
'--metric-options',
|
|
|
|
nargs='+',
|
|
|
|
action=DictAction,
|
|
|
|
help='custom options for evaluation, the key-value pair in xxx=yyy '
|
|
|
|
'format will be kwargs for dataset.evaluate() function')
|
2021-09-22 19:42:16 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--speed-test', action='store_true', help='activate speed test')
|
|
|
|
parser.add_argument(
|
|
|
|
'--warmup',
|
|
|
|
type=int,
|
2021-11-09 11:43:38 +08:00
|
|
|
help='warmup before counting inference elapse, require setting '
|
2021-09-22 19:42:16 +08:00
|
|
|
'speed-test first',
|
|
|
|
default=10)
|
|
|
|
parser.add_argument(
|
|
|
|
'--log-interval',
|
|
|
|
type=int,
|
|
|
|
help='the interval between each log, require setting '
|
|
|
|
'speed-test first',
|
|
|
|
default=100)
|
|
|
|
parser.add_argument(
|
|
|
|
'--log2file',
|
|
|
|
type=str,
|
2021-11-09 11:43:38 +08:00
|
|
|
help='log speed in file format, require speed-test first')
|
2021-08-03 17:12:44 +08:00
|
|
|
|
2021-09-22 19:42:16 +08:00
|
|
|
args = parser.parse_args()
|
2021-08-03 17:12:44 +08:00
|
|
|
return args
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = parse_args()
|
|
|
|
if args.out is not None and not args.out.endswith(('.pkl', '.pickle')):
|
|
|
|
raise ValueError('The output file must be a pkl file.')
|
|
|
|
deploy_cfg_path = args.deploy_cfg
|
|
|
|
model_cfg_path = args.model_cfg
|
|
|
|
|
|
|
|
# load deploy_cfg
|
2021-09-01 15:43:49 +08:00
|
|
|
deploy_cfg, model_cfg = load_config(deploy_cfg_path, model_cfg_path)
|
2021-09-24 10:40:39 +08:00
|
|
|
# merge options for model cfg
|
|
|
|
if args.cfg_options is not None:
|
|
|
|
model_cfg.merge_from_dict(args.cfg_options)
|
2021-08-03 17:12:44 +08:00
|
|
|
|
2021-11-25 09:57:05 +08:00
|
|
|
task_processor = build_task_processor(model_cfg, deploy_cfg, args.device)
|
|
|
|
|
2021-08-03 17:12:44 +08:00
|
|
|
# prepare the dataset loader
|
2021-09-01 15:43:49 +08:00
|
|
|
dataset_type = 'test'
|
2021-11-25 09:57:05 +08:00
|
|
|
dataset = task_processor.build_dataset(model_cfg, dataset_type)
|
|
|
|
data_loader = task_processor.build_dataloader(
|
2021-09-01 15:43:49 +08:00
|
|
|
dataset,
|
|
|
|
samples_per_gpu=1,
|
|
|
|
workers_per_gpu=model_cfg.data.workers_per_gpu)
|
2021-08-03 17:12:44 +08:00
|
|
|
|
|
|
|
# load the model of the backend
|
2021-11-25 09:57:05 +08:00
|
|
|
model = task_processor.init_backend_model(args.model)
|
|
|
|
|
|
|
|
device_id = parse_device_id(args.device)
|
2021-08-13 10:06:28 +08:00
|
|
|
|
2021-08-03 17:12:44 +08:00
|
|
|
model = MMDataParallel(model, device_ids=[0])
|
2021-09-22 19:42:16 +08:00
|
|
|
if args.speed_test:
|
|
|
|
with_sync = device_id == 0
|
|
|
|
output_file = sys.stdout
|
|
|
|
if args.log2file:
|
|
|
|
output_file = args.log2file
|
2021-08-03 17:12:44 +08:00
|
|
|
|
2021-09-22 19:42:16 +08:00
|
|
|
with TimeCounter.activate(
|
|
|
|
warmup=args.warmup,
|
|
|
|
log_interval=args.log_interval,
|
|
|
|
with_sync=with_sync,
|
|
|
|
file=output_file):
|
2021-11-25 09:57:05 +08:00
|
|
|
outputs = task_processor.single_gpu_test(model, data_loader,
|
|
|
|
args.show, args.show_dir)
|
2021-09-22 19:42:16 +08:00
|
|
|
else:
|
2021-11-25 09:57:05 +08:00
|
|
|
outputs = task_processor.single_gpu_test(model, data_loader, args.show,
|
|
|
|
args.show_dir)
|
|
|
|
task_processor.evaluate_outputs(model_cfg, outputs, dataset, args.metrics,
|
|
|
|
args.out, args.metric_options,
|
|
|
|
args.format_only)
|
2021-08-03 17:12:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|