Caffe LENET或Imagenet模型中的参数数量

10

如何计算模型(例如用于MNIST的LENET或用于Imagenet的ConvNet)中的参数数量?是否在caffe中有特定的函数可以返回或保存模型中的参数数量? 谢谢。


把CNN加载到变量net中后,请查看net.params。它包含每个层的参数(权重和偏置)。 - pir
1
你知道在终端中使用Caffe的命令吗?不过我找到了公式,即 Filters x channels x Kernel_Width x Kernel_Height + Bias's。这将给出一个层的参数。类似地,对于其他层也是如此。然而,我需要在Caffe中使用终端的任何命令,例如在Matlab中我们有numel(net.params)。 - khan
1
caffe的github上有一个针对这个功能的特性请求 - Shai
1
感谢您提出请求。 - khan
@khan,看起来没有人注意到这个功能请求。如果其他人能在 Github 上的那个帖子上发表评论,以引起 Caffe 社区的关注,那就太好了。 - Shai
2个回答

4

这是一个用Python编写的片段,用于计算Caffe模型中参数的数量:

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


1

我可以通过Matlab界面提供一个明确的方法来做到这一点(首先确保已经安装了matcaffe)。 基本上,你需要从每个网络层中提取一组参数并计算它们。 在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

最终,“counter”包含参数的数量。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接