(原)caffe中通过图像生成lmdb格式的数据

转载请注明出处:

http://www.cnblogs.com/darkknightzh/p/5909121.html

参考网址:

http://www.cnblogs.com/wangxiaocvpr/p/5096265.html

可以根据caffe-masterexamplesimagenet eadme.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_ 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。

原文地址:https://www.cnblogs.com/darkknightzh/p/5909121.html