mnist训练lenet网络

在深度学习中,mnist就至关于hello world,是大多数初学者接触的第一个例子。通过mnist训练的lenet能够用于对0-9这是个数字作分类。下面先来看下lenet的网络结构:
这里写图片描述
比起如今的VGG/resnet等等来讲,lenet能够算得上是至关简单了,最初Yann Lecun是为了解决邮编问题而设计的,在当时能够达到90%以上的准确率。到2012年,Alex等人只是对lenet作了很小的改动,就取得了imagenet比赛的冠军。说完了背景介绍,那简单的看一下caffe中给出的例子。git

1.准备数据
1)直接用做者准备好的数据集训练:github

cd $CAFFE_ROOT
./data/mnist/get_mnist.sh

2)本身的数据集:
若是用本身的数据集,应该首先把图片resize到28X28,最好处理为二值图或者灰度图,而后github有热心人作了wrapper,能够生成mnist格式的压缩文件:
gskielian/JPG-PNG-to-MNIST-NN-Formatweb

2.生成lmdb文件
这一步直接用官方给的专门用于mnist的脚本便可网络

cd $CAFFE_ROOT
./examples/mnist/create_mnist.sh

3.配置训练网络描述文件lenet_train_test.prototxt
在这里咱们有几个参数能够调整:
1)第15行训练的batch size,默认是64,表示的意思是每次训练放入多少个数据
2)第32行测试的batchsize,默认100,表示在validation时候每次计算多少个数据的accuracy
3)第143行的num output,表示要作几分类问题。这里须要注意,若是是从0开始训练,这里能够只更改数字;若是是finetuning那么这一层的weight是没法继承的,应该将这一层换个名字,以避免输出不匹配。
固然更进一步的,咱们能够修改激活函数,损失函数,甚至是网络层数等等,这就要根据具体问题具体分析了。app

4.配置网络的超参数svg

# The train/test net protocol buffer definition
net: "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
# Carry out testing every 500 training iterations.
test_interval: 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
# The maximum number of iterations
max_iter: 5000
# snapshot intermediate results
snapshot: 1000
snapshot_prefix: "examples/mnist/lenet_two_base_other"

在这里每一个参数的意思官方都已经解释的很清晰了,在这里不赘述。 函数

5.训练网络
1)从0开始训练能够直接调用官方的脚本:学习

cd $CAFFE_ROOT
./examples/mnist/train_lenet.sh

2)作finetuning就是要选择一个初始的weight测试

./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel

而这个初始参数咱们就能够选取官方迭代10000次的结果。ui

6.测试网络

cd $CAFFE_ROOT
build/tools/caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel  -iterations 100