Caffe 命令使用说明

caffe的c++主程序(caffe.cpp)放在根目录下的tools文件夹内, 当然还有一些其它的功能文件,如:convert_imageset.cpp, train_net.cpp, test_net.cpp等也放在这个文件夹内。经过编译后,这些文件都被编译成了可执行文件,放在了 ./build/tools/ 文件夹内。因此我们要执行caffe程序,都需要加 ./build/tools/ 前缀。
如:

sudo sh ./build/tools/caffe train --solver=examples/mnist/train_lenet.sh

caffe程序的命令行执行格式如下:

caffe <command> <args>

其中的<command>有这样四种:
(1)train----训练或finetune模型(model),
(2)test-----测试模型
(3)device_query---显示gpu信息
(4)time-----显示程序执行时间
其中的<args>参数有:
(1)-solver:必选参数。一个protocol buffer类型的文件,即模型的配置文件。如:

./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt

(2)-gpu: 可选参数。该参数用来指定用哪一块gpu运行,根据gpu的id进行选择,如果设置为'-gpu all'则使用所有的gpu运行。如使用第二块gpu运行:

./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt -gpu 2

(3)-snapshot:可选参数。该参数用来从快照(snapshot)中恢复训练。可以在solver配置文件设置快照,保存solverstate。如:

./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt -snapshot examples/mnist/lenet_iter_5000.solverstate

(4)-weights:可选参数。用预先训练好的权重来fine-tuning模型,需要一个caffemodel,不能和-snapshot同时使用。如:

./build/tools/caffe train -solver examples/finetuning_on_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel

(5)-iterations: 可选参数,迭代次数,默认为50。 如果在配置文件文件中没有设定迭代次数,则默认迭代50次。
(6)-model:可选参数,定义在protocol buffer文件中的模型。也可以在solver配置文件中指定。
(7)-sighup_effect:可选参数。用来设定当程序发生挂起事件时,执行的操作,可以设置为snapshot, stop或none, 默认为snapshot
(8)-sigint_effect: 可选参数。用来设定当程序发生键盘中止事件时(ctrl+c), 执行的操作,可以设置为snapshot, stop或none, 默认为stop

刚才举例了一些train参数的例子,现在我们来看看其它三个<command>:
test参数用在测试阶段,用于最终结果的输出,要模型配置文件中我们可以设定需要输入accuracy还是loss. 假设我们要在验证集中验证已经训练好的模型,就可以这样写

./build/tools/caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 100

这个例子比较长,不仅用到了test参数,还用到了-model, -weights, -gpu和-iteration四个参数。意思是利用训练好了的权重(-weight),输入到测试模型中(-model),用编号为0的gpu(-gpu)测试100次(-iteration)。
time参数用来在屏幕上显示程序运行时间。如:

./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -iterations 10

这个例子用来在屏幕上显示lenet模型迭代10次所使用的时间。包括每次迭代的forward和backward所用的时间,也包括每层forward和backward所用的平均时间。

./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -gpu 0

这个例子用来在屏幕上显示lenet模型用gpu迭代50次所使用的时间。

./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 10

利用给定的权重,利用第一块gpu,迭代10次lenet模型所用的时间。
device_query参数用来诊断gpu信息。

./build/tools/caffe device_query -gpu 0

最后,我们来看两个关于gpu的例子

./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt -gpu 0,1
./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt -gpu all

这两个例子表示: 用两块或多块GPU来平行运算,这样速度会快很多。但是如果你只有一块或没有gpu, 就不要加-gpu参数了,加了反而慢。
最后,在linux下,本身就有一个time命令,因此可以结合进来使用,因此我们运行mnist例子的最终命令是(一块gpu):

sudo time ./build/toos/caffe train -solver examples/mnist/lenet_solver.prototxt
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Caffe GitHub页面 1. Caffe目录结构 data/用于存放下载的训练数据docs/ 帮助文档exa...
    sixfold_yuan阅读 5,621评论 3 14
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,092评论 19 139
  • 本文从CSDN上转移过来:http://blog.csdn.net/mounty_fsc/article/deta...
    沤江一流阅读 4,906评论 0 0
  • 姓名:王飞 公司:广东思沃精密机械有限公司 六项精进245期谦虚1组王飞 【日精进打卡第2天】 【知-学习】 诵《...
    王飞_1d19阅读 2,444评论 0 1
  • 因<未来简史>这本书最近很火,所以我去看了作者的另一本书<人类简史>为什么不是看很火的那本,为什么呢,不知道啊。 ...
    四日阅读 1,801评论 0 0

友情链接更多精彩内容