caffe源码分析之数据结构Blob

Caffe提供了一些基础的数据结构,从上到下包括net —>layer —>blob.
可以看出blob就是构成caffe大厦的最基础的砖头。本篇就来介绍一下Blob类的一些知识。
1.Blob类是caffe用来存储数据的类,是caffe的基本存储单元,提供了一系列接口来set数据,get数据,同时可以存储diff。Blob不仅可以存放比如图像等数据,还可以存放权值以及权值的diff。不同的layer之间传递数据必须通过Blob。
2.Blob是一个模板类,所以创建对象的时候需要指定类型,比如float,int等。初始化为float,里面存储的数据就是float类型。
3.caffe还会利用protobuf来生成一个BlobProto类,这个类是用来帮助Blob存储为文件和从文件中载入。因为一个net training出来的各个layer的权重参数也是存储在Blob中的,要把这个train好的网络存储下来,就需要将Blob中的权重数据存储下来,所以BlobProto就可以帮助干这个事情。还能帮助从文件中将以前train好的网络载入到内存中使用。BlobProto是利用protobuf自动生成的。在Blob类的内部没有创建BlobProto类,而是提供了两个接口,FromProto()和ToProto()来实现从BlobProto类来初始化一个Blob和将Blob的数据转换为BlobProto来写入文件等。关于protobuf的内容,可以参考我的另一篇博客 caffe中protobuf的使用
4.Blob类中有描述自己的形状,shape, shape是一个vector<int>, 用来描述数据的格式,对于图片数据来说,一般是num x channel x height x width. 也就是说明一个Blob可以存放多张图片,也就是一批图片,一张图片包含channel x height x width. 里面的数据是放在一个数组中的,所以是顺序存放的,所以需要访问某一张图片的某一个channel的第几行的第几个像素值,需要利用num, channel, height, width来计算出这个像素在数组中的索引才能找到。
5. Blob中存放数据的数组是利用shared_ptr来管理的,所以创建Blob的时候,就会根据传入的num, channel, height, width来计算buffer的大小,分配好内存了,这是自己分配的内存,所以需要自己free。但同时Blob还可以从外部传入数据指针,这时Blob内部有一个flag来记录是不是自己独占的数据,如果是外部传来的指针,则不会free。所以可以实现两个Blob拥有同一套数据,也就是同一个buffer。
6. Blob基本只提供了对数据的管理,没有神经网络方面的内容,另外提供了两个简单的计算,计算L1范数(元素和)和L2范数(元素的平方和)。
7. Caffe因为要兼容cpu mode和GPU mode,所以blob内部也实现了cpu mode和gpu mode的兼容,并提供了接口来获得cpu data或者gpu data。

这篇博客写的更详细一些,可以参考:
https://blog.csdn.net/happyday_d/article/details/103132379?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

https://blog.csdn.net/u010327085/article/details/50448912

blob的简单test: http://www.pianshen.com/article/6535187156/

下面我们进行一些源码分析:
1.我们前面介绍过protobuf在caffe中的作用,在caffe源码中,在src/caffe/proto目录下有一个protobuf描述文件caffe.proto,描述了在caffe中使用的structure(基本caffe中用到的所有的protobuf的message定义,都在这个文件中),这个文件通过protoc命令的编译,就生成了caffe.pb.h和caffe.pb.cc两个文件,这个不熟悉的可以参考我上面那篇介绍protobuf的博客。涉及blob的主要是 下面几个:

// Specifies the shape (dimensions) of a Blob. 说明Blob的数据维度,比如图片一般为num * channels * height * width 4维,存权重的话可能只有2维等,这里是表明存储的数据是几维的。
message BlobShape {
  repeated int64 dim = 1 [packed = true];
}

message BlobProto {
  optional BlobShape shape = 7;//数据的维度
  repeated float data = 5 [packed = true]; //float类型的data,一般会编译成vector的形式
  repeated float diff = 6 [packed = true]; //float类型的diff 数据
  repeated double double_data = 8 [packed = true]; //double类型的数据
  repeated double double_diff = 9 [packed = true]; //double类型的diff 数据

  // 4D dimensions -- deprecated.  Use "shape" instead.这几个参数废弃了,用BlobShape来替代了
  optional int32 num = 1 [default = 0];
  optional int32 channels = 2 [default = 0];
  optional int32 height = 3 [default = 0];
  optional int32 width = 4 [default = 0];
}

// The BlobProtoVector is simply a way to pass multiple blobproto instances
// around.
message BlobProtoVector {
  repeated BlobProto blobs = 1;
}

Blob通过上面几个定义,就和protobuf联系起来了。然后Blob类定义了两个接口:

void FromProto(const BlobProto& proto, bool reshape = true);
void ToProto(BlobProto* proto, bool write_diff = false) const;

通过ToProto函数,我们就可以将Blob对象转换为BlobProto对象,然后就可以将Blob中的数据串行化后存储到文件中,当从文件中将参数load到内存中的BlobProto对象后,就可以通过FromProto函数转换为Blob对象,这样我们就实现了将Blob对象中存储的数据存到文件中和从文件中加载到Blob对象中的功能。所以说一定要搞清楚Blob类和BlobProto类的关系。

下面我们看一下Blob类的头文件:我们把Deprecated函数都暂时删掉了

#ifndef CAFFE_BLOB_HPP_
#define CAFFE_BLOB_HPP_

#include <algorithm>
#include <string>
#include <vector>

#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"  //caffe.proto生成的头文件
#include "caffe/syncedmem.hpp" //为了cpu和gpu数据同步设计的类

const int kMaxBlobAxes = 32;

namespace caffe {
/**
 * @brief A wrapper around SyncedMemory holders serving as the basic
 *        computational unit through which Layer%s, Net%s, and Solver%s
 *        interact.
 *
 * TODO(dox): more thorough description.
 */
template <typename Dtype>
class Blob {
 public:
  Blob()
       : data_(), diff_(), count_(0), capacity_(0) {}
  explicit Blob(const vector<int>& shape); //构造函数,构造的时候需要指定维度
  /**
   * @brief Change the dimensions of the blob, allocating new memory if
   *        necessary.
   *
   * This function can be called both to create an initial allocation
   * of memory, and to adjust the dimensions of a top blob during Layer::Reshape
   * or Layer::Forward. When changing the size of blob, memory will only be
   * reallocated if sufficient memory does not already exist, and excess memory
   * will never be freed.
   *
   * Note that reshaping an input blob and immediately calling Net::Backward is
   * an error; either Net::Forward or Net::Reshape need to be called to
   * propagate the new input shape to higher layers.
   */
  void Reshape(const vector<int>& shape); //重新根据输入的形状扩充这个blob。
  void Reshape(const BlobShape& shape); //根据protobuf输入的shape来扩充这个blob。
  void ReshapeLike(const Blob& other); 
  inline string shape_string() const {  //输出shape的字符串信息,便于打印Log.
    ostringstream stream;
    for (int i = 0; i < shape_.size(); ++i) {
      stream << shape_[i] << " ";
    }
    stream << "(" << count_ << ")";
    return stream.str();
  }
  inline const vector<int>& shape() const { return shape_; }
  /**
   * @brief Returns the dimension of the index-th axis (or the negative index-th
   *        axis from the end, if index is negative).
   *
   * @param index the axis index, which may be negative as it will be
   *        "canonicalized" using CanonicalAxisIndex.
   *        Dies on out of range index.
   */
  inline int shape(int index) const {  //返回对应index的维度。CanonicalAxisIndex函数是为了可以输入负数来从后面开始index
    return shape_[CanonicalAxisIndex(index)];
  }
  inline int num_axes() const { return shape_.size(); } //返回这个blob有几个维度
  inline int count() const { return count_; }

  /**
   * @brief Compute the volume of a slice; i.e., the product of dimensions
   *        among a range of axes.
   *
   * @param start_axis The first axis to include in the slice.
   *
   * @param end_axis The first axis to exclude from the slice.
   */
  inline int count(int start_axis, int end_axis) const { //返回对应维度的数据数量,比如返回height*width
    CHECK_LE(start_axis, end_axis);
    CHECK_GE(start_axis, 0);
    CHECK_GE(end_axis, 0);
    CHECK_LE(start_axis, num_axes());
    CHECK_LE(end_axis, num_axes());
    int count = 1;
    for (int i = start_axis; i < end_axis; ++i) {
      count *= shape(i);
    }
    return count;
  }
  /**
   * @brief Compute the volume of a slice spanning from a particular first
   *        axis to the final axis.
   *
   * @param start_axis The first axis to include in the slice.
   */
  inline int count(int start_axis) const {
    return count(start_axis, num_axes());
  }

  /**
   * @brief Returns the 'canonical' version of a (usually) user-specified axis,
   *        allowing for negative indexing (e.g., -1 for the last axis).
   *
   * @param axis_index the axis index.
   *        If 0 <= index < num_axes(), return index.
   *        If -num_axes <= index <= -1, return (num_axes() - (-index)),
   *        e.g., the last axis index (num_axes() - 1) if index == -1,
   *        the second to last if index == -2, etc.
   *        Dies on out of range index.
   */
  inline int CanonicalAxisIndex(int axis_index) const {  //这就是上面用到的Index转换函数
    CHECK_GE(axis_index, -num_axes())
        << "axis " << axis_index << " out of range for " << num_axes()
        << "-D Blob with shape " << shape_string();
    CHECK_LT(axis_index, num_axes())
        << "axis " << axis_index << " out of range for " << num_axes()
        << "-D Blob with shape " << shape_string();
    if (axis_index < 0) {
      return axis_index + num_axes();
    }
    return axis_index;
  }

  /// @brief Deprecated legacy shape accessor num: use shape(0) instead. 都废弃了,用shape(index)来替代
  inline int num() const { return LegacyShape(0); }
  /// @brief Deprecated legacy shape accessor channels: use shape(1) instead.
  inline int channels() const { return LegacyShape(1); }
  /// @brief Deprecated legacy shape accessor height: use shape(2) instead.
  inline int height() const { return LegacyShape(2); }
  /// @brief Deprecated legacy shape accessor width: use shape(3) instead.
  inline int width() const { return LegacyShape(3); }
  inline int LegacyShape(int index) const {  //这个就是对Index做了一个有效性检测,确保不会越界
    CHECK_LE(num_axes(), 4)
        << "Cannot use legacy accessors on Blobs with > 4 axes.";
    CHECK_LT(index, 4);
    CHECK_GE(index, -4);
    if (index >= num_axes() || index < -num_axes()) {
      // Axis is out of range, but still in [0, 3] (or [-4, -1] for reverse
      // indexing) -- this special case simulates the one-padding used to fill
      // extraneous axes of legacy blobs.
      return 1;
    }
    return shape(index);
  }

  inline int offset(const int n, const int c = 0, const int h = 0, //返回对应num, channel, heigth, width的元素的位置(因为数据都是顺序存储的,offset其实就是对应数据的index)
      const int w = 0) const {
    CHECK_GE(n, 0);
    CHECK_LE(n, num());
    CHECK_GE(channels(), 0);
    CHECK_LE(c, channels());
    CHECK_GE(height(), 0);
    CHECK_LE(h, height());
    CHECK_GE(width(), 0);
    CHECK_LE(w, width());
    return ((n * channels() + c) * height() + h) * width() + w;
  }

  inline int offset(const vector<int>& indices) const {
    CHECK_LE(indices.size(), num_axes());
    int offset = 0;
    for (int i = 0; i < num_axes(); ++i) {
      offset *= shape(i);
      if (indices.size() > i) {
        CHECK_GE(indices[i], 0);
        CHECK_LT(indices[i], shape(i));
        offset += indices[i];
      }
    }
    return offset;
  }
  /**
   * @brief Copy from a source Blob.
   *
   * @param source the Blob to copy from
   * @param copy_diff if false, copy the data; if true, copy the diff
   * @param reshape if false, require this Blob to be pre-shaped to the shape
   *        of other (and die otherwise); if true, Reshape this Blob to other's
   *        shape if necessary
   */
  void CopyFrom(const Blob<Dtype>& source, bool copy_diff = false,
      bool reshape = false);//复制一个别的blob.

  inline Dtype data_at(const int n, const int c, const int h,  //返回对应的数据
      const int w) const {
    return cpu_data()[offset(n, c, h, w)];
  }

  inline Dtype diff_at(const int n, const int c, const int h, //返回对应的diff数据
      const int w) const {
    return cpu_diff()[offset(n, c, h, w)];
  }

  inline Dtype data_at(const vector<int>& index) const {
    return cpu_data()[offset(index)];
  }

  inline Dtype diff_at(const vector<int>& index) const {
    return cpu_diff()[offset(index)];
  }

  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;
  void set_gpu_data(Dtype* data);
  const Dtype* cpu_diff() const;
  const Dtype* gpu_diff() const;
  Dtype* mutable_cpu_data(); //用mutable修饰的数据指针,外部可以修改
  Dtype* mutable_gpu_data();
  Dtype* mutable_cpu_diff();
  Dtype* mutable_gpu_diff();
  void Update();
  void FromProto(const BlobProto& proto, bool reshape = true); //这两个函数就是前面介绍的两个函数,从BlobProto转换为Blob.
  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;  //求所有数据的绝对值的和,也就是L1范数
  /// @brief Compute the sum of absolute values (L1 norm) of the diff.
  Dtype asum_diff() const; //求所有diff的绝对值的和,也就是L1范数
  /// @brief Compute the sum of squares (L2 norm squared) of the data.
  Dtype sumsq_data() const; //求data的平方和,也就是L2范数
  /// @brief Compute the sum of squares (L2 norm squared) of the diff.
  Dtype sumsq_diff() const; //求diff的平方和,也就是L2范数

  /// @brief Scale the blob data by a constant factor.
  void scale_data(Dtype scale_factor);  //对data和diff作scale
  /// @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);//将other blob内部的数据指针(shared_ptr)复制到本Blob中,这样本blob就和other blob拥有了同一份数据
  /**
   * @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);//判断维度是否一样

 protected:
  shared_ptr<SyncedMemory> data_; 
  shared_ptr<SyncedMemory> diff_;
  shared_ptr<SyncedMemory> shape_data_;//这个里面放的数据和下面的shape_是一样的,也是为了给GPU同步用的。因为GPU接口不能认识vector
  vector<int> shape_; //内容和shape_data_一样,只是存储为vector.
  int count_; //存放有效元素数目信息
  int capacity_; //存放Blob容器的容量信息

  DISABLE_COPY_AND_ASSIGN(Blob);
};  // class Blob

}  // namespace caffe

#endif  // CAFFE_BLOB_HPP_

成员变量用到了SyncedMemory,下面这篇博客就是介绍SyncedMemory的,可以参考。
https://blog.csdn.net/happyday_d/article/details/102991885
下面分析blob.cpp文件:

#include <climits>
#include <vector>

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/syncedmem.hpp"
#include "caffe/util/math_functions.hpp"

namespace caffe {

template <typename Dtype>
void Blob<Dtype>::Reshape(const int num, const int channels, const int height,
    const int width) {
  vector<int> shape(4);
  shape[0] = num;
  shape[1] = channels;
  shape[2] = height;
  shape[3] = width;
  Reshape(shape); //利用上面传入的数据组建vector, 调用参数为vector的Reshape函数来初始化
}

template <typename Dtype>
void Blob<Dtype>::Reshape(const vector<int>& shape) {
  CHECK_LE(shape.size(), kMaxBlobAxes);
  count_ = 1; //暂时初始化为1
  shape_.resize(shape.size()); //利用传入的shape vector来初始化内部的shape_,同时下面创建shape_data_,并存放进同样的数据。
  if (!shape_data_ || shape_data_->size() < shape.size() * sizeof(int)) {
    shape_data_.reset(new SyncedMemory(shape.size() * sizeof(int)));
  }
  int* shape_data = static_cast<int*>(shape_data_->mutable_cpu_data());
  for (int i = 0; i < shape.size(); ++i) {
    CHECK_GE(shape[i], 0);
    if (count_ != 0) {
      CHECK_LE(shape[i], INT_MAX / count_) << "blob size exceeds INT_MAX";
    }
    count_ *= shape[i]; //这里计算了count, num * channel * height * width
    shape_[i] = shape[i];
    shape_data[i] = shape[i];
  }
  if (count_ > capacity_) {
    capacity_ = count_; //初始化容量capacity_,并且下面创建相应的内存来存放data & diff
    data_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));
    diff_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));
  }
}

template <typename Dtype>
void Blob<Dtype>::Reshape(const BlobShape& shape) {//利用protobuf的BlobShape来初始化
  CHECK_LE(shape.dim_size(), kMaxBlobAxes);
  vector<int> shape_vec(shape.dim_size());
  for (int i = 0; i < shape.dim_size(); ++i) {
    shape_vec[i] = shape.dim(i);
  }
  Reshape(shape_vec);//最后也一样是调用的vector为参数的Reshape函数
}

template <typename Dtype>
void Blob<Dtype>::ReshapeLike(const Blob<Dtype>& other) {//利用其他Blob的shape来初始化Blob
  Reshape(other.shape());
}

template <typename Dtype>
Blob<Dtype>::Blob(const int num, const int channels, const int height,  //构造函数,内部其实就是调用了Reshape函数来分配内存资源
    const int width)
  // capacity_ must be initialized before calling Reshape
  : capacity_(0) {
  Reshape(num, channels, height, width);
}

template <typename Dtype>
Blob<Dtype>::Blob(const vector<int>& shape)
  // capacity_ must be initialized before calling Reshape
  : capacity_(0) {
  Reshape(shape);
}

template <typename Dtype>
const int* Blob<Dtype>::gpu_shape() const {//GPU的shape就使用shape_data_,是个指针形式
  CHECK(shape_data_);
  return (const int*)shape_data_->gpu_data();
}

template <typename Dtype>
const Dtype* Blob<Dtype>::cpu_data() const {
  CHECK(data_);
  return (const Dtype*)data_->cpu_data();
}

template <typename Dtype>
void Blob<Dtype>::set_cpu_data(Dtype* data) {
  CHECK(data);
  // Make sure CPU and GPU sizes remain equal
  size_t size = count_ * sizeof(Dtype);
  if (data_->size() != size) {//如果data_里面buffer size不够大,需要重新分配。
    data_.reset(new SyncedMemory(size));
    diff_.reset(new SyncedMemory(size));
  }
  data_->set_cpu_data(data);//调用了SyncedMemory的接口。将data的指针set进去,注意这里不是copy,而是直接指针赋值,也就是Blob内部共享了外部传进来的指针。下面GPU也是同样。
}

template <typename Dtype>
const Dtype* Blob<Dtype>::gpu_data() const {
  CHECK(data_);
  return (const Dtype*)data_->gpu_data();
}

template <typename Dtype>
void Blob<Dtype>::set_gpu_data(Dtype* data) {
  CHECK(data);
  // Make sure CPU and GPU sizes remain equal
  size_t size = count_ * sizeof(Dtype);
  if (data_->size() != size) {
    data_.reset(new SyncedMemory(size));
    diff_.reset(new SyncedMemory(size));
  }
  data_->set_gpu_data(data);
}

template <typename Dtype>
const Dtype* Blob<Dtype>::cpu_diff() const {
  CHECK(diff_);
  return (const Dtype*)diff_->cpu_data();
}

template <typename Dtype>
const Dtype* Blob<Dtype>::gpu_diff() const {
  CHECK(diff_);
  return (const Dtype*)diff_->gpu_data();
}

template <typename Dtype>
Dtype* Blob<Dtype>::mutable_cpu_data() {  //用mutable修饰的,也就是可以更改buffer里面的数据
  CHECK(data_);
  return static_cast<Dtype*>(data_->mutable_cpu_data());
}

template <typename Dtype>
Dtype* Blob<Dtype>::mutable_gpu_data() {
  CHECK(data_);
  return static_cast<Dtype*>(data_->mutable_gpu_data());
}

template <typename Dtype>
Dtype* Blob<Dtype>::mutable_cpu_diff() {
  CHECK(diff_);
  return static_cast<Dtype*>(diff_->mutable_cpu_data());
}

template <typename Dtype>
Dtype* Blob<Dtype>::mutable_gpu_diff() {
  CHECK(diff_);
  return static_cast<Dtype*>(diff_->mutable_gpu_data());
}

template <typename Dtype>
void Blob<Dtype>::ShareData(const Blob& other) {//share 其他blob的数据
  CHECK_EQ(count_, other.count());
  data_ = other.data();
}

template <typename Dtype>
void Blob<Dtype>::ShareDiff(const Blob& other) {
  CHECK_EQ(count_, other.count());
  diff_ = other.diff();
}

// The "update" method is used for parameter blobs in a Net, which are stored
// as Blob<float> or Blob<double> -- hence we do not define it for
// Blob<int> or Blob<unsigned int>.
//从上面的英文注释可以看出,这个函数只定义了float和double类型的函数,没有定义int 和uint类型的
//void caffe_gpu_axpy(const int N, const Dtype alpha, const Dtype* X,
    Dtype* Y)  用到的这个函数是cblas计算库中的函数,这个函数是计算result = alpha * x + y,N代表x和y里面各有N个数据。所以其实update就是将data = data - diff,来更新权重值。
template <> void Blob<unsigned int>::Update() { NOT_IMPLEMENTED; }
template <> void Blob<int>::Update() { NOT_IMPLEMENTED; }

template <typename Dtype>
void Blob<Dtype>::Update() {
  // We will perform update based on where the data is located.
  switch (data_->head()) {
  case SyncedMemory::HEAD_AT_CPU:
    // perform computation on CPU
    caffe_axpy<Dtype>(count_, Dtype(-1),
        static_cast<const Dtype*>(diff_->cpu_data()),
        static_cast<Dtype*>(data_->mutable_cpu_data()));
    break;
  case SyncedMemory::HEAD_AT_GPU:
  case SyncedMemory::SYNCED:
#ifndef CPU_ONLY
    // perform computation on GPU
    caffe_gpu_axpy<Dtype>(count_, Dtype(-1),
        static_cast<const Dtype*>(diff_->gpu_data()),
        static_cast<Dtype*>(data_->mutable_gpu_data()));
#else
    NO_GPU;
#endif
    break;
  default:
    LOG(FATAL) << "Syncedmem not initialized.";
  }
}

template <> unsigned int Blob<unsigned int>::asum_data() const {
  NOT_IMPLEMENTED;
  return 0;
}

template <> int Blob<int>::asum_data() const {
  NOT_IMPLEMENTED;
  return 0;
}

template <typename Dtype>
Dtype Blob<Dtype>::asum_data() const { //计算绝对值之和
  if (!data_) { return 0; }
  switch (data_->head()) {
  case SyncedMemory::HEAD_AT_CPU:
    return caffe_cpu_asum(count_, cpu_data());
  case SyncedMemory::HEAD_AT_GPU:
  case SyncedMemory::SYNCED:
#ifndef CPU_ONLY
  {
    Dtype asum;
    caffe_gpu_asum(count_, gpu_data(), &asum);
    return asum;
  }
#else
    NO_GPU;
#endif
  case SyncedMemory::UNINITIALIZED:
    return 0;
  default:
    LOG(FATAL) << "Unknown SyncedMemory head state: " << data_->head();
  }
  return 0;
}

template <> unsigned int Blob<unsigned int>::asum_diff() const {
  NOT_IMPLEMENTED;
  return 0;
}

template <> int Blob<int>::asum_diff() const {
  NOT_IMPLEMENTED;
  return 0;
}

template <typename Dtype>
Dtype Blob<Dtype>::asum_diff() const {
  if (!diff_) { return 0; }
  switch (diff_->head()) {
  case SyncedMemory::HEAD_AT_CPU:
    return caffe_cpu_asum(count_, cpu_diff());
  case SyncedMemory::HEAD_AT_GPU:
  case SyncedMemory::SYNCED:
#ifndef CPU_ONLY
  {
    Dtype asum;
    caffe_gpu_asum(count_, gpu_diff(), &asum);
    return asum;
  }
#else
    NO_GPU;
#endif
  case SyncedMemory::UNINITIALIZED:
    return 0;
  default:
    LOG(FATAL) << "Unknown SyncedMemory head state: " << diff_->head();
  }
  return 0;
}

template <> unsigned int Blob<unsigned int>::sumsq_data() const {
  NOT_IMPLEMENTED;
  return 0;
}

template <> int Blob<int>::sumsq_data() const {
  NOT_IMPLEMENTED;
  return 0;
}

template <typename Dtype>
Dtype Blob<Dtype>::sumsq_data() const { //计算平方和,也就是L2范数
  Dtype sumsq;
  const Dtype* data;
  if (!data_) { return 0; }
  switch (data_->head()) {
  case SyncedMemory::HEAD_AT_CPU:
    data = cpu_data();
    sumsq = caffe_cpu_dot(count_, data, data);
    break;
  case SyncedMemory::HEAD_AT_GPU:
  case SyncedMemory::SYNCED:
#ifndef CPU_ONLY
    data = gpu_data();
    caffe_gpu_dot(count_, data, data, &sumsq);
#else
    NO_GPU;
#endif
    break;
  case SyncedMemory::UNINITIALIZED:
    return 0;
  default:
    LOG(FATAL) << "Unknown SyncedMemory head state: " << data_->head();
  }
  return sumsq;
}

template <> unsigned int Blob<unsigned int>::sumsq_diff() const {
  NOT_IMPLEMENTED;
  return 0;
}

template <> int Blob<int>::sumsq_diff() const {
  NOT_IMPLEMENTED;
  return 0;
}

template <typename Dtype>
Dtype Blob<Dtype>::sumsq_diff() const {
  Dtype sumsq;
  const Dtype* diff;
  if (!diff_) { return 0; }
  switch (diff_->head()) {
  case SyncedMemory::HEAD_AT_CPU:
    diff = cpu_diff();
    sumsq = caffe_cpu_dot(count_, diff, diff);
    break;
  case SyncedMemory::HEAD_AT_GPU:
  case SyncedMemory::SYNCED:
#ifndef CPU_ONLY
    diff = gpu_diff();
    caffe_gpu_dot(count_, diff, diff, &sumsq);
    break;
#else
    NO_GPU;
#endif
  case SyncedMemory::UNINITIALIZED:
    return 0;
  default:
    LOG(FATAL) << "Unknown SyncedMemory head state: " << data_->head();
  }
  return sumsq;
}

template <> void Blob<unsigned int>::scale_data(unsigned int scale_factor) {
  NOT_IMPLEMENTED;
}

template <> void Blob<int>::scale_data(int scale_factor) {
  NOT_IMPLEMENTED;
}

template <typename Dtype>
void Blob<Dtype>::scale_data(Dtype scale_factor) { //对data统一作scale
  Dtype* data;
  if (!data_) { return; }
  switch (data_->head()) {
  case SyncedMemory::HEAD_AT_CPU:
    data = mutable_cpu_data();
    caffe_scal(count_, scale_factor, data);
    return;
  case SyncedMemory::HEAD_AT_GPU:
  case SyncedMemory::SYNCED:
#ifndef CPU_ONLY
    data = mutable_gpu_data();
    caffe_gpu_scal(count_, scale_factor, data);
    return;
#else
    NO_GPU;
#endif
  case SyncedMemory::UNINITIALIZED:
    return;
  default:
    LOG(FATAL) << "Unknown SyncedMemory head state: " << data_->head();
  }
}

template <> void Blob<unsigned int>::scale_diff(unsigned int scale_factor) {
  NOT_IMPLEMENTED;
}

template <> void Blob<int>::scale_diff(int scale_factor) {
  NOT_IMPLEMENTED;
}

template <typename Dtype>
void Blob<Dtype>::scale_diff(Dtype scale_factor) {
  Dtype* diff;
  if (!diff_) { return; }
  switch (diff_->head()) {
  case SyncedMemory::HEAD_AT_CPU:
    diff = mutable_cpu_diff();
    caffe_scal(count_, scale_factor, diff);
    return;
  case SyncedMemory::HEAD_AT_GPU:
  case SyncedMemory::SYNCED:
#ifndef CPU_ONLY
    diff = mutable_gpu_diff();
    caffe_gpu_scal(count_, scale_factor, diff);
    return;
#else
    NO_GPU;
#endif
  case SyncedMemory::UNINITIALIZED:
    return;
  default:
    LOG(FATAL) << "Unknown SyncedMemory head state: " << diff_->head();
  }
}

template <typename Dtype>
bool Blob<Dtype>::ShapeEquals(const BlobProto& other) { //看看两个shape是否一样
  if (other.has_num() || other.has_channels() ||
      other.has_height() || other.has_width()) {
    // Using deprecated 4D Blob dimensions --
    // shape is (num, channels, height, width).
    // Note: we do not use the normal Blob::num(), Blob::channels(), etc.
    // methods as these index from the beginning of the blob shape, where legacy
    // parameter blobs were indexed from the end of the blob shape (e.g., bias
    // Blob shape (1 x 1 x 1 x N), IP layer weight Blob shape (1 x 1 x M x N)).
    return shape_.size() <= 4 &&
           LegacyShape(-4) == other.num() &&
           LegacyShape(-3) == other.channels() &&
           LegacyShape(-2) == other.height() &&
           LegacyShape(-1) == other.width();
  }
  vector<int> other_shape(other.shape().dim_size());
  for (int i = 0; i < other.shape().dim_size(); ++i) {
    other_shape[i] = other.shape().dim(i);
  }
  return shape_ == other_shape;
}

//利用另一个Blob来初始化本Blob,这里的数据是copy,不是指针的赋值。copy_diff是表明copy的是diff还是data
template <typename Dtype>
void Blob<Dtype>::CopyFrom(const Blob& source, bool copy_diff, bool reshape) {
  if (source.count() != count_ || source.shape() != shape_) {
    if (reshape) {
      ReshapeLike(source);
    } else {
      LOG(FATAL) << "Trying to copy blobs of different sizes.";
    }
  }
  switch (Caffe::mode()) {
  case Caffe::GPU:
    if (copy_diff) {
      caffe_copy(count_, source.gpu_diff(),
          static_cast<Dtype*>(diff_->mutable_gpu_data()));
    } else {
      caffe_copy(count_, source.gpu_data(),
          static_cast<Dtype*>(data_->mutable_gpu_data()));
    }
    break;
  case Caffe::CPU:
    if (copy_diff) {
      caffe_copy(count_, source.cpu_diff(),
          static_cast<Dtype*>(diff_->mutable_cpu_data()));
    } else {
      caffe_copy(count_, source.cpu_data(),
          static_cast<Dtype*>(data_->mutable_cpu_data()));
    }
    break;
  default:
    LOG(FATAL) << "Unknown caffe mode.";
  }
}

template <typename Dtype>
void Blob<Dtype>::FromProto(const BlobProto& proto, bool reshape) { //这个即使前面介绍过的从protobuf定义的structure来初始化Blob。便于从文件中读取出BlobProto,并且初始化一个BLob
  if (reshape) {
    vector<int> shape;
    if (proto.has_num() || proto.has_channels() ||
        proto.has_height() || proto.has_width()) {
      // Using deprecated 4D Blob dimensions --
      // shape is (num, channels, height, width).
      shape.resize(4);
      shape[0] = proto.num();
      shape[1] = proto.channels();
      shape[2] = proto.height();
      shape[3] = proto.width();
    } else {
      shape.resize(proto.shape().dim_size());
      for (int i = 0; i < proto.shape().dim_size(); ++i) {
        shape[i] = proto.shape().dim(i);
      }
    }
    Reshape(shape);
  } else {
    CHECK(ShapeEquals(proto)) << "shape mismatch (reshape not set)";
  }
  // copy data
  Dtype* data_vec = mutable_cpu_data();
  if (proto.double_data_size() > 0) {
    CHECK_EQ(count_, proto.double_data_size());
    for (int i = 0; i < count_; ++i) {
      data_vec[i] = proto.double_data(i);
    }
  } else {
    CHECK_EQ(count_, proto.data_size());
    for (int i = 0; i < count_; ++i) {
      data_vec[i] = proto.data(i);
    }
  }
  if (proto.double_diff_size() > 0) {
    CHECK_EQ(count_, proto.double_diff_size());
    Dtype* diff_vec = mutable_cpu_diff();
    for (int i = 0; i < count_; ++i) {
      diff_vec[i] = proto.double_diff(i);
    }
  } else if (proto.diff_size() > 0) {
    CHECK_EQ(count_, proto.diff_size());
    Dtype* diff_vec = mutable_cpu_diff();
    for (int i = 0; i < count_; ++i) {
      diff_vec[i] = proto.diff(i);
    }
  }
}
//也是前面介绍过的将Blob转换为BlobProto,便于串行化后存入文件
template <>
void Blob<double>::ToProto(BlobProto* proto, bool write_diff) const {
  proto->clear_shape();
  for (int i = 0; i < shape_.size(); ++i) {
    proto->mutable_shape()->add_dim(shape_[i]);
  }
  proto->clear_double_data();
  proto->clear_double_diff();
  const double* data_vec = cpu_data();
  for (int i = 0; i < count_; ++i) {
    proto->add_double_data(data_vec[i]);
  }
  if (write_diff) {
    const double* diff_vec = cpu_diff();
    for (int i = 0; i < count_; ++i) {
      proto->add_double_diff(diff_vec[i]);
    }
  }
}

template <>
void Blob<float>::ToProto(BlobProto* proto, bool write_diff) const {
  proto->clear_shape();
  for (int i = 0; i < shape_.size(); ++i) {
    proto->mutable_shape()->add_dim(shape_[i]);
  }
  proto->clear_data();
  proto->clear_diff();
  const float* data_vec = cpu_data();
  for (int i = 0; i < count_; ++i) {
    proto->add_data(data_vec[i]);
  }
  if (write_diff) {
    const float* diff_vec = cpu_diff();
    for (int i = 0; i < count_; ++i) {
      proto->add_diff(diff_vec[i]);
    }
  }
}

INSTANTIATE_CLASS(Blob);
template class Blob<int>;
template class Blob<unsigned int>;

}  // namespace caffe


下面我们写一个test程序来验证Blob的功能

#include "base_include.h"

#include <iostream>
#include "blob.hpp"
#include "caffe/util/io.hpp"


using namespace std;
using namespace caffe;
/*定义了两个函数来打印Blob内部的data和diff*/
template <typename Dtype>
void print_blob_data(const Blob<Dtype> &blob);
template <typename Dtype>
void print_blob_diff(const Blob<Dtype> &blob);
int main()
{
    do {
        Blob<float> a;
        printf("blob size is %s\n", a.shape_string().c_str());
        vector<int> shape(4);
        shape.at(0) = 3;
        shape.at(1) = 4;
        shape.at(2) = 5;
        shape.at(3) = 6;
        a.Reshape(shape);
        printf("blob size is %s\n", a.shape_string().c_str());
        /*Init Blob data*/
        float *data = a.mutable_cpu_data();
        if (!data) {
            printf("get mutable cpu data error.");
            break;
        }
        for (int i = 0; i < a.count(); ++ i) {
            data[i] = i * 2;
        }
        print_blob_data(a);
        /*Init Blob diff data*/
        float *diff = a.mutable_cpu_diff();
        if (!diff) {
            printf("get mutable cpu diff error.");
            break;
        }
        for (int i = 0; i < a.count(); ++ i) {
            diff[i] = i;
        }
        print_blob_diff(a);
        /*update*/
        a.Update();//data = data - diff
        printf("after update\n");
        print_blob_data(a);

        /*To protobuf & from protobuf, 这里只是串行化为string,没有写入文件,
        把这个string可以直接写进文件。*/
        BlobProto bp;
        a.ToProto(&bp, true);
        string serial_result;
        bp.SerializeToString(&serial_result);
        //printf("serial string is : %s\n", serial_result.c_str());
        BlobProto bp_new;
        bp_new.ParseFromString(serial_result);
        Blob<float> b;
        b.FromProto(bp_new, true);
        printf("###########################################\n");
        print_blob_data(b);
        print_blob_diff(b);
        printf("serial_result size is %u\n", serial_result.size());
        /*caffe util 里面提供了一个io 文件,来帮助进行io操作,我们可以直接调用里面的函数*/
        BlobProto bp3;
        a.ToProto(&bp3, true);
        WriteProtoToBinaryFile(bp3, "a.blob");//文件生成在执行目录
        BlobProto bp4;
        ReadProtoFromBinaryFileOrDie("a.blob", &bp4);
        Blob<float> c;
        c.FromProto(bp4, true);
         printf("###########################################\n");
        print_blob_data(c);
        print_blob_diff(c);
    } while (false);
    return 0;
}

template <typename Dtype>
void print_blob_data(const Blob<Dtype> &blob)
{
    const vector<int>& blob_shape = blob.shape();
    for (int number = 0; number < blob_shape[0]; ++ number) {
        for (int channel = 0; channel < blob_shape[1]; ++ channel) {
            for (int height = 0; height < blob_shape[2]; ++ height) {
                for (int width = 0; width < blob_shape[3]; ++ width) {
                    float tmp_data = blob.data_at(number, channel, height, width);
                    printf("blob[%d][%d][%d][%d] = %f\n", number, channel, height, width, tmp_data);
                }
            }
        }
    }
    printf("data sum is %f\n", blob.asum_data());//L1
    printf("data sumsq is %f\n", blob.sumsq_data());//L2
}

template <typename Dtype>
void print_blob_diff(const Blob<Dtype> &blob)
{
    const vector<int>& blob_shape = blob.shape();
    for (int number = 0; number < blob_shape[0]; ++ number) {
        for (int channel = 0; channel < blob_shape[1]; ++ channel) {
            for (int height = 0; height < blob_shape[2]; ++ height) {
                for (int width = 0; width < blob_shape[3]; ++ width) {
                    float tmp_data = blob.diff_at(number, channel, height, width);
                    printf("blob diff[%d][%d][%d][%d] = %f\n", number, channel, height, width, tmp_data);
                }
            }
        }
    }
    printf("diff sum is %f\n", blob.asum_diff());//L1
    printf("diff sumsq is %f\n", blob.sumsq_diff());//L2
}

注意编译的时候需要链接libcaffe.so, libglog.so, libboost_system.so, libprotobuf.so这些用到的库。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值