Blob是一个模板类, 其对象在内存中表示4维数组(num_, channels_,height_,width_).
在src/caffe/proto/caffe.proto中与Blob相关的数据结构(BlobShape 和BlobProto):
syntax = "proto2";
package caffe;
// Specifies the shape (dimensions) of a Blob.该结构描述Blob 的形状信息
message BlobShape {
repeated int64 dim = 1 [packed = true];
}
//该结构描述Blob 在磁盘中序列化后的状态
message BlobProto {
optional BlobShape shape = 7; //可选,包括一个Blobshape对象
repeated float data = 5 [packed = true]; //若干浮点元素,存储数据或权值, 元素数目由shape或(num,channel,height,width)确定
repeated float diff = 6 [packed = true]; //存储增量信息,维度与data一致
repeated double double_data = 8 [packed = true]; //与data并列,类型为double
repeated double double_diff = 9 [packed = true];
// 4D dimensions -- deprecated. Use "shape" instead. 推荐使用shape
optional int32 num = 1 [default = 0];
optional int32 channels = 2 [default = 0];
optional int32 height = 3 [default = 0];
optional int32 width = 4 [default = 0];
}
Blob模板类的声明和实现:
//blob.hpp
#ifndef CAFFE_BLOB_HPP_
#define CAFFE_BLOB_HPP_
#include <algorithm>
#include <string>
#include <vector>
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"//由protoc生成的头文件,声明了BlobProto/BlobShape等遵循caffe.proto协议的数据结构
#include "caffe/syncedmem.hpp" //CPU/GPU共享内存类,用于数据同步
//SyncedMemory类是一个CPU/GPU共享内存类,负责分配内存,以及主机和设备间的同步.
const int kMaxBlobAxes = 32;//Blob最大维数目
namespace caffe {
template <typename Dtype>
class Blob { //模板类Blob封装了SyncedMemory类
public:
Blob()
: data_(), diff_(), count_(0), capacity_(0) {}//默认构造函数
/// @brief Deprecated; use <code>Blob(const vector<int>& shape)</code>.
//默认构造函数
explicit Blob(const int num, const int channels, const int height, const int width);
//显示构造函数,explicit只用于构造函数,防止隐式转换
explicit Blob(const vector<int>& shape);
/// @brief Deprecated; use <code>Reshape(const vector<int>& shape)</code>.
void Reshape(const int num, const int channels, const int height, const int width);
//变形函数, 根据输入参数重新设置BLOB形状,必要时重新分配内存
void Reshape(const vector<int>& shape);
void Reshape(const BlobShape& shape);
void ReshapeLike(const Blob& other);
////得到BLOB形状字符串,用于打印log
inline string shape_string() const {
ostringstream stream;
for (int i = 0; i < shape_.size(); ++i) {
stream << shape_[i] << " ";
}
stream << "(" << count_ << ")";
return stream.str();
}
//返回Blob形状
inline const vector<int>& shape() const { return shape_; }
inline int shape(int index) const { //返回某一维度的尺寸
return shape_[CanonicalAxisIndex(index)];
}
inline int num_axes() const { return shape_.size(); }//返回维度数目
inline int count() const { return count_; }//返回Blob中元素总数
inline int count(int start_axis, int end_axis) const {//返回BLOB中某几维子集的元素总数
CHECK_LE(start_axis, end_axis);//保证start_axis<=end_axis
CHECK_GE(start_axis, 0); //保证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 count(int start_axis) const {//计算从某一维度开始的元素总数
return count(start_axis, num_axes());
}
inline int CanonicalAxisIndex(int axis_index) const {//转换坐标轴索引[-N,N]为普通索引[0,N)
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
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;
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();//读写访问CPU data
Dtype* mutable_gpu_data();
Dtype* mutable_cpu_diff();
Dtype* mutable_gpu_diff();
void Update();//BLOB 更新运算,可简单理解为data 与diff 的更新过程
//反序列化函数,从BlobProto中恢复一个Blob对象. BlobProto对象实现了磁盘和内存间的数据通信
void FromProto(const BlobProto& proto, bool reshape = true);
void ToProto(BlobProto* proto, bool write_diff = false) const;//序列化函数
Dtype asum_data() const; //计算data的L1范数
Dtype asum_diff() const;
Dtype sumsq_data() const; //计算data的L2范数
Dtype sumsq_diff() const;
void scale_data(Dtype scale_factor);//data乘以一个标量
void scale_diff(Dtype scale_factor);
void ShareData(const Blob& other);//共享另一个blob的data_
void ShareDiff(const Blob& other);
bool ShapeEquals(const BlobProto& other);
protected:
shared_ptr<SyncedMemory> data_;//存放指向data的指针
shared_ptr<SyncedMemory> diff_;
shared_ptr<SyncedMemory> shape_data_;
vector<int> shape_;
int count_;//存放有效元素数目信息
int capacity_;//存放Blob容器的容量信息
DISABLE_COPY_AND_ASSIGN(Blob);//禁用拷贝构造函数,赋值运算符重载
}; // class Blob
} // namespace caffe
#endif // CAFFE_BLOB_HPP_