Windows caffe 跑mnist實例


     一. 裝完caffe當然要來跑跑自帶的demo,在examples文件夾下。

先來試試用於手寫數字識別的mnist,在 examples/mnist/ 下有需要的代碼文件,但是沒有圖像庫。

mnist庫有50000個訓練樣本,10000個測試樣本,都是手寫數字圖像。

  caffe支持的數據格式為:LMDB  LEVELDB

  IMDB比LEVELDB大,但是速度更快,且允許多種訓練模型同時讀取同一數據集。

  默認情況,examples里支持的是IMDB文件,不過你可以修改為LEVELDB,后面詳解。

  mnist數據集建議網上搜索下載,網盤有很多,注意將文件夾放到\examples\mnist目錄下,且最好命名為圖中格式,

否則可能無法讀取文件需手動配置。

  筆者之前下的數據集命名的下划線是連接線就會報錯無法讀取文件,所以注意文件夾名字!

 Windows下最好選擇LEVELDB文件,Linux則隨意了。下好了LEVELDB文件就不用再使用convert_imageset函數了,省去了轉換圖片格式和計算均值的步驟。

  二. 訓練mnist模型

  mnist的網絡訓練模型文件為: lenet_train_test.prototxt

name: "LeNet"
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_train_leveldb"
    batch_size: 64
    backend: LEVELDB
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_test_leveldb"
    batch_size: 100
    backend: LEVELDB
  }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

一般修改兩個DATA層的 “source”文件路徑就行,上面的例子中,我已經改了,改為mnist的訓練集和測試集文件夾路徑。再就是注意“backend: LEVELDB”,默認的backend應該是IMDB要修改!

  網絡模型 lenet_train_test.prototxt修改后再修改 lenet_solver.prototxt

該文件主要是一些學習參數和策略:

  

 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: CPU

帶#的注釋可以不管,能理解最好:

  第二行的 net:  路徑需改為自己的網絡模型xx_train_test.prototxt路徑。其他的學習率 base_lr,lr_policy等不建議修改;max_iter最大迭代次數可以稍微改小,display顯示間隔也可以隨意修改~最后一行,我是只有CPU模式所以設為CPU,如果可以用GPU加速可設為GPU!

  到這基本設置就結束了,然后就是寫命令執行測試程序了:

我選擇寫了批處理.bat文件執行,也可以直接在CMD環境輸命令執行。

  新建mnist_train.bat,內容如下:

cd ../../
"Build/x64/Debug/caffe.exe" train --solver=examples/mnist/lenet_solver.prototxt 
pause 

根據自己的情況修改第二行的路徑位置,Windows應該都是在Build/x64目錄下,有的博客寫的/bin/目錄其實是Linux的並不適用於Windows環境。還要注意使用斜線“/”,不要使用“\”無法識別,Python代碼多為后者要修改!

我的環境只有Debug目錄,如果你有Realease目錄,使用Realease目錄。

 運行.bat成功后,會開始訓練,訓練結束界面如下:

  最后幾行可以看到accuracy的准確率可以達到99%,也是相當准確了!

提示,caffe文件夾內會生成.caffemodel文件

使用caffemodel文件開始測試:

  三.測試數據

  由於測試數據集也是直接下載好了的LEVELDB文件,所以省了不少步驟

  直接新建mnist_test.bat文件,類似訓練mnist模型一樣,對該模型進行數據測試。  

cd ../../
"Build/x64/Debug/caffe.exe" test --model=examples/mnist/lenet_train_test.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel
pause 

  類似mnits_train.bat,修改文件路徑名,test表示用於測試,model指向自己的網絡模型文件,最后添加權值文件.caffemodel進行測試。

  運行mnist_test.bat后,成功界面如下:

  

  最后一行還是有98%的准確率還是很不錯的,說明模型生成的還不錯。

  

總結:其實還遇到了不少零零碎碎的問題,大多都可以百度解決,主要是記得修改對自己的文件路徑目錄,Windows下一定要使用LEVELDB數據文件,.prototxt也記得修改,然后就是等待模型跑完看結果了,看到高准確率還是很開心的~

  四. 使用該模型

  模型訓練好了,數據也只是測試了,那么我們要使用該模型判斷一張圖片是數字幾該如何做呢?

這個時候需要生成 classification.exe,然后執行相應的.bat命令來預測圖片的分類結果。

  mnist分類使用可以參考http://www.cnblogs.com/yixuan-xu/p/5862657.html

  發現OpenCV可以加載caffe 框架模型,准備再寫一篇博客進行實踐介紹~

http://docs.opencv.org/3.1.0/d5/de7/tutorial_dnn_googlenet.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM