前言
本篇<深度学习21天实战caffe>这本书的阅读笔记.
打开proto/caffe.proto中,刚开始就是介绍Blob数据结构,这个数据结构是其他大部分数据结构的重要依赖.
caffe中进行网络层计算时,每一层的输入输出都是以Blob对象为缓冲,是cagge的基本存储单元.。
一、先看Blob的数据结构描述
// Specifies the shape (dimensions) of a Blob.
message BlobShape {
//来实现高维度的封装,既vector<N>,分别表示Blob每个维度的大小;packed表示这些值在内存中紧密排布
repeated int64 dim = 1 [packed = true];
}
message BlobProto {
optional BlobShape shape = 7; //可选,包含一个blobshape对象
repeated float data = 5 [packed = true];
// 包含若干浮点数元素,存储数据和权值,元素数目有shape或(num, channels, height, width)确定,在内存中紧密排布
repeated float diff = 6 [packed = true]; // 浮点数,用来存储增量信息
repeated double double_data = 8 [packed = true];
repeated double double_diff = 9 [packed = true];
// 4D dimensions -- deprecated. Use "shape" instead.
// 数据4D形状,已经使用shape代替了,意思就是新版本推荐使用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];
}
// The BlobProtoVector is simply a way to pass multiple blobproto instances
// around.
message BlobProtoVector {
repeated BlobProto blobs = 1;//多个 BlobProto
}
使用protobuf数据结构而不用结构体是因为
1)结构体的序列化和反序列化需要额外的编程实现,难以做到借口标准化
2)结构体中包含很长的数据(一般指向某个内存的指针),需要更加细致的工作保证数据结构的完整性
3)protobuffer可以将编程更容易出问题的地方加以隐藏,让机器自动处理,提高程序的健壮性.
二、Blob基本用法
笔者使用的是linux下的Clion编译器,关于Clion调试\调用caffe以及CmakeLists.txt文件编写请看
利用Clion调用caffe的c++接口
直接看cpp文件
#include <iostream>
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/util/io.hpp"
using namespace caffe;
using namespace std;
int main() {
Blob<float> a;
cout<<"size:"<<a.shape_string()<<endl;
a.Reshape(1,2,3,4);
cout<<"size:"<<a.shape_string()<<endl;
// 上面输出的是
// size:(0)
// size:1 2 3 4 (24)
//一、创建好Blob对象后,可以通过mutable_cpu[gpu]_data[diff]修改内部的数值
float *p = a.mutable_cpu_data();
for(int i=0;i<a.count();i++){
p[i] = i;
}
//二、Blob下标访问几乎与c++高维数组一直,Blob的强大之处在于可以自动同步cpu/gpu上的数据
for(int n=0;n<a.num();n++){
for(int c=0;c<a.channels();c++){
for(int h=0;h<a.height();h++){
for(int w=0;w<a.width();w++){
cout<<"a["<<n<<"]["<<c<<"]["<<h<<"]["<<w<<"]="<<a.data_at(n,c,h,w)<<endl;
}
}
}
}
//上面输出的是 a[0][0][0][0]=0 等
//三、Blob还支持计算所有元素绝对值之和(L1范数),(L2范数\平方和)
cout<<"aL1sum="<<a.asum_data()<<endl;
cout<<"aL2sum="<<a.sumsq_data()<<endl;
//四、来试试修改diff部分,与data部分操作是一样的,试试将diff初始化成23,22,21...
float *q = a.mutable_cpu_diff();
for(int i=0;i<a.count();i++){
q[i] = a.count()-1-i;
}
//五、执行Update操作,将diff与data融合,这也是cnn权值更新步骤,实现的是data-diff操作
a.Update();
for(int n=0;n<a.num();n++){
for(int c=0;c<a.channels();c++){
for(int h=0;h<a.height();h++){
for(int w=0;w<a.width();w++){
cout<<"a["<<n<<"]["<<c<<"]["<<h<<"]["<<w<<"]="<<a.data_at(n,c,h,w)<<endl;
}
}
}
}
//六、BlobProto对象实现了磁盘\内存之间的数据同学.这对于保存\载入训练好的模型权值非常实用
BlobProto bp; //构造一个BlobProto对象
a.ToProto(&bp, true); //将a序列化,连同diff(默认不带)
WriteProtoToBinaryFile(bp, "a.blob"); //写入磁盘文件"a.blob",这个是路径
BlobProto bp2; // 构造一个新的BlobProto对象
ReadProtoFromBinaryFileOrDie("a.blob", &bp2); //读取磁盘
Blob<float>b; // 新建立一个新的Blob对象
b.FromProto(bp2, true);//从序列化对象中克隆b,连同形状一起
for(int n=0;n<b.num();n++){
for(int c=0;c<b.channels();c++){
for(int h=0;h<b.height();h++){
for(int w=0;w<b.width();w++){
cout<<"b["<<n<<"]["<<c<<"]["<<h<<"]["<<w<<"]="<<b.data_at(n,c,h,w)<<endl;
}
}
}
}
std::cout << "Hello, end!" << std::endl;
return 0;
}