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 

 

posted @ 2015-03-26 22:01  dupuleng  阅读(4942)  评论(0编辑  收藏  举报