pytorch如何獲得模型的計算量和參數(shù)量
方法1 自帶
pytorch自帶方法,計算模型參數(shù)總量
total = sum([param.nelement() for param in model.parameters()]) print("Number of parameter: %.2fM" % (total/1e6))
或者
total = sum(p.numel() for p in model.parameters()) print("Total params: %.2fM" % (total/1e6))
方法2 編寫代碼
計算模型參數(shù)總量和模型計算量
def count_params(model, input_size=224): # param_sum = 0 with open('models.txt', 'w') as fm: fm.write(str(model)) # 計算模型的計算量 calc_flops(model, input_size) # 計算模型的參數(shù)總量 model_parameters = filter(lambda p: p.requires_grad, model.parameters()) params = sum([np.prod(p.size()) for p in model_parameters]) print('The network has {} params.'.format(params)) # 計算模型的計算量 def calc_flops(model, input_size): def conv_hook(self, input, output): batch_size, input_channels, input_height, input_width = input[0].size() output_channels, output_height, output_width = output[0].size() kernel_ops = self.kernel_size[0] * self.kernel_size[1] * (self.in_channels / self.groups) * ( 2 if multiply_adds else 1) bias_ops = 1 if self.bias is not None else 0 params = output_channels * (kernel_ops + bias_ops) flops = batch_size * params * output_height * output_width list_conv.append(flops) def linear_hook(self, input, output): batch_size = input[0].size(0) if input[0].dim() == 2 else 1 weight_ops = self.weight.nelement() * (2 if multiply_adds else 1) bias_ops = self.bias.nelement() flops = batch_size * (weight_ops + bias_ops) list_linear.append(flops) def bn_hook(self, input, output): list_bn.append(input[0].nelement()) def relu_hook(self, input, output): list_relu.append(input[0].nelement()) def pooling_hook(self, input, output): batch_size, input_channels, input_height, input_width = input[0].size() output_channels, output_height, output_width = output[0].size() kernel_ops = self.kernel_size * self.kernel_size bias_ops = 0 params = output_channels * (kernel_ops + bias_ops) flops = batch_size * params * output_height * output_width list_pooling.append(flops) def foo(net): childrens = list(net.children()) if not childrens: if isinstance(net, torch.nn.Conv2d): net.register_forward_hook(conv_hook) if isinstance(net, torch.nn.Linear): net.register_forward_hook(linear_hook) if isinstance(net, torch.nn.BatchNorm2d): net.register_forward_hook(bn_hook) if isinstance(net, torch.nn.ReLU): net.register_forward_hook(relu_hook) if isinstance(net, torch.nn.MaxPool2d) or isinstance(net, torch.nn.AvgPool2d): net.register_forward_hook(pooling_hook) return for c in childrens: foo(c) multiply_adds = False list_conv, list_bn, list_relu, list_linear, list_pooling = [], [], [], [], [] foo(model) if '0.4.' in torch.__version__: if assets.USE_GPU: input = torch.cuda.FloatTensor(torch.rand(2, 3, input_size, input_size).cuda()) else: input = torch.FloatTensor(torch.rand(2, 3, input_size, input_size)) else: input = Variable(torch.rand(2, 3, input_size, input_size), requires_grad=True) _ = model(input) total_flops = (sum(list_conv) + sum(list_linear) + sum(list_bn) + sum(list_relu) + sum(list_pooling)) print(' + Number of FLOPs: %.2fM' % (total_flops / 1e6 / 2))
方法3 thop
需要安裝thop
pip install thop
調(diào)用方法:計算模型參數(shù)總量和模型計算量,而且會打印每一層網(wǎng)絡(luò)的具體信息
from thop import profile input = torch.randn(1, 3, 224, 224) flops, params = profile(model, inputs=(input,)) print(flops) print(params)
或者
from torchvision.models import resnet50 from thop import profile # model = resnet50() checkpoints = '模型path' model = torch.load(checkpoints) model_name = 'yolov3 cut asff' input = torch.randn(1, 3, 224, 224) flops, params = profile(model, inputs=(input, ),verbose=True) print("%s | %.2f | %.2f" % (model_name, params / (1000 ** 2), flops / (1000 ** 3)))#這里除以1000的平方,是為了化成M的單位,
注意:輸入必須是四維的
提高輸出可讀性, 加入一下代碼。
from thop import clever_format macs, params = clever_format([flops, params], "%.3f")
方法4 torchstat
from torchstat import stat from torchvision.models import resnet50, resnet101, resnet152, resnext101_32x8d model = resnet50() stat(model, (3, 224, 224)) # (3,224,224)表示輸入圖片的尺寸
使用torchstat這個庫來查看網(wǎng)絡(luò)模型的一些信息,包括總的參數(shù)量params、MAdd、顯卡內(nèi)存占用量和FLOPs等。需要安裝torchstat:
pip install torchstat
方法5 ptflops
作用:計算模型參數(shù)總量和模型計算量
安裝方法: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.resnet18() flops, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True, print_per_layer_stat=True) #不用寫batch_size大小,默認(rèn)batch_size=1 print('Flops: ' + flops) print('Params: ' + params)
或者
from torchvision.models import resnet50 import torch import torchvision.models as models # import torch from ptflops import get_model_complexity_info # model = models.resnet50() #調(diào)用官方的模型, checkpoints = '自己模型的path' model = torch.load(checkpoints) model_name = 'yolov3 cut' flops, params = get_model_complexity_info(model, (3,320,320),as_strings=True,print_per_layer_stat=True) print("%s |%s |%s" % (model_name,flops,params))
注意,這里輸入一定是要tuple類型,且不需要輸入batch,直接輸入輸入通道數(shù)量與尺寸,如(3,320,320) 320為網(wǎng)絡(luò)輸入尺寸。
輸出為網(wǎng)絡(luò)模型的總參數(shù)量(單位M,即百萬)與計算量(單位G,即十億)
方法6 torchsummary
安裝:pip install torchsummary
使用方法:
from torchsummary import summary ... summary(your_model, input_size=(channels, H, W))
作用:
1、每一層的類型、shape 和 參數(shù)量
2、模型整體的參數(shù)量
3、模型大小,和 fp/bp 一次需要的內(nèi)存大小,可以用來估計最佳 batch_size
補(bǔ)充:pytorch計算模型算力與參數(shù)大小
ptflops介紹
這個腳本設(shè)計用于計算卷積神經(jīng)網(wǎng)絡(luò)中乘法-加法操作的理論數(shù)量。它還可以計算參數(shù)的數(shù)量和打印給定網(wǎng)絡(luò)的每層計算成本。
支持layer:Conv1d/2d/3d,ConvTranspose2d,BatchNorm1d/2d/3d,激活(ReLU, PReLU, ELU, ReLU6, LeakyReLU),Linear,Upsample,Poolings (AvgPool1d/2d/3d、MaxPool1d/2d/3d、adaptive ones)
安裝要求:Pytorch >= 0.4.1, torchvision >= 0.2.1
get_model_complexity_info()
get_model_complexity_info是ptflops下的一個方法,可以計算出網(wǎng)絡(luò)的算力與模型參數(shù)大小,并且可以輸出每層的算力消耗。
栗子
以輸出Mobilenet_v2算力信息為例:
from ptflops import get_model_complexity_info from torchvision import models net = models.mobilenet_v2() ops, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True, print_per_layer_stat=True, verbose=True)
從圖中可以看到,MobileNetV2在輸入圖像尺寸為(3, 224, 224)的情況下將會產(chǎn)生3.505MB的參數(shù),算力消耗為0.32G,同時還打印出了每個層所占用的算力,權(quán)重參數(shù)數(shù)量。當(dāng)然,整個模型的算力大小與模型大小也被存到了變量ops與params中。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python使用代理ip訪問網(wǎng)站的實(shí)例
今天小編就為大家分享一篇python使用代理ip訪問網(wǎng)站的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05如何使用python socket模塊實(shí)現(xiàn)簡單的文件下載
這篇文章主要介紹了如何使用python socket模塊實(shí)現(xiàn)簡單的文件下載,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09Appium+python自動化之連接模擬器并啟動淘寶APP(超詳解)
這篇文章主要介紹了Appium+python自動化之 連接模擬器并啟動淘寶APP(超詳解)本文以淘寶app為例,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-06-06使用Python實(shí)現(xiàn)管理系統(tǒng)附源碼
這篇文章主要為大家介紹了Python實(shí)現(xiàn)管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01python循環(huán)神經(jīng)網(wǎng)絡(luò)RNN函數(shù)tf.nn.dynamic_rnn使用
這篇文章主要為大家介紹了python循環(huán)神經(jīng)網(wǎng)絡(luò)RNN的tf.nn.dynamic_rnn使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05python使用opencv resize圖像不進(jìn)行插值的操作
這篇文章主要介紹了python使用opencv resize圖像不進(jìn)行插值的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07