【问题标题】:number of parameters in Caffe LENET or Imagenet modelsCaffe LENET 或 Imagenet 模型中的参数数量
【发布时间】:2015-08-04 21:11:57
【问题描述】:

如何计算模型中的参数数量,例如LENET 用于 mnist,或 ConvNet 用于 imagent 模型等。 caffe 中是否有任何特定函数可以返回或保存模型中的参数数量。 问候

【问题讨论】:

  • 在将 CNN 加载到变量 net 后查看 net.params。它包含每一层的参数(权重和偏差)。
  • 你知道caffe使用终端的命令吗?但是我找到了farmula。即过滤器 x 通道 x Kernel_Width x Kernel_Height + Bias's 。这将为您提供一层参数。对其他人也是如此。但是我需要使用终端在 caffe 中使用任何命令,例如在 matlab 我们有 numel(net.params) 你可以说。
  • 在 caffe 的 github 上有一个开放的 feature request 用于此功能。
  • 感谢您提出请求。
  • @khan 似乎没有人接受这个功能请求。如果其他人能在 github 上对该线程发表评论以引起 caffe 社区的注意,那就太好了。

标签: computer-vision neural-network deep-learning caffe matcaffe


【解决方案1】:

这是一个 python sn-p 来计算 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

【讨论】:

    【解决方案2】:

    我可以通过 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
    

    最后,“计数器”包含参数的数量。

    【讨论】:

      猜你喜欢
      • 2017-04-24
      • 2015-07-27
      • 2015-10-15
      • 2016-12-11
      • 2018-02-23
      • 1970-01-01
      • 2016-10-27
      • 2017-01-01
      • 1970-01-01
      相关资源
      最近更新 更多