尽管caffe还是通过python接口方便调试,这里也首先记录C++接口(其实是命令行模式)下如何使用caffe训练工具。
1. 训练工具
编译之后的可执行文件位于 build/tools/caffe,其使用参数的查看方法:
$./build/tools/caffe --help
commands:
train train or finetune a model
test score a model
device_query show GPU diagnostic information
time benchmark model execution time
……
Flags from /home/zsh/zsh_dl_code/caffe-master/tools/caffe.cpp:
-gpu (Optional; run in GPU mode on given device IDs separated by ','.Use
'-gpu all' to run on all available GPUs. The effective training batch
size is multiplied by the number of devices.) type: string default: ""
-iterations (The number of iterations to run.) type: int32 default: 50
-level (Optional; network level.) type: int32 default: 0
-model (The model definition protocol buffer text file.) type: string
default: ""
-phase (Optional; network phase (TRAIN or TEST). Only used for 'time'.)
type: string default: ""
-sighup_effect (Optional; action to take when a SIGHUP signal is received:
snapshot, stop or none.) type: string default: "snapshot"
-sigint_effect (Optional; action to take when a SIGINT signal is received:
snapshot, stop or none.) type: string default: "stop"
-snapshot (Optional; the snapshot solver state to resume training.)
type: string default: ""
-solver (The solver definition protocol buffer text file.) type: string
default: ""
-stage (Optional; network stages (not to be confused with phase), separated
by ','.) type: string default: ""
-weights (Optional; the pretrained weights to initialize finetuning,
separated by ','. Cannot be set simultaneously with snapshot.)
type: string default: ""
……
可以看见详细的参数列表,常用的方法有两种:一种是从头开始训练,参见examples/mnist/train_lenet.sh
./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt $@
一种是继续训练已有的模型,如:
./build/tools/caffe train
-solver=examples/finetune_pascal_detection/pascal_finetune_solver.prototxt
-weights=$PRETRAINED_CAFFEMODEL$
2. 其他工具
(1) convert_imageset 将图片resize为给定大小,参见examples/imagenet/create_imagenet.sh
$TOOLS/convert_imageset
--resize_height=$RESIZE_HEIGHT
--resize_width=$RESIZE_WIDTH
--shuffle
$TRAIN_DATA_ROOT
$DATA/train.txt
$EXAMPLE/ilsvrc12_train_lmdb
(2) compute_image_mean 计算训练集均值并存储为二进制文件,参见 examples/imagenet/make_imagenet_mean.sh
$TOOLS/compute_image_mean $EXAMPLE/ilsvrc12_train_lmdb $DATA/imagenet_mean.binaryproto
(3) extract_features 提取特征,注释说明了它的用法(注意:可以一次性对多个数据集进行特征提取):
extract_features
pretrained_net_param feature_extraction_proto_file
extract_feature_blob_name1[,name2,...]
save_feature_dataset_name1[,name2,...]
num_mini_batches
db_type
[CPU/GPU] [DEVICE_ID=0]
例如:
$TOOLS/extract_features
models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel
examples/feature_extraction/imagenet_val.prototxt
fc7
examples/feature_extraction/features
10
leveldb
3. 流程
第一步,将图片库转化为leveldb/lmdb格式的数据库,这个过程包含了图片resize;
第二步,计算均值并存储;
第三步,准备好网络参数文件:train_test.prototxt,deploy.prototxt;
第四步,训练,可视化,测试。