计算这两个值之前要进行模型加载,模型加载的方法见https://blog.csdn.net/confusingbird/article/details/103913915,
加载完模型后得到模型model
方法1:pytorch自带方法,计算模型参数总量
total = sum([param.nelement() for param in model.parameters()])
print("Number of parameter: %.2fM" % (total/1e6))
方法2:FeatherNet作者提供的一个方法,计算模型参数总量和模型计算量
def count_params(model, input_size=224):
# param_sum = 0
with open('models.txt', 'w') as fm:
fm.write(str(model))
# 计算模型的计算量
calc_flops(model, input_size)
# 计算模型的参数总量
model_parameters = filter(lambda p: p.requires_grad, model.parameters())
params = sum([np.prod(p.size()) for p in model_parameters])
print('The network has {} params.'.format(params))
# 计算模型的计算量
def calc_flops(model, input_size):
def conv_hook(self, input, output):
batch_size, input_channels, input_height, input_width = input[0].size()
output_channels, output_height, output_width = output[0].size()
kernel_ops = self.kernel_size[0] * self.kernel_size[1] * (self.in_channels / self.groups) * (
2 if multiply_adds else 1)
bias_ops = 1 if self.bias is not None else 0
params = output_channels * (kernel_ops + bias_ops)
flops = batch_size * params * output_height * output_width
list_conv.append(flops)
def linear_hook(self, input, output):
batch_size = input[0].size(0) if input[0].dim() == 2 else 1
weight_ops = self.weight.nelement() * (2 if multiply_adds else 1)
bias_ops = self.bias.nelement()
flops = batch_size * (weight_ops + bias_ops)
list_linear.append(flops)
def bn_hook(self, input, output):
list_bn.append(input[0].nelement())
def relu_hook(self, input, output):
list_relu.append(input[0].nelement())
def pooling_hook(self, input, output):
batch_size, input_channels, input_height, input_width = input[0].size()
output_channels, output_height, output_width = output[0].size()
kernel_ops = self.kernel_size * self.kernel_size
bias_ops = 0
params = output_channels * (kernel_ops + bias_ops)
flops = batch_size * params * output_height * output_width
list_pooling.append(flops)
def foo(net):
childrens = list(net.children())
if not childrens:
if isinstance(net, torch.nn.Conv2d):
net.register_forward_hook(conv_hook)
if isinstance(net, torch.nn.Linear):
net.register_forward_hook(linear_hook)
if isinstance(net, torch.nn.BatchNorm2d):
net.register_forward_hook(bn_hook)
if isinstance(net, torch.nn.ReLU):
net.register_forward_hook(relu_hook)
if isinstance(net, torch.nn.MaxPool2d) or isinstance(net, torch.nn.AvgPool2d):
net.register_forward_hook(pooling_hook)
return
for c in childrens:
foo(c)
multiply_adds = False
list_conv, list_bn, list_relu, list_linear, list_pooling = [], [], [], [], []
foo(model)
if '0.4.' in torch.__version__:
if assets.USE_GPU:
input = torch.cuda.FloatTensor(torch.rand(2, 3, input_size, input_size).cuda())
else:
input = torch.FloatTensor(torch.rand(2, 3, input_size, input_size))
else:
input = Variable(torch.rand(2, 3, input_size, input_size), requires_grad=True)
_ = model(input)
total_flops = (sum(list_conv) + sum(list_linear) + sum(list_bn) + sum(list_relu) + sum(list_pooling))
print(' + Number of FLOPs: %.2fM' % (total_flops / 1e6 / 2))
方法3:安装第三方库Thop
安装方法 :
pip install thop
调用方法:计算模型参数总量和模型计算量,而且会打印每一层网络的具体信息
from thop import profile
input = torch.randn(1, 3, 224, 224)
flops, params = profile(model, inputs=(input,))
print(flops)
print(params)
方法2和方法3计算的模型计算量有点差异,不知道什么原因