|
a |
|
b/ViTPose/tools/analysis/get_flops.py |
|
|
1 |
# Copyright (c) OpenMMLab. All rights reserved. |
|
|
2 |
import argparse |
|
|
3 |
from functools import partial |
|
|
4 |
|
|
|
5 |
import torch |
|
|
6 |
|
|
|
7 |
from mmpose.apis.inference import init_pose_model |
|
|
8 |
|
|
|
9 |
try: |
|
|
10 |
from mmcv.cnn import get_model_complexity_info |
|
|
11 |
except ImportError: |
|
|
12 |
raise ImportError('Please upgrade mmcv to >0.6.2') |
|
|
13 |
|
|
|
14 |
|
|
|
15 |
def parse_args(): |
|
|
16 |
parser = argparse.ArgumentParser(description='Train a recognizer') |
|
|
17 |
parser.add_argument('config', help='train config file path') |
|
|
18 |
parser.add_argument( |
|
|
19 |
'--shape', |
|
|
20 |
type=int, |
|
|
21 |
nargs='+', |
|
|
22 |
default=[256, 192], |
|
|
23 |
help='input image size') |
|
|
24 |
parser.add_argument( |
|
|
25 |
'--input-constructor', |
|
|
26 |
'-c', |
|
|
27 |
type=str, |
|
|
28 |
choices=['none', 'batch'], |
|
|
29 |
default='none', |
|
|
30 |
help='If specified, it takes a callable method that generates ' |
|
|
31 |
'input. Otherwise, it will generate a random tensor with ' |
|
|
32 |
'input shape to calculate FLOPs.') |
|
|
33 |
parser.add_argument( |
|
|
34 |
'--batch-size', '-b', type=int, default=1, help='input batch size') |
|
|
35 |
parser.add_argument( |
|
|
36 |
'--not-print-per-layer-stat', |
|
|
37 |
'-n', |
|
|
38 |
action='store_true', |
|
|
39 |
help='Whether to print complexity information' |
|
|
40 |
'for each layer in a model') |
|
|
41 |
args = parser.parse_args() |
|
|
42 |
return args |
|
|
43 |
|
|
|
44 |
|
|
|
45 |
def batch_constructor(flops_model, batch_size, input_shape): |
|
|
46 |
"""Generate a batch of tensors to the model.""" |
|
|
47 |
batch = {} |
|
|
48 |
|
|
|
49 |
img = torch.ones(()).new_empty( |
|
|
50 |
(batch_size, *input_shape), |
|
|
51 |
dtype=next(flops_model.parameters()).dtype, |
|
|
52 |
device=next(flops_model.parameters()).device) |
|
|
53 |
|
|
|
54 |
batch['img'] = img |
|
|
55 |
return batch |
|
|
56 |
|
|
|
57 |
|
|
|
58 |
def main(): |
|
|
59 |
|
|
|
60 |
args = parse_args() |
|
|
61 |
|
|
|
62 |
if len(args.shape) == 1: |
|
|
63 |
input_shape = (3, args.shape[0], args.shape[0]) |
|
|
64 |
elif len(args.shape) == 2: |
|
|
65 |
input_shape = (3, ) + tuple(args.shape) |
|
|
66 |
else: |
|
|
67 |
raise ValueError('invalid input shape') |
|
|
68 |
|
|
|
69 |
model = init_pose_model(args.config) |
|
|
70 |
|
|
|
71 |
if args.input_constructor == 'batch': |
|
|
72 |
input_constructor = partial(batch_constructor, model, args.batch_size) |
|
|
73 |
else: |
|
|
74 |
input_constructor = None |
|
|
75 |
|
|
|
76 |
if args.input_constructor == 'batch': |
|
|
77 |
input_constructor = partial(batch_constructor, model, args.batch_size) |
|
|
78 |
else: |
|
|
79 |
input_constructor = None |
|
|
80 |
|
|
|
81 |
if hasattr(model, 'forward_dummy'): |
|
|
82 |
model.forward = model.forward_dummy |
|
|
83 |
else: |
|
|
84 |
raise NotImplementedError( |
|
|
85 |
'FLOPs counter is currently not currently supported with {}'. |
|
|
86 |
format(model.__class__.__name__)) |
|
|
87 |
|
|
|
88 |
flops, params = get_model_complexity_info( |
|
|
89 |
model, |
|
|
90 |
input_shape, |
|
|
91 |
input_constructor=input_constructor, |
|
|
92 |
print_per_layer_stat=(not args.not_print_per_layer_stat)) |
|
|
93 |
split_line = '=' * 30 |
|
|
94 |
input_shape = (args.batch_size, ) + input_shape |
|
|
95 |
print(f'{split_line}\nInput shape: {input_shape}\n' |
|
|
96 |
f'Flops: {flops}\nParams: {params}\n{split_line}') |
|
|
97 |
print('!!!Please be cautious if you use the results in papers. ' |
|
|
98 |
'You may need to check if all ops are supported and verify that the ' |
|
|
99 |
'flops computation is correct.') |
|
|
100 |
|
|
|
101 |
|
|
|
102 |
if __name__ == '__main__': |
|
|
103 |
main() |