轉載請注明出處:
http://www.cnblogs.com/darkknightzh/p/5909121.html
參考網址:
http://www.cnblogs.com/wangxiaocvpr/p/5096265.html
可以根據caffe-master\examples\imagenet \readme.md進行理解。
1 生成LmDB格式文件
caffe中通過圖像生成lmdb格式文件的程序為examples/imagenet/create_imagenet.sh。該文件調用build/tools/convert_imageset(對應的源碼為tools/convert_imageset.cpp)。
為了不改變原來的程序,在examples內新建testCreateLmDB文件夾。新建create_imagenet.sh,並輸入:
1 #!/usr/bin/env sh 2 # Create the imagenet lmdb inputs 3 # N.B. set the path to the imagenet train + val data dirsset -e 4 5 EXAMPLE=examples/testCreateLmDB 6 DATA=/home/xxx/database/CASIA 7 TOOLS=build/tools 8 9 TRAIN_DATA_ROOT=/home/xxx/database/CASIA/ 10 VAL_DATA_ROOT=/home/xxx/database/CASIA/ 11 12 # Set RESIZE=true to resize the images to 256x256. Leave as false if images have 13 # already been resized using another tool. 14 RESIZE=true 15 if $RESIZE; then 16 RESIZE_HEIGHT=128 17 RESIZE_WIDTH=128 18 else 19 RESIZE_HEIGHT=0 20 RESIZE_WIDTH=0 21 fi 22 23 if [ ! -d "$TRAIN_DATA_ROOT" ]; then 24 echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT" 25 echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \ 26 "where the ImageNet training data is stored." 27 exit 1 28 fi 29 30 if [ ! -d "$VAL_DATA_ROOT" ]; then 31 echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT" 32 echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \ 33 "where the ImageNet validation data is stored." 34 exit 1 35 fi 36 37 echo "Creating train lmdb..." 38 39 GLOG_logtostderr=1 $TOOLS/convert_imageset \ 40 --resize_height=$RESIZE_HEIGHT \ 41 --resize_width=$RESIZE_WIDTH \ 42 --shuffle \ 43 $TRAIN_DATA_ROOT \ 44 $DATA/train_all.txt \ 45 $EXAMPLE/face_train_lmdb 46 47 echo "Creating val lmdb..." 48 49 #GLOG_logtostderr=1 $TOOLS/convert_imageset \ 50 # --resize_height=$RESIZE_HEIGHT \ 51 # --resize_width=$RESIZE_WIDTH \ 52 # --shuffle \ 53 # $VAL_DATA_ROOT \ 54 # $DATA/val.txt \ 55 # $EXAMPLE/face_val_lmdb 56 57 echo "Done."
之后,在caffe根目錄打開終端,並輸入sh examples/testCreateLmDB/create_imagenet.sh
說明:
1) 程序第6行EXAMPLE為當前文件在caffe目錄的相對路徑。
2) 程序第7行DATA為train_all.txt所在的文件夾(如果train_all.txt就在TRAIN_DATA_ROOT文件夾內,則DATA和TRAIN_DATA_ROOT一樣),如下圖:
其中第一列為數據庫中所有文件的文件名相對於數據庫目錄的位置,第二列為圖像類別。
3) 第10行TRAIN_DATA_ROOT為訓練數據的絕對路徑。
4) 第11行VAL_DATA_ROOT為驗證數據的絕對路徑。
5) 程序第15行RESIZE為是否對圖像進行縮放。如果直接讀圖像的話,可以使用
new_height: 128
new_width: 128
進行縮放。但是使用lmdb的話,貌似沒辦法在prototxt里面設置縮放,只能在創建lmdb數據庫時,進行縮放。縮放時,更改程序17、18行的RESIZE_HEIGHT和RESIZE_WIDTH。經測試,如果不縮放的話,生成數據庫大小為28.2G,縮放后,生成數據庫大小為21.2G(此處和圖像具體大小有關,給出數據只為了說明縮放應該在哪里設置。)
6. 程序第46行EXAMPLE/face_train_lmdb為生成的LmDB文件所在的路徑。注意:EXAMPLE/oriface_train_lmdb文件夾最好為空,或者刪除該文件夾,否則可能會提示:
2 生成mean.binaryproto文件
為了不更改源文件,在testCreateLmDB內新建make_imagenet_mean.sh,並輸入:
1 #!/usr/bin/env sh 2 # Compute the mean image from the imagenet training lmdb 3 # N.B. this is available in data/ilsvrc12 4 5 EXAMPLE=examples/testCreateLmDB 6 DATA=examples/testCreateLmDB 7 TOOLS=build/tools 8 9 $TOOLS/compute_image_mean $EXAMPLE/face_train_lmdb \ 10 $DATA/face_train_mean.binaryproto 11 12 echo "Done."
說明:
1) 程序第3行EXAMPLE為當前程序所在目錄(實際上為face_train_lmdb庫文件所在目錄。見第9行)。
2) 程序第4行DATA為需要生成的face_train_mean.binaryproto所在目錄(見程序第10行)。
3) 生成的face_train_mean.binaryproto文件大小為192KB。