mirror of https://github.com/open-mmlab/mmocr.git
[WIP] support get flops and parameters in dev-1.x (#1414)
* [Feature] support get_flops * [Fix] add the divisor * [Doc] add the get_flops doc * [Doc] update the get_flops doc * [Doc] update get FLOPs doc * [Fix] delete unnecessary args * [Fix] delete unnecessary code in get_flops * [Doc] update get flops doc * [Fix] remove unnecessary code * [Doc] add space between Chinese and English * [Doc] add English doc of get flops * Update docs/zh_cn/user_guides/useful_tools.md Co-authored-by: Tong Gao <gaotongxiao@gmail.com> * Update docs/zh_cn/user_guides/useful_tools.md Co-authored-by: Tong Gao <gaotongxiao@gmail.com> * Update docs/en/user_guides/useful_tools.md Co-authored-by: Tong Gao <gaotongxiao@gmail.com> * Update docs/en/user_guides/useful_tools.md Co-authored-by: Tong Gao <gaotongxiao@gmail.com> * Update docs/en/user_guides/useful_tools.md Co-authored-by: Tong Gao <gaotongxiao@gmail.com> * Update docs/en/user_guides/useful_tools.md Co-authored-by: Tong Gao <gaotongxiao@gmail.com> * [Docs] fix the lint * fix * fix docs Co-authored-by: Tong Gao <gaotongxiao@gmail.com>pull/1435/head
parent
769d845b4f
commit
e7e46771ba
|
@ -45,8 +45,85 @@ python tools/analysis_tools/offline_eval.py configs/textdet/psenet/psenet_r50_fp
|
|||
|
||||
In addition, based on this tool, users can also convert predictions obtained from other libraries into MMOCR-supported formats, then use MMOCR's built-in metrics to evaluate them.
|
||||
|
||||
| ARGS | Type | Description |
|
||||
| ------------- | ----- | --------------------------------- |
|
||||
| config | str | (required) Path to the config. |
|
||||
| pkl_results | str | (required) The saved predictions. |
|
||||
| --cfg-options | float | Override configs. [Example](<>) |
|
||||
| ARGS | Type | Description |
|
||||
| ------------- | ----- | ------------------------------------------------------------------ |
|
||||
| config | str | (required) Path to the config. |
|
||||
| pkl_results | str | (required) The saved predictions. |
|
||||
| --cfg-options | float | Override configs. [Example](./config.md#command-line-modification) |
|
||||
|
||||
### Calculate FLOPs and the Number of Parameters
|
||||
|
||||
We provide a method to calculate the FLOPs and the number of parameters, first we install the dependencies using the following command.
|
||||
|
||||
```shell
|
||||
pip install fvcore
|
||||
```
|
||||
|
||||
The usage of the script to calculate FLOPs and the number of parameters is as follows.
|
||||
|
||||
```shell
|
||||
python tools/analysis_tools/get_flops.py ${config} --shape ${IMAGE_SHAPE}
|
||||
```
|
||||
|
||||
| ARGS | Type | Description |
|
||||
| ------- | ---- | ----------------------------------------------------------------------------------------- |
|
||||
| config | str | (required) Path to the config. |
|
||||
| --shape | int | Image size to use when calculating FLOPs, such as `--shape 320 320`. Default is `640 640` |
|
||||
|
||||
For example, you can run the following command to get FLOPs and the number of parameters of `dbnet_resnet18_fpnc_100k_synthtext.py`:
|
||||
|
||||
```shell
|
||||
python tools/analysis_tools/get_flops.py configs/textdet/dbnet/dbnet_resnet18_fpnc_100k_synthtext.py --shape 1024 1024
|
||||
```
|
||||
|
||||
The output is as follows:
|
||||
|
||||
```shell
|
||||
input shape is (1, 3, 1024, 1024)
|
||||
| module | #parameters or shape | #flops |
|
||||
| :------------------------ | :------------------- | :------ |
|
||||
| model | 12.341M | 63.955G |
|
||||
| backbone | 11.177M | 38.159G |
|
||||
| backbone.conv1 | 9.408K | 2.466G |
|
||||
| backbone.conv1.weight | (64, 3, 7, 7) | |
|
||||
| backbone.bn1 | 0.128K | 83.886M |
|
||||
| backbone.bn1.weight | (64,) | |
|
||||
| backbone.bn1.bias | (64,) | |
|
||||
| backbone.layer1 | 0.148M | 9.748G |
|
||||
| backbone.layer1.0 | 73.984K | 4.874G |
|
||||
| backbone.layer1.1 | 73.984K | 4.874G |
|
||||
| backbone.layer2 | 0.526M | 8.642G |
|
||||
| backbone.layer2.0 | 0.23M | 3.79G |
|
||||
| backbone.layer2.1 | 0.295M | 4.853G |
|
||||
| backbone.layer3 | 2.1M | 8.616G |
|
||||
| backbone.layer3.0 | 0.919M | 3.774G |
|
||||
| backbone.layer3.1 | 1.181M | 4.842G |
|
||||
| backbone.layer4 | 8.394M | 8.603G |
|
||||
| backbone.layer4.0 | 3.673M | 3.766G |
|
||||
| backbone.layer4.1 | 4.721M | 4.837G |
|
||||
| neck | 0.836M | 14.887G |
|
||||
| neck.lateral_convs | 0.246M | 2.013G |
|
||||
| neck.lateral_convs.0.conv | 16.384K | 1.074G |
|
||||
| neck.lateral_convs.1.conv | 32.768K | 0.537G |
|
||||
| neck.lateral_convs.2.conv | 65.536K | 0.268G |
|
||||
| neck.lateral_convs.3.conv | 0.131M | 0.134G |
|
||||
| neck.smooth_convs | 0.59M | 12.835G |
|
||||
| neck.smooth_convs.0.conv | 0.147M | 9.664G |
|
||||
| neck.smooth_convs.1.conv | 0.147M | 2.416G |
|
||||
| neck.smooth_convs.2.conv | 0.147M | 0.604G |
|
||||
| neck.smooth_convs.3.conv | 0.147M | 0.151G |
|
||||
| det_head | 0.329M | 10.909G |
|
||||
| det_head.binarize | 0.164M | 10.909G |
|
||||
| det_head.binarize.0 | 0.147M | 9.664G |
|
||||
| det_head.binarize.1 | 0.128K | 20.972M |
|
||||
| det_head.binarize.3 | 16.448K | 1.074G |
|
||||
| det_head.binarize.4 | 0.128K | 83.886M |
|
||||
| det_head.binarize.6 | 0.257K | 67.109M |
|
||||
| det_head.threshold | 0.164M | |
|
||||
| det_head.threshold.0 | 0.147M | |
|
||||
| det_head.threshold.1 | 0.128K | |
|
||||
| det_head.threshold.3 | 16.448K | |
|
||||
| det_head.threshold.4 | 0.128K | |
|
||||
| det_head.threshold.6 | 0.257K | |
|
||||
!!!Please be cautious if you use the results in papers. You may need to check if all ops are supported and verify that the flops computation is correct.
|
||||
```
|
||||
|
|
|
@ -45,8 +45,85 @@ python tools/analysis_tools/offline_eval.py configs/textdet/psenet/psenet_r50_fp
|
|||
|
||||
此外,基于此工具,用户也可以将其他算法库获取的预测结果转换成 MMOCR 支持的格式,从而使用 MMOCR 内置的评估指标来对其他算法库的模型进行评测。
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
| ------------- | ----- | ---------------------------------------- |
|
||||
| config | str | (必须)配置文件路径。 |
|
||||
| pkl_results | str | (必须)预先保存的预测结果文件。 |
|
||||
| --cfg-options | float | 用于覆写配置文件中的指定参数。[示例](<>) |
|
||||
| 参数 | 类型 | 说明 |
|
||||
| ------------- | ----- | ---------------------------------------------------------------- |
|
||||
| config | str | (必须)配置文件路径。 |
|
||||
| pkl_results | str | (必须)预先保存的预测结果文件。 |
|
||||
| --cfg-options | float | 用于覆写配置文件中的指定参数。[示例](./config.md#命令行修改配置) |
|
||||
|
||||
### 计算 FLOPs 和参数量
|
||||
|
||||
我们提供一个计算 FLOPs 和参数量的方法,首先我们使用以下命令安装依赖。
|
||||
|
||||
```shell
|
||||
pip install fvcore
|
||||
```
|
||||
|
||||
计算 FLOPs 和参数量的脚本使用方法如下:
|
||||
|
||||
```shell
|
||||
python tools/analysis_tools/get_flops.py ${config} --shape ${IMAGE_SHAPE}
|
||||
```
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
| ------- | ------ | ------------------------------------------------------------------ |
|
||||
| config | str | (必须) 配置文件路径。 |
|
||||
| --shape | int\*2 | 计算 FLOPs 使用的图片尺寸,如 `--shape 320 320`。 默认为 `640 640` |
|
||||
|
||||
获取 `dbnet_resnet18_fpnc_100k_synthtext.py` FLOPs 和参数量的示例命令如下。
|
||||
|
||||
```shell
|
||||
python tools/analysis_tools/get_flops.py configs/textdet/dbnet/dbnet_resnet18_fpnc_100k_synthtext.py --shape 1024 1024
|
||||
```
|
||||
|
||||
输出如下:
|
||||
|
||||
```shell
|
||||
input shape is (1, 3, 1024, 1024)
|
||||
| module | #parameters or shape | #flops |
|
||||
| :------------------------ | :------------------- | :------ |
|
||||
| model | 12.341M | 63.955G |
|
||||
| backbone | 11.177M | 38.159G |
|
||||
| backbone.conv1 | 9.408K | 2.466G |
|
||||
| backbone.conv1.weight | (64, 3, 7, 7) | |
|
||||
| backbone.bn1 | 0.128K | 83.886M |
|
||||
| backbone.bn1.weight | (64,) | |
|
||||
| backbone.bn1.bias | (64,) | |
|
||||
| backbone.layer1 | 0.148M | 9.748G |
|
||||
| backbone.layer1.0 | 73.984K | 4.874G |
|
||||
| backbone.layer1.1 | 73.984K | 4.874G |
|
||||
| backbone.layer2 | 0.526M | 8.642G |
|
||||
| backbone.layer2.0 | 0.23M | 3.79G |
|
||||
| backbone.layer2.1 | 0.295M | 4.853G |
|
||||
| backbone.layer3 | 2.1M | 8.616G |
|
||||
| backbone.layer3.0 | 0.919M | 3.774G |
|
||||
| backbone.layer3.1 | 1.181M | 4.842G |
|
||||
| backbone.layer4 | 8.394M | 8.603G |
|
||||
| backbone.layer4.0 | 3.673M | 3.766G |
|
||||
| backbone.layer4.1 | 4.721M | 4.837G |
|
||||
| neck | 0.836M | 14.887G |
|
||||
| neck.lateral_convs | 0.246M | 2.013G |
|
||||
| neck.lateral_convs.0.conv | 16.384K | 1.074G |
|
||||
| neck.lateral_convs.1.conv | 32.768K | 0.537G |
|
||||
| neck.lateral_convs.2.conv | 65.536K | 0.268G |
|
||||
| neck.lateral_convs.3.conv | 0.131M | 0.134G |
|
||||
| neck.smooth_convs | 0.59M | 12.835G |
|
||||
| neck.smooth_convs.0.conv | 0.147M | 9.664G |
|
||||
| neck.smooth_convs.1.conv | 0.147M | 2.416G |
|
||||
| neck.smooth_convs.2.conv | 0.147M | 0.604G |
|
||||
| neck.smooth_convs.3.conv | 0.147M | 0.151G |
|
||||
| det_head | 0.329M | 10.909G |
|
||||
| det_head.binarize | 0.164M | 10.909G |
|
||||
| det_head.binarize.0 | 0.147M | 9.664G |
|
||||
| det_head.binarize.1 | 0.128K | 20.972M |
|
||||
| det_head.binarize.3 | 16.448K | 1.074G |
|
||||
| det_head.binarize.4 | 0.128K | 83.886M |
|
||||
| det_head.binarize.6 | 0.257K | 67.109M |
|
||||
| det_head.threshold | 0.164M | |
|
||||
| det_head.threshold.0 | 0.147M | |
|
||||
| det_head.threshold.1 | 0.128K | |
|
||||
| det_head.threshold.3 | 16.448K | |
|
||||
| det_head.threshold.4 | 0.128K | |
|
||||
| det_head.threshold.6 | 0.257K | |
|
||||
!!!Please be cautious if you use the results in papers. You may need to check if all ops are supported and verify that the flops computation is correct.
|
||||
```
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
import argparse
|
||||
|
||||
import torch
|
||||
from fvcore.nn import FlopCountAnalysis, flop_count_table
|
||||
from mmengine import Config
|
||||
|
||||
from mmocr.registry import MODELS
|
||||
from mmocr.utils import register_all_modules
|
||||
|
||||
register_all_modules()
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(description='Train a detector')
|
||||
parser.add_argument('config', help='train config file path')
|
||||
parser.add_argument(
|
||||
'--shape',
|
||||
type=int,
|
||||
nargs='+',
|
||||
default=[640, 640],
|
||||
help='input image size')
|
||||
args = parser.parse_args()
|
||||
return args
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
args = parse_args()
|
||||
|
||||
if len(args.shape) == 1:
|
||||
h = w = args.shape[0]
|
||||
elif len(args.shape) == 2:
|
||||
h, w = args.shape
|
||||
else:
|
||||
raise ValueError('invalid input shape, please use --shape h w')
|
||||
|
||||
input_shape = (1, 3, h, w)
|
||||
|
||||
cfg = Config.fromfile(args.config)
|
||||
model = MODELS.build(cfg.model)
|
||||
|
||||
flops = FlopCountAnalysis(model, torch.ones(input_shape))
|
||||
|
||||
# params = parameter_count_table(model)
|
||||
flops_data = flop_count_table(flops)
|
||||
|
||||
print(flops_data)
|
||||
|
||||
print('!!!Please be cautious if you use the results in papers. '
|
||||
'You may need to check if all ops are supported and verify that the '
|
||||
'flops computation is correct.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue