Windows caffe 跑mnist執行個體

來源:互聯網
上載者:User

標籤:mode   sse   fill   載入   output   注釋   迭代   st表   es2017   

     一. 裝完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.0111 momentum: 0.912 weight_decay: 0.000513 # The learning rate policy14 lr_policy: "inv"15 gamma: 0.000116 power: 0.7517 # Display every 100 iterations18 display: 10019 # The maximum number of iterations20 max_iter: 1000021 # snapshot intermediate results22 snapshot: 500023 snapshot_prefix: "examples/mnist/lenet"24 # solver mode: CPU or GPU25 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.caffemodelpause 

  類似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

Windows caffe 跑mnist執行個體

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.