caffe中的Blob塊


首先說明:Blob定義了一個類模板。

讓我們看一下Blob的頭文件里有什么哈:

定義了一個全局變量:

const int kMaxBlobAxes = 32;

看看它的構造函數:

Blob() : data_(), diff_(), count_(0), capacity_(0) {};
explicit Blob(const int num, const int channels, const int height,const int width);
explicit Blob(const vector<int>& shape);

Reshape函數:

void Reshape(const int num, const int channels, const int height, const int width);
void Reshape(const vector
<int>& shape);
void Reshape(const BlobShape
& shape);

void ReshapeLike(const Blob& other);

 

內聯函數shape_string:作用就是把shape的數據變為字符輸出,里面用到了ostringstream流;

inline string shape_string() const {                                                                                                         
    ostringstream stream;
    for (int i = 0; i < shape_.size(); ++i) {
      stream << shape_[i] << " ";
    }
    stream << "(" << count_ << ")";
    return stream.str();
  }

內聯函數shape():

//作用:返回shape_的引用;
inline const vector<int>& shape() const { return shape_; }
//作用:輸入第幾維度,返回該維度的大小;
inline int shape(int index) const {
    return shape_[CanonicalAxisIndex(index)];
 
 
//內聯函數:num_axes(),作用:返回是多少維度的塊;
inline int num_axes() const { return shape_.size(); }
//內聯函數:count():
inline int count() const { return count_; }   //返回一共多少數據啊,;
inline int count(int start_axis, int end_axis) const //返回輸入的索引之間的大小;
inline int count(int start_axis) const  //太麻煩了,不寫了,自己看代碼吧;

canonicalAxisIndex的作用就是:把正的索引不變,負的改為正的,另外,還是檢查一下它們的范圍啦;

CanonicalAxisIndex(int axis_index) const

offset函數就是計算一下索引的偏移值,因為吧,多維數組在內存里也是按一維 存放的:

inline int offset(const int n, const int c = 0, const int h = 0, const int w = 0) const {
……
return ((n * channels() + c) * height() + h) * width() + w;
}
// 用向量的方式把坐標值傳入:
inline int offset(const vector<int>& indices) const

函數copyfrom,作用把數據從Blob塊中復制到內存中

void CopyFrom(const Blob<Dtype>& source, bool copy_diff = false, bool reshape = false);

查看數據的函數:

inline Dtype data_at(const int n, const int c, const int h, const int w) const
inline Dtype diff_at(const int n, const int c, const int h, const int w) const
 inline Dtype data_at(const vector<int>& index) const
inline Dtype diff_at(const vector<int>& index) const

獲得data_與diff_的函數:

inline const shared_ptr<SyncedMemory>& data() const {
    CHECK(data_);
    return data_;
  }

inline const shared_ptr<SyncedMemory>& diff() const {
    CHECK(diff_);
    return diff_;                                                                                                                              
  }

另外還有好幾個函數,尼瑪,有點多哎

const Dtype* cpu_data() const;
  void set_cpu_data(Dtype* data);
  const int* gpu_shape() const;
  const Dtype* gpu_data() const;
  const Dtype* cpu_diff() const;
  const Dtype* gpu_diff() const;
  Dtype* mutable_cpu_data();
  Dtype* mutable_gpu_data();
  Dtype* mutable_cpu_diff();
  Dtype* mutable_gpu_diff();                                                                                                                   
  void Update();
  void FromProto(const BlobProto& proto, bool reshape = true);
  void ToProto(BlobProto* proto, bool write_diff = false) const;

  /// @brief Compute the sum of absolute values (L1 norm) of the data.
  Dtype asum_data() const;
  /// @brief Compute the sum of absolute values (L1 norm) of the diff.
  Dtype asum_diff() const;
  /// @brief Compute the sum of squares (L2 norm squared) of the data.
  Dtype sumsq_data() const;
  /// @brief Compute the sum of squares (L2 norm squared) of the diff.
  Dtype sumsq_diff() const;
/// @brief Scale the blob data by a constant factor.
  void scale_data(Dtype scale_factor);
  /// @brief Scale the blob diff by a constant factor.
  void scale_diff(Dtype scale_factor);

還有:

/**
   * @brief Set the data_ shared_ptr to point to the SyncedMemory holding the
   *        data_ of Blob other -- useful in Layer%s which simply perform a copy
   *        in their Forward pass.
   *
   * This deallocates the SyncedMemory holding this Blob's data_, as
   * shared_ptr calls its destructor when reset with the "=" operator.
   */
  void ShareData(const Blob& other);
  /**
   * @brief Set the diff_ shared_ptr to point to the SyncedMemory holding the
   *        diff_ of Blob other -- useful in Layer%s which simply perform a copy
   *        in their Forward pass.
   *
   * This deallocates the SyncedMemory holding this Blob's diff_, as
   * shared_ptr calls its destructor when reset with the "=" operator.
   */
  void ShareDiff(const Blob& other);

  bool ShapeEquals(const BlobProto& other);

下面是定義的變量:

shared_ptr<SyncedMemory> data_;
  shared_ptr<SyncedMemory> diff_;
  shared_ptr<SyncedMemory> shape_data_;
  vector<int> shape_;
  int count_;
  int capacity_;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM