blob.hpp

caffe中最基础的数据结构是blob,它是一个四维的数组。维度从高到低分别是:(num_,channels_,height_,width_)对于图像数据来说就是:图片个数,彩色通道个数,宽,高,比如说有10张图片,分别是512*256大小,彩色三通道,则为:(10,3,256,512)

*************************************************************************
#ifndef CAFFE_BLOB_HPP_
#define CAFFE_BLOB_HPP_
#include <algorithm>
#include <string>
#include <vector>
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h" //里面声明了Blobproto、Blobshape等遵循caffe.proto协议的数据结构
#include "caffe/syncedmem.hpp"    //CPU/GPU共享内存类,用于数据同步,很多实际的动作都在这里面执行
*************************************************************************
const int kMaxBlobAxes = 32;     //blob的最大维数目
namespace caffe {
template <typename Dtype>
class Blob {
 public:

//blob的构造函数,不允许隐式的数据类型转换
  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函数,对blob的维度进行更改
  void Reshape(const int num, const int channels, const int height, const int width);       // 用户的reshape接口(常用)
  void Reshape(const vector<int>& shape);       // 通过重载调用真正的reshape函数
  void Reshape(const BlobShape& shape);         // 用户的reshape接口
  void ReshapeLike(const Blob& other);          // 用户的reshape接口
  //获取Blob的形状字符串,用于打印log,比如: 10 3 256 512 (3932160),总元素个数  
  inline string shape_string() const {
    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_; }  // 成员函数,返回blob的形状信息(常用)

  inline int shape(int index) const {                         // 返回blob特定维度的大小(常用)
    return shape_[CanonicalAxisIndex(index)]; 
  }
  inline int num_axes() const { return shape_.size(); }       // 返回blob维度
  inline int count() const { return count_; }                 // 返回元素的个数

  inline int count(int start_axis, int end_axis) const {      // 返回特定维度区间的元素的个数
    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;
  }

  inline int CanonicalAxisIndex(int axis_index) const {       // 检查输入的维度的合法性
    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;
  }

  inline int num() const { return LegacyShape(0); }           // 返回样本的个数(常用)
  inline int channels() const { return LegacyShape(1); }      // 返回通道的个数(常用)
  inline int height() const { return LegacyShape(2); }        // 返回样本维度一,对于图像而言是高度(常用)
  inline int width() const { return LegacyShape(3); }         // 返回样本维度二,对于图像而言是宽度(常用)

  //返回特定维度的大小,包含对输入维度的合法性检查,被上面函数调用
  inline int LegacyShape(int index) const {     
    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,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;
  }

  // 从其他的blob来拷贝到当前的blob中,默认是不拷贝梯度的,如果形状不一致需要使能reshape,不然无法拷贝
  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 {
    return cpu_data()[offset(n, c, h, w)];
  }
  // 返回特定位置的梯度值
  inline Dtype diff_at(const int n, const int c, const int h,
      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;   // 只读获取cpu的data_的指针
  void set_cpu_data(Dtype* data);  // 设置cpu的data_指针,修改指针仅
  const int* gpu_shape() const;    // 只读获取gpu上数据的形状信息
  const Dtype* gpu_data() const;   // 只读获取gpu上的data_的指针
  const Dtype* cpu_diff() const;   // 只读获取cpu的diff_的指针
  const Dtype* gpu_diff() const;   // 只读获取gpu的diff_的指针
  Dtype* mutable_cpu_data();       // 读写访问cpu data
  Dtype* mutable_gpu_data();       // 读写访问gpu data
  Dtype* mutable_cpu_diff();       // 读写访问cpu diff
  Dtype* mutable_gpu_diff();       // 读写访问cpu diff
  void Update();                   // 数据更新,即减去当前计算出来的梯度
  void FromProto(const BlobProto& proto, bool reshape = true);   // 将数据进行反序列化,从磁盘导入之前存储的blob
  void ToProto(BlobProto* proto, bool write_diff = false) const; // 将数据进行序列化,便于存储

  Dtype asum_data() const;         // 计算data的L1范数,绝对值之和
  Dtype asum_diff() const;         // 计算diff的L1范数,绝对值之和
  Dtype sumsq_data() const;        // 计算data的L2范数,平方和
  Dtype sumsq_diff() const;        // 计算diff的L2范数,平方和

  void scale_data(Dtype scale_factor);   // 按照一个标量进行伸缩data_
  void scale_diff(Dtype scale_factor);   // 按照一个标量进行伸缩diff_


  void ShareData(const Blob& other);     // 只是拷贝过来other的data
  void ShareDiff(const Blob& other);     // 只是拷贝过来other的diff

  bool ShapeEquals(const BlobProto& other);  // 判断两个blob的形状是否一致

 protected:
  shared_ptr<SyncedMemory> data_;            // 类的属性---数据
  shared_ptr<SyncedMemory> diff_;            // 类的属性---梯度
  shared_ptr<SyncedMemory> shape_data_;       
  vector<int> shape_;                        // 类的属性---形状信息
  int count_;                                // 有效元素总的个数
  int capacity_;                             // 存放bolb容器的容量信息,大于等于count_

  DISABLE_COPY_AND_ASSIGN(Blob);
};  // class Blob


}  // namespace caffe


#endif  // CAFFE_BLOB_HPP_

关键元素:

protected:
shared_ptr<SyncedMemory> data_;            // 类的属性---数据
shared_ptr<SyncedMemory> diff_;            // 类的属性---梯度
shared_ptr<SyncedMemory> shape_data_;      //老版本的shape数据
vector<int> shape_;                        // 类的属性---形状信息,如10,3,256,512
int count_;                                // 有效元素总的个数,如果向量为10维,这里就是10
int capacity_;                             // 存放blob容器的容量信息,大于等于count_

这里写图片描述
在 Update()函数中 , 实现了 data = data - diff 操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值