mnist训练lenet网络

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

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

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

2)自己的数据集:
如果用自己的数据集,应该首先把图片resize到28X28,最好处理为二值图或者灰度图,然后github有热心人做了wrapper,可以生成mnist格式的压缩文件:
gskielian/JPG-PNG-to-MNIST-NN-Format

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是无法继承的,应该将这一层换个名字,以免输出不匹配。
当然更进一步的,我们可以修改激活函数,损失函数,甚至是网络层数等等,这就要根据具体问题具体分析了。

4.配置网络的超参数

# 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次的结果。

6.测试网络

cd $CAFFE_ROOT
build/tools/caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel  -iterations 100
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值