实际操作中,原始图片是jpg、jpeg、png等格式,图片大小不一致,需要同时变换到caffe使用的lmdb或leveldb数据格式。
caffe里常用文件夹的作用
build:编译的文件夹
cmake:编译用
data:存放数据,里面有例子的文件夹,获取数据的.sh文件在里面。
自己的数据需要建立以下文件夹和文件:
(your-caffe-rootpath/data/myfile)可以把自己的图片或其他形式的数据放在自建文件夹中,按train和val文件分别存放训练和验证数据。
(your-caffe-rootpath/data/myfile/create_filelist.sh)用于创建train.txt和val.txt训练和测试的清单。
(your-caffe-rootpath/data/myfile/create_lmdb.sh)用于在caffe-master/examples/myfile中建立caffe转换后的数据
examples:用于存放自己转换好的数据、网络配置文件,均值模型等
models:存放了几个常用的网络模型
build/tools:存放一些常用的工具
1、存放数据
进入caffe根目录,所有命令都在这一目录输入。
在data和example中分別建立myfile文件夾。
mkdir data/myfile
mkdir examples/myfile
准备图片数据,按照训练和测试数据分别存储在data/myfile/train
和data/myfile/val
文件夹,再按找一定的规律命名每个图片,例如按分类分别以不同数字开头,各为一类,方便后面生成训练和测试清单。
2、生成训练和验证清单
清单用于指导caffe如何读取训练和验证数据。
主要通过create_filelist.sh脚本
touch data/myfile/create_filelist.sh
script:
# /usr/bin/env sh
DATA=data/myfile
EXAMPLE=examples/myfile
echo "Create train.txt..."
rm -rf $DATA/train.txt
for i in 3 4 5 6 7
do
find $DATA/train -name $i*.jpg | cut -d '/' -f4-5 | sed "s/$/ $i/">>$DATA/train.txt
done
echo "Create val.txt..."
rm -rf $DATA/val.txt
for i in 3 4 5 6 7
do
find $DATA/val -name $i*.jpg | cut -d '/' -f4-5 | sed "s/$/ $i/">>$DATA/val.txt
done
echo "All done"
必须在caffe根目录里运行:
sudo sh data/myfile/create_filelist.sh
效果
在 data/myfile
中生成 train.txt val.txt
3、转换图片为lmdb格式
主要是通过create_lmdb.sh脚本,使用resize工具将图片统一尺寸。
touch data/myfile/create_lmdb.sh
script:(换成你的名字)
#!/usr/bin/env sh
# Create the yourdata lmdb inputs
# N.B. set the path to the imagenet train + val data dirs
EXAMPLE=examples/myfile
DATA=data/myfile
TOOLS=build/tools
TRAIN_DATA_ROOT=/home/<your-caffe-rootpath>/data/myfile/train/
TEST_DATA_ROOT=/home/<your-caffe-rootpath>/data/myfile/val/
# 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..."
rm -rf $EXAMPLE/img_train_lmdb
GLOG_logtostderr=1 $TOOLS/convert_imageset \
--resize_height=$RESIZE_HEIGHT \
--resize_width=$RESIZE_WIDTH \
--shuffle \
$TRAIN_DATA_ROOT \
$DATA/train.txt \
$EXAMPLE/img_train_lmdb
echo "Creating val lmdb..."
rm -rf $EXAMPLE/img_val_lmdb
GLOG_logtostderr=1 $TOOLS/convert_imageset \
--resize_height=$RESIZE_HEIGHT \
--resize_width=$RESIZE_WIDTH \
--shuffle \
$TEST_DATA_ROOT \
$DATA/val.txt \
$EXAMPLE/img_val_lmdb
echo "Done."
必须在caffe根目录里运行:
sudo sh data/myfile/create_lmdb.sh
效果
在examples/myfile
中生成 img_train_lmdb img_val_lmdb
两个文件夹
4、计算图像均值
caffe程序提供了一个计算均值的文件compute_image_mean.cpp,我们直接编写脚本文件使用就可以了。
touch examples/myfile/create_image_mean.sh
script:
#!/usr/bin/env sh
# Compute the mean image from the imagenet training lmdb
# N.B. this is available in data/myfile
EXAMPLE=examples/myfile
DATA=data/myfile
TOOLS=build/tools
$TOOLS/compute_image_mean $EXAMPLE/img_train_lmdb \
$DATA/img_mean.binaryproto
echo "Done."
必须在caffe根目录里运行:
sudo sh examples/myfile/create_image_mean.sh
效果
在data/myfile
中生成均值文件img_mean.binaryproto
5、定义网络
模型就用程序自带的caffenet模型,位置在models/bvlc_reference_caffenet/
文件夹下, 将需要的两个配置文件,复制到myfile文件夹内
sudo cp models/bvlc_reference_caffenet/solver.prototxt examples/myfile/
sudo cp models/bvlc_reference_caffenet/train_val.prototxt examples/myfile/
solver.prototxt用来定义训练参数
train_val.prototxt定义网路结构
solver.prototxt:(注意修改网络模型文件的路径)
net: "examples/myfile/train_val.prototxt"
test_iter: 1000 #是指测试的批次,我们就 10 张照片,设置 10 就可以了。这个值和bench_size的乘积就是要测试的图片数量
test_interval: 1000 #是指每n次迭代测试一次,我们改成 500 次测试一次。
base_lr: 0.01 #是基础学习率,因为数据量小,0.01 就会下降太快了,因此改成 0.001
lr_policy: "step" #学习率变化
gamma: 0.1 #学习率变化的比率
stepsize: 100000 #每 100000 次迭代减少学习率
display: 20 #每 20 层显示一次
max_iter: 450000 #最大迭代次数,
momentum: 0.9# 学习的参数,不用变
weight_decay: 0.0005 #学习的参数,不用变
snapshot: 10000 #每迭代 10000 次显示状态,这里改为 2000 次
solver_mode: GPU #末尾加一行,代表用 GPU 进行
train_val.prototxt:(主要修改两个阶段的data层的mean_file和source的路径)
name: "CaffeNet"
layer {
name: "data"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
mirror: true
crop_size: 227
mean_file: "data/myfile/mean.binaryproto"
}
data_param {
source: "examples/myfile/img_train_lmdb"
batch_size: 256
backend: LMDB
}
}
layer {
name: "data"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
transform_param {
mirror: false
crop_size: 227
mean_file: "data/myfile/mean.binaryproto"
}
data_param {
source: "examples/myfile/img_val_lmdb"
batch_size: 50
backend: LMDB
}
}
根据GPU的内存设置batch_size,太多容易内存溢出。
6、训练和测试
sudo build/tools/caffe train -solver examples/myfile/solver.prototxt
7、问题
如果出现Check failed: error == cudaSuccess (9 vs. 0) invalid configuration argument
之类的问题,首先考虑是不是内存不够用,减小bench_size,或者把图像resize更小一下比如128*128。