pytorch查看網(wǎng)絡(luò)參數(shù)顯存占用量等操作
1.使用torchstat
pip install torchstat from torchstat import stat import torchvision.models as models model = models.resnet152() stat(model, (3, 224, 224))
關(guān)于stat函數(shù)的參數(shù),第一個(gè)應(yīng)該是模型,第二個(gè)則是輸入尺寸,3為通道數(shù)。我沒有調(diào)研該函數(shù)的詳細(xì)參數(shù),也不知道為什么使用的時(shí)候并不提示相應(yīng)的參數(shù)。
2.使用torchsummary
pip install torchsummary from torchsummary import summary summary(model.cuda(),input_size=(3,32,32),batch_size=-1)
使用該函數(shù)直接對(duì)參數(shù)進(jìn)行提示,可以發(fā)現(xiàn)直接有顯式輸入batch_size的地方,我自己的感覺好像該函數(shù)更好一些。但是!??!不知道為什么,該函數(shù)在我的機(jī)器上一直報(bào)錯(cuò)?。?!
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
Update:經(jīng)過論壇咨詢,報(bào)錯(cuò)的原因找到了,只需要把
pip install torchsummary
修改為
pip install torch-summary
補(bǔ)充:Pytorch查看模型參數(shù)并計(jì)算模型參數(shù)量與可訓(xùn)練參數(shù)量
查看模型參數(shù)(以AlexNet為例)
import torch
import torch.nn as nn
import torchvision
class AlexNet(nn.Module):
def __init__(self,num_classes=1000):
super(AlexNet,self).__init__()
self.feature_extraction = nn.Sequential(
nn.Conv2d(in_channels=3,out_channels=96,kernel_size=11,stride=4,padding=2,bias=False),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3,stride=2,padding=0),
nn.Conv2d(in_channels=96,out_channels=192,kernel_size=5,stride=1,padding=2,bias=False),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3,stride=2,padding=0),
nn.Conv2d(in_channels=192,out_channels=384,kernel_size=3,stride=1,padding=1,bias=False),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=384,out_channels=256,kernel_size=3,stride=1,padding=1,bias=False),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=256,out_channels=256,kernel_size=3,stride=1,padding=1,bias=False),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=0),
)
self.classifier = nn.Sequential(
nn.Dropout(p=0.5),
nn.Linear(in_features=256*6*6,out_features=4096),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5),
nn.Linear(in_features=4096, out_features=4096),
nn.ReLU(inplace=True),
nn.Linear(in_features=4096, out_features=num_classes),
)
def forward(self,x):
x = self.feature_extraction(x)
x = x.view(x.size(0),256*6*6)
x = self.classifier(x)
return x
if __name__ =='__main__':
# model = torchvision.models.AlexNet()
model = AlexNet()
# 打印模型參數(shù)
#for param in model.parameters():
#print(param)
#打印模型名稱與shape
for name,parameters in model.named_parameters():
print(name,':',parameters.size())
feature_extraction.0.weight : torch.Size([96, 3, 11, 11]) feature_extraction.3.weight : torch.Size([192, 96, 5, 5]) feature_extraction.6.weight : torch.Size([384, 192, 3, 3]) feature_extraction.8.weight : torch.Size([256, 384, 3, 3]) feature_extraction.10.weight : torch.Size([256, 256, 3, 3]) classifier.1.weight : torch.Size([4096, 9216]) classifier.1.bias : torch.Size([4096]) classifier.4.weight : torch.Size([4096, 4096]) classifier.4.bias : torch.Size([4096]) classifier.6.weight : torch.Size([1000, 4096]) classifier.6.bias : torch.Size([1000])
計(jì)算參數(shù)量與可訓(xùn)練參數(shù)量
def get_parameter_number(model):
total_num = sum(p.numel() for p in model.parameters())
trainable_num = sum(p.numel() for p in model.parameters() if p.requires_grad)
return {'Total': total_num, 'Trainable': trainable_num}
第三方工具
from torchstat import stat import torchvision.models as models model = models.alexnet() stat(model, (3, 224, 224))

from torchvision.models import alexnet import torch from thop import profile model = alexnet() input = torch.randn(1, 3, 224, 224) flops, params = profile(model, inputs=(input, )) print(flops, params)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Python中reduce()函數(shù)的語法參數(shù)與作用詳解
這篇文章主要介紹了Python中reduce()函數(shù)的語法參數(shù)與作用詳解,reduce函數(shù)是通過函數(shù)對(duì)迭代器對(duì)象中的元素進(jìn)行遍歷操作,Python3.x中reduce函數(shù)已經(jīng)從內(nèi)置函數(shù)中取消了,轉(zhuǎn)而放在functools模塊中,需要的朋友可以參考下2023-08-08
pycharm 無法加載文件activate.ps1的原因分析及解決方法
這篇文章主要介紹了pycharm報(bào)錯(cuò)提示:無法加載文件\venv\Scripts\activate.ps1,因?yàn)樵诖讼到y(tǒng)上禁止運(yùn)行腳本,解決方法終端輸入get-executionpolicy,回車返回Restricted即可,需要的朋友可以參考下2022-11-11
使用Python PIL庫讀取文件批量處理圖片大小實(shí)現(xiàn)
這篇文章主要為大家介紹了使用Python PIL庫讀取文件批量處理圖片大小實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Python快速實(shí)現(xiàn)一鍵摳圖功能的全過程
你有沒想過,Python也能成為這樣的一種工具:在只有一張圖片,需要細(xì)致地?fù)赋鋈宋锏那闆r下,能幫你減少摳圖步驟,這篇文章主要給大家介紹了關(guān)于Python快速實(shí)現(xiàn)一鍵摳圖功能的相關(guān)資料,需要的朋友可以參考下2021-06-06
python使用pandas自動(dòng)化合并Excel文件的實(shí)現(xiàn)方法
在數(shù)據(jù)分析和處理工作中,經(jīng)常會(huì)遇到需要合并多個(gè)Excel文件的情況,本文介紹了一種使用Python編程語言中的Pandas庫和Glob模塊來自動(dòng)化合并Excel文件的方法,需要的朋友可以參考下2024-06-06
從CentOS安裝完成到生成詞云python的實(shí)例
下面小編就為大家分享一篇從CentOS安裝完成到生成詞云python的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-12-12
使用Python將PDF表格提取到文本,CSV和Excel文件中
本文將介紹如何使用簡單的Python代碼從PDF文檔中提取表格數(shù)據(jù)并將其寫入文本、CSV和Excel文件,從而輕松實(shí)現(xiàn)PDF表格的自動(dòng)化提取,有需要的可以參考下2024-11-11
numpy判斷數(shù)值類型、過濾出數(shù)值型數(shù)據(jù)的方法
今天小編就為大家分享一篇numpy判斷數(shù)值類型、過濾出數(shù)值型數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06

