把每个图片的名称和上级目录名给截取出来,并分别以开头数字3,4,5,6,7作为一个种类的标签。
# /usr/bin/env sh
DATA1="/home/joczu/caffe-master/data/re"
DATA2="/home/joczu/caffe-master/data/re/train/cat"
DATA3="/home/joczu/caffe-master/data/re/train/dog"
DATA4="/home/joczu/caffe-master/data/re/val"
echo "Create train.txt..."
rm -rf $DATA1/train.txt
rm -rf $DATA1/val.txt
find $DATA2 -name Abyssinian*.jpg | cut -d '/' -f8-9 | sed "s/$/ 1/">>$DATA1/train.txt
find $DATA3 -name american_bulldog*.jpg | cut -d '/' -f8-9 | sed "s/$/ 2/">>$DATA1/tmp.txt
find $DATA4 -name Abyssinian*.jpg | cut -d '/' -f8-9 | sed "s/$/ 1/">>$DATA1/val.txt
find $DATA4 -name american_bulldog*.jpg | cut -d '/' -f8-9 | sed "s/$/ 2/">>$DATA1/tmp_val.txt
cat $DATA1/tmp.txt>>$DATA1/train.txt
cat $DATA1/tmp_val.txt>>$DATA1/val.txt
rm -rf $DATA1/tmp.txt
rm -rf $DATA1/tmp_val.txt
echo "Done.."
编写creat_lmdb.sh文件,首先转换图片大小,像素均变化成256X256,再调用build/tools/convert_imageset转换图片的数据格式,生成img_test_lmdb和img_train_lmdb文件
#!/usr/bin/env sh
MY=examples/myfile
echo "Creating train lmdb..."
rm -rf $MY/img_train_lmdb
build/tools/convert_imageset \
--shuffle \
--resize_height=256 \
--resize_width=256 \
#/data/re/train \
$MY/train.txt \
$MY/img_train_lmdb
echo "Done."
echo "Creating test lmdb.."
rm -rf $MY/img_test_lmdb
build/tools/convert_imageset \
--shuffle \
--resize_width=256 \
--resize_height=256 \
#/data/re/val \
$MY/val.txt \
$MY/img_test_lmdb
echo "All Done."
net: "examples/myfile/train_val.prototxt"
test_iter: 2
test_interval: 50
base_lr: 0.001
lr_policy: "step"
gamma: 0.1
stepsize: 100
display: 20
max_iter: 200
momentum: 0.9
weight_decay: 0.005
solver_mode: CPU
snapshot: 200
snapshot_prefix: "examples/myfile/myfile"
修改train_val.protxt,只需要修改两个阶段的data层的文件目录
name: "CaffeNet"
layer {
name: "data"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
mirror: true
crop_size: 227
mean_file: "data/re/imagenet_mean.binaryproto"
}
data_param {
source: "examples/myfile/imagenet_train_leveldb"
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/re/imagenet_mean.binaryproto"
}
data_param {
source: "examples/myfile/imagenet_val_leveldb"
batch_size: 20
backend: LMDB
}
}
(3) 注意设置solver.protptxt及train_val.prototxt文件的参数设置,需根据自己的训练集和测试集进行匹配;