caffe的学习之路---Blob的基本用法
2016-10-05 19:54
483 查看
首先,参考书籍《21天实战caffe》
以前没有C++基础,所以第一次看这本书时候觉得好垃圾;
现在有了C++基础,再次看这本书,发现还是很值得学习。
在本文之前,我已经把官方tutorial看完。
要看懂blob.hpp建议将<C++ Primer>前七章看完,再学习caffe源码。
如果要看layer的相关文件,至少把前十五章看完,一起加油。
所以现在开始读源码,首先还是类模板Blob。
下面是自己将Blob写入磁盘,从磁盘读取内容存入Blob的程序,当然《21天实战caffe》的源码。主要写给自己看。
首先先说几个Member Function
1 void Reshape(const int num,const int channels,const int height,const int weight)
2 void FromProto(const BlobProto &proto, bool reshape=true)
用法a.FromProto 将Blob类型的变量a写入一个类型为BlobProto的变量proto中
这是数据从Blob到磁盘的中间处理层(作用就是序列化)
3 void ToProto(BlobProto *proto, bool write_diff=fasle )
第二个参数输入true (将diff也序列化,默认是只把data序列化)
另外需要认识2个IO函数
1. void WriteProToBinaryFile(const Message &proto,const char *filename)
void WriteProToBinaryFile(const Message &proto,const string filename)
从名字就看出是将BlobPro写入二进制文件filename
2. bool ReadProtoFromBinaryFile(const char *filename,Message *proto)
bool ReadProtoFromBinaryFile(const string &filename,Message *proto)
以前没有C++基础,所以第一次看这本书时候觉得好垃圾;
现在有了C++基础,再次看这本书,发现还是很值得学习。
在本文之前,我已经把官方tutorial看完。
要看懂blob.hpp建议将<C++ Primer>前七章看完,再学习caffe源码。
如果要看layer的相关文件,至少把前十五章看完,一起加油。
所以现在开始读源码,首先还是类模板Blob。
下面是自己将Blob写入磁盘,从磁盘读取内容存入Blob的程序,当然《21天实战caffe》的源码。主要写给自己看。
首先先说几个Member Function
1 void Reshape(const int num,const int channels,const int height,const int weight)
void Reshape(const vector<int> &shape)
这2个函数的作用是重构blob的维数(Dim)2 void FromProto(const BlobProto &proto, bool reshape=true)
用法a.FromProto 将Blob类型的变量a写入一个类型为BlobProto的变量proto中
这是数据从Blob到磁盘的中间处理层(作用就是序列化)
3 void ToProto(BlobProto *proto, bool write_diff=fasle )
第二个参数输入true (将diff也序列化,默认是只把data序列化)
另外需要认识2个IO函数
1. void WriteProToBinaryFile(const Message &proto,const char *filename)
void WriteProToBinaryFile(const Message &proto,const string filename)
从名字就看出是将BlobPro写入二进制文件filename
2. bool ReadProtoFromBinaryFile(const char *filename,Message *proto)
bool ReadProtoFromBinaryFile(const string &filename,Message *proto)
#include <vector> #include <iostream> #include <caffe/blob.hpp> #include <caffe/util/io.hpp> using namespace caffe; using namespace std; int main(){ Blob<float> a; a.Reshape(1,2,3,4); float *p=a.mutable_cpu_data(); float *q=a.mutable_cpu_diff(); for(int i=0;i<a.count();i++){ p[i]=i; q[i]=a.count()-i-1; } a.Update(); for(int u=0;u<a.num();u++){ for(int v=0;v<a.channels();v++){ for(int w=0;w<a.height();w++){ for(int x=0;x<a.width();x++){ cout<<"a["<<u<<"]["<<v<<"]["<<w<<"]["<<x<<"]=" <<a.data_at(u,v,w,x)<<endl; } } } } BlobProto bp1; a.ToProto(&bp1,true); WriteProtoToBinaryFile(bp1,"a.blob"); BlobProto bp2; ReadProtoFromBinaryFile("a.blob",&bp2); Blob<float> b; b.FromProto(bp2,true); for(int u=0;u<b.num();u++){ for(int v=0;v<b.channels();v++){ for(int w=0;w<b.height();w++){ for(int x=0;x<b.width();x++){ cout<<"b["<<u<<"]["<<v<<"]["<<w<<"]["<<x<<"]=" <<b.data_at(u,v,w,x)<<endl; } } } } return 0; }
相关文章推荐
- caffe源码学习--blob基本用法(基于《21天实战caffe》)
- Caffe源码使用(一)------Blob基本用法
- 【深度学习】【Caffe源代码解读1】笔记19 Caffe的基本数据结构之Blob
- 21天实战caff--2 第8天 Caffe数据结构-Blob基本用法
- [EntLib]微软企业库5.0 学习之路——第一步、基本入门 [转]
- AfxBeginThread的基本用法_自我学习
- VS2005验证控件学习下基本用法
- [EntLib]微软企业库5.0 学习之路——第一步、基本入门
- EXTJS学习系列基础篇:第九篇(转载)作者殷良胜,Ext组件系列之--field组件的基本用法
- ZendFramework学习第三章(核心组件—会话处理之Zend_Session基本用法)
- C语言中基本的输入和输出函数的用法第二讲(C语言学习随笔四)
- EXTJS学习系列基础篇:第八篇(转载)作者殷良胜,Ext组件系列之--textfield组件的基本用法
- 我的安卓学习之路--java的基本语法
- java学习-JAVA基本排序法示例,JAVA数据类型转换 ,ArrayList用法,Calendar 简单应用
- Extjs学习 Window的基本用法
- 【Android组件学习】Spinner的基本用法和Adapter的作用
- EXTJS学习系列基础篇:第八篇(转载)作者殷良胜,Ext组件系列之--textfield组件的基本用法
- EXTJS学习系列基础篇:第七篇(转载)作者殷良胜,Ext组件系列之--label组件的基本用法
- [EntLib]微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART1——基本使用介绍
- (他山之石)MFC学习之路(四)MultiByteToWideChar和WideCharToMultiByte用法详解