You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

useful_tools.md 16 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. Apart from training/testing scripts, We provide lots of useful tools under the
  2. `tools/` directory.
  3. ## Log Analysis
  4. `tools/analysis_tools/analyze_logs.py` plots loss/mAP curves given a training
  5. log file. Run `pip install seaborn` first to install the dependency.
  6. ```shell
  7. python tools/analysis_tools/analyze_logs.py plot_curve [--keys ${KEYS}] [--title ${TITLE}] [--legend ${LEGEND}] [--backend ${BACKEND}] [--style ${STYLE}] [--out ${OUT_FILE}]
  8. ```
  9. ![loss curve image](../resources/loss_curve.png)
  10. Examples:
  11. - Plot the classification loss of some run.
  12. ```shell
  13. python tools/analysis_tools/analyze_logs.py plot_curve log.json --keys loss_cls --legend loss_cls
  14. ```
  15. - Plot the classification and regression loss of some run, and save the figure to a pdf.
  16. ```shell
  17. python tools/analysis_tools/analyze_logs.py plot_curve log.json --keys loss_cls loss_bbox --out losses.pdf
  18. ```
  19. - Compare the bbox mAP of two runs in the same figure.
  20. ```shell
  21. python tools/analysis_tools/analyze_logs.py plot_curve log1.json log2.json --keys bbox_mAP --legend run1 run2
  22. ```
  23. - Compute the average training speed.
  24. ```shell
  25. python tools/analysis_tools/analyze_logs.py cal_train_time log.json [--include-outliers]
  26. ```
  27. The output is expected to be like the following.
  28. ```text
  29. -----Analyze train time of work_dirs/some_exp/20190611_192040.log.json-----
  30. slowest epoch 11, average time is 1.2024
  31. fastest epoch 1, average time is 1.1909
  32. time std over epochs is 0.0028
  33. average iter time: 1.1959 s/iter
  34. ```
  35. ## Result Analysis
  36. `tools/analysis_tools/analyze_results.py` calculates single image mAP and saves or shows the topk images with the highest and lowest scores based on prediction results.
  37. **Usage**
  38. ```shell
  39. python tools/analysis_tools/analyze_results.py \
  40. ${CONFIG} \
  41. ${PREDICTION_PATH} \
  42. ${SHOW_DIR} \
  43. [--show] \
  44. [--wait-time ${WAIT_TIME}] \
  45. [--topk ${TOPK}] \
  46. [--show-score-thr ${SHOW_SCORE_THR}] \
  47. [--cfg-options ${CFG_OPTIONS}]
  48. ```
  49. Description of all arguments:
  50. - `config` : The path of a model config file.
  51. - `prediction_path`: Output result file in pickle format from `tools/test.py`
  52. - `show_dir`: Directory where painted GT and detection images will be saved
  53. - `--show`:Determines whether to show painted images, If not specified, it will be set to `False`
  54. - `--wait-time`: The interval of show (s), 0 is block
  55. - `--topk`: The number of saved images that have the highest and lowest `topk` scores after sorting. If not specified, it will be set to `20`.
  56. - `--show-score-thr`: Show score threshold. If not specified, it will be set to `0`.
  57. - `--cfg-options`: If specified, the key-value pair optional cfg will be merged into config file
  58. **Examples**:
  59. Assume that you have got result file in pickle format from `tools/test.py` in the path './result.pkl'.
  60. 1. Test Faster R-CNN and visualize the results, save images to the directory `results/`
  61. ```shell
  62. python tools/analysis_tools/analyze_results.py \
  63. configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
  64. result.pkl \
  65. results \
  66. --show
  67. ```
  68. 2. Test Faster R-CNN and specified topk to 50, save images to the directory `results/`
  69. ```shell
  70. python tools/analysis_tools/analyze_results.py \
  71. configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
  72. result.pkl \
  73. results \
  74. --topk 50
  75. ```
  76. 3. If you want to filter the low score prediction results, you can specify the `show-score-thr` parameter
  77. ```shell
  78. python tools/analysis_tools/analyze_results.py \
  79. configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
  80. result.pkl \
  81. results \
  82. --show-score-thr 0.3
  83. ```
  84. ## Visualization
  85. ### Visualize Datasets
  86. `tools/misc/browse_dataset.py` helps the user to browse a detection dataset (both
  87. images and bounding box annotations) visually, or save the image to a
  88. designated directory.
  89. ```shell
  90. python tools/misc/browse_dataset.py ${CONFIG} [-h] [--skip-type ${SKIP_TYPE[SKIP_TYPE...]}] [--output-dir ${OUTPUT_DIR}] [--not-show] [--show-interval ${SHOW_INTERVAL}]
  91. ```
  92. ### Visualize Models
  93. First, convert the model to ONNX as described
  94. [here](#convert-mmdetection-model-to-onnx-experimental).
  95. Note that currently only RetinaNet is supported, support for other models
  96. will be coming in later versions.
  97. The converted model could be visualized by tools like [Netron](https://github.com/lutzroeder/netron).
  98. ### Visualize Predictions
  99. If you need a lightweight GUI for visualizing the detection results, you can refer [DetVisGUI project](https://github.com/Chien-Hung/DetVisGUI/tree/mmdetection).
  100. ## Error Analysis
  101. `tools/analysis_tools/coco_error_analysis.py` analyzes COCO results per category and by
  102. different criterion. It can also make a plot to provide useful information.
  103. ```shell
  104. python tools/analysis_tools/coco_error_analysis.py ${RESULT} ${OUT_DIR} [-h] [--ann ${ANN}] [--types ${TYPES[TYPES...]}]
  105. ```
  106. Example:
  107. Assume that you have got [Mask R-CNN checkpoint file](https://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_r50_fpn_1x_coco/mask_rcnn_r50_fpn_1x_coco_20200205-d4b0c5d6.pth) in the path 'checkpoint'. For other checkpoints, please refer to our [model zoo](./model_zoo.md). You can use the following command to get the results bbox and segmentation json file.
  108. ```shell
  109. # out: results.bbox.json and results.segm.json
  110. python tools/test.py \
  111. configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco.py \
  112. checkpoint/mask_rcnn_r50_fpn_1x_coco_20200205-d4b0c5d6.pth \
  113. --format-only \
  114. --options "jsonfile_prefix=./results"
  115. ```
  116. 1. Get COCO bbox error results per category , save analyze result images to the directory `results/`
  117. ```shell
  118. python tools/analysis_tools/coco_error_analysis.py \
  119. results.bbox.json \
  120. results \
  121. --ann=data/coco/annotations/instances_val2017.json \
  122. ```
  123. 2. Get COCO segmentation error results per category , save analyze result images to the directory `results/`
  124. ```shell
  125. python tools/analysis_tools/coco_error_analysis.py \
  126. results.segm.json \
  127. results \
  128. --ann=data/coco/annotations/instances_val2017.json \
  129. --types='segm'
  130. ```
  131. ## Model Serving
  132. In order to serve an `MMDetection` model with [`TorchServe`](https://pytorch.org/serve/), you can follow the steps:
  133. ### 1. Convert model from MMDetection to TorchServe
  134. ```shell
  135. python tools/deployment/mmdet2torchserve.py ${CONFIG_FILE} ${CHECKPOINT_FILE} \
  136. --output-folder ${MODEL_STORE} \
  137. --model-name ${MODEL_NAME}
  138. ```
  139. **Note**: ${MODEL_STORE} needs to be an absolute path to a folder.
  140. ### 2. Build `mmdet-serve` docker image
  141. ```shell
  142. docker build -t mmdet-serve:latest docker/serve/
  143. ```
  144. ### 3. Run `mmdet-serve`
  145. Check the official docs for [running TorchServe with docker](https://github.com/pytorch/serve/blob/master/docker/README.md#running-torchserve-in-a-production-docker-environment).
  146. In order to run in GPU, you need to install [nvidia-docker](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html). You can omit the `--gpus` argument in order to run in CPU.
  147. Example:
  148. ```shell
  149. docker run --rm \
  150. --cpus 8 \
  151. --gpus device=0 \
  152. -p8080:8080 -p8081:8081 -p8082:8082 \
  153. --mount type=bind,source=$MODEL_STORE,target=/home/model-server/model-store \
  154. mmdet-serve:latest
  155. ```
  156. [Read the docs](https://github.com/pytorch/serve/blob/072f5d088cce9bb64b2a18af065886c9b01b317b/docs/rest_api.md/) about the Inference (8080), Management (8081) and Metrics (8082) APis
  157. ### 4. Test deployment
  158. ```shell
  159. curl -O curl -O https://raw.githubusercontent.com/pytorch/serve/master/docs/images/3dogs.jpg
  160. curl http://127.0.0.1:8080/predictions/${MODEL_NAME} -T 3dogs.jpg
  161. ```
  162. You should obtain a response similar to:
  163. ```json
  164. [
  165. {
  166. "class_name": "dog",
  167. "bbox": [
  168. 294.63409423828125,
  169. 203.99111938476562,
  170. 417.048583984375,
  171. 281.62744140625
  172. ],
  173. "score": 0.9987992644309998
  174. },
  175. {
  176. "class_name": "dog",
  177. "bbox": [
  178. 404.26019287109375,
  179. 126.0080795288086,
  180. 574.5091552734375,
  181. 293.6662292480469
  182. ],
  183. "score": 0.9979367256164551
  184. },
  185. {
  186. "class_name": "dog",
  187. "bbox": [
  188. 197.2144775390625,
  189. 93.3067855834961,
  190. 307.8505554199219,
  191. 276.7560119628906
  192. ],
  193. "score": 0.993338406085968
  194. }
  195. ]
  196. ```
  197. And you can use `test_torchserver.py` to compare result of torchserver and pytorch, and visualize them.
  198. ```shell
  199. python tools/deployment/test_torchserver.py ${IMAGE_FILE} ${CONFIG_FILE} ${CHECKPOINT_FILE} ${MODEL_NAME}
  200. [--inference-addr ${INFERENCE_ADDR}] [--device ${DEVICE}] [--score-thr ${SCORE_THR}]
  201. ```
  202. Example:
  203. ```shell
  204. python tools/deployment/test_torchserver.py \
  205. demo/demo.jpg \
  206. configs/yolo/yolov3_d53_320_273e_coco.py \
  207. checkpoint/yolov3_d53_320_273e_coco-421362b6.pth \
  208. yolov3
  209. ```
  210. ## Model Complexity
  211. `tools/analysis_tools/get_flops.py` is a script adapted from [flops-counter.pytorch](https://github.com/sovrasov/flops-counter.pytorch) to compute the FLOPs and params of a given model.
  212. ```shell
  213. python tools/analysis_tools/get_flops.py ${CONFIG_FILE} [--shape ${INPUT_SHAPE}]
  214. ```
  215. You will get the results like this.
  216. ```text
  217. ==============================
  218. Input shape: (3, 1280, 800)
  219. Flops: 239.32 GFLOPs
  220. Params: 37.74 M
  221. ==============================
  222. ```
  223. **Note**: This tool is still experimental and we do not guarantee that the
  224. number is absolutely correct. You may well use the result for simple
  225. comparisons, but double check it before you adopt it in technical reports or papers.
  226. 1. FLOPs are related to the input shape while parameters are not. The default
  227. input shape is (1, 3, 1280, 800).
  228. 2. Some operators are not counted into FLOPs like GN and custom operators. Refer to [`mmcv.cnn.get_model_complexity_info()`](https://github.com/open-mmlab/mmcv/blob/master/mmcv/cnn/utils/flops_counter.py) for details.
  229. 3. The FLOPs of two-stage detectors is dependent on the number of proposals.
  230. ## Model conversion
  231. ### MMDetection model to ONNX (experimental)
  232. We provide a script to convert model to [ONNX](https://github.com/onnx/onnx) format. We also support comparing the output results between Pytorch and ONNX model for verification.
  233. ```shell
  234. python tools/deployment/pytorch2onnx.py ${CONFIG_FILE} ${CHECKPOINT_FILE} --output_file ${ONNX_FILE} [--shape ${INPUT_SHAPE} --verify]
  235. ```
  236. **Note**: This tool is still experimental. Some customized operators are not supported for now. For a detailed description of the usage and the list of supported models, please refer to [pytorch2onnx](tutorials/pytorch2onnx.md).
  237. ### MMDetection 1.x model to MMDetection 2.x
  238. `tools/model_converters/upgrade_model_version.py` upgrades a previous MMDetection checkpoint
  239. to the new version. Note that this script is not guaranteed to work as some
  240. breaking changes are introduced in the new version. It is recommended to
  241. directly use the new checkpoints.
  242. ```shell
  243. python tools/model_converters/upgrade_model_version.py ${IN_FILE} ${OUT_FILE} [-h] [--num-classes NUM_CLASSES]
  244. ```
  245. ### RegNet model to MMDetection
  246. `tools/model_converters/regnet2mmdet.py` convert keys in pycls pretrained RegNet models to
  247. MMDetection style.
  248. ```shell
  249. python tools/model_converters/regnet2mmdet.py ${SRC} ${DST} [-h]
  250. ```
  251. ### Detectron ResNet to Pytorch
  252. `tools/model_converters/detectron2pytorch.py` converts keys in the original detectron pretrained
  253. ResNet models to PyTorch style.
  254. ```shell
  255. python tools/model_converters/detectron2pytorch.py ${SRC} ${DST} ${DEPTH} [-h]
  256. ```
  257. ### Prepare a model for publishing
  258. `tools/model_converters/publish_model.py` helps users to prepare their model for publishing.
  259. Before you upload a model to AWS, you may want to
  260. 1. convert model weights to CPU tensors
  261. 2. delete the optimizer states and
  262. 3. compute the hash of the checkpoint file and append the hash id to the
  263. filename.
  264. ```shell
  265. python tools/model_converters/publish_model.py ${INPUT_FILENAME} ${OUTPUT_FILENAME}
  266. ```
  267. E.g.,
  268. ```shell
  269. python tools/model_converters/publish_model.py work_dirs/faster_rcnn/latest.pth faster_rcnn_r50_fpn_1x_20190801.pth
  270. ```
  271. The final output filename will be `faster_rcnn_r50_fpn_1x_20190801-{hash id}.pth`.
  272. ## Dataset Conversion
  273. `tools/data_converters/` contains tools to convert the Cityscapes dataset
  274. and Pascal VOC dataset to the COCO format.
  275. ```shell
  276. python tools/dataset_converters/cityscapes.py ${CITYSCAPES_PATH} [-h] [--img-dir ${IMG_DIR}] [--gt-dir ${GT_DIR}] [-o ${OUT_DIR}] [--nproc ${NPROC}]
  277. python tools/dataset_converters/pascal_voc.py ${DEVKIT_PATH} [-h] [-o ${OUT_DIR}]
  278. ```
  279. ## Benchmark
  280. ### Robust Detection Benchmark
  281. `tools/analysis_tools/test_robustness.py` and`tools/analysis_tools/robustness_eval.py` helps users to evaluate model robustness. The core idea comes from [Benchmarking Robustness in Object Detection: Autonomous Driving when Winter is Coming](https://arxiv.org/abs/1907.07484). For more information how to evaluate models on corrupted images and results for a set of standard models please refer to [robustness_benchmarking.md](robustness_benchmarking.md).
  282. ### FPS Benchmark
  283. `tools/analysis_tools/benchmark.py` helps users to calculate FPS. The FPS value includes model forward and post-processing. In order to get a more accurate value, currently only supports single GPU distributed startup mode.
  284. ```shell
  285. python -m torch.distributed.launch --nproc_per_node=1 --master_port=${PORT} tools/analysis_tools/benchmark.py \
  286. ${CONFIG} \
  287. ${CHECKPOINT} \
  288. [--repeat-num ${REPEAT_NUM}] \
  289. [--max-iter ${MAX_ITER}] \
  290. [--log-interval ${LOG_INTERVAL}] \
  291. --launcher pytorch
  292. ```
  293. Examples: Assuming that you have already downloaded the `Faster R-CNN` model checkpoint to the directory `checkpoints/`.
  294. ```shell
  295. python -m torch.distributed.launch --nproc_per_node=1 --master_port=29500 tools/analysis_tools/benchmark.py \
  296. configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
  297. checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth \
  298. --launcher pytorch
  299. ```
  300. ## Miscellaneous
  301. ### Evaluating a metric
  302. `tools/analysis_tools/eval_metric.py` evaluates certain metrics of a pkl result file
  303. according to a config file.
  304. ```shell
  305. python tools/analysis_tools/eval_metric.py ${CONFIG} ${PKL_RESULTS} [-h] [--format-only] [--eval ${EVAL[EVAL ...]}]
  306. [--cfg-options ${CFG_OPTIONS [CFG_OPTIONS ...]}]
  307. [--eval-options ${EVAL_OPTIONS [EVAL_OPTIONS ...]}]
  308. ```
  309. ### Print the entire config
  310. `tools/misc/print_config.py` prints the whole config verbatim, expanding all its
  311. imports.
  312. ```shell
  313. python tools/misc/print_config.py ${CONFIG} [-h] [--options ${OPTIONS [OPTIONS...]}]
  314. ```
  315. ## Hyper-parameter Optimization
  316. ### YOLO Anchor Optimization
  317. `tools/analysis_tools/optimize_anchors.py` provides two method to optimize YOLO anchors.
  318. One is k-means anchor cluster which refers from [darknet](https://github.com/AlexeyAB/darknet/blob/master/src/detector.c#L1421).
  319. ```shell
  320. python tools/analysis_tools/optimize_anchors.py ${CONFIG} --algorithm k-means --input-shape ${INPUT_SHAPE [WIDTH HEIGHT]} --output-dir ${OUTPUT_DIR}
  321. ```
  322. Another is using differential evolution to optimize anchors.
  323. ```shell
  324. python tools/analysis_tools/optimize_anchors.py ${CONFIG} --algorithm differential_evolution --input-shape ${INPUT_SHAPE [WIDTH HEIGHT]} --output-dir ${OUTPUT_DIR}
  325. ```
  326. E.g.,
  327. ```shell
  328. python tools/analysis_tools/optimize_anchors.py configs/yolo/yolov3_d53_320_273e_coco.py --algorithm differential_evolution --input-shape 608 608 --device cuda --output-dir work_dirs
  329. ```
  330. You will get:
  331. ```
  332. loading annotations into memory...
  333. Done (t=9.70s)
  334. creating index...
  335. index created!
  336. 2021-07-19 19:37:20,951 - mmdet - INFO - Collecting bboxes from annotation...
  337. [>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 117266/117266, 15874.5 task/s, elapsed: 7s, ETA: 0s
  338. 2021-07-19 19:37:28,753 - mmdet - INFO - Collected 849902 bboxes.
  339. differential_evolution step 1: f(x)= 0.506055
  340. differential_evolution step 2: f(x)= 0.506055
  341. ......
  342. differential_evolution step 489: f(x)= 0.386625
  343. 2021-07-19 19:46:40,775 - mmdet - INFO Anchor evolution finish. Average IOU: 0.6133754253387451
  344. 2021-07-19 19:46:40,776 - mmdet - INFO Anchor differential evolution result:[[10, 12], [15, 30], [32, 22], [29, 59], [61, 46], [57, 116], [112, 89], [154, 198], [349, 336]]
  345. 2021-07-19 19:46:40,798 - mmdet - INFO Result saved in work_dirs/anchor_optimize_result.json
  346. ```

No Description

Contributors (3)