7.caffe:create_lmdb.sh(数据预处理转换成lmdb格式)

个人实践代码如下:

#!/usr/bin/env sh
# Create the imagenet lmdb inputs
# N.B. set the path to the imagenet train + val data dirs
set -e

EXAMPLE=/home/wp/CAFFE/caffe-master/myself/00b
DATA=/home/wp/CAFFE/caffe-master/myself/00b
TOOLS=build/tools

TRAIN_DATA_ROOT=/home/wp/CAFFE/caffe-master/myself/00b/train/
VAL_DATA_ROOT=/home/wp/CAFFE/caffe-master/myself/00b/val/

# Set RESIZE=true to resize the images to 256x256. Leave as false if images have
# already been resized using another tool.
RESIZE=true
if $RESIZE; then
  RESIZE_HEIGHT=101
  RESIZE_WIDTH=101
else
  RESIZE_HEIGHT=0
  RESIZE_WIDTH=0
fi

if [ ! -d "$TRAIN_DATA_ROOT" ]; then
  echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"
  echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \
       "where the ImageNet training data is stored."
  exit 1
fi

if [ ! -d "$VAL_DATA_ROOT" ]; then
  echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"
  echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \
       "where the ImageNet validation data is stored."
  exit 1
fi

echo "Creating train lmdb..."

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    $TRAIN_DATA_ROOT \
    $DATA/train.txt \
    $EXAMPLE/00b_train_lmdb

echo "Creating val lmdb..."

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    $VAL_DATA_ROOT \
    $DATA/val.txt \
    $EXAMPLE/00b_val_lmdb

echo "Done."

# cd CAFFE/caffe-master
# sh ./myself/00b/create_lmdb.sh

结果生成两个文件:00b_train_lmdb.sh; 00b_val_lmdb.sh

参考一:

由于参数比较多,因此我们可以编写一个sh脚本来执行命令:

首先,创建sh脚本文件:

# sudo vi examples/images/create_lmdb.sh

编辑,输入下面的代码并保存

[cpp]
#!/usr/bin/en sh
DATA=examples/images
rm -rf $DATA/img_train_lmdb
build/tools/convert_imageset --shuffle \
--resize_height=256 --resize_width=256 \
/home/xxx/caffe/examples/images/ $DATA/train.txt  $DATA/img_train_lmdb

 注释:/convert_imageset --shuffle \  //使用shuffle  

  • --resize_height=256 --resize_width=256 \ //图片的大小都会调用opencv来获得固定的大小  
  • /opt/modules/caffe-master/examples/images/ \ // 图片的绝对存储路径  
  • /opt/modules/caffe-master/examples/images/train.txt \ // 文件的列表信息  
  • /opt/modules/caffe-master/examples/images/img_train_lmdb \ //最终生成的数据库保存的路径

设置参数-shuffle,打乱图片顺序。设置参数-resize_height和-resize_width将所有图片尺寸都变为256*256.

/home/xxx/caffe/examples/images/ 为图片保存的绝对路径。

最后,运行这个脚本文件

[cpp]
# sudo sh examples/images/create_lmdb.sh

就会在examples/images/ 目录下生成一个名为 img_train_lmdb的文件夹,里面的文件就是我们需要的db文件了

上面就将图像数据转换成db(leveldb/lmdb)文件了。

 

参考二:

create_filelist.sh后 接着再编写一个脚本文件create_lmdb.sh,调用convert_imageset命令来转换数据格式。

# sudo vi examples/myfile/create_lmdb.sh

插入:

 
#!/usr/bin/env sh
MY=examples/myfile

echo "Create train lmdb.."
rm -rf $MY/img_train_lmdb
build/tools/convert_imageset \
--shuffle \
--resize_height=256 \
--resize_width=256 \
/home/xxx/caffe/data/re/ \
$MY/train.txt \
$MY/img_train_lmdb

echo "Create test lmdb.."
rm -rf $MY/img_test_lmdb
build/tools/convert_imageset \
--shuffle \
--resize_width=256 \
--resize_height=256 \
/home/xxx/caffe/data/re/ \
$MY/test.txt \
$MY/img_test_lmdb

echo "All Done.."

因为图片大小不一,因此我统一转换成256*256大小。运行成功后,会在 examples/myfile下面生成两个文件夹img_train_lmdb和img_test_lmdb,分别用于保存图片转换后的lmdb文件。

posted on 2016-08-17 10:58  WP的烂笔头  阅读(5218)  评论(0编辑  收藏  举报