Caffe 入门(训练mnist)

使用caffe训练模型只需要以下几个步骤:

(1)准备好数据;

(2)写好模型配置文件;

(3)写好优化配置文件;

(4)命令行执行;

这样就可以得到训练的模型.caffemodel文件了


1.caffe的下载与安装:

(1)下载

(2)安装

扫描二维码关注公众号,回复: 4927877 查看本文章

(3)caffe的下载与安装以及一些基本的介绍官网已经描述地比较详细,这里不再重复;

2.caffe的使用接口有命令行,python跟matlab,个人觉得训练模型的时候使用命令行已经足够简单了,至于训练好的模型可以使用python与matlab的接口进行调用,本文先描述基于命令行的模型训练,以LeNet模型为例;

LeNet模型


PS:LeNet是手写数字库MNIST上应用比较经典的模型,具有7层网络结构,分别是卷积-下采样-卷积-下采样-全连-全连-分类层,具体网络细节可以参考文章:

Gradient based learning applied to document recognition

1.安装编译完caffe后,其主目录下有:


2.训练模型之前需要先准备好训练数据MNIST,执行以下命令可以下载MNIST数据库:


3.由于caffe支持的数据类型不包括图像类型,所以常规做法需要将图像类型转为lmdb类型:


4.准备好数据之后,我们需要定义我们的网络模型,在caffe中是通过.prototxt配置文件来定义的,执行以下命令:


可以看到各个网络层是如何定义的:

(1)输入层(数据层):

[plain]  view plain  copy
  1. layer {  
  2.   name: "mnist"      //表示层名  
  3.   type: "Data"       //表示层的类型  
  4.   top: "data"          
  5.   top: "label"  
  6.   include {  
  7.     phase: TRAIN      //表示仅在训练阶段起作用  
  8.   }  
  9.   transform_param {  
  10.     scale: 0.00390625  //将图像像素值归一化  
  11.   }  
  12.   data_param {  
  13.     source: "examples/mnist/mnist_train_lmdb"   //数据来源  
  14.     batch_size: 64                              //训练时每个迭代的输入样本数量  
  15.     backend: LMDB                               //数据类型  
  16.   }  
  17. }  
(2)卷积层:

[plain]  view plain  copy
  1. layer {  
  2.   name: "conv1"  
  3.   type: "Convolution"  
  4.   bottom: "data"               //输入是data  
  5.   top: "conv1"                 //输出是卷积特征  
  6.   param {  
  7.     lr_mult: 1                 //权重参数w的学习率倍数  
  8.   }  
  9.   param {  
  10.     lr_mult: 2                 //偏置参数b的学习率倍数  
  11.   }  
  12.   convolution_param {  
  13.     num_output: 20  
  14.     kernel_size: 5  
  15.     stride: 1  
  16.     weight_filler {           //权重参数w的初始化方案,使用xavier算法  
  17.       type: "xavier"  
  18.     }  
  19.     bias_filler {  
  20.       type: "constant"       //偏置参数b初始化化为常数,一般为0  
  21.     }  
  22.   }  
  23. }  

(3)下采样层(pool):

[plain]  view plain  copy
  1. layer {  
  2.   name: "pool1"  
  3.   type: "Pooling"  
  4.   bottom: "conv1"  
  5.   top: "pool1"  
  6.   pooling_param {  
  7.     pool: MAX  
  8.     kernel_size: 2  
  9.     stride: 2  
  10.   }  
  11. }  

(4)全连层:

[plain]  view plain  copy
  1. layer {  
  2.   name: "ip1"  
  3.   type: "InnerProduct"  
  4.   bottom: "pool2"  
  5.   top: "ip1"  
  6.   param {  
  7.     lr_mult: 1  
  8.   }  
  9.   param {  
  10.     lr_mult: 2  
  11.   }  
  12.   inner_product_param {  
  13.     num_output: 500  
  14.     weight_filler {  
  15.       type: "xavier"  
  16.     }  
  17.     bias_filler {  
  18.       type: "constant"  
  19.     }  
  20.   }  
  21. }  
(5)非线性层:

[plain]  view plain  copy
  1. layer {  
  2.   name: "relu1"  
  3.   type: "ReLU"  
  4.   bottom: "ip1"  
  5.   top: "ip1"  
  6. }  
(6)准确率层(计算准确率):

[plain]  view plain  copy
  1. layer {  
  2.   name: "accuracy"  
  3.   type: "Accuracy"  
  4.   bottom: "ip2"  
  5.   bottom: "label"  
  6.   top: "accuracy"  
  7.   include {  
  8.     phase: TEST  
  9.   }  
  10. }  
(7)损失估计层:

[plain]  view plain  copy
  1. layer {  
  2.   name: "loss"  
  3.   type: "SoftmaxWithLoss"  
  4.   bottom: "ip2"  
  5.   bottom: "label"  
  6.   top: "loss"  
  7. }  

5.定义完网络模型,还需要配置关于模型优化的文件:

配置文件如下:

[plain]  view plain  copy
  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  
  7. # Carry out testing every 500 training iterations.  
  8. test_interval: 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  
  18. display: 100  
  19. # The maximum number of iterations  
  20. max_iter: 10000  
  21. # snapshot intermediate results  
  22. snapshot: 5000  
  23. snapshot_prefix: "examples/mnist/lenet"  
  24. # solver mode: CPU or GPU  
  25. solver_mode: GPU  

6.接下来一步就是进行训练了,直接执行命令就可以:


执行后可以看到:首先会读取配置文件初始化网络跟优化器:


紧接着开始优化:

可以看到训练过程中每100次迭代就会显示一个loss,每500次迭代就会计算一次test准确率,总共10000次迭代,这些都可以在配置文件中设置;

7.训练完之后的模型就保存在.caffemodel文件中,该文件可以被c,python,matlab等调用;


猜你喜欢

转载自blog.csdn.net/alva_bobo/article/details/79084763
今日推荐