10

How to calculate number of parameters in a model e.g. LENET for mnist, or ConvNet for imagent model etc. Is there any specific function in caffe that returns or saves number of parameters in a model. regards

khan
  • 531
  • 6
  • 29
  • Look at net.params after you've loaded the CNN into the variable net. It contains the parameters (weights and biases) for each layer. – pir May 22 '15 at 22:15
  • 1
    do you know the command using terminal for caffe. However i found the farmula. i.e. Filters x channels x Kernel_Width x Kernel_Height + Bias's . This will give you parameters at one layer. similarly for others. However i needed any command in caffe using terminal, e.g. in matlab we have numel(net.params) you can say. – khan May 22 '15 at 22:35
  • 1
    There is an open [feature request](https://github.com/BVLC/caffe/issues/2507) at caffe's github for this functionality. – Shai Jun 01 '15 at 05:18
  • 1
    Thank you for raising the request. – khan Jun 01 '15 at 20:37
  • @khan it seems like no one is picking up on this feature request. It would be nice if others would comment on that thread on github to bring it to the attention of the caffe community. – Shai Jul 02 '15 at 05:58

2 Answers2

4

Here is a python snippet to compute the number of parameters in a Caffe model:

import caffe
caffe.set_mode_cpu()
import numpy as np
from numpy import prod, sum
from pprint import pprint

def print_net_parameters (deploy_file):
    print "Net: " + deploy_file
    net = caffe.Net(deploy_file, caffe.TEST)
    print "Layer-wise parameters: "
    pprint([(k, v[0].data.shape) for k, v in net.params.items()])
    print "Total number of parameters: " + str(sum([prod(v[0].data.shape) for k, v in net.params.items()]))

deploy_file = "/home/ubuntu/deploy.prototxt"
print_net_parameters(deploy_file)

# Sample output:
# Net: /home/ubuntu/deploy.prototxt
# Layer-wise parameters: 
#[('conv1', (96, 3, 11, 11)),
# ('conv2', (256, 48, 5, 5)),
# ('conv3', (384, 256, 3, 3)),
# ('conv4', (384, 192, 3, 3)),
# ('conv5', (256, 192, 3, 3)),
# ('fc6', (4096, 9216)),
# ('fc7', (4096, 4096)),
# ('fc8', (819, 4096))]
# Total number of parameters: 60213280

https://gist.github.com/kaushikpavani/a6a32bd87fdfe5529f0e908ed743f779

Kaushik Pavani
  • 381
  • 4
  • 10
1

I can offer an explicit way to do this via the Matlab interface (make sure the matcaffe is installed first). Basically, you extract set of parameters from each network layer and count them. In Matlab:

% load the network
net_model = <path to your *deploy.prototxt file>
net_weights = <path to your *.caffemodel file>
phase = 'test';
test_net = caffe.Net(net_model, net_weights, phase);

% get the list of layers
layers_list = test_net.layer_names;
% for those layers which have parameters, count them
counter = 0;
for j = 1:length(layers_list),
    if ~isempty(test_net.layers(layers_list{j}).params)
    feat = test_net.layers(layers_list{j}).params(1).get_data();
    counter = counter + numel(feat)
    end
end

In the end, 'counter' contains the number of parameters.