pytorch 計(jì)算Parameter和FLOP的操作
深度學(xué)習(xí)中,模型訓(xùn)練完后,查看模型的參數(shù)量和浮點(diǎn)計(jì)算量,在此記錄下:
1 THOP
在pytorch中有現(xiàn)成的包thop用于計(jì)算參數(shù)數(shù)量和FLOP,首先安裝thop:
pip install thop
注意安裝thop時(shí)可能出現(xiàn)如下錯(cuò)誤:

解決方法:
pip install --upgrade git+https://github.com/Lyken17/pytorch-OpCounter.git # 下載源碼安裝
使用方法如下:
from torchvision.models import resnet50 # 引入ResNet50模型 from thop import profile model = resnet50() flops, params = profile(model, input_size=(1, 3, 224,224)) # profile(模型,輸入數(shù)據(jù))
對(duì)于自己構(gòu)建的函數(shù)也一樣,例如shuffleNetV2
from thop import profile
from utils.ShuffleNetV2 import shufflenetv2 # 導(dǎo)入shufflenet2 模塊
import torch
model_shuffle = shufflenetv2(width_mult=0.5)
model = torch.nn.DataParallel(model_shuffle) # 調(diào)用shufflenet2 模型,該模型為自己定義的
flop, para = profile(model, input_size=(1, 3, 224, 224),)
print("%.2fM" % (flop/1e6), "%.2fM" % (para/1e6))
更多細(xì)節(jié),可參考thop GitHub鏈接: https://github.com/Lyken17/pytorch-OpCounter
2 計(jì)算參數(shù)
pytorch本身帶有計(jì)算參數(shù)的方法
from thop import profile
from utils.ShuffleNetV2 import shufflenetv2 # 導(dǎo)入shufflenet2 模塊
import torch
model_shuffle = shufflenetv2(width_mult=0.5)
model = torch.nn.DataParallel(model_shuffle)
total = sum([param.nelement() for param in model.parameters()])
print("Number of parameter: %.2fM" % (total / 1e6))
補(bǔ)充:pytorch: 計(jì)算網(wǎng)絡(luò)模型的計(jì)算量(FLOPs)和參數(shù)量(Params)
計(jì)算量:
FLOPs,F(xiàn)LOP時(shí)指浮點(diǎn)運(yùn)算次數(shù),s是指秒,即每秒浮點(diǎn)運(yùn)算次數(shù)的意思,考量一個(gè)網(wǎng)絡(luò)模型的計(jì)算量的標(biāo)準(zhǔn)。
參數(shù)量:
Params,是指網(wǎng)絡(luò)模型中需要訓(xùn)練的參數(shù)總數(shù)。
第一步:安裝模塊(thop)
pip install thop
第二步:計(jì)算
import torch
from thop import profile
net = Model() # 定義好的網(wǎng)絡(luò)模型
input = torch.randn(1, 3, 112, 112)
flops, params = profile(net, (inputs,))
print('flops: ', flops, 'params: ', params)
注意:
輸入input的第一維度是批量(batch size),批量的大小不回影響參數(shù)量, 計(jì)算量是batch_size=1的倍數(shù)
profile(net, (inputs,))的 (inputs,)中必須加上逗號(hào),否者會(huì)報(bào)錯(cuò)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
基于Python實(shí)現(xiàn)視頻的人臉融合功能
這篇文章主要介紹了用Python快速實(shí)現(xiàn)視頻的人臉融合功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
使用Tensorflow將自己的數(shù)據(jù)分割成batch訓(xùn)練實(shí)例
今天小編就為大家分享一篇使用Tensorflow將自己的數(shù)據(jù)分割成batch訓(xùn)練實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
如何使用PyCharm將代碼上傳到GitHub上(圖文詳解)
這篇文章主要介紹了如何使用PyCharm將代碼上傳到GitHub上(圖文詳解),文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python打印scrapy蜘蛛抓取樹(shù)結(jié)構(gòu)的方法
這篇文章主要介紹了Python打印scrapy蜘蛛抓取樹(shù)結(jié)構(gòu)的方法,實(shí)例分析了打印scrapy蜘蛛抓取樹(shù)結(jié)構(gòu)的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
pytorch DataLoader的num_workers參數(shù)與設(shè)置大小詳解
這篇文章主要介紹了pytorch DataLoader的num_workers參數(shù)與設(shè)置大小詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
Python循環(huán)語(yǔ)句之while循環(huán)和for循環(huán)詳解
在Python中,循環(huán)語(yǔ)句用于重復(fù)執(zhí)行一段代碼,直到滿(mǎn)足某個(gè)條件為止,在Python中,有兩種主要的循環(huán)語(yǔ)句:for循環(huán)和while循環(huán),本文就來(lái)給大家介紹一下這兩個(gè)循環(huán)的用法,需要的朋友可以參考下2023-08-08

