所有的操作是基于caffe的根目录/caffe-master/来操作的:
数据准备
我所用的图片是车牌识别中,0~9数字图片,在data下面新建一个number目录,用来存放训练图片(caffenumimg_train)与测试图片(caffenumimg_test),0~9分别有200张左右的训练图片和50张左右的测试图片
转换成lmdb格式文件
首先,在data/number/目录编写一个脚本create_filelist.sh文件,用来生成train.txt和test.txt清单文件。清单文件train.txt与test.txt主要用来记录训练图片与测试图片的目录与标签。
#!/usr/bin/env sh
DATA_TRAIN=data/number/caffenumimg_train
DATA_TEST=data/number/caffenumimg_test
MY=data/number
echo "Create train.txt..."
rm -rf $MY/train.txt
for i in 0 1 2 3 4 5 6 7 8 9
do
find $DATA_TRAIN/$i/ -name *.bmp | cut -d '/' -f1-5 | sed "s/$/ $i/">>$MY/train.txt
done
echo "Create test.txt..."
rm -rf $MY/test.txt
for i in 0 1 2 3 4 5 6 7 8 9
do
find $DATA_TEST/$i/ -name *.bmp | cut -d '/' -f1-5 | sed "s/$/ $i/">>$MY/test.txt
done
echo "All done"
这段脚本的大致意思(生成test.txt与生成train.txt大体相同这里只介绍生成train.txt脚本的逻辑):
首先删除已经有的train.txt清单文件,然后遍历data/number/caffenumimg_train目录中的0~9文件夹,
读取所有的.bmp文件的目录,以/划分目录字段,截取1到5这几个字段,在字段结尾处加类别标签,保存到train.txt文件中。
然后,运行脚本
sh /data/number/create_filelist.sh
得到如下图所示的结果:
接着再编写一个脚本文件放到目录examples/number/,调用convert_imageset命令来转换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=examples/number
DATA=data/number
TOOLS=build/tools
TRAIN_DATA_ROOT=./
TEST_DATA_ROOT=./
rm $EXAMPLE/number_train_lmdb -rf
rm $EXAMPLE/number_test_lmdb -rf
# 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=256
RESIZE_WIDTH=256
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 "$TEST_DATA_ROOT" ]; then
echo "Error: TEST_DATA_ROOT is not a path to a directory: $TEST_DATA_ROOT"
echo "Set the TEST_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/number_train_lmdb