Caffe 命令行解析

Caffe的提供三種接口:c++接口(命令行)、python接口和matlab接口,這里介紹命令行的使用。

命令行

caffe的c++主程序(caffe.cpp)放在根目錄下的tools文件夾內(nèi), 當(dāng)然還有一些其它的功能文件,如:convert_imageset.cpp, train_net.cpp, test_net.cpp等也放在這個(gè)文件夾內(nèi)。經(jīng)過編譯后,這些文件都被編譯成了可執(zhí)行文件,放在了./build/tools/ 文件夾內(nèi)。因此我們要執(zhí)行caffe程序,都需要加 ./build/tools/ 前綴。如:
sudo sh ./build/tools/caffe train --solver=examples/mnist/train_lenet.sh


caffe程序的命令行執(zhí)行格式如下:
caffe <command> <args>
其中的<command>有這樣四種:

  • train
  • test
  • device_query
  • time

對(duì)應(yīng)的功能為:
train----訓(xùn)練或finetune模型,
test-----測試模型
device_query---顯示GPU信息
time-----顯示程序執(zhí)行時(shí)間

其中的<args>參數(shù)有:

  • -solver
  • -gpu
  • -snapshot
  • -weights
  • -iteration
  • -model
  • -sighup_effect
  • -sigint_effect
    注意前面有個(gè)-符號(hào)。

train對(duì)應(yīng)的功能為:

  • -solver:必選參數(shù)。一個(gè)protocol buffer類型的文件,即模型的配置文件。如:
    ./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt

  • -gpu: 可選參數(shù)。該參數(shù)用來指定用哪一塊gpu運(yùn)行,根據(jù)gpu的id進(jìn)行選擇,如果設(shè)置為'-gpu all'則使用所有的gpu運(yùn)行。如使用第二塊gpu運(yùn)行:
    ./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt -gpu 2

  • -snapshot:可選參數(shù)。該參數(shù)用來從快照(snapshot)中恢復(fù)訓(xùn)練。可以在solver配置文件設(shè)置快照,保存solverstate。如:
    ./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt -snapshot examples/mnist/lenet_iter_5000.solverstate

  • -weights:可選參數(shù)。用預(yù)先訓(xùn)練好的權(quán)重來fine-tuning模型,需要一個(gè)caffemodel,不能和-snapshot同時(shí)使用。如:
    ./build/tools/caffe train -solver examples/finetuning_on_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel

  • -iterations: 可選參數(shù),迭代次數(shù),默認(rèn)為50。 如果在配置文件文件中沒有設(shè)定迭代次數(shù),則默認(rèn)迭代50次。

  • -model:可選參數(shù),定義在protocol buffer文件中的模型。也可以在solver配置文件中指定。

  • -sighup_effect:可選參數(shù)。用來設(shè)定當(dāng)程序發(fā)生掛起事件時(shí),執(zhí)行的操作,可以設(shè)置為snapshot, stop或none, 默認(rèn)為snapshot

  • -sigint_effect: 可選參數(shù)。用來設(shè)定當(dāng)程序發(fā)生鍵盤中止事件時(shí)(ctrl+c), 執(zhí)行的操作,可以設(shè)置為snapshot, stop或none, 默認(rèn)為stop


剛才舉例了一些train參數(shù)的例子,現(xiàn)在我們來看看其它三個(gè)<command>:

test參數(shù)用在測試階段,用于最終結(jié)果的輸出,要模型配置文件中我們可以設(shè)定需要輸入accuracy還是loss. 假設(shè)我們要在驗(yàn)證集中驗(yàn)證已經(jīng)訓(xùn)練好的模型,就可以這樣寫
./build/tools/caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 100
這個(gè)例子比較長,不僅用到了test參數(shù),還用到了-model, -weights, -gpu和-iteration四個(gè)參數(shù)。意思是利用訓(xùn)練好了的權(quán)重(-weight),輸入到測試模型中(-model),用編號(hào)為0的gpu(-gpu)測試100次(-iteration)。

time參數(shù)用來在屏幕上顯示程序運(yùn)行時(shí)間。如:
./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -iterations 10
這個(gè)例子用來在屏幕上顯示lenet模型迭代10次所使用的時(shí)間。包括每次迭代的forward和backward所用的時(shí)間,也包括每層forward和backward所用的平均時(shí)間。
./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -gpu 0
這個(gè)例子用來在屏幕上顯示lenet模型用gpu迭代50次所使用的時(shí)間。
./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 10
利用給定的權(quán)重,利用第一塊gpu,迭代10次lenet模型所用的時(shí)間。


device_query參數(shù)用來診斷gpu信息。
./build/tools/caffe device_query -gpu 0
最后,我們來看兩個(gè)關(guān)于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
這兩個(gè)例子表示: 用兩塊或多塊GPU來平行運(yùn)算,這樣速度會(huì)快很多。但是如果你只有一塊或沒有g(shù)pu, 就不要加-gpu參數(shù)了,加了反而慢。

最后,在linux下,本身就有一個(gè)time命令,因此可以結(jié)合進(jìn)來使用,因此我們運(yùn)行mnist例子的最終命令是(一塊gpu):
sudo time ./build/toos/caffe train -solver examples/mnist/lenet_solver.prototxt

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容