使用 Flops工具计算模型的计算参数量和计算量

使用 Flops工具计算模型的计算参数量和计算量

源代码:https://github.com/Lyken17/pytorch-OpCounter

"""
    使用 Flops 工具计算模型的计算参数量和计算量。
    author:czjing
    source: https://github.com/Lyken17/pytorch-OpCounter
    # 安装thop包,使用下面的代码
    # pip install thop
"""

# 导入必须的包
import torch
import torchvision
from thop import profile


if __name__ == '__main__':
    # 1. 定义好的模型
    model = torchvision.models.resnet50()

    # 2. 模型的输入(tensor)
    inputs = torch.randn(1, 3, 200, 200)

    # 3. 调用thop计算
    flops, params = profile(model, inputs=(inputs,))
    print('flops:', flops)
    print('params:', params)

输出结果示例:

"D:\Program Files (x86)\anaconda3\envs\cv\python.exe" E:/Pycharm/handTracking/models/flops_test/flops_test.py
[INFO] Register count_convNd() for <class 'torch.nn.modules.conv.Conv2d'>.
[INFO] Register count_bn() for <class 'torch.nn.modules.batchnorm.BatchNorm2d'>.
[INFO] Register zero_ops() for <class 'torch.nn.modules.activation.ReLU'>.
[INFO] Register zero_ops() for <class 'torch.nn.modules.pooling.MaxPool2d'>.
[WARN] Cannot find rule for <class 'torch.nn.modules.container.Sequential'>. Treat it as zero Macs and zero Params.
[WARN] Cannot find rule for <class 'torchvision.models.resnet.Bottleneck'>. Treat it as zero Macs and zero Params.
[INFO] Register count_adap_avgpool() for <class 'torch.nn.modules.pooling.AdaptiveAvgPool2d'>.
[INFO] Register count_linear() for <class 'torch.nn.modules.linear.Linear'>.
[WARN] Cannot find rule for <class 'torchvision.models.resnet.ResNet'>. Treat it as zero Macs and zero Params.
flops: 3517126400.0
params: 25557032.0

Process finished with exit code 0
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值