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

What is the difference between the `num` and `count` parameters in a Caffe blob when using a Python layer?

在 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() 返回的值。

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

根据these Caffe docsnum是"Deprecated legacy shape accessor num: use shape(0) instead."

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

因此,num 为您提供了许多元素,每个元素可能有多个通道、高度和宽度。 count 是值的总数。他们应该只同意 shape 中的每个维度都是 1,除了 shape(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