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