Blob是一个类模板,封装了SyncedMemory类,作为基本计算单元服务Layer,Net,Solver等。使用同步时必须#include”caffe/syncedmem.hpp”。
//Blob中的shape_string()函数
inline string shape_string() const{//使用const的函数,使得传入的this是const的,所以不能对数据成员进行修改。
ostringstream stream;//定义一个输出字符串流对象
for(int I = 0; i < shape_size(); i++){
stream << shape_[i] << " ";//将shape_的vector中每个元素都都给stream
}
stream << << "(" << count_ << ")";//将count_的值输出到stream
return stream.str();//将stream转换成string并将该字符串返回
}
//转换坐标轴函数:
inline int CanonicalAxisIndex(int axis_index) const{…};
该函数保证对于坐标索引 [-N,N) 转换到普通索引 [0,N)。如果输入的是负索引值,表示从后向前访问,-1表示最后一个元素。
偏移量计算函数:
inline int offset(const int n,const int c = 0,const int h = 0;const int w = 0)const{
//CHECK各个参数额范围合法性
…
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.size(),shape(i));
offset += indices[i];
}
}
}
//通过inline const shared_ptr<SyncedMemory>& diff() const{…}函数来实现CPU/GPU的内存共享。
//反序列化函数
void FromProto(const BlobProto &proto,bool reshape = true);
//序列化函数
void ToProto(BlobProto* proto,bool write_diff = false);//默认序列化时不带diff