MCPcopy Create free account
hub / github.com/OpenGVLab/HumanBench / count_parameters_num

Function count_parameters_num

PATH/helper/param_count.py:17–43  ·  view source on GitHub ↗
(model)

Source from the content-addressed store, hash-verified

15
16
17def count_parameters_num(model):
18 count = 0
19 count_fc = 0
20 param_dict = {name:param for name,param in model.named_parameters()}
21 param_keys = param_dict.keys()
22 for m_name, m in model.named_modules():
23 if isinstance(m, nn.Conv2d) or isinstance(m, nn.BatchNorm2d) or isinstance(m, SyncBatchNorm2d):
24 weight_name = m_name + '.weight'
25 bias_name = m_name + '.bias'
26 if weight_name in param_keys:
27 temp_params = param_dict[weight_name]
28 count += temp_params.data.nelement()
29 if bias_name in param_keys:
30 temp_params = param_dict[bias_name]
31 count += temp_params.data.nelement()
32 elif isinstance(m, nn.Linear):
33 weight_name = m_name + '.weight'
34 bias_name = m_name + '.bias'
35 if weight_name in param_keys:
36 temp_params = param_dict[weight_name]
37 count_fc += temp_params.data.nelement()
38 if bias_name in param_keys:
39 temp_params = param_dict[bias_name]
40 count_fc += temp_params.data.nelement()
41 print('Number of conv/bn params: %.2fM' % (count / 1e6))
42 print('Number of linear params: %.2fM' % (count_fc / 1e6))
43 print('Number of all params: %.2fM' % ( (count+count_fc) / 1e6))
44
45# def count_flops(model, input_image_size):
46# counts = []

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected