caffe 中將圖片轉化為lmdb


因為caffe的底層輸入是leveldb 或者lmdb形式,因此當我們使用caffe時,需要先將圖片統一轉化為相應的形式。

caffe中有相應的接口可以使用$CAFFEROOT/build/tools/conver_imageset.bin,非常方便。源碼在$CAFFEROOT/tools/convert_imageset.cpp

接下來我們參考$CAFFE_ROOT / examples/ imagenet / create_imagenet.sh介紹如何使用該接口

1、將圖片放在一個目錄中,如9,10行,分別表示test,val數據的目錄

2、然后將圖片的名字及相應標簽存放在txt中 , 如6行表示相應txt目錄,第44,54行分別設置

3、調用接口將圖片轉化為lmdb形式,如第5行,表示lmdb存放地址

4、caffe的輸入是統一大小的,所以在將數據傳給caffe時,需要轉化大小,使用14行的RESIZE 來控制,如果需要轉換RESIZE = true,否則RESIZE = false。博主今天就是由於沒設置所以一直報錯。

 

 1 #!/usr/bin/env sh
 2 # Create the imagenet lmdb inputs
 3 # N.B. set the path to the imagenet train + val data dirs
 4 
 5 EXAMPLE=examples/imagenet
 6 DATA=data/ilsvrc12
 7 TOOLS=build/tools
 8 
 9 TRAIN_DATA_ROOT=/path/to/imagenet/train/
10 VAL_DATA_ROOT=/path/to/imagenet/val/
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=false
15 if $RESIZE; then
16   RESIZE_HEIGHT=256
17   RESIZE_WIDTH=256
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.txt \
45     $EXAMPLE/ilsvrc12_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/ilsvrc12_val_lmdb
56 
57 echo "Done."

可以使用轉換后的lmdb數據訓練網絡,檢測lmdb是否可用。

如果執行以下語句無誤,證明lmdb可用。

./build/tools/caffe train -solver models/bvlc_reference_caffenet/deploy.prototxt 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM