flops counter.pytorch
1.0.0
该工具旨在计算神经网络中的倍增操作的理论量。它还可以计算给定网络的参数数量和打印每层计算成本。
ptflops有两个后端, pytorch和aten 。 pytorch后端是遗产,它仅考虑nn.Modules 。但是,它仍然很有用,因为它为CNN提供了更好的标准层分析。在所有其他情况下,建议使用aten Backend,它考虑了Aten操作,因此它涵盖了更多的模型架构(包括变压器)。默认后端是aten 。请,不要使用pytorch后端进行变压器体系结构。
aten后端verbose=True查看复杂性计算过程中未考虑的操作。nn.Module中的模块进行人均统计。筑巢第二层的更深模块在每层统计中未显示。ignore_modules选项强制ptflops忽略列出的模块。这对于研究目的可能很有用。例如,可以从计数过程中删除所有卷积,指定ignore_modules=[torch.ops.aten.convolution, torch.ops.aten._convolution] 。 pytorch后端实验支持:
torch.nn.functional.*和tensor.*操作。因此,不支持的操作并不促进最终的复杂性估计。请参阅ptflops/pytorch_ops.py:FUNCTIONAL_MAPPING,TENSOR_OPS_MAPPING以检查支持的操作。有时功能级别的挂钩与nn.Module的钩子发生冲突(例如,自定义)。在这种情况下,可以通过传递backend_specific_config={"count_functional" : False}来禁用使用这些操作。ptflops在随机张量上启动了给定的模型,并估计了推理过程中的计算量。复杂的模型可以具有多个输入,其中一些可能是可选的。要构建非平凡输入,可以使用get_model_complexity_info的input_constructor参数。 input_constructor是一个将输入空间分辨率作为元组的函数,并带有模型的命名输入参数返回dict。接下来,此命令将作为关键字参数传递给模型。verbose参数允许获取有关不影响最终数字的模块的信息。ignore_modules选项强制ptflops忽略列出的模块。这对于研究目的可能很有用。例如,一个人可以从计数过程中删除所有卷积,指定ignore_modules=[torch.nn.Conv2d] 。 pytorch> = 2.0。使用pip install ptflops==0.7.2.2与Torch 1.x一起使用。
来自PYPI:
pip install ptflops从这个存储库中:
pip install --upgrade git+https://github.com/sovrasov/flops-counter.pytorch.git 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 , backend = 'pytorch'
print_per_layer_stat = True , verbose = True )
print ( '{:<30} {:<8}' . format ( 'Computational complexity: ' , macs ))
print ( '{:<30} {:<8}' . format ( 'Number of parameters: ' , params ))
macs , params = get_model_complexity_info ( net , ( 3 , 224 , 224 ), as_strings = True , backend = 'aten'
print_per_layer_stat = True , verbose = True )
print ( '{:<30} {:<8}' . format ( 'Computational complexity: ' , macs ))
print ( '{:<30} {:<8}' . format ( 'Number of parameters: ' , params ))如果PTFlops对您的论文或技术报告很有用,请引用我:
@online{ptflops,
author = {Vladislav Sovrasov},
title = {ptflops: a flops counting tool for neural networks in pytorch framework},
year = 2018-2024,
url = {https://github.com/sovrasov/flops-counter.pytorch},
}
感谢@warmspringwinds和Horace He的最初版本。
| 模型 | 输入分辨率 | 参数(m) | Mac(g)( pytorch ) | Mac(g)( aten ) |
|---|---|---|---|---|
| Alexnet | 224x224 | 61.10 | 0.72 | 0.71 |
| Consnext_base | 224x224 | 88.59 | 15.43 | 15.38 |
| Densenet121 | 224x224 | 7.98 | 2.90 | |
| 效率网络_b0 | 224x224 | 5.29 | 0.41 | |
| EditigyNet_v2_m | 224x224 | 54.14 | 5.43 | |
| Googlenet | 224x224 | 13.00 | 1.51 | |
| inception_v3 | 224x224 | 27.16 | 5.75 | 5.71 |
| maxvit_t | 224x224 | 30.92 | 5.48 | |
| mnasnet1_0 | 224x224 | 4.38 | 0.33 | |
| Mobilenet_v2 | 224x224 | 3.50 | 0.32 | |
| mobilenet_v3_large | 224x224 | 5.48 | 0.23 | |
| regnet_y_1_6gf | 224x224 | 11.20 | 1.65 | |
| RESNET18 | 224x224 | 11.69 | 1.83 | 1.81 |
| RESNET50 | 224x224 | 25.56 | 4.13 | 4.09 |
| Resnext50_32x4d | 224x224 | 25.03 | 4.29 | |
| shufflenet_v2_x1_0 | 224x224 | 2.28 | 0.15 | |
| squeezenet1_0 | 224x224 | 1.25 | 0.84 | 0.82 |
| VGG16 | 224x224 | 138.36 | 15.52 | 15.48 |
| vit_b_16 | 224x224 | 86.57 | 17.61(错误) | 16.86 |
| wide_resnet50_2 | 224x224 | 68.88 | 11.45 |
模型|输入分辨率|参数(m)| Mac(G)