以下内容主要来自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格式的训练数据集:
从头开始训练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