From 60a515f41fa90b2b9272559608ac67e7241573ae Mon Sep 17 00:00:00 2001 From: Range King Date: Sat, 26 Nov 2022 19:59:37 +0800 Subject: [PATCH] [Enhance] Add option of json output in `test.py` (#316) * add json args * Update how_to.md * Update test.py * Update how_to.md * Update how_to.md --- docs/en/advanced_guides/how_to.md | 41 ++++++++++++++++++++++++++++ docs/zh_cn/advanced_guides/how_to.md | 41 ++++++++++++++++++++++++++++ tools/test.py | 18 ++++++++++-- 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/docs/en/advanced_guides/how_to.md b/docs/en/advanced_guides/how_to.md index 5f7c0bec..e84a0ef8 100644 --- a/docs/en/advanced_guides/how_to.md +++ b/docs/en/advanced_guides/how_to.md @@ -311,3 +311,44 @@ model = dict( widen_factor=widen_factor)) ) ``` + +## Output prediction results + +If you want to save the prediction results as a specific file for offline evaluation, MMYOLO currently supports both json and pkl formats. + +```{note} +The json file only save `image_id`, `bbox`, `score` and `category_id`. The json file can be read using the json library. +The pkl file holds more content than the json file, and also holds information such as the file name and size of the predicted image; the pkl file can be read using the pickle library. The pkl file can be read using the pickle library. +``` + +### Output into json file + +If you want to output the prediction results as a json file, the command is as follows. + +```shell +python tools/test.py {path_to_config} {path_to_checkpoint} --json-prefix {json_prefix} +``` + +The argument after `--json-prefix` should be a filename prefix (no need to enter the `.json` suffix) and can also contain a path. For a concrete example: + +```shell +python tools/test.py configs\yolov5\yolov5_s-v61_syncbn_8xb16-300e_coco.py yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth --json-prefix work_dirs/demo/json_demo +``` + +Running the above command will output the `json_demo.bbox.json` file in the `work_dirs/demo` folder. + +### Output into pkl file + +If you want to output the prediction results as a pkl file, the command is as follows. + +```shell +python tools/test.py {path_to_config} {path_to_checkpoint} --out {path_to_output_file} +``` + +The argument after `--out` should be a full filename (**must be** with a `.pkl` or `.pickle` suffix) and can also contain a path. For a concrete example: + +```shell +python tools/test.py configs\yolov5\yolov5_s-v61_syncbn_8xb16-300e_coco.py yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth --out work_dirs/demo/pkl_demo.pkl +``` + +Running the above command will output the `pkl_demo.pkl` file in the `work_dirs/demo` folder. diff --git a/docs/zh_cn/advanced_guides/how_to.md b/docs/zh_cn/advanced_guides/how_to.md index 1d8727e0..2754197f 100644 --- a/docs/zh_cn/advanced_guides/how_to.md +++ b/docs/zh_cn/advanced_guides/how_to.md @@ -314,3 +314,44 @@ model = dict( widen_factor=widen_factor)) ) ``` + +## 输出预测结果 + +如果想将预测结果保存为特定的文件,用于离线评估,目前 MMYOLO 支持 json 和 pkl 两种格式。 + +```{note} +json 文件仅保存 `image_id`、`bbox`、`score` 和 `category_id`; json 文件可以使用 json 库读取。 +pkl 保存内容比 json 文件更多,还会保存预测图片的文件名和尺寸等一系列信息; pkl 文件可以使用 pickle 库读取。 +``` + +### 输出为 json 文件 + +如果想将预测结果输出为 json 文件,则命令如下: + +```shell +python tools/test.py {path_to_config} {path_to_checkpoint} --json-prefix {json_prefix} +``` + +`--json-prefix` 后的参数输入为文件名前缀(无需输入 `.json` 后缀),也可以包含路径。举一个具体例子: + +```shell +python tools/test.py configs\yolov5\yolov5_s-v61_syncbn_8xb16-300e_coco.py yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth --json-prefix work_dirs/demo/json_demo +``` + +运行以上命令会在 `work_dirs/demo` 文件夹下,输出 `json_demo.bbox.json` 文件。 + +### 输出为 pkl 文件 + +如果想将预测结果输出为 pkl 文件,则命令如下: + +```shell +python tools/test.py {path_to_config} {path_to_checkpoint} --out {path_to_output_file} +``` + +`--out` 后的参数输入为完整文件名(**必须输入** `.pkl` 或 `.pickle` 后缀),也可以包含路径。举一个具体例子: + +```shell +python tools/test.py configs\yolov5\yolov5_s-v61_syncbn_8xb16-300e_coco.py yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth --out work_dirs/demo/pkl_demo.pkl +``` + +运行以上命令会在 `work_dirs/demo` 文件夹下,输出 `pkl_demo.pkl` 文件。 diff --git a/tools/test.py b/tools/test.py index fc80c887..0c5b89b8 100644 --- a/tools/test.py +++ b/tools/test.py @@ -12,7 +12,7 @@ from mmyolo.registry import RUNNERS from mmyolo.utils import register_all_modules -# TODO: support fuse_conv_bn and format_only +# TODO: support fuse_conv_bn def parse_args(): parser = argparse.ArgumentParser( description='MMYOLO test (and eval) a model') @@ -24,7 +24,13 @@ def parse_args(): parser.add_argument( '--out', type=str, - help='dump predictions to a pickle file for offline evaluation') + help='output result file (must be a .pkl file) in pickle format') + parser.add_argument( + '--json-prefix', + type=str, + help='the prefix of the output json file without perform evaluation, ' + 'which is useful when you want to format the result to a specific ' + 'format and submit it to the test server') parser.add_argument( '--show', action='store_true', help='show prediction results') parser.add_argument( @@ -92,6 +98,14 @@ def main(): if args.deploy: cfg.custom_hooks.append(dict(type='SwitchToDeployHook')) + # add `format_only` and `outfile_prefix` into cfg + if args.json_prefix is not None: + cfg_json = { + 'test_evaluator.format_only': True, + 'test_evaluator.outfile_prefix': args.json_prefix + } + cfg.merge_from_dict(cfg_json) + # build the runner from config if 'runner_type' not in cfg: # build the default runner