【问题标题】:What is the difference between the `num` and `count` parameters in a Caffe blob when using a Python layer?使用 Python 层时,Caffe blob 中的“num”和“count”参数有什么区别?
【发布时间】:2017-06-08 17:21:31
【问题描述】:

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

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

【问题讨论】:

    标签: python caffe


    【解决方案1】:

    根据these Caffe docs,num 是“已弃用的旧版形状访问器编号:改用 shape(0)。”

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

    所以,num 为您提供了许多元素,每个元素可能有多个通道、高度和宽度。 count 是值的总数。他们应该只在shape 中的每个维度都是 1 时才同意,shape(0) 除外。

    【讨论】:

      【解决方案2】:

      @Kundor 已经给出了很好的答案。我会放一个代码sn-p,它的输出在这里供以后仍然有困惑的人使用。而且如您所见,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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-02
        • 2011-02-22
        • 2020-03-02
        • 1970-01-01
        • 2018-04-08
        • 2020-11-10
        相关资源
        最近更新 更多