由于caffe-master\examples\imagenet中有生成lmdb和计算均值的脚本,所以,我们可以使用这些脚本。
我们将训练所用的图片文件夹放在image下,train.txt和val.txt也放在image下,如:
(BMW等文件夹放的是图像)
然后,将image文件夹拷贝到Linux下的caffe-master\examples\imagenet文件夹里,接着,修改create_imagenet.sh这个脚本
#!/usr/bin/env sh
# Create the imagenet lmdb inputs
# N.B. set the path to the imagenet train + val data dirs
EXAMPLE=examples/imagenet
DATA=examples/imagenet/image
TOOLS=build/tools
TRAIN_DATA_ROOT=examples/imagenet/
VAL_DATA_ROOT=examples/imagenet/
# Set RESIZE=true to resize the images to 256x256. Leave as false if images have
# already been resized using another tool.
RESIZE=true #改为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 "$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/mydata_train_lmdb #生成训练集的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/mydata_val_lmdb #生成验证集的lmdb,名字可修改
echo "Done."
然后,在caffe-master下运行改脚本:$caffe-master ./examples/imagenet/create_imagenet.sh
这样,就得到训练集和验证集的lmdb。
接着,计算均值,打开make_imagenet_mean.sh,修改:
#!/usr/bin/env sh
# Compute the mean image from the imagenet training lmdb
# N.B. this is available in data/ilsvrc12
EXAMPLE=examples/imagenet
DATA=examples/imagenet
TOOLS=build/tools
$TOOLS/compute_image_mean $EXAMPLE/mydata_train_lmdb \ #改成你的lmdb
$DATA/mydata_mean.binaryproto #生成的均值文件名,可修改
echo "Done."
生成了lmdb和均值文件,接着就可以修改网络来训练了,具体过程请看:基于caffe的图像分类(3)——修改网络并训练模型