pytorch 模型参数量、FLOPs统计方法

一、使用第三方工具:

torchstat:

安装:pip install torchstat
torchstat GitHub 源码页面

例子:

from torchstat import stat
model = model()
stat(model, (3, 1280, 1280))

输出:会输出模型各层网络的信息,最后进行总结统计。
在这里插入图片描述

ptflops:

安装:pip install ptflops
ptflops GithHub源码页面

例子:

import torchvision.models as models
import torch
from ptflops import get_model_complexity_info

with torch.cuda.device(0):
  net = models.densenet161()
  macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True,
                                           print_per_layer_stat=True, verbose=True)
  print('{:<30}  {:<8}'.format('Computational complexity: ', macs))
  print('{:<30}  {:<8}'.format('Number of parameters: ', params))

输出:同样会输出模型各层的信息,最后总结统计 参数量 和 FLOPs。

注意: 使用第三方工具时, 网络中有些层可能会不支持计算。

其他工具:

  • torchsummary
  • thop

二、使用函数统计模型参数量:

计算模型参数量 与 可训练参数量:

def get_parameter_number(model):
    total_num = sum(p.numel() for p in model.parameters())
    trainable_num = sum(p.numel() for p in model.parameters() if p.requires_grad)
    return {'Total': total_num, 'Trainable': trainable_num}

result = get_parameter_number(model)
print(result['Total'],result['Trainable']) #打印参数量
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值