[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
pull/319/head
Range King 2022-11-26 19:59:37 +08:00 committed by GitHub
parent 5efa56b3b5
commit 60a515f41f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 2 deletions

View File

@ -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.

View File

@ -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` 文件。

View File

@ -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