Skip to content

Commit e7e4677

Browse files
vansingaotongxiao
andauthored
[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 <[email protected]> * Update docs/zh_cn/user_guides/useful_tools.md Co-authored-by: Tong Gao <[email protected]> * Update docs/en/user_guides/useful_tools.md Co-authored-by: Tong Gao <[email protected]> * Update docs/en/user_guides/useful_tools.md Co-authored-by: Tong Gao <[email protected]> * Update docs/en/user_guides/useful_tools.md Co-authored-by: Tong Gao <[email protected]> * Update docs/en/user_guides/useful_tools.md Co-authored-by: Tong Gao <[email protected]> * [Docs] fix the lint * fix * fix docs Co-authored-by: Tong Gao <[email protected]>
1 parent 769d845 commit e7e4677

File tree

3 files changed

+220
-10
lines changed

3 files changed

+220
-10
lines changed

docs/en/user_guides/useful_tools.md

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,85 @@ python tools/analysis_tools/offline_eval.py configs/textdet/psenet/psenet_r50_fp
4545

4646
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.
4747

48-
| ARGS | Type | Description |
49-
| ------------- | ----- | --------------------------------- |
50-
| config | str | (required) Path to the config. |
51-
| pkl_results | str | (required) The saved predictions. |
52-
| --cfg-options | float | Override configs. [Example](<>) |
48+
| ARGS | Type | Description |
49+
| ------------- | ----- | ------------------------------------------------------------------ |
50+
| config | str | (required) Path to the config. |
51+
| pkl_results | str | (required) The saved predictions. |
52+
| --cfg-options | float | Override configs. [Example](./config.md#command-line-modification) |
53+
54+
### Calculate FLOPs and the Number of Parameters
55+
56+
We provide a method to calculate the FLOPs and the number of parameters, first we install the dependencies using the following command.
57+
58+
```shell
59+
pip install fvcore
60+
```
61+
62+
The usage of the script to calculate FLOPs and the number of parameters is as follows.
63+
64+
```shell
65+
python tools/analysis_tools/get_flops.py ${config} --shape ${IMAGE_SHAPE}
66+
```
67+
68+
| ARGS | Type | Description |
69+
| ------- | ---- | ----------------------------------------------------------------------------------------- |
70+
| config | str | (required) Path to the config. |
71+
| --shape | int | Image size to use when calculating FLOPs, such as `--shape 320 320`. Default is `640 640` |
72+
73+
For example, you can run the following command to get FLOPs and the number of parameters of `dbnet_resnet18_fpnc_100k_synthtext.py`:
74+
75+
```shell
76+
python tools/analysis_tools/get_flops.py configs/textdet/dbnet/dbnet_resnet18_fpnc_100k_synthtext.py --shape 1024 1024
77+
```
78+
79+
The output is as follows:
80+
81+
```shell
82+
input shape is (1, 3, 1024, 1024)
83+
| module | #parameters or shape | #flops |
84+
| :------------------------ | :------------------- | :------ |
85+
| model | 12.341M | 63.955G |
86+
| backbone | 11.177M | 38.159G |
87+
| backbone.conv1 | 9.408K | 2.466G |
88+
| backbone.conv1.weight | (64, 3, 7, 7) | |
89+
| backbone.bn1 | 0.128K | 83.886M |
90+
| backbone.bn1.weight | (64,) | |
91+
| backbone.bn1.bias | (64,) | |
92+
| backbone.layer1 | 0.148M | 9.748G |
93+
| backbone.layer1.0 | 73.984K | 4.874G |
94+
| backbone.layer1.1 | 73.984K | 4.874G |
95+
| backbone.layer2 | 0.526M | 8.642G |
96+
| backbone.layer2.0 | 0.23M | 3.79G |
97+
| backbone.layer2.1 | 0.295M | 4.853G |
98+
| backbone.layer3 | 2.1M | 8.616G |
99+
| backbone.layer3.0 | 0.919M | 3.774G |
100+
| backbone.layer3.1 | 1.181M | 4.842G |
101+
| backbone.layer4 | 8.394M | 8.603G |
102+
| backbone.layer4.0 | 3.673M | 3.766G |
103+
| backbone.layer4.1 | 4.721M | 4.837G |
104+
| neck | 0.836M | 14.887G |
105+
| neck.lateral_convs | 0.246M | 2.013G |
106+
| neck.lateral_convs.0.conv | 16.384K | 1.074G |
107+
| neck.lateral_convs.1.conv | 32.768K | 0.537G |
108+
| neck.lateral_convs.2.conv | 65.536K | 0.268G |
109+
| neck.lateral_convs.3.conv | 0.131M | 0.134G |
110+
| neck.smooth_convs | 0.59M | 12.835G |
111+
| neck.smooth_convs.0.conv | 0.147M | 9.664G |
112+
| neck.smooth_convs.1.conv | 0.147M | 2.416G |
113+
| neck.smooth_convs.2.conv | 0.147M | 0.604G |
114+
| neck.smooth_convs.3.conv | 0.147M | 0.151G |
115+
| det_head | 0.329M | 10.909G |
116+
| det_head.binarize | 0.164M | 10.909G |
117+
| det_head.binarize.0 | 0.147M | 9.664G |
118+
| det_head.binarize.1 | 0.128K | 20.972M |
119+
| det_head.binarize.3 | 16.448K | 1.074G |
120+
| det_head.binarize.4 | 0.128K | 83.886M |
121+
| det_head.binarize.6 | 0.257K | 67.109M |
122+
| det_head.threshold | 0.164M | |
123+
| det_head.threshold.0 | 0.147M | |
124+
| det_head.threshold.1 | 0.128K | |
125+
| det_head.threshold.3 | 16.448K | |
126+
| det_head.threshold.4 | 0.128K | |
127+
| det_head.threshold.6 | 0.257K | |
128+
!!!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.
129+
```

docs/zh_cn/user_guides/useful_tools.md

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,85 @@ python tools/analysis_tools/offline_eval.py configs/textdet/psenet/psenet_r50_fp
4545

4646
此外,基于此工具,用户也可以将其他算法库获取的预测结果转换成 MMOCR 支持的格式,从而使用 MMOCR 内置的评估指标来对其他算法库的模型进行评测。
4747

48-
| 参数 | 类型 | 说明 |
49-
| ------------- | ----- | ---------------------------------------- |
50-
| config | str | (必须)配置文件路径。 |
51-
| pkl_results | str | (必须)预先保存的预测结果文件。 |
52-
| --cfg-options | float | 用于覆写配置文件中的指定参数。[示例](<>) |
48+
| 参数 | 类型 | 说明 |
49+
| ------------- | ----- | ---------------------------------------------------------------- |
50+
| config | str | (必须)配置文件路径。 |
51+
| pkl_results | str | (必须)预先保存的预测结果文件。 |
52+
| --cfg-options | float | 用于覆写配置文件中的指定参数。[示例](./config.md#命令行修改配置) |
53+
54+
### 计算 FLOPs 和参数量
55+
56+
我们提供一个计算 FLOPs 和参数量的方法,首先我们使用以下命令安装依赖。
57+
58+
```shell
59+
pip install fvcore
60+
```
61+
62+
计算 FLOPs 和参数量的脚本使用方法如下:
63+
64+
```shell
65+
python tools/analysis_tools/get_flops.py ${config} --shape ${IMAGE_SHAPE}
66+
```
67+
68+
| 参数 | 类型 | 说明 |
69+
| ------- | ------ | ------------------------------------------------------------------ |
70+
| config | str | (必须) 配置文件路径。 |
71+
| --shape | int\*2 | 计算 FLOPs 使用的图片尺寸,如 `--shape 320 320`。 默认为 `640 640` |
72+
73+
获取 `dbnet_resnet18_fpnc_100k_synthtext.py` FLOPs 和参数量的示例命令如下。
74+
75+
```shell
76+
python tools/analysis_tools/get_flops.py configs/textdet/dbnet/dbnet_resnet18_fpnc_100k_synthtext.py --shape 1024 1024
77+
```
78+
79+
输出如下:
80+
81+
```shell
82+
input shape is (1, 3, 1024, 1024)
83+
| module | #parameters or shape | #flops |
84+
| :------------------------ | :------------------- | :------ |
85+
| model | 12.341M | 63.955G |
86+
| backbone | 11.177M | 38.159G |
87+
| backbone.conv1 | 9.408K | 2.466G |
88+
| backbone.conv1.weight | (64, 3, 7, 7) | |
89+
| backbone.bn1 | 0.128K | 83.886M |
90+
| backbone.bn1.weight | (64,) | |
91+
| backbone.bn1.bias | (64,) | |
92+
| backbone.layer1 | 0.148M | 9.748G |
93+
| backbone.layer1.0 | 73.984K | 4.874G |
94+
| backbone.layer1.1 | 73.984K | 4.874G |
95+
| backbone.layer2 | 0.526M | 8.642G |
96+
| backbone.layer2.0 | 0.23M | 3.79G |
97+
| backbone.layer2.1 | 0.295M | 4.853G |
98+
| backbone.layer3 | 2.1M | 8.616G |
99+
| backbone.layer3.0 | 0.919M | 3.774G |
100+
| backbone.layer3.1 | 1.181M | 4.842G |
101+
| backbone.layer4 | 8.394M | 8.603G |
102+
| backbone.layer4.0 | 3.673M | 3.766G |
103+
| backbone.layer4.1 | 4.721M | 4.837G |
104+
| neck | 0.836M | 14.887G |
105+
| neck.lateral_convs | 0.246M | 2.013G |
106+
| neck.lateral_convs.0.conv | 16.384K | 1.074G |
107+
| neck.lateral_convs.1.conv | 32.768K | 0.537G |
108+
| neck.lateral_convs.2.conv | 65.536K | 0.268G |
109+
| neck.lateral_convs.3.conv | 0.131M | 0.134G |
110+
| neck.smooth_convs | 0.59M | 12.835G |
111+
| neck.smooth_convs.0.conv | 0.147M | 9.664G |
112+
| neck.smooth_convs.1.conv | 0.147M | 2.416G |
113+
| neck.smooth_convs.2.conv | 0.147M | 0.604G |
114+
| neck.smooth_convs.3.conv | 0.147M | 0.151G |
115+
| det_head | 0.329M | 10.909G |
116+
| det_head.binarize | 0.164M | 10.909G |
117+
| det_head.binarize.0 | 0.147M | 9.664G |
118+
| det_head.binarize.1 | 0.128K | 20.972M |
119+
| det_head.binarize.3 | 16.448K | 1.074G |
120+
| det_head.binarize.4 | 0.128K | 83.886M |
121+
| det_head.binarize.6 | 0.257K | 67.109M |
122+
| det_head.threshold | 0.164M | |
123+
| det_head.threshold.0 | 0.147M | |
124+
| det_head.threshold.1 | 0.128K | |
125+
| det_head.threshold.3 | 16.448K | |
126+
| det_head.threshold.4 | 0.128K | |
127+
| det_head.threshold.6 | 0.257K | |
128+
!!!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.
129+
```

tools/analysis_tools/get_flops.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright (c) OpenMMLab. All rights reserved.
2+
import argparse
3+
4+
import torch
5+
from fvcore.nn import FlopCountAnalysis, flop_count_table
6+
from mmengine import Config
7+
8+
from mmocr.registry import MODELS
9+
from mmocr.utils import register_all_modules
10+
11+
register_all_modules()
12+
13+
14+
def parse_args():
15+
parser = argparse.ArgumentParser(description='Train a detector')
16+
parser.add_argument('config', help='train config file path')
17+
parser.add_argument(
18+
'--shape',
19+
type=int,
20+
nargs='+',
21+
default=[640, 640],
22+
help='input image size')
23+
args = parser.parse_args()
24+
return args
25+
26+
27+
def main():
28+
29+
args = parse_args()
30+
31+
if len(args.shape) == 1:
32+
h = w = args.shape[0]
33+
elif len(args.shape) == 2:
34+
h, w = args.shape
35+
else:
36+
raise ValueError('invalid input shape, please use --shape h w')
37+
38+
input_shape = (1, 3, h, w)
39+
40+
cfg = Config.fromfile(args.config)
41+
model = MODELS.build(cfg.model)
42+
43+
flops = FlopCountAnalysis(model, torch.ones(input_shape))
44+
45+
# params = parameter_count_table(model)
46+
flops_data = flop_count_table(flops)
47+
48+
print(flops_data)
49+
50+
print('!!!Please be cautious if you use the results in papers. '
51+
'You may need to check if all ops are supported and verify that the '
52+
'flops computation is correct.')
53+
54+
55+
if __name__ == '__main__':
56+
main()

0 commit comments

Comments
 (0)