使用Python层时,Caffe blob中的`num`和`count`参数有什么区别?

时间:2017-06-08 17:21:32

标签: python caffe

在Caffe的Python图层的Euclidean Loss Example中,使用bottom[0].num以及bottom[0].count

似乎两者具有完全相同的含义。

在Caffe blob.hpp中,有一些名称相同的函数定义为:

inline int count() const { return count_; }

inline int num() const { return LegacyShape(0); }

似乎count_跟踪blob中元素的数量,这似乎也是num()返回的值。

是这样的吗?我可以互换使用吗?

2 个答案:

答案 0 :(得分:4)

根据these Caffe docs`Operand should contain 2 column(s)e` 是一个"已弃用的旧版形状访问者编号:使用形状(0)代替。"

另一方面,count是所有维度的乘积。

因此,num为您提供了许多元素,每个元素可能有多个通道,高度和宽度。 num是值的总数。他们应该只同意count中的每个维度是否为1,shape除外。

答案 1 :(得分:0)

@Kundor已经给出了很好的答案。我将放置一个代码段,并将其输出到这里,供那些仍然有困惑的人使用。如您所见,count()方法更像是大步向前,num()height() width()一起显示尺寸。

#include <vector>
#include <iostream>
#include <caffe/blob.hpp>

using namespace std;
using namespace caffe;

int main(int argc, char *argv[])
{
    Blob<float> blob;
    cout << "size: " << blob.shape_string() << endl;

    blob.Reshape(1, 2, 3, 4);

    cout << "size: " << blob.shape_string() << endl;

    auto shape_vec = blob.shape();
    cout << "shape dimension: " << shape_vec.size() << endl;
    cout << "shape[0] " << shape_vec[0] << endl;
    cout << "shape[1] " << shape_vec[1] << endl;
    cout << "shape[2] " << shape_vec[2] << endl;
    cout << "shape[3] " << shape_vec[3] << endl;

    cout << "shape(0) " << blob.shape(0) << endl;
    cout << "shape(1) " << blob.shape(1) << endl;
    cout << "shape(2) " << blob.shape(2) << endl;
    cout << "shape(3) " << blob.shape(3) << endl;
    // cout << "shape(4) " << blob.shape(4) << endl;

    cout << "cout() test \n";
    cout << "num_axes() " << blob.num_axes() << endl; // 4
    cout << "cout() " << blob.count() << endl; // 24
    cout << "cout(0) " << blob.count(0) << endl; // 24
    cout << "cout(1) " << blob.count(1) << endl; // 24
    cout << "cout(2) " << blob.count(2) << endl; // 12
    cout << "cout(3) " << blob.count(3) << endl; // 4
    cout << "cout(4) " << blob.count(4) << endl;
    // cout << "cout(5) " << blob.count(5) << endl; // start_axis <= end_axis(5 vs. 4)

    // legacy interface
    cout << "num() " << blob.num() << endl;
    cout << "channels() " << blob.channels() << endl;
    cout << "height() " << blob.height() << endl;
    cout << "width() " << blob.width() << endl;

    return 0;
}

输出:

size: (0)
size: 1 2 3 4 (24)
shape dimension: 4
shape[0] 1
shape[1] 2
shape[2] 3
shape[3] 4
shape(0) 1
shape(1) 2
shape(2) 3
shape(3) 4
cout() test
num_axes() 4
cout() 24
cout(0) 24
cout(1) 24
cout(2) 12
cout(3) 4
cout(4) 1
num() 1
channels() 2
height() 3
width() 4