forked from osmr/imgclsmob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_stats.py
207 lines (191 loc) · 7.7 KB
/
model_stats.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import logging
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
from .pytorchcv.models.common import ChannelShuffle, ChannelShuffle2, Identity
from .pytorchcv.models.fishnet import InterpolationBlock, ChannelSqueeze
from .pytorchcv.models.irevnet import IRevDownscale, IRevSplitBlock, IRevMergeBlock
__all__ = ['measure_model']
def calc_block_num_params2(net):
net_params = filter(lambda p: p.requires_grad, net.parameters())
weight_count = 0
for param in net_params:
weight_count += np.prod(param.size())
return weight_count
def calc_block_num_params(module):
assert isinstance(module, nn.Module)
net_params = filter(lambda p: isinstance(p[1], nn.parameter.Parameter) and
p[1].requires_grad, module._parameters.items())
weight_count = 0
for param in net_params:
weight_count += np.prod(param[1].size())
return weight_count
def measure_model(model,
in_channels,
in_size):
"""
Calculate model statistics.
Parameters:
----------
model : HybridBlock
Tested model.
in_channels : int
Number of input channels.
in_size : tuple of two ints
Spatial size of the expected input image.
"""
global num_flops
global num_macs
global num_params
# global names
num_flops = 0
num_macs = 0
num_params = 0
# names = {}
def call_hook(module, x, y):
if not (isinstance(module, IRevSplitBlock) or isinstance(module, IRevMergeBlock)):
assert (len(x) == 1)
assert (x[0].shape[0] == 1)
assert (len(module._modules) == 0)
if isinstance(module, nn.Linear):
in_units = module.in_features
out_units = module.out_features
extra_num_macs = in_units * out_units
if module.bias is None:
extra_num_flops = (2 * in_units - 1) * out_units
else:
extra_num_flops = 2 * in_units * out_units
elif isinstance(module, nn.ReLU):
extra_num_flops = x[0].numel()
extra_num_macs = 0
elif isinstance(module, nn.Sigmoid):
extra_num_flops = 4 * x[0].numel()
extra_num_macs = 0
elif isinstance(module, nn.LeakyReLU):
extra_num_flops = 2 * x[0].numel()
extra_num_macs = 0
elif isinstance(module, nn.ReLU6):
extra_num_flops = x[0].numel()
extra_num_macs = 0
elif isinstance(module, nn.PReLU):
extra_num_flops = 3 * x[0].numel()
extra_num_macs = 0
elif isinstance(module, nn.Conv2d):
x_h = x[0].shape[2]
x_w = x[0].shape[3]
kernel_size = module.kernel_size
stride = module.stride
dilation = module.dilation
padding = module.padding
groups = module.groups
in_channels = module.in_channels
out_channels = module.out_channels
y_h = (x_h + 2 * padding[0] - dilation[0] * (kernel_size[0] - 1) - 1) // stride[0] + 1
y_w = (x_w + 2 * padding[1] - dilation[1] * (kernel_size[1] - 1) - 1) // stride[1] + 1
assert (out_channels == y.shape[1])
assert (y_h == y.shape[2])
assert (y_w == y.shape[3])
kernel_total_size = kernel_size[0] * kernel_size[1]
y_size = y_h * y_w
extra_num_macs = kernel_total_size * in_channels * y_size * out_channels // groups
if module.bias is None:
extra_num_flops = (2 * kernel_total_size * y_size - 1) * in_channels * out_channels // groups
else:
extra_num_flops = 2 * kernel_total_size * in_channels * y_size * out_channels // groups
elif isinstance(module, nn.BatchNorm2d):
extra_num_flops = 4 * x[0].numel()
extra_num_macs = 0
elif isinstance(module, nn.InstanceNorm2d):
extra_num_flops = 4 * x[0].numel()
extra_num_macs = 0
elif isinstance(module, nn.BatchNorm1d):
extra_num_flops = 4 * x[0].numel()
extra_num_macs = 0
elif type(module) in [nn.MaxPool2d, nn.AvgPool2d]:
assert (x[0].shape[1] == y.shape[1])
kernel_size = module.kernel_size if isinstance(module.kernel_size, tuple) else\
(module.kernel_size, module.kernel_size)
y_h = y.shape[2]
y_w = y.shape[3]
channels = x[0].shape[1]
y_size = y_h * y_w
pool_total_size = kernel_size[0] * kernel_size[1]
extra_num_flops = channels * y_size * pool_total_size
extra_num_macs = 0
elif type(module) in [nn.AdaptiveAvgPool2d, nn.AdaptiveMaxPool2d]:
assert (x[0].shape[1] == y.shape[1])
x_h = x[0].shape[2]
x_w = x[0].shape[3]
y_h = y.shape[2]
y_w = y.shape[3]
channels = x[0].shape[1]
y_size = y_h * y_w
pool_total_size = x_h * x_w
extra_num_flops = channels * y_size * pool_total_size
extra_num_macs = 0
elif isinstance(module, nn.Dropout):
extra_num_flops = 0
extra_num_macs = 0
elif isinstance(module, nn.Sequential):
assert (len(module._modules) == 0)
extra_num_flops = 0
extra_num_macs = 0
elif type(module) in [ChannelShuffle, ChannelShuffle2]:
extra_num_flops = x[0].numel()
extra_num_macs = 0
elif isinstance(module, nn.ZeroPad2d):
extra_num_flops = 0
extra_num_macs = 0
elif isinstance(module, Identity):
extra_num_flops = 0
extra_num_macs = 0
elif isinstance(module, InterpolationBlock):
extra_num_flops = x[0].numel()
extra_num_macs = 0
elif isinstance(module, ChannelSqueeze):
extra_num_flops = x[0].numel()
extra_num_macs = 0
elif isinstance(module, IRevDownscale):
extra_num_flops = 5 * x[0].numel()
extra_num_macs = 0
elif isinstance(module, IRevSplitBlock):
extra_num_flops = x[0].numel()
extra_num_macs = 0
elif isinstance(module, IRevMergeBlock):
extra_num_flops = x[0].numel()
extra_num_macs = 0
else:
raise TypeError('Unknown layer type: {}'.format(type(module)))
global num_flops
global num_macs
global num_params
# global names
num_flops += extra_num_flops
num_macs += extra_num_macs
# if module.name not in names:
# names[module.name] = 1
# num_params += calc_block_num_params(module)
num_params += calc_block_num_params(module)
def register_forward_hooks(a_module):
if len(a_module._modules) > 0:
assert (calc_block_num_params(a_module) == 0)
children_handles = []
for child_module in a_module._modules.values():
child_handles = register_forward_hooks(child_module)
children_handles += child_handles
return children_handles
else:
handle = a_module.register_forward_hook(call_hook)
return [handle]
hook_handles = register_forward_hooks(model)
x = Variable(torch.zeros(1, in_channels, in_size[0], in_size[1]))
model.eval()
model(x)
num_params1 = calc_block_num_params2(model)
if num_params != num_params1:
logging.warning(
'Calculated numbers of parameters are different: standard method: {},\tper-leaf method: {}'.format(
num_params1, num_params))
[h.remove() for h in hook_handles]
return num_flops, num_macs, num_params1