参考博客:blog.csdn.net/drrlalala/article/details/47274549

1,首先在网上下载图片,猫和狗。
直接保存下载该网页,会生成一个有图片的文件夹。
caffe-master/data  新建 myself
myself/  新建  train   dog
                                cat
                       test   dog
                                cat
之后将图片分别复制到文件夹中

2,生成train.txt和test.txt
在train文件夹下运行    find -name *.jpg |cut -d '/' -f2-3 >train.txt
              手动加标签    sed -i 's/$/ 0/g' train.txt   在末位加0
val.txt同理可得,也要加标签。

3,图片转化为lmdb格式
caffe-master/examples/    新建 myself
复制examples/imagenet/create_imagenet.sh到 examples/myself

修改参照下面,主要是针对路径的修改

#!/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/myself      ##路径需要自己修改,默认的相对路径是在caffe-master下  
                                               
DATA=data/myself          ##是指生成的train.txt和val.txt的路径                     
TOOLS=build/tools

TRAIN_DATA_ROOT=data/myself/train/   ##注生成的数据最前面就不需要加/了
VAL_DATA_ROOT=data/myself/test/

# Set RESIZE=true to resize the images to 256x256. Leave as false if images have
# already been resized using another tool.
RESIZE=true    ##如果填true说明事先没有将其转为256*256格式
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 \           ##之前生成的list
    $EXAMPLE/myself_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/myself_test_lmdb

echo "Done."

之后 cd caffe-master
运行  sudo ./examples/myself/create_imagenet.sh
在examples/myself/   下生成两个lmdb文件夹

4,计算图像均值

复制examples/imagenet/make_imagenet_mean.sh到 examples/myself

使用make_imagenet_mean.sh计算图像均值,在data/myself 下产生imagenet_mean.binaryproto文件
相对路径仍为 caffe-master下,按照自己的文件修改路径。
之后 sudo ./examples/myself/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/myself
DATA=data/myself
TOOLS=build/tools

$TOOLS/compute_image_mean $EXAMPLE/myself_train_lmdb
$DATA/imagenet_mean.binaryproto

echo "Done."

5,定义网络
主要是修改下面三个文件:
my_train.prototxt
my_test.prototxt
solver.prototxt
这三个文件可以从caffe的已有例子中复制过来。接下来需要自己修改,修改的地方主要是,一些文件的路径和输出层的个数(即类别数目)。

6,写训练脚本my_train_lenet.sh 如下:(我这个是根据mnist中的文件改过来的)

#!/usr/bin/env sh
set -e

./build/tools/caffe train --solver=examples/myself/solver.prototxt $@

 未完待续。