[pytorch]thop计算模型算力和参数量

从github官网上转载的:
Count the MACs / FLOPs of your PyTorch model.

THOP: PyTorch-OpCounter

How to install

pip install thop (now continously intergrated on Github actions)

OR

pip install --upgrade git+https://github.com/Lyken17/pytorch-OpCounter.git

How to use

  • Basic usage

    from torchvision.models import resnet50
    from thop import profile
    model = resnet50()
    input = torch.randn(1, 3, 224, 224)
    macs, params = profile(model, inputs=(input, ))
    
  • Define the rule for 3rd party module.

    class YourModule(nn.Module):
        # your definition
    def count_your_model(model, x, y):
        # your rule here
    
    input = torch.randn(1, 3, 224, 224)
    macs, params = profile(model, inputs=(input, ), 
                            custom_ops={YourModule: count_your_model})
    
  • Improve the output readability

    Call thop.clever_format to give a better format of the output.

    from thop import clever_format
    macs, params = clever_format([macs, params], "%.3f")
    

Results of Recent Models

The implementation are adapted from torchvision. Following results can be obtained using benchmark/evaluate_famous_models.py.

ModelParams(M)MACs(G)
alexnet61.100.77
vgg11132.867.74
vgg11_bn132.877.77
vgg13133.0511.44
vgg13_bn133.0511.49
vgg16138.3615.61
vgg16_bn138.3715.66
vgg19143.6719.77
vgg19_bn143.6819.83
resnet1811.691.82
resnet3421.803.68
resnet5025.564.14
resnet10144.557.87
resnet15260.1911.61
wide_resnet101_2126.8922.84
wide_resnet50_268.8811.46
ModelParams(M)MACs(G)
resnext50_32x4d25.034.29
resnext101_32x8d88.7916.54
densenet1217.982.90
densenet16128.687.85
densenet16914.153.44
densenet20120.014.39
squeezenet1_01.250.82
squeezenet1_11.240.35
mnasnet0_52.220.14
mnasnet0_753.170.24
mnasnet1_04.380.34
mnasnet1_36.280.53
mobilenet_v23.500.33
shufflenet_v2_x0_51.370.05
shufflenet_v2_x1_02.280.15
shufflenet_v2_x1_53.500.31
shufflenet_v2_x2_07.390.60
inception_v327.165.75

关于macs和FLPOS

这篇博客里进行了论述:https://blog.csdn.net/weixin_42627397/article/details/124770595

Macs的定义为: 一次加法和一次乘法等于一个Mac
FLOPs的定义为: 一次加法算一个FLOP,而一次乘法也算一个FLOP
所以最终FLOPs = 2 * Macs

FLOPs = 2 * Macs

因此thop得到的macs还需要乘以2才是FLOPS

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值