使用protobuf的原由
一个好的软件框架应该要有明确的输入和输出,对于CNN网络而言,其主要有两部分组成:网络具体结构和网络的具体优化算法及参数。对于框架的使用者而言,用户只需输入两个描述文件即可得到对该网络的优化结果,这无疑是非常方便的。
caffe框架选择使用谷歌的开源protobuf工具对这两部分进行描述,解析和存储,这一部分为caffe的实现节省了大量的代码。
如前面讲述的目标检测demo,py-faster-rcnn,其主要分为训练和测试两个过程,两个过程的核心文件都是prototxt格式的文本文件。
如训练过程
输入:
(1)slover.prototxt。描述网络训练时的各种参数文件,如训练的策略,学习率的变化率,模型保存的频率等参数
(2)train.prototxt。描述训练网络的网络结构文件。
(3)test.prototxt。描述测试网络的网络结构文件。
输出:
VGG16.caffemodel:保存的训练好的网络参数文件。
protobuf的使用流程
protobuf工具主要是数据序列化存储和解析。在实际使用的时候主要是作为一个代码自动生成工具来使用,通过生成对所定义的数据结构的标准读写代码,用户可以通过标准的读写接口从文件中进行数据的读取,解析和存储。
目前proto支持C++,python,java等语言,这里主要演示caffe中使用的C++调用。
主要使用过程为:
(1)编写XXX.proto文件。该文件里主要定义了各种数据结构及对应的数据类型,如int,string等。
(2)使用protoc对XXX.proto文件进行编译,生成对应的数据结构文件的读取和写入程序,程序接口都是标准化的。生成的文件一般名为XXX.pb.cc和XXX.pb.h。
(3)在新程序中使用XXX.pb.c和XXX.pb.h提供的代码。
简易caffe.proto编写解析示例
为了后面更加清楚的理解protobuf工具,这里一个简单的caffe.proto为例进行solver.prototxt和train.prototxt的解析
caffe.proto文件编写:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
编译生成caffe.pb.cc与caffe.pb.h文件
- 1
编写测试文件main.cpp
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
编写solver与train网络描述文件
solver.prototxt内容
- 1
- 2
- 3
train.prototxt内容:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
编译链接,生成main
- 1