|
a |
|
b/tools/get_flops.py |
|
|
1 |
# Copyright (c) OpenMMLab. All rights reserved. |
|
|
2 |
import argparse |
|
|
3 |
|
|
|
4 |
from mmcv import Config |
|
|
5 |
from mmcv.cnn import get_model_complexity_info |
|
|
6 |
|
|
|
7 |
from mmseg.models import build_segmentor |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
def parse_args(): |
|
|
11 |
parser = argparse.ArgumentParser(description='Train a segmentor') |
|
|
12 |
parser.add_argument('config', help='train config file path') |
|
|
13 |
parser.add_argument( |
|
|
14 |
'--shape', |
|
|
15 |
type=int, |
|
|
16 |
nargs='+', |
|
|
17 |
default=[2048, 1024], |
|
|
18 |
help='input image size') |
|
|
19 |
args = parser.parse_args() |
|
|
20 |
return args |
|
|
21 |
|
|
|
22 |
|
|
|
23 |
def main(): |
|
|
24 |
|
|
|
25 |
args = parse_args() |
|
|
26 |
|
|
|
27 |
if len(args.shape) == 1: |
|
|
28 |
input_shape = (3, args.shape[0], args.shape[0]) |
|
|
29 |
elif len(args.shape) == 2: |
|
|
30 |
input_shape = (3, ) + tuple(args.shape) |
|
|
31 |
else: |
|
|
32 |
raise ValueError('invalid input shape') |
|
|
33 |
|
|
|
34 |
cfg = Config.fromfile(args.config) |
|
|
35 |
cfg.model.pretrained = None |
|
|
36 |
model = build_segmentor( |
|
|
37 |
cfg.model, |
|
|
38 |
train_cfg=cfg.get('train_cfg'), |
|
|
39 |
test_cfg=cfg.get('test_cfg')).cuda() |
|
|
40 |
model.eval() |
|
|
41 |
|
|
|
42 |
if hasattr(model, 'forward_dummy'): |
|
|
43 |
model.forward = model.forward_dummy |
|
|
44 |
else: |
|
|
45 |
raise NotImplementedError( |
|
|
46 |
'FLOPs counter is currently not currently supported with {}'. |
|
|
47 |
format(model.__class__.__name__)) |
|
|
48 |
|
|
|
49 |
flops, params = get_model_complexity_info(model, input_shape) |
|
|
50 |
split_line = '=' * 30 |
|
|
51 |
print('{0}\nInput shape: {1}\nFlops: {2}\nParams: {3}\n{0}'.format( |
|
|
52 |
split_line, input_shape, flops, params)) |
|
|
53 |
print('!!!Please be cautious if you use the results in papers. ' |
|
|
54 |
'You may need to check if all ops are supported and verify that the ' |
|
|
55 |
'flops computation is correct.') |
|
|
56 |
|
|
|
57 |
|
|
|
58 |
if __name__ == '__main__': |
|
|
59 |
main() |