可以先看下官網說明文檔
http://caffe.berkeleyvision.org/gathered/examples/cifar10.html
cifar10是什麼?
CIFAR-10 是一個包含60000張圖片的數據集。
其中每張照片爲32*32的彩色照片,每個像素點包括RGB三個數值,數值範圍 0 ~ 255。
所有照片分屬10個不同的類別,分別是 ‘airplane’, ‘automobile’, ‘bird’, ‘cat’, ‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’
數據集的數據存在一個10000*3072 的 numpy數組中,單位是uint8s,3072是存儲了一個32*32的彩色圖像。(3072=1024*3)。前1024位是r值,中間1024是g值,後面1024是b值。就像下圖這樣:
cifar10 工程的作用是一個將彩色的 32X32 圖片辨認出來。
默認已經配置好caffe環境,各種工具都編譯好了的,下面就進行訓練和測試,上一篇對於mnist的訓練採用的leveldb格式,這次我們用lmdb來試試。
1、下載數據集
進入官網鏈接下載
http://www.cs.toronto.edu/~kriz/cifar.html
解壓後會得到二進制文件,直接拷貝到./caffe/data/cifar10下就可以了:
其中五萬張圖片被劃分爲訓練集,剩下的一萬張圖片屬於測試集。
2、數據格式處理
使用 convert_cifar_data.exe 工具,具體源碼工程裏可以看到。
建立一個批處理文件,比如名字叫 create_cifar10_lmdb.bat
echo "Creating lmdb..."
rd /s /q cifar10_train_lmdbrd /s /q
cifar10_test_lmdbe:/caffe/Build/x64/Release/convert_cifar_data.exe
e:/caffe/data/cifar10 e:/caffe/examples/cifar10 lmdb
echo "Done."
pause
雙擊運行後會生成數據庫文件 cifar10_train_lmdb 和 cifar10_test_lmdb
3、計算均值
使用compute_image_mean.exe工具,具體源碼工程裏可以看到。
建立一個批處理文件,比如名字叫 mean_image.bat
echo "Computing image mean..."
E:/caffe/Build/x64/Release/compute_image_mean.exe -backend=lmdb e:/caffe/examples/cifar10/cifar10_train_lmdb e:/caffe/examples/cifar10/mean.binaryprotoecho
"Done."
pause
注意::-backend=lmdb參數一定要加上,雖然默認是lmdb,如果要編譯leveldb的時候忘了就會報錯。
雙擊運行後生成數據庫圖像均值二進制文件./mean.binaryproto
4、網絡配置
在網絡文件 cifar10_quick_train_test.prototxt 中設置數據路徑
說明:
1)均值和模型路徑修改爲絕對值
2)注意backend:LMDB 的數據庫類型要用大寫,無空格,我就是卡在這兒很久。
5、求解設置
cifar10_full_solver.prototxt
說明:
1)網絡路徑設置爲絕對路徑
2)指定模型輸出路徑,創建一個cifar10_quick文件夾
3)定位到最後一行:solver_mode: GPU,默認是GPU,如你沒有GPU則改爲CPU
6、訓練數據
使用caffe.exe訓練,具體源碼工程裏可以看到。
建立一個批處理文件,比如名字叫 train_quick.bat
echo "Start Train..."
e:/caffe/Build/x64/Release/caffe train --solver=e:/caffe/examples/cifar10/cifar10_quick_solver.prototxt
pause
開始訓練過程:
訓練完成就可以得到:
cifar10_quick_iter_4000.solverstate.h5
cifar10_quick_iter_4000.caffemodel.h5
7、測試模型
工具採用classification.exe,具體源碼工程裏可以看到。
先建立一個分類文本,比如叫 synset_words.txt
airplane
automobile
bird
cat
deer
dog
frog
horse
ship
truck
創建一個測試的批處理文件,比如叫 test_quick.bat
echo "Start Test..."
e:/caffe/Build/x64/Release/classification.exe ^
e:/caffe/examples/cifar10/cifar10_quick.prototxt ^
e:/caffe/examples/cifar10/cifar10_quick_iter_4000.caffemodel.h5 ^
e:/caffe/examples/cifar10/mean.binaryproto ^
e:/caffe/examples/cifar10/synset_words.txt ^
e:/caffe/examples/images/cat.jpg
pause
雙擊運行就可以開始分類測試了,下面是分類結果。
用的自帶圖片.\caffe\examples\images\cat.jpg,測試結果不是很準啊。