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)