Caffe上配置和运行MNIST

MNIST,一个经典的手写数字库,包含60000个训练样本和10000个测试样本,图片大小28*28,在Caffe上配置的第一个案例
 
1首先,获取minist的数据包。 这个版本是四个数据包
cd $CAFFE_ROOT
./data/mnist/get_mnist.sh


[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #!/usr/bin/env sh  
  2. This scripts downloads the mnist data and unzips it.  
  3. DIR="$( cd "$(dirname "$0")" pwd -P )"  
  4. cd $DIR  
  5. echo "Downloading..."  
  6.   
  7. wget --no-check-certificate http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz  
  8. wget --no-check-certificate http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz  
  9. wget --no-check-certificate http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz  
  10. wget --no-check-certificate http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz  
  11.   
  12. echo "Unzipping..."  
  13.   
  14.   
  15. gunzip train-images-idx3-ubyte.gz  
  16. gunzip train-labels-idx1-ubyte.gz  
  17. gunzip t10k-images-idx3-ubyte.gz  
  18. gunzip t10k-labels-idx1-ubyte.gz  
  19.   
  20.   
  21. Creation is split out because leveldb sometimes causes segfault  
  22. and needs to be re-created.  
  23.   
  24. echo "Done."  


然后执行

./examples/mnist/create_mnist.sh


[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Creating lmdb...  
  2. Done.  

在这一步做了什么工作呢?
create_mnist.sh是利用caffe-master/build/examples/mnist/的convert_mnist_data.bin工具,将mnist date转化为可用的lmdb格式的文件。并将新生成的2个文件mnist-train-lmdb 和 mnist-test-lmdb放于create_mnist.sh同目录下。

2 数据准备好了,那么接下来的工作就是训练了。

http://caffe.berkeleyvision.org/gathered/examples/mnist.html

给出来的例子是 

./examples/mnist/train_lenet.sh

这个脚本调用的工具如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt  


还有其他的示例,如:

./examples/mnist/train_mnist_autoencoder.sh
这个脚本调用的工具如下: 
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ./build/tools/caffe train  
  2.   --solver=examples/mnist/mnist_autoencoder_solver.prototxt  



运行完结果如下:

生成四个文件

lenet_iter_10000.caffemodel         
lenet_iter_10000.solverstate      
lenet_iter_5000.caffemodel         
lenet_iter_5000.solverstate 

屏幕显示每次
.......................
I0126 17:32:32.171516 18290 solver.cpp:246] Iteration 10000, loss = 0.00453533
I0126 17:32:32.171550 18290 solver.cpp:264] Iteration 10000, Testing net (#0)
I0126 17:32:40.498195 18290 solver.cpp:315]     Test net output #0: accuracy = 0.9903
I0126 17:32:40.498236 18290 solver.cpp:315]     Test net output #1: loss = 0.0309918 (* 1 = 0.0309918 loss)
I0126 17:32:40.498245 18290 solver.cpp:251] Optimization Done.
I0126 17:32:40.498249 18290 caffe.cpp:121] Optimization Done.


首先讨论训练的网络模型:LeNet: the MNIST Classification Model   http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf
  
LeNet 模型之前在手写识别上就有非常好的表现。caffe 这里提供的是一个改进版的LeNet模型,其中的 sigmoid 被rectified linear units (ReLUs) 替换。
(标准的sigmoid输出不具备稀疏性,需要用一些惩罚因子来训练出一大堆接近0的冗余数据来,从而产生稀疏数据,例如L1、L1/L2或Student-t作惩罚因子。因此需要进行无监督的预训练。多层的神经网络如果用sigmoid或tanh激活函数也不做pre-training的话会因为 gradient vanishing problem 而会无法收敛。ReLU则这没有这个问题。ReLU是线性修正,公式为:g(x) = max(0, x),是purelin的折线版。它的作用是如果计算出的值小于0,就让它等于0,否则保持原来的值不变。这是一种简单粗暴地强制某些数据为0的方法,然而经实践证明,训练后的网络完全具备适度的稀疏性。而且训练后的可视化效果和传统方式预训练出的效果很相似,这也说明了ReLU具备引导适度稀疏的能力。 来自讨论 http://tieba.baidu.com/p/3061925556

LeNet的结构和CNN的结构是由相似之处的,是由两个 convolutional layer 和 pooling layer交错连接 ,然后两个fully connected layers结构组成。  具体可参见http://deeplearning.net/tutorial/lenet.html,此外DeepID 有个简单直观的结构图,可以辅助了解 http://blog.csdn.net/stdcoutzyx/article/details/42091205

定义MNIST网络和MNIST Solver:

从 ./examples/mnist/train_lenet.sh 脚本调用的指令我们就可以看到,solver是定义在了$CAFFE_ROOT/examples/mnist/lenet_solver.prototxt 中。

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. The train/test net protocol buffer definition  
  2. net: "examples/mnist/lenet_train_test.prototxt"        //网络协议具体定义  
  3. test_iter specifies how many forward passes the test should carry out.  
  4. In the case of MNIST, we have test batch size 100 and 100 test iterations,  
  5. covering the full 10,000 testing images.  
  6. test_iter: 100                                         //test迭代次数 如果batch_size =100,则100张图一批,训练100次,则可以覆盖10000张图的需求  
  7. Carry out testing every 500 training iterations.        
  8. test_interval: 500                                     //训练迭代500次,测试一次  
  9. The base learning rate, momentum and the weight decay of the network. //网络参数:学习率,动量,权重的衰减  
  10. base_lr: 0.01  
  11. momentum: 0.9  
  12. weight_decay: 0.0005  
  13. The learning rate policy                            //学习策略:有固定学习率和每步递减学习率  
  14. lr_policy: "inv"  
  15. gamma: 0.0001  
  16. power: 0.75  
  17. Display every 100 iterations                        //每迭代100次显示一次   
  18. display: 100  
  19. The maximum number of iterations                    //最大迭代次数  
  20. max_iter: 10000  
  21. snapshot intermediate results                       // 每5000次迭代存储一次数据,路径前缀是<</span>span style="font-family: Arial, Helvetica, sans-serif;">examples/mnist/lenet</</span>span>  
  22. snapshot: 5000  
  23. snapshot_prefix: "examples/mnist/lenet"  
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. solver_mode: CPU  


 

再看一下./examples/mnist/train_mnist_autoencoder.sh 调用的 mnist_autoencoder_solver.prototxt  的solver定义 

当所有数据都训练好之后,接下来就是如何将模型应用到实际数据了:

./build/tools/caffe.bin test -model=examples/mnist/lenet_train_test.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel -gpu=0 

如果没有GPU则使用

./build/tools/caffe.bin test -model=examples/mnist/lenet_train_test.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel

 

test:表示对训练好的模型进行Testing,而不是training。其他参数包括train, time, device_query。

-model=XXX:指定模型prototxt文件,这是一个文本文件,详细描述了网络结构和数据集信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值