caffe 练习1:training LeNet on MNIST with Caffe/ 用LeNet识别手写字符集 Mnist------by 香蕉麦乐迪
這個實驗拿來入門最好了,很輕松,執行作者寫好的腳本就可以;
官網鏈接:http://caffe.berkeleyvision.org/gathered/examples/mnist.html
跟著這個實驗,可以觀察caffe使用的流程,深入一點,可以看看作者寫的shell腳本做了哪些事情;
下面我們一步步來執行,并作簡單的分析:
1 首先準備數據集
執行下面的命令。$CAFFE_ROOT是指caffe安裝的目錄,比如說我的是/home/sloanqin/caffe-master/
[cpp] view plaincopy
cd $CAFFE_ROOT
./data/mnist/get_mnist.sh //執行下載mnist數據集腳本
./examples/mnist/create_mnist.sh //將下載的mnist數據集轉換成lmdb格式的數據
我們來看看這兩個腳本做了什么:
1.1 執行腳本get_mnist.sh:
[cpp] view plaincopy
get_mnist.sh腳本文件如下:
[cpp] view plaincopy
DIR=”(cd"(dirname “0”)”;pwd?P)”cdDIR
echo “Downloading…”
wget –no-check-certificate http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
wget –no-check-certificate http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
wget –no-check-certificate http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
wget –no-check-certificate http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
echo “Unzipping…”
gunzip train-images-idx3-ubyte.gz
gunzip train-labels-idx1-ubyte.gz
gunzip t10k-images-idx3-ubyte.gz
gunzip t10k-labels-idx1-ubyte.gz
# Creation is split out because leveldb sometimes causes segfault
# and needs to be re-created.
echo “Done.”
[html] view plaincopy
作用:下載下面四個文件,并解壓;
[html] view plaincopy
train-images-idx3-ubyte.gz訓練集
train-labels-idx1-ubyte.gz訓練集的標簽
t10k-images-idx3-ubyte.gz測試集
t10k-labels-idx1-ubyte.gz測試集的標簽
2 定義caffe的網絡結構:.prototxt 文件
caffe的網絡結構定義在后綴名為.prototxt的文件中,我們根據自己的需要定義自己的網絡結構;
在這個實驗中,我們使用作者已經為我們定義好的lenet網絡結構,大家可以在下面的目錄中找到該文件:
$CAFFE_ROOT/examples/mnist/lenet_train_test.prototxt
在我的電腦上,目錄是/home/sloanqin/examples/mnist/lenet_train_test.prototxt
在后續的工作中,定義好自己的網絡結構是最關鍵的,直接決定了性能,這里我們就不多說了;
3 定義caffe運算的時候的一些規則:solver.prototxt 文件
該文件在下面的目錄中:
$CAFFE_ROOT/examples/mnist/lenet_solver.prototxt
文件內容如下:作者給出了英文注釋,我再給出中文的注釋
[html] view plaincopy
# The train/test net protocol buffer definition
net: “examples/mnist/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: “examples/mnist/lenet”
# solver mode: CPU or GPU
solver_mode: GPU
中文注釋版本:
[html] view plaincopy
net: “examples/mnist/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 // 這個參數指定測試的時候送入多少個
// 這里說明一個知識:GPU在計算的時候,每次迭代是多張圖片,我們稱為一個batch
// 作者提到:test batch size 100,就是說每個包有100張圖片
// 這里設置 st_iter=100,就是測試的時候一共輸入100*100=10000張圖片
//所以,test_iter 的英文翻譯就是:測試時迭代次數
# Carry out testing every 500 training iterations.
test_interval: 500 //定義每500次迭代,做一次測試
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01 // 定義了剛開始的學習速率是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 //每迭代100次,顯示一次計算結果
# The maximum number of iterations
max_iter: 10000 //設置最大的迭代次數
# snapshot intermediate results
snapshot: 5000 // 保存中間運行時得到的參數結果,這里設置成每5000次迭代保存一次,這樣運行中間斷掉了,我們可以從斷掉的地方繼續開始
snapshot_prefix: “examples/mnist/lenet”
# solver mode: CPU or GPU
solver_mode: GPU //使用CPU還是GPU計算
4 執行命令進行訓練
最后一步就是執行腳本開始訓練:
cd $CAFFE_ROOT
./examples/mnist/train_lenet.sh
我們打開這個腳本,可以看到特別簡單,就一行:
./build/tools/caffe train –solver=examples/mnist/lenet_solver.prototxt
這行代碼的意思:調用./build/tools/caffe目錄下面的train函數,train函數的輸入參數是solver.prototxt文件的路徑:–solver=examples/mnist/lenet_solver.prototxt
5 結果
運行的過程中,可以卡到test 的準確率在不斷上升;運行結束后,會生成模型文件:lenet_iter_10000.caffemodel
還有一個文件是snapshot 保存的:lenet_iter_10000.solverstate
原文鏈接:http://write.blog.csdn.net/postedit/49147935
總結
以上是生活随笔為你收集整理的caffe 练习1:training LeNet on MNIST with Caffe/ 用LeNet识别手写字符集 Mnist------by 香蕉麦乐迪的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: libsvm使用
- 下一篇: 1:ImageNet Classifica