从LeNet中分析Caffe模型要素

来源:互联网 发布:打鸟汽枪100元淘宝 编辑:程序博客网 时间:2024/06/03 16:42

Caffe的模型需要两个重要的参数文件:网络模型参数配置(分别是 .prototxt文件和.solver.prototxt文件),本文通过经典的LeNet网络分析具体的参数意义。

网络模型

Caffe的网络模型文件定义了网络的每一层行为,下图是用Caffe中的python/draw_net.py画出的LeNet的模型(如果图片太小看不清可以右击在选择在新窗口打开或者保存到本地用图片查看器放大查看):

这里写图片描述

数据层

LeNet网络模型的输入层为数据层,即网络模型的数据输入定义,一般包括训练数据层和测试数据层两种类型。

LeNet训练数据层

layer {  name: "mnist"  type: "Data"  top: "data"  top: "label"  include {    phase: TRAIN  }0p  transform_param {    scale: 0.00390625//数据缩放因子  }  data_param {    source: "examples/mnist/mnist_train_lmdb"//数据路径    batch_size: 64//批处理数据大小即一次性读取图片的数目    backend: LMDB  }}

LeNet测试数据层与训练数据层各字段含义相同。

LeNet训练卷积(Convoluation)层

layer {  name: "conv1"  type: "Convolution"  bottom: "data"  top: "conv1"  param {    lr_mult: 1//表示weight(权重)更新时的学习率,1倍表示与全局参数一致  }  param {    lr_mult: 2/*表示bias(偏差)更新时的学习率,一般为权重学习率(weight)学习率的2倍,这样一般会取得很好的收敛速率*/  }  convolution_param {//卷积计算参数    num_output: 20//滤波个数即输出特征图(feature map)的数目    kernel_size: 5//滤波大小即卷积核尺寸(在这里为5×5)    stride: 1//步长即卷积输出跳跃间隔,1表示连续输出,无跳跃    weight_filler {      type: "xavier"/*滤波类型,在这里的意思就是权值使用xavier填充器*/    }    bias_filler {      type: "constant"//bias使用常数填充器,默认为0    }  }}

LeNet训练池化(Pooling)层

layer {//又叫下采样层  name: "pool1"  type: "Pooling"  bottom: "conv1"//输入blob  top: "pool1"//输出blob  pooling_param {//下采样参数    pool: MAX//采样方式,这里用的是最大采样    kernel_size: 2//下采样窗口尺寸    stride: 2//下采样跳跃间隔,这里为2×2  }}

LeNet训练全连接层

layer {  name: "ip1"  type: "InnerProduct"  bottom: "pool2"//输入blob  top: "ip1"//输出blob  param {    lr_mult: 1  }  param {    lr_mult: 2  }  inner_product_param {//全连接层参数    num_output: 500//该层输出参数为500    weight_filler {      type: "xavier"    }    bias_filler {      type: "constant"    }  }}

LeNet训练激活函数层

ReLU层

layer {//非线性层  name: "relu1"  type: "ReLU"  bottom: "ip1"  top: "ip1"}

Softmax层

layer {//损失层  name: "loss"  type: "SoftmaxWithLoss"//损失函数  bottom: "ip2"  bottom: "label"  top: "loss"}

参数配置

Caffe中的参数配置文件.solver.prototxt定义了网络模型训练过程中需要设置的参数,比如学习率、权重衰减系数、迭代次数、使用CPU还是GPU等。

LeNet的参数文件解析

# The train/test net protocol buffer definitionnet: "examples/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//预测阶段迭代次数(要求与TEST层的batch_size相乘之后等于总的预测集的图片数)# 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 policylr_policy: "inv"//学习速率的衰减策略gamma: 0.0001power: 0.75# Display every 100 iterationsdisplay: 100//没经过100次迭代在屏幕上打印一次运行log# The maximum number of iterationsmax_iter: 10000//最大迭代次数# snapshot intermediate resultssnapshot: 5000snapshot_prefix: "examples/mnist/lenet"# solver mode: CPU or GPUsolver_mode: GPU//选择使用CPU还是GPU进行训练

训练出的输出文件格式为.caffemodel即所求的model,可以拷贝至目标机器进行分类、定位和识别。

参考资料:
【1】:深度学习——Caffe之经典模型详解与实战 乐毅 王斌 编著
【2】:深度学习——21天实战caffe 赵永科 编著

原创粉丝点击