接口Interfaces
Caffe提供丰富的接口,比如命令行,python,matlab。先说一下命令行
命令行
Caffe的程序位于caffe / build / tools
,运行时可以在根目录执行./build/tools/caffe <command><args>
。
其中<command>
有四种:
- train:训练或finetune模型(model)
- test:测试模型
- device_query:显示gpu信息
- time:显示程序执行时间
其中的<args>
参数有:
- -solver
- -gpu
- -snapshot
- -weights
- -model
- -sighup_effect
- -sigint_effect
训练train
caffe train
可以从头开始学习模型、从已保存的快照中恢复学习或添加新数据进行fine-tunes。具体来说,所有训练需要通过-solver solver.prototxt
参数进行求解器配置;恢复需要使用-snapshot model_iter_1000.solverstate
参数来加载求解程序快照;fine-tunes微调需要模型初始化的-weights model.caffemodel
参数。
# train LeNet 训练LeNet
caffe train -solver examples/mnist/lenet_solver.prototxt
# train on GPU 2 在特定的GPU上
caffe train -solver examples/mnist/lenet_solver.prototxt -gpu 2
# resume training from the half-way point snapshot 从快照恢复
caffe train -solver examples/mnist/lenet_solver.prototxt -snapshot examples/mnist/lenet_iter_5000.solverstate
# fine-tune CaffeNet model weights for style recognition 完整例子参阅examples/finetuning_on_flickr_style,仅调用可使用:
caffe train -solver examples/finetuning_on_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel
对于train的参数,功能为:
-solver
:必选,后跟一个protocol buffer类型(.prototxt)的文件,即模型的配置文件。-gpu
:可选,指定某一块GPU运行,-gpu all
是所有运行:
# train on GPUs 0 & 1 (doubling the batch size)
caffe train -solver examples/mnist/lenet_solver.prototxt -gpu 0,1
# train on all GPUs (multiplying batch size by number of devices)
caffe train -solver examples/mnist/lenet_solver.prototxt -gpu all
-snapshot
:可选,从快照中恢复,设置快照可从solver配置中进行,保存为solverstate。-weights
:可选参数。用预先训练好的权重来fine-tuning模型,需要一个caffemodel,不能和-snapshot同时使用。-iterations
: 可选参数,迭代次数,默认为50。 如果在配置文件文件中没有设定迭代次数,则默认迭代50次。-model
:可选参数,定义在protocol buffer文件中的模型。也可以在solver配置文件中指定。-sighup_effect
:可选参数。用来设定当程序发生挂起事件时,执行的操作,可以设置为snapshot, stop或none, 默认为snapshot。-sigint_effect
: 可选参数。用来设定当程序发生键盘中止事件时(ctrl+c), 执行的操作,可以设置为snapshot, stop或none, 默认为stop。
测试test
测试时输出每个batch得分,最后返回平均值。test参数用在测试阶段,用于最终结果的输出,要模型配置文件中我们可以设定需要输入accuracy还是loss. 假设我们要在验证集中验证已经训练好的模型,就可以这样写
# score the learned LeNet model on the validation set as defined in the
# model architeture lenet_train_test.prototxt
caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 100
意思是利用训练好了的权重(-weight),输入到测试模型中(-model),用编号为0的gpu(-gpu)测试100次(-iteration)。
时间time
time参数用来在屏幕上显示程序运行时间。如:
# (These example calls require you complete the LeNet / MNIST example first.)
# time LeNet training on CPU for 10 iterations
caffe time -model examples/mnist/lenet_train_test.prototxt -iterations 10
这个例子用来在屏幕上显示lenet模型迭代10次所使用的时间。包括每次迭代的forward和backward所用的时间,也包括每层forward和backward所用的平均时间。