caffe接口—cmdcaffe的基本使用方法

以下内容主要来自caffe官方文档:Caffe|Interfaces

caffe的日常使用中包含3种接口:命令行、python和matlab接口,也就是cmdcaffe、pycaffe和matcaffe。下面以caffe源代码中包含的LeNet/MNIST为例,简单总结cmdcaffe的使用方法。假设已经编译好了caffe(如果没有,参考:Ubuntu16.04配置caffe-GPU环境),以下操作的bash路径均在caffe根目录进行。

1. cmdcaffe的基本操作

编译好的cmdcaffe的二进制文件位于./build/tools/caffe.bin,直接在bash中运行caffe即可大致了解cmdcaffe的使用方法:

ys@ysubuntu:~/caffe$ ./build/tools/caffe
caffe: command line brew
usage: caffe <command> <args>

commands:
  train           train or finetune a model
  test            score a model
  device_query    show GPU diagnostic information
  time            benchmark model execution time

  Flags from 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: ""

由上可见,命令行caffe的命令选项有4种:

  • train #用于训练或者微调模型
  • test #测试模型
  • time #测试某个模型的运行时间
  • device_query #显示GPU的诊断信息

1.1 train

caffe可以使用train命令从头开始训练模型,或是从某个保存的snapshots状态文件(.solverstate文件)中恢复训练,或者微调某个模型到新的数据集和任务(.caffemodel文件)。

假设你已经制作好了mnist数据集的LMDB格式的文件,如果没有,执行以下命令:

bash data/mnist/get_mnist.sh    #下载mnist二进制数据集文件
bash examples/mnist/create_mnist.sh #将下载得到的二进制文件转换成LMDB格式

于是会在./examples/mnist/目录下得到以下两个LMDB格式的训练数据集:

Screenshot from 2018-03-15 13-35-07.png

从头开始训练LeNet模型:

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

可见,训练一个模型只需要添加一个solver参数。

从之前保存的某个训练状态中恢复训练:

假设训练周期很长(需要10000迭代),求解器solver.prototxt文件中定义每隔1000次保存一次快照/snapshot,这次训练到了5000多次,临时有事希望中断训练,但又不想下次从头开始训练,希望下次从5000次时的状态接着训练,没问题,只要你保存了快照.solverstate文件就可以,以LeNet为例:

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

此时除了要提供solver文件外,你希望从第几次迭代的地方恢复训练,就提供对应的.solverstate文件。

微调模型:

假设你已经训练好了一个模型,你希望将模型应用到新的数据集,也就是迁移学习,此时则需要用到-weights model.caffemodel参数,很多目标检测模型如R-CNN系列都使用了迁移学习的思路,也就是目标检测模型卷积层的模型参数使用公开的已训练好的相应图像分类的模型参数来初始化。

1.2 test

假设已经经过前面的train命令训练好了LeNet模型,得到了模型参数文件:lenet_iter_10000.caffemodel。现在使用test命令来查看训练好的模型在测试集上的表现:

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

其中,-model-weights两个参数是必须的,第3个-gpu(如果没指定则使用cpu模式)和第4个-iterations(如果没指定,默认是50次迭代)是optional。

1.3 time

使用time命令可以知道某个模型训练的运行时间,同样以LeNet为例:

测量LeNet在cpu上进行20迭代训练的平均时间:

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

输出结果:

I0315 14:29:33.101918 25597 caffe.cpp:366] Testing for 20 iterations.
I0315 14:29:33.151660 25597 caffe.cpp:394] Iteration: 1 forward-backward time: 49 ms.
I0315 14:29:33.204447 25597 caffe.cpp:394] Iteration: 2 forward-backward time: 52 ms.
I0315 14:29:33.251971 25597 caffe.cpp:394] Iteration: 3 forward-backward time: 47 ms.
I0315 14:29:33.300416 25597 caffe.cpp:394] Iteration: 4 forward-backward time: 48 ms.
I0315 14:29:33.347710 25597 caffe.cpp:394] Iteration: 5 forward-backward time: 47 ms.
I0315 14:29:33.397945 25597 caffe.cpp:394] Iteration: 6 forward-backward time: 50 ms.
I0315 14:29:33.447299 25597 caffe.cpp:394] Iteration: 7 forward-backward time: 49 ms.
I0315 14:29:33.495932 25597 caffe.cpp:394] Iteration: 8 forward-backward time: 48 ms.
I0315 14:29:33.543184 25597 caffe.cpp:394] Iteration: 9 forward-backward time: 47 ms.
I0315 14:29:33.591707 25597 caffe.cpp:394] Iteration: 10 forward-backward time: 48 ms.
I0315 14:29:33.639215 25597 caffe.cpp:394] Iteration: 11 forward-backward time: 47 ms.
I0315 14:29:33.687556 25597 caffe.cpp:394] Iteration: 12 forward-backward time: 48 ms.
I0315 14:29:33.736264 25597 caffe.cpp:394] Iteration: 13 forward-backward time: 48 ms.
I0315 14:29:33.783486 25597 caffe.cpp:394] Iteration: 14 forward-backward time: 47 ms.
I0315 14:29:33.831017 25597 caffe.cpp:394] Iteration: 15 forward-backward time: 47 ms.
I0315 14:29:33.879411 25597 caffe.cpp:394] Iteration: 16 forward-backward time: 48 ms.
I0315 14:29:33.927126 25597 caffe.cpp:394] Iteration: 17 forward-backward time: 47 ms.
I0315 14:29:33.975950 25597 caffe.cpp:394] Iteration: 18 forward-backward time: 48 ms.
I0315 14:29:34.025713 25597 caffe.cpp:394] Iteration: 19 forward-backward time: 49 ms.
I0315 14:29:34.074343 25597 caffe.cpp:394] Iteration: 20 forward-backward time: 48 ms.
I0315 14:29:34.074419 25597 caffe.cpp:397] Average time per layer: 
I0315 14:29:34.074462 25597 caffe.cpp:400]      mnist   forward: 0.38435 ms.
I0315 14:29:34.074538 25597 caffe.cpp:403]      mnist   backward: 0.00075 ms.
I0315 14:29:34.074615 25597 caffe.cpp:400]      conv1   forward: 5.61235 ms.
I0315 14:29:34.074689 25597 caffe.cpp:403]      conv1   backward: 5.32245 ms.
I0315 14:29:34.074764 25597 caffe.cpp:400]      pool1   forward: 5.7118 ms.
I0315 14:29:34.074838 25597 caffe.cpp:403]      pool1   backward: 0.98785 ms.
I0315 14:29:34.074913 25597 caffe.cpp:400]      conv2   forward: 9.19835 ms.
I0315 14:29:34.074988 25597 caffe.cpp:403]      conv2   backward: 14.0461 ms.
I0315 14:29:34.075063 25597 caffe.cpp:400]      pool2   forward: 2.649 ms.
I0315 14:29:34.075137 25597 caffe.cpp:403]      pool2   backward: 2.0672 ms.
I0315 14:29:34.075212 25597 caffe.cpp:400]        ip1   forward: 0.7406 ms.
I0315 14:29:34.075290 25597 caffe.cpp:403]        ip1   backward: 1.42295 ms.
I0315 14:29:34.075366 25597 caffe.cpp:400]      relu1   forward: 0.0571 ms.
I0315 14:29:34.075441 25597 caffe.cpp:403]      relu1   backward: 0.07105 ms.
I0315 14:29:34.075515 25597 caffe.cpp:400]        ip2   forward: 0.07905 ms.
I0315 14:29:34.075592 25597 caffe.cpp:403]        ip2   backward: 0.09795 ms.
I0315 14:29:34.075665 25597 caffe.cpp:400]       loss   forward: 0.0655 ms.
I0315 14:29:34.075740 25597 caffe.cpp:403]       loss   backward: 0.002 ms.
I0315 14:29:34.075819 25597 caffe.cpp:408] Average Forward pass: 24.5086 ms.
I0315 14:29:34.075862 25597 caffe.cpp:410] Average Backward pass: 24.0275 ms.
I0315 14:29:34.075906 25597 caffe.cpp:412] Average Forward-Backward: 48.65 ms.

最后一行显示,LeNet模型在cpu模式下进行一次迭代训练的平均时间是48.65ms。

测量LeNet在gpu上进行20迭代训练的平均时间:

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

输出结果:

I0315 14:34:35.215425 26301 caffe.cpp:366] Testing for 20 iterations.
I0315 14:34:35.216894 26301 caffe.cpp:394] Iteration: 1 forward-backward time: 1.22784 ms.
I0315 14:34:35.218123 26301 caffe.cpp:394] Iteration: 2 forward-backward time: 1.2072 ms.
I0315 14:34:35.219348 26301 caffe.cpp:394] Iteration: 3 forward-backward time: 1.20384 ms.
I0315 14:34:35.220634 26301 caffe.cpp:394] Iteration: 4 forward-backward time: 1.26378 ms.
I0315 14:34:35.222271 26301 caffe.cpp:394] Iteration: 5 forward-backward time: 1.61626 ms.
I0315 14:34:35.223536 26301 caffe.cpp:394] Iteration: 6 forward-backward time: 1.22819 ms.
I0315 14:34:35.224771 26301 caffe.cpp:394] Iteration: 7 forward-backward time: 1.21261 ms.
I0315 14:34:35.226001 26301 caffe.cpp:394] Iteration: 8 forward-backward time: 1.20934 ms.
I0315 14:34:35.227252 26301 caffe.cpp:394] Iteration: 9 forward-backward time: 1.22982 ms.
I0315 14:34:35.228492 26301 caffe.cpp:394] Iteration: 10 forward-backward time: 1.2199 ms.
I0315 14:34:35.229722 26301 caffe.cpp:394] Iteration: 11 forward-backward time: 1.20662 ms.
I0315 14:34:35.231030 26301 caffe.cpp:394] Iteration: 12 forward-backward time: 1.28819 ms.
I0315 14:34:35.232254 26301 caffe.cpp:394] Iteration: 13 forward-backward time: 1.20256 ms.
I0315 14:34:35.233466 26301 caffe.cpp:394] Iteration: 14 forward-backward time: 1.19267 ms.
I0315 14:34:35.234683 26301 caffe.cpp:394] Iteration: 15 forward-backward time: 1.19501 ms.
I0315 14:34:35.237324 26301 caffe.cpp:394] Iteration: 16 forward-backward time: 2.62144 ms.
I0315 14:34:35.238819 26301 caffe.cpp:394] Iteration: 17 forward-backward time: 1.47446 ms.
I0315 14:34:35.240056 26301 caffe.cpp:394] Iteration: 18 forward-backward time: 1.21731 ms.
I0315 14:34:35.241310 26301 caffe.cpp:394] Iteration: 19 forward-backward time: 1.23222 ms.
I0315 14:34:35.242533 26301 caffe.cpp:394] Iteration: 20 forward-backward time: 1.20291 ms.
I0315 14:34:35.242545 26301 caffe.cpp:397] Average time per layer: 
I0315 14:34:35.242549 26301 caffe.cpp:400]      mnist   forward: 0.0034384 ms.
I0315 14:34:35.242554 26301 caffe.cpp:403]      mnist   backward: 0.0009648 ms.
I0315 14:34:35.242558 26301 caffe.cpp:400]      conv1   forward: 0.0863696 ms.
I0315 14:34:35.242561 26301 caffe.cpp:403]      conv1   backward: 0.130837 ms.
I0315 14:34:35.242565 26301 caffe.cpp:400]      pool1   forward: 0.025216 ms.
I0315 14:34:35.242568 26301 caffe.cpp:403]      pool1   backward: 0.0641232 ms.
I0315 14:34:35.242573 26301 caffe.cpp:400]      conv2   forward: 0.107947 ms.
I0315 14:34:35.242578 26301 caffe.cpp:403]      conv2   backward: 0.363768 ms.
I0315 14:34:35.242583 26301 caffe.cpp:400]      pool2   forward: 0.0104 ms.
I0315 14:34:35.242585 26301 caffe.cpp:403]      pool2   backward: 0.0247184 ms.
I0315 14:34:35.242590 26301 caffe.cpp:400]        ip1   forward: 0.039376 ms.
I0315 14:34:35.242594 26301 caffe.cpp:403]        ip1   backward: 0.0504832 ms.
I0315 14:34:35.242599 26301 caffe.cpp:400]      relu1   forward: 0.0097104 ms.
I0315 14:34:35.242602 26301 caffe.cpp:403]      relu1   backward: 0.0079312 ms.
I0315 14:34:35.242606 26301 caffe.cpp:400]        ip2   forward: 0.0291408 ms.
I0315 14:34:35.242610 26301 caffe.cpp:403]        ip2   backward: 0.0271744 ms.
I0315 14:34:35.242614 26301 caffe.cpp:400]       loss   forward: 0.0832176 ms.
I0315 14:34:35.242619 26301 caffe.cpp:403]       loss   backward: 0.0276144 ms.
I0315 14:34:35.242625 26301 caffe.cpp:408] Average Forward pass: 0.530309 ms.
I0315 14:34:35.242630 26301 caffe.cpp:410] Average Backward pass: 0.779861 ms.
I0315 14:34:35.242638 26301 caffe.cpp:412] Average Forward-Backward: 1.34835 ms.

最后一行显示,LeNet模型在gpu模式(GTX1080ti)下进行一次迭代训练的平均时间是1.35ms。训练速度是cpu模式下的36倍。

1.4 device_query

caffe的device_query命令用于获取gpu设备的详细信息:

./build/tools/caffe device_query -gpu 0

输出结果:

ys@ysubuntu:~/caffe$ ./build/tools/caffe device_query -gpu 0
I0315 14:39:02.546105 27083 caffe.cpp:138] Querying GPUs 0
I0315 14:39:02.592700 27083 common.cpp:178] Device id:                     0
I0315 14:39:02.593533 27083 common.cpp:179] Major revision number:         6
I0315 14:39:02.593580 27083 common.cpp:180] Minor revision number:         1
I0315 14:39:02.593621 27083 common.cpp:181] Name:                          GeForce GTX 1080 Ti
I0315 14:39:02.593662 27083 common.cpp:182] Total global memory:           11711807488
I0315 14:39:02.593705 27083 common.cpp:183] Total shared memory per block: 49152
I0315 14:39:02.593746 27083 common.cpp:184] Total registers per block:     65536
I0315 14:39:02.593786 27083 common.cpp:185] Warp size:                     32
I0315 14:39:02.593827 27083 common.cpp:186] Maximum memory pitch:          2147483647
I0315 14:39:02.593868 27083 common.cpp:187] Maximum threads per block:     1024
I0315 14:39:02.593907 27083 common.cpp:188] Maximum dimension of block:    1024, 1024, 64
I0315 14:39:02.593947 27083 common.cpp:191] Maximum dimension of grid:     2147483647, 65535, 65535
I0315 14:39:02.593988 27083 common.cpp:194] Clock rate:                    1657500
I0315 14:39:02.594029 27083 common.cpp:195] Total constant memory:         65536
I0315 14:39:02.594069 27083 common.cpp:196] Texture alignment:             512
I0315 14:39:02.594108 27083 common.cpp:197] Concurrent copy and execution: Yes
I0315 14:39:02.594149 27083 common.cpp:199] Number of multiprocessors:     28
I0315 14:39:02.594188 27083 common.cpp:200] Kernel execution timeout:      Yes
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,445评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,889评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,047评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,760评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,745评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,638评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,011评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,669评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,923评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,655评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,740评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,406评论 4 320
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,995评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,961评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,023评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,483评论 2 342

推荐阅读更多精彩内容