一.Lenet简介
- Lenet是YANN LECUN等人提出的一种识别手写体的深度神经网络架构.
- Lenet训练网络:输入层定义大小为32*32,有两个卷积层(CNN),两个池化层,两个全连接层,一个loss.输出层的神经元个数为10(也就是10个数字)
二.caffe上Lenet的训练和测试
- caffe深度学习框架自带有lenet的训练和测试,接下来详细介绍训练和测试的流程.
- caffe里会用到的文件data/mnist/ 和 examples/mnist/这两个路径下的相关文件
-data/mnist/路径下的 get_mnist.sh是来下载原始手写体数据
- examples/mnist/路径下的 create_mnist.sh s是把原始数据转化位LMDB的脚本
- examples/mnist/路径下的 lenet_tarin_test.prototxt, lenet_solver.prototxt,lenet.prototxt,是训练的相关文件
- 训练
- 准备原始数据
- 生成LMDB文件
- 修改网络文件lenet_train_test.prototxt
- 配置参数文件lenet_solver.prototxt
- 执行tain_lenet.sh 开始训练
- 准备原始数据
- 在/data/mnist/路径下打开终端,输入下面命令,执行成功后会此目录下生成四个文件
sh get_mnist.sh
- 下载后在caffe文件夹下打开终端,输入下面命令,执行create_mnist.sh生成两个LMDB文件夹:mnist_train_lmdb和mnist_test_lmdb
sh ./examples/mnist/create_mnist.sh
模型文件lenet_train_test.prototxt介绍
网络模型图
lenet网络文件
name: "LeNet" #网络名称
layer {
name: "mnist"
type: "Data" #数据层
top: "data"
top: "label"
include {
phase: TRAIN #训练
}
transform_param {
scale: 0.00390625 #归一化
}
data_param {
source: "examples/mnist/mnist_train_lmdb" #lmdb文件文字
batch_size: 64
backend: LMDB
}
}
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST #测试
}
transform_param {
scale: 0.00390625
}
data_param {
source: "examples/mnist/mnist_test_lmdb" #lmdb文件的位置
batch_size: 100
backend: LMDB
}
}
layer { #卷积层
name: "conv1" #名称
type: "Convolution" #cnn
bottom: "data" #输入
top: "conv1" #输出
param { #参数设置,基础学习速率的倍数
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param { #卷积核的参数设置
num_output: 20 #输出的数量
kernel_size: 5 #卷积核的大小5*5
stride: 1 #步长为1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer { #POOLING层
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX #采用MAXpooling
kernel_size: 2 #polling核的大小
stride: 2 #步长为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 { #全连接层
name: "ip1" #名称
type: "InnerProduct" #全连接层
bottom: "pool2" #输入
top: "ip1" #输出
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 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
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer { #精度层,测试的时候才参与计算,只有前向传播
name: "accuracy"
type: "Accuracy"
bottom: "ip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
layer { #loss层
name: "loss"
type: "SoftmaxWithLoss" #SoftMax计算loss
bottom: "ip2" #输入每类预测结果
bottom: "label" #输入标签
top: "loss"
}
- lenet_solver,prototxt 参数文件
- lenet_solver.prototxt 是设置网络的路径,学习基础速率,权重衰减,训练次数,测试次数,训练是否选则GPU,多久保存一次权重的参数文件
# The train/test net protocol buffer definition
net: "examples/mnist/lenet_train_test.prototxt" #网络文件位置
test_iter: 100 #测试迭代次数
# 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 #每训练190次迭代,终端打印一次信息
# The maximum number of iterations
max_iter: 10000 #训练次数
# snapshot intermediate results
snapshot: 5000 #每训练5000次保存一次权重
snapshot_prefix: "examples/mnist/lenet" #examples/mnist代表权重的保存路径,lenet权重文件开头命名
# solver mode: CPU or GPU
solver_mode:GPU #是否使用GPU,如果只调用CPU则改为CPU
- 开始训练
在 /examples/mnist/ 目录下新建lenet_train_test.sh文件,文件编辑以下代码后保存.
#!/usr/bin/env sh
set -e
LOG=./examples/mnist/train-`date +%Y-%m-%d-%H-%M-%S`.log #运行工程保存为日志文件
build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt 2>&1 | tee $LOG
- 保存后在 caffe 目录下打开终端输入以下命令,开始训练.
sh ./examples/mnist/lenet_train_test.sh
训练开始和结束如下图所示
查看/examples/mnist/下的 train -时间-.log日志文件,保存了上面图中训练过程的所有参数.