您的位置:首页 > Web前端

区分caffe中train.prototxt,solver.prototxt,deploy.prototxt等文件

2017-07-20 10:58 591 查看
刚开始接触caffe的时候,对于各种一堆prototxt文件傻傻分不清楚,不清楚每个文件的作用,摸索了一段时间后,写下自己的体会,一来免得忘记,也可以给初学者提供一个小小的参考(虽然我也是初学者,一直在路上),如有错误,敬请批评指正。

以lenet为例:

1:lenet_solver.prototxt

# The train/test net protocol buffer definition
net: "/home/yan/caffe-master/models/mnist/lenet_train_test.prototxt"
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100            #预测阶段迭代100次可以覆盖全部10000个测试集
# Carry out testing every 500 training iterations.
test_interval: 500        #训练每迭代500次,进行一次预测
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01             #基础学习率
momentum: 0.9             #动量
weight_decay: 0.0005      #权重衰减
# The learning rate policy
lr_policy: "inv"          #采用衰减学习策略
gamma: 0.0001
power: 0.75
# Display every 100 iterations
display: 100              #每经过100次迭代,在屏幕上打印一次log
# The maximum number of iterations
max_iter: 10000           #最大迭代次数
# snapshot intermediate results
snapshot: 5000            #每5000次迭代打印一次快照
#设置保存路径
snapshot_prefix: "/home/yan/caffe-master/models/mnist/lenet"
# solver mode: CPU or GPU
solver_mode: GPU          #caffe求解模式为gpu


这个文件告诉了我们训练的网络和超参数。用caffe训练时会首先读取该文件,获得其中特定字段的数值,并据此设置内存中模型训练时的超参数变量值。

net: “/home/yan/caffe-master/models/mnist/lenet_train_test.prototxt”给出了网络的路径(我这里写的是绝对路径)。

深度学习中,loss function往往都是非凸的,没有解析解,我们需要通过优化方法来求解,solver.prototxt的主要作用就是交替调用前向算法和后向算法来更新参数,从而最小化loss,实际上就是一种迭代的优化算法。

2:lenet_train_test.prototxt

name: "LeNet"
layer {
name: "mnist"       #输入层的名称mnist
type: "Data"        #输入层的类型data层
top: "data"         #层的输出blob有两个:data和label
top: "label"
include {
phase: TRAIN      #训练阶段,该层参数只在训练阶段有效
}
transform_param {
scale: 0.00390625 #输入像素归一化到【0,1】 1/256=0.00390625
}
data_param {
source: "examples/mnist/mnist_train_lmdb"  #LMDB的路径
batch_size: 64                             #一次读取64张图
backend: LMDB                              #数据格式为LMDB
}
}
layer { #一个新数据层,名字也叫作mnist,输出blob也是data和label,但是这里定义的参数只在分类阶段有效
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST   #测试阶段
}
transform_param {
scale: 0.00390625
}
data_param {
source: "examples/mnist/mnist_test_lmdb"
batch_size:100                 #batchsize大小,乘以test_iter = 测试集大小
backend: LMDB
}
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"     #本层使用上一层的data,生成下一层conv1的blob
top: "conv1"
param {
lr_mult: 1       #权重参数w的学习率倍数,1表示保持与全局参数一致
}
param {
lr_mult: 2       #偏置参数b的学习率倍数,是全局参数的2倍
}
convolution_param {
num_output: 20      #输出单元数20
kernel_size: 5      #卷积核大小为5*5
stride: 1           #步长为1
weight_filler {     #权值使用xavier填充器
type: "xavier"
}
bias_filler {       #bias使用常数填充器,默认为0
type: "constant"
}
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"      #本层的上一层是conv1,生成下一层Pool1的blob
top: "pool1"
pooling_param {     #下采样参数
pool: MAX         #使用最大值下采样方法
kernel_size: 2    #pooling核是2*2
stride: 2        #pooling步长是2
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {                    #新的全连接层,输入blob为pool2,输出blob为ip1
name: "ip1"
type: "InnerProduct"
bottom: "pool2"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {    #全连接层的参数
num_output: 5      #输出500个节点
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {                   #新的非线性层,用RELU方法
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
layer {                  #第二个全连接层
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10      #输出10个单元
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {    #分类准确率层(计算网络输出相对目标值的准确率),只在testing阶段有效,输入blob为iP2和label,输出blob为accuracy
name: "accuracy"     #该层用于计算分类准确率
type: "Accuracy"
bottom: "ip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
layer {   #损失层,损失函数采用softmaxloss,输入blob为iP2和label,输出blob为loss
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label"
top: "loss"
}


这个文件是网络配置文件,给出了网络的具体定义, source给出了参数文件路径(用的数据),上面lenet_solver.prototxt中的net就是这个网络。

3:deploy.prototxt

name: "LeNet"
layer {
name: "data"
type: "Input"
top: "data"
input_param {shape:{dim:1 dim:1 dim:28  dim:28}}
# dim:1 batchsize  dim:1 number of colour channels - rgb
# dim:28 width dim:28 height
}
layer{
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
}
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "pool2"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 500
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
}
}
layer {
name: "prob"
type: "Softmax"
bottom: "ip2"
top: "prob"
}


deploy.prototxt是测试时用的文件。可以看出,lenet_train_test.prototxt删除再改变一些东西就变成了deploy.prototxt文件,最大的区别就是deploy.prototxt文件删除了lenet_train_test.prototxt文件开始的输入数据test部分;删除了分类准确率层accuracy;把trian部分的输入数据部分修改,告诉我们输入维度;将损失层loss改成了prob。

deploy.prototxt没有反向传播训练部分。关于deploy与train的详细区别,可以参考看看这篇博客http://blog.csdn.net/fx409494616/article/details/53008971,博主说得很细。

4:lenet_iter_10000.caffemodel与lenet_iter_10000.solverstate文件

训练完后会出现这样的文件



caffemodel文件保存了最终训练的模型权值,训练好的caffemodel后面可以用来对图片进行分类等。

solverstate文件保存了训练状态。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐