1. 程式人生 > >Caffe的運行mnist手寫數字識別

Caffe的運行mnist手寫數字識別

而不是 所在 結果 ack cif sting one efi 打開

老規矩,首先附上官方教程:http://caffe.berkeleyvision.org/gathered/examples/mnist.html

1、必要軟件

  因為Caffe中使用的是Linux才能運行的shell腳本,因此首先的安裝 wget(將wget放入C:\windows\system32)和 Git 方能運行。

2、而後按照官方教程,首先進入caffe路徑的根目錄,而後打開cmd輸入命令:

./data/mnist/get_mnist.sh

這個命令是通過打開/data/mnist目錄下的get_mnist.sh腳本來下載mnist的數據,若cmd出現錯誤可以直接進入打開get_mnist.sh腳本效果是一樣的,運行完成後會出現如下4個數據文件:

技術分享圖片

而後繼續輸入以下命令,或者進入路徑打開也一樣

./examples/mnist/create_mnist.sh

若不存在該文件可以自己創建一個create_mnist.sh,具體的代碼如下(註:第九行BUILD可能老版本的路徑會不一樣,根據自己路徑來修改):

#!/usr/bin/env sh
        
# This script converts the mnist data into lmdb/leveldb format,
# depending on the value assigned to $BACKEND.
set -e

EXAMPLE=.
DATA=../../data/mnist
BUILD=../../scripts/build/examples/mnist/Release

BACKEND="lmdb"

echo "Creating ${BACKEND}..."

rm -rf $EXAMPLE/mnist_train_${BACKEND}
rm -rf $EXAMPLE/mnist_test_${BACKEND}

$BUILD/convert_mnist_data.exe $DATA/train-images-idx3-ubyte   $DATA/train-labels-idx1-ubyte $EXAMPLE/mnist_train_${BACKEND} --backend=${BACKEND}
$BUILD/convert_mnist_data.exe $DATA/t10k-images-idx3-ubyte   $DATA/t10k-labels-idx1-ubyte $EXAMPLE/mnist_test_${BACKEND} --backend=${BACKEND}

echo "Done."

read -p "回車繼續..."

  運行完成後會出現mnist_test_lmdb和mnist_train_lmdb兩個文件夾:

技術分享圖片

cmd顯示:

技術分享圖片

3、打開路徑/scripts/build/examples/mnist/Release下的lenet_solver.prototxt(不同版本的caffe的路徑不一樣,有些老版本的caffe的路徑為:/Build/x64/Release),根據自己的情況修改參數:

  第二行:若lenet_train_test.prototxt和lenet_solver.prototxt不在同一路徑下,則需要在其之前寫上lenet_train_test.prototxt所在的路徑

  第23行:snapshot_prefix:生成的model為產生的訓練模型,可根據自己來修改路徑

  最後一行為選擇安裝的caffe是CPU還是GPU,我這裏安裝的是GPU版本

註意:不要直接將文件路徑復制過去,因為在這裏面路徑分隔符是/ ,而不是\,如果使用\後面運行時會出現以下錯誤(下面幾步同樣如此,如果不確定就照著我的寫):

技術分享圖片

修改參數後的結果:

# The train/test net protocol buffer definition
net: "lenet_train_test.prototxt" 
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100
# Carry out testing every 500 training iterations.
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations
display: 100
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 5000
snapshot_prefix: "E:/CaffeSource/caffe/data/mnist/model"
# solver mode: CPU or GPU
solver_mode: GPU

4、打開lenet_train_test.prototxt(上面第二行那個文件)

技術分享圖片

而後更改上圖路徑,這兩個文件是執行./data/mnist/get_mnist.sh命令時下載的文件,將其路徑添加進去

5、在目錄\examples\mnist下新建一個train_lenet.txt文檔,添加下面一段,然後改後綴名為.bat

..\..\Build\x64\Release\caffe.exe train --solver="lenet_solver.prototxt" --gpu 0
pause

  或者在該目錄下修改train_lenet.sh文件:

#!/usr/bin/env sh
set -e
BUILD=../../Build/x64/Release/
echo "Training lenet_solver.prototxt..."

$BUILD/caffe.exe train --solver=lenet_solver.prototxt $@
echo "Done."

read -p "回車繼續..."

6、運行該文件,大概運行幾分鐘後結果如下:

技術分享圖片

若沒有報錯,則測試就算大功告成啦!

可以看出準確度為99%,訓練好的模型保存在 lenet_iter_10000.caffemodel, 訓練狀態保存在lenet_iter_10000.solverstate裏,結果如下:

技術分享圖片





Caffe的運行mnist手寫數字識別