训练caffe模型的一般方法

2018-08-30

获取已标注数据

有数据才好训练模型。

将真实标签转换为整数(从零开始),并创建train.txt和val.txt文件。

txt文件每一行保存训练样本的路径和其标签。

根据train.txt和val.txt创建lmdb数据库文件。

终端执行以下shell命令(复制内容到create_lmdb.sh并打开终端执行sh create_lmdb.sh):

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

PROJECT_ROOT=/path/to/project/root  # 该目录下有train.txt和val.txt文件。
TOOLS=/path/to/caffe/build/tools


TRAIN_DATA_ROOT=/path/to/train data/
VAL_DATA_ROOT=/path/to/val data/
# 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 "$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=true \
    --gray=true \
    --is_train=true \
    $TRAIN_DATA_ROOT \
    $PROJECT_ROOT/train.txt \
    $PROJECT_ROOT/train_lmdb
  


echo "Creating val lmdb..."

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle=false \
    --gray=true \
    --is_train=false \
    $VAL_DATA_ROOT \
    $PROJECT_ROOT/val.txt \
    $PROJECT_ROOT/val_lmdb

echo "Done."

根据lmdb数据库文件生成均值文件。

终端执行以下shell命令(复制内容到make_data_mean.sh并打开终端执行sh make_data_mean.sh):

#!/usr/bin/env sh
# Compute the mean image from the imagenet training lmdb
# N.B. this is available in data/ilsvrc12

PROJECT_ROOT=/path/to/project/root  # 该目录下有train_lmdb和val_lmdb目录。
TOOLS=TOOLS=/path/to/caffe/build/tools

$TOOLS/compute_image_mean $PROJECT_ROOT/train_lmdb \
  $PROJECT_ROOT/mean.binaryproto

echo "Done."

创建模型文件train.prototxt和val.prototxt。

创建solver.prototxt并设置训练参数。

训练模型

caffe train -solver /path/to/solver.prototxt -gpu 0

测试模型

caffe test -model /path/to/val.prototxt -weights /path/to/**.caffemodel -iterations 500 -gpu 0