1 训练前准备

  caffe常使用的数据是db格式(leveldb/lmdb),我们手中有的图片数据,jpg、bmp格式等,必须转换;此操作caffe已经提供工具。它的路径是/home/your/caffe-master/build/tools/convert_imageset.bin;不过,使用它之前,需要先生成一个依赖文本文件。

convert_imageset.bin程序的使用格式:

 convert_imageset [FLAGS] ROOTFOLDER/ LISTFILE DB_NAME

需要带四个参数:

FLAGS: 图片参数组,后面详细介绍

ROOTFOLDER/: 图片存放的绝对路径,从Linux系统根目录开始

LISTFILE: 图片文件列表清单,一般为一个txt文件,一行一张图片

DB_NAME: 最终生成的db文件存放目录

2 训练文本LISTFILE生成

用gedit编辑如下shell代码,并存为mktraintxt.sh
# /usr/bin/env sh DATA=examples/images echo "Create train.txt..." rm -rf $DATA/train.txt find $DATA -name *cat.jpg | cut -d '/' -f3 | sed "s/$/ 1/">>$DATA/train.txt find $DATA -name *bike.jpg | cut -d '/' -f3 | sed "s/$/ 2/">>$DATA/tmp.txt cat $DATA/tmp.txt>>$DATA/train.txt rm -rf $DATA/tmp.txt echo "Done.."

3 图像转换代码
sudo vim data/mydata/create_lmdb.sh(我的数据放在data文件下的mydata目录下)。

#!/usr/bin/env sh
# This script converts the mnist data into lmdb/leveldb format,
# depending on the value assigned to $BACKEND.
set -e

EXAMPLE=examples/mnist
DATA=data/mnist
BUILD=build/examples/mnist

BACKEND="lmdb"

TRAIN_DATA_ROOT=/home/yan/image-face/train/
VAL_DATA_ROOT=/home/yan/image-face/val/

RESIZE=true
if $RESIZE; then
    RESIZE_HEIGHT=227
    RESIZE_WIDTH=227
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 ${BACKEND}..."

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    $TRAIN_DATA_ROOT \
    $DATA/train_all.txt \
    $EXAMPLE/face_train_lmdb

echo "Creating val ${BACKEND}..."

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

echo "Done."