caffe代码层次:
- Blob:是基础的数据结构,是用来保存学习到的参数以及网络传输过程中产生数据的类。
- Layer:是网络的基本单元,由此派生出了各种层类。修改这部分的人主要是研究特征表达方向的。
- Net:是网络的搭建,将Layer所派生出层类组合成网络。Solver:是Net的求解,修改这部分人主要会是研究DL求解方向的。
BLob只是一个基本的数据结构,以Protobuf方式存储,Protobuf是一种可以实现内存与外存交换的协议接口。
1、主要变量:
shared_ptr<SyncedMemory> data_;shared_ptr<SyncedMemory> diff_;shared_ptr<SyncedMemory> shape_data_;vector<int> shape_;int count_;int capacity_;Data: 主要存储前向传递的数据;diff:主要存储的是反向传播中的梯度;s hape_data和shape_都是存储Blo b的形状,一个是老版本一个是新版本,对于Blob中的4个基本变 量 num,channel,height,width 可以直接通过 shape(0),shape(1),shape(2),shape(3) 来访问。 ;
2、主要函数:
void Reshape(const int num, const int channels, const int height, const int width)Reshape函数在Layer中的reshape或者forward操作中来adjust dimension。同时在改变Blob大小时,内存将会被重新分配如果内存大小不够了,并且额外的内存将不会被释放;
inline int count(int start_axis, int end_axis)count:函数用于统计Blob的容量;
inline int offset(const int n, const int c = 0, const int h = 0, const int w = 0) inline int offset(const vector<int>& indices)计算offset;
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();这一部分函数主要在通过cpu_data*指针获得地址,
inline Dtype data_at(const int n, const int c, const int h, const int w)inline Dtype diff_at(const int n, const int c, const int h, const int w)inline Dtype data_at(const vector<int>& index)inline Dtype diff_at(const vector<int>& index)inline const shared_ptr<SyncedMemory>& data()inline const shared_ptr<SyncedMemory>& diff()通过给定的位置访问数据,根据位置计算与数据起始的偏差offset,。
void CopyFrom( const Blob<Dtype>& source, bool copy_diff = false,bool reshape = false)从一个Blob中copy数据,若copy_diff是False则copy data,否则copy diff,reshape控制是否需要reshape;
void FromProto( const BlobProto& proto, bool reshape = true)void ToProto(BlobProto* proto, bool write_diff = false) const这两个函数主要 是将数据序列化,对Protobuf 存储或读取 ,Protobuf是谷歌的一个数据序列化的存储格式,参见: http://blog.youkuaiyun.com/kkk584520/article/details/41046827