Caffe CPU模式自己训练模型——MNIST数据集

运行环境 Ubuntu14.04.4

MNIST数据集是大型的手写数字数据库,60000个训练集和10000个测试集。

一、下载MNIST数据集
   
   
  1. cd /home/wjx/caffe/data/mnist
  2. ./get_mnist.sh

二、格式转换
下载的原始数据集为二进制文件,需要转化成LEVELDB或LMDB才能被Caffe识别。
   
   
  1. cd /home/wjx/caffe/examples/mnist
  2. ./create_mnist.sh
如果没有修改create_mnist.sh文件的话,会出现下面的情况:

说明路径出现问题,要对 create_mnist.sh做如下修改:
更改EXAMPLE,DATA,BUILD为绝对路径

然后再次运行./create_mnist.sh,得到结果:


说明已经改好了,浏览所在的目录 example/mnist,发现生成了mnist_train_lmdb/和mnist_test_lmdb两个目录,每个目录下都有两个文件:data.mdb和lock.mdb。接下来可以训练


三、训练
   
   
  1. ./train_lenet.sh
直接在终端输入会出现下面的情况:无法找到路径

对train_lenet.sh做如下的修改:修改路径为绝对路径


再次输入命令进行训练得到如下结果:


发现问题,还是出现路径的问题,这次修改lenet_solver.prototxt文件
特别注意最后一行的CPU/GPU模式
   
   
  1. # The train/test net protocol buffer definition
  2. net: "/home/neutech/caffe/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: "/home/neutech/caffe/examples/mnist/lenet"                #修改为绝对路径
  24. # solver mode: CPU or GPU
  25. solver_mode: CPU                                                           #如果为CPU模式改成CPU,默认为GPU
再次运行命令开始训练,出现下面的情况:


还是训练失败,同样的问题,找不到mnist_train_lmdb和 mnist_train_lmdb文件的路径。通过查看 lenet_solver.prototxt的内容
   
   
  1. net: "/home/neutech/caffe/examples/mnist/lenet_train_test.prototxt"    #修改为绝对路径
这一行描述的用于训练/预测的网络描述文件(ProtoBuffer文件格式),打开lenet_train_test.prototxt文件,修改:
   
   
  1. name: "LeNet"
  2. layer {
  3.  name: "mnist"
  4.  type: "Data"
  5.  top: "data"
  6.  top: "label"
  7.  include {
  8.    phase: TRAIN
  9.  }
  10.  transform_param {
  11.    scale: 0.00390625
  12.  }
  13.  data_param {
  14.    source: "/home/neutech/caffe/examples/mnist/mnist_train_lmdb"      #此处文件路径要检查清楚,修改为绝对路径
  15.    batch_size: 64
  16.    backend: LMDB
  17.  }
  18. }
  19. layer {
  20.  name: "mnist"
  21.  type: "Data"
  22.  top: "data"
  23.  top: "label"
  24.  include {
  25.    phase: TEST
  26.  }
  27.  transform_param {
  28.    scale: 0.00390625
  29.  }
  30.  data_param {
  31.    source: "/home/neutech/caffe/examples/mnist/mnist_test_lmdb"     #此处文件路径要检查清楚,修改为绝对路径
  32.    batch_size: 100
  33.    backend: LMDB
  34.  }
  35. }
  36. layer {
  37.  name: "conv1"
  38.  type: "Convolution"
  39.  bottom: "data"
  40.  top: "conv1"
  41.  param {
  42.    lr_mult: 1
  43.  }
  44.  param {
  45.    lr_mult: 2
  46.  }
  47.  convolution_param {
  48.    num_output: 20
  49.    kernel_size: 5
  50.    stride: 1
  51.    weight_filler {
  52.      type: "xavier"
  53.    }
  54.    bias_filler {
  55.      type: "constant"
  56.    }
  57.  }
  58. }
  59. layer {
  60.  name: "pool1"
  61.  type: "Pooling"
  62.  bottom: "conv1"
  63.  top: "pool1"
  64.  pooling_param {
  65.    pool: MAX
  66.    kernel_size: 2
  67.    stride: 2
  68.  }
  69. }
  70. layer {
  71.  name: "conv2"
  72.  type: "Convolution"
  73.  bottom: "pool1"
  74.  top: "conv2"
  75.  param {
  76.    lr_mult: 1
  77.  }
  78.  param {
  79.    lr_mult: 2
  80.  }
  81.  convolution_param {
  82.    num_output: 50
  83.    kernel_size: 5
  84.    stride: 1
  85.    weight_filler {
  86.      type: "xavier"
  87.    }
  88.    bias_filler {
  89.      type: "constant"
  90.    }
  91.  }
  92. }
  93. layer {
  94.  name: "pool2"
  95.  type: "Pooling"
  96.  bottom: "conv2"
  97.  top: "pool2"
  98.  pooling_param {
  99.    pool: MAX
  100.    kernel_size: 2
  101.    stride: 2
  102.  }
  103. }
  104. layer {
  105.  name: "ip1"
  106.  type: "InnerProduct"
  107.  bottom: "pool2"
  108.  top: "ip1"
  109.  param {
  110.    lr_mult: 1
  111.  }
  112.  param {
  113.    lr_mult: 2
  114.  }
  115.  inner_product_param {
  116.    num_output: 500
  117.    weight_filler {
  118.      type: "xavier"
  119.    }
  120.    bias_filler {
  121.      type: "constant"
  122.    }
  123.  }
  124. }
  125. layer {
  126.  name: "relu1"
  127.  type: "ReLU"
  128.  bottom: "ip1"
  129.  top: "ip1"
  130. }
  131. layer {
  132.  name: "ip2"
  133.  type: "InnerProduct"
  134.  bottom: "ip1"
  135.  top: "ip2"
  136.  param {
  137.    lr_mult: 1
  138.  }
  139.  param {
  140.    lr_mult: 2
  141.  }
  142.  inner_product_param {
  143.    num_output: 10
  144.    weight_filler {
  145.      type: "xavier"
  146.    }
  147.    bias_filler {
  148.      type: "constant"
  149.    }
  150.  }
  151. }
  152. layer {
  153.  name: "accuracy"
  154.  type: "Accuracy"
  155.  bottom: "ip2"
  156.  bottom: "label"
  157.  top: "accuracy"
  158.  include {
  159.    phase: TEST
  160.  }
  161. }
  162. layer {
  163.  name: "loss"
  164.  type: "SoftmaxWithLoss"
  165.  bottom: "ip2"
  166.  bottom: "label"
  167.  top: "loss"
  168. }
接下来可以进行训练了。会出现下面的训练日志:

 OK,大功告成!



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值