1. 程式人生 > >【12】Caffe學習系列:訓練和測試自己的圖片

【12】Caffe學習系列:訓練和測試自己的圖片

一、準備資料

有條件的同學,可以去imagenet的官網http://www.image-net.org/download-images,下載imagenet圖片來訓練。驗證碼始終出不來需要翻牆(是google網站的驗證碼)。但是我沒有下載,原因是資料太大了。。。

我去網上找了一些其它的圖片來代替,共有500張圖片,分為大巴車、恐龍、大象、鮮花和馬五個類,每個類100張。需要的同學,可到網盤下載:http://pan.baidu.com/s/1nuqlTnN

編號分別以3,4,5,6,7開頭,各為一類。我從其中每類選出20張作為測試,其餘80張作為訓練。因此最終訓練圖片400張,測試圖片100張,共5類。我將圖片放在caffe根目錄下的data資料夾下面。即訓練圖片目錄:data/four_classify/train/ ,測試圖片目錄: data/four_classify/test/

二、轉換為lmdb格式

具體的轉換過程,可參見我的前一篇博文:【11】Caffe學習系列:影象資料轉換成db(leveldb/lmdb)檔案

首先,在examples下面建立一個myfile的資料夾,來用存放配置檔案和指令碼檔案。然後編寫一個指令碼create_filelist.sh,用來生成train.txt和test.txt清單檔案

# mkdir examples/four_classify
# vi examples/four_classify/create_filelist.sh

編輯此檔案,寫入如下程式碼,並儲存

#!/usr/bin/env sh
DATA=data/four_classify
MY_example=examples/four_classify

echo "Create train.tx..."
rm -rf $MY_example/train.txt
for i in 3 4 5 6
do
find $DATA/train -name $i*.jpg | cut -d '/' -f4-5 | sed "s/$/ $i/">>$MY_example/train.txt
done
echo "Create test.txt..."
rm -rf $MY_example/test.txt
for i in 3 4 5 6 7
do
find $DATA/test -name $i*.jpg | cut -d '/' -f4-5 | sed "s/$/ $i/">>$MY_example/test.txt
done
echo "All done"

然後,執行此指令碼

# sh examples/four_classify/create_filelist.sh

成功的話,就會在examples/four_classify/ 資料夾下生成train.txt和test.txt兩個文字檔案,裡面就是圖片的列表清單。

 

接著再編寫一個指令碼檔案,呼叫convert_imageset命令來轉換資料格式。

# vi examples/four_classify/create_lmdb.sh

插入:

#!/usr/bin/env sh
MY_example=examples/four_classify

echo "Create train lmdb.."
rm -rf $MY_example/img_train_lmdb
build/tools/convert_imageset \
--shuffle \
--resize_height=256 \
--resize_width=256 \
./data/four_classify/train/ \
$MY_example/train.txt \
$MY_example/img_train_lmdb

echo "Create test lmdb.."
rm -rf $MY_example/img_test_lmdb
build/tools/convert_imageset \
--shuffle \
--resize_width=256 \
--resize_height=256 \
./data/four_classify/test/ \
$MY_example/test.txt \
$MY_example/img_test_lmdb

echo "All Done.."

然後,執行此指令碼

#  sh examples/four_classify/create_lmdb.sh

因為圖片大小不一,因此我統一轉換成256*256大小。執行成功後,會在 examples/myfile下面生成兩個資料夾img_train_lmdb和img_test_lmdb,分別用於儲存圖片轉換後的lmdb檔案。

三、計算均值並儲存

圖片減去均值再訓練,會提高訓練速度和精度。因此,一般都會有這個操作。

caffe程式提供了一個計算均值的檔案compute_image_mean.cpp,我們直接使用就可以了

# build/tools/compute_image_mean examples/four_classify/img_train_lmdb examples/four_classify/mean.binaryproto

compute_image_mean帶兩個引數,第一個引數是lmdb訓練資料位置,第二個引數設定均值檔案的名字及儲存路徑。
執行成功後,會在 examples/four_classify/ 下面生成一個mean.binaryproto的均值檔案。

四、建立模型並編寫配置檔案

模型就用程式自帶的caffenet模型,位置在 models/bvlc_reference_caffenet/資料夾下, 將需要的兩個配置檔案,複製到four_classify資料夾內.

# cp models/bvlc_reference_caffenet/solver.prototxt examples/four_classify/
# cp models/bvlc_reference_caffenet/train_val.prototxt examples/four_classify/

修改其中的solver.prototxt

# vim examples/four_classify/solver.prototxt
net: "examples/four_classify/train_val.prototxt"
test_iter: 2
test_interval: 50
base_lr: 0.001
lr_policy: "step"
gamma: 0.1
stepsize: 100
display: 20
max_iter: 500
momentum: 0.9
weight_decay: 0.005
solver_mode: GPU

100個測試資料,batch_size為50,因此test_iter設定為2,就能全cover了。在訓練過程中,調整學習率,逐步變小。

修改train_val.protxt,只需要修改兩個階段的data層就可以了,其它可以不用管。

name: "CaffeNet"
layer {
  name: "data"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    mirror: true
    crop_size: 227
    mean_file: "examples/four_classify/mean.binaryproto"
  }
  data_param {
    source: "examples/four_classify/img_train_lmdb"
    batch_size: 256
    backend: LMDB
  }
}
layer {
  name: "data"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    mirror: false
    crop_size: 227
    mean_file: "examples/four_classify/mean.binaryproto"
  }
  data_param {
    source: "examples/four_classify/img_test_lmdb"
    batch_size: 50
    backend: LMDB
  }
}
...

實際上就是修改兩個data layer的mean_file和source這兩個地方,其它都沒有變化 。

五、訓練和測試

如果前面都沒有問題,資料準備好了,配置檔案也配置好了,這一步就比較簡單了。

# build/tools/caffe train -solver examples/four_classify/solver.prototxt

執行時間和最後的精確度,會根據機器配置,引數設定的不同而不同。我的是gpu+cudnn執行500次,大約8分鐘,精度為95%。