image thumbnail

Calculate total number of parameters in pretrained models

version 1.3.0 (2.16 KB) by Amr Ez Eldin Elsayed Rashed
calculate total number of parameters in pretrained models

53 Downloads

Updated 15 Jul 2020

View License

calculate the total number of parameters in pretrained models
like alexnet,googlenet,...
clc;clear
%x=['googlenet','resnet18','resnet50','VGG16','darknet19','alexnet','mobilenet','Xception','custome'];
%par=[5976627,134272835,23487619,136310547,20838179,56880515,4233133,20808883,2985263]
%accuracy=[91.3,91.3,92,90.3,88.9,89.7,90.7,91.2,99.8]
%executiontime=[1,2,3,4,5,6,7,8,9]
%myDLnet=load('ALEXNET_1_among_5_folds.mat','netTransfer');%par 56880515
%myDLnet=load('xception_1_among_5_folds.mat','netTransfer');%par 20808883
%myDLnet=load('darknet19_1_among_5_folds.mat','netTransfer');%par 20838179
%myDLnet=load('googlenet_1_among_5_folds.mat','netTransfer');%par 5976627
%myDLnet=load('ResNet18_1_among_5_folds.mat','netTransfer');%par 11173251
%myDLnet=load('ResNet50_1_among_5_folds.mat','netTransfer');%par 23487619
%myDLnet=load('nasnetmobile_2_among_5_folds.mat','netTransfer');%par 4233133
% myDLnet=load('newcustomisedmodel_1_among_5_folds.mat','netTransfer');%par 2985263
% myDLnet=load('vgg16_1_among_5_folds.mat','netTransfer');%par 136310547
layers=myDLnet.netTransfer.Layers;
num_layers = size(layers,1);
num_para=0;
num_para1=0;
num_para2=0;
num_para3=0;
for i=2:num_layers
n=contains(class(myDLnet.netTransfer.Layers(i)),["Conv","Fully"]);
m=contains(class(myDLnet.netTransfer.Layers(i)),"Batch");
if (n==1)
num_para=num_para+prod(size(myDLnet.netTransfer.Layers(i,1).Weights));
num_para1=num_para1+prod(size(myDLnet.netTransfer.Layers(i,1).Bias));
end

if (m==1)
num_para2=num_para2+prod(size(myDLnet.netTransfer.Layers(i,1).Offset));
num_para3=num_para3+prod(size(myDLnet.netTransfer.Layers(i,1).Scale));
% disp(['num of Weights=',num2str(num_para),'for layer number =',num2str(i)])
% disp(['num of Bias=',num2str(num_para1),'for layer number =',num2str(i)])
end
end
total=num_para+num_para1;
disp(['total number of parameters=',num2str(total)])

Cite As

Amr Ez Eldin Elsayed Rashed (2021). Calculate total number of parameters in pretrained models (https://www.mathworks.com/matlabcentral/fileexchange/77771-calculate-total-number-of-parameters-in-pretrained-models), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2020a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!