Caffe : Tutorial : インターフェイス

Caffe : Tutorial : インターフェイス (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
日時 : 04/02/2017

* 本ページは、Caffe の本家サイトの Tutorial – Interfaces を翻訳した上で適宜、補足説明したものです:
    http://caffe.berkeleyvision.org/tutorial/interfaces.html
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

 

Caffe はコマンドライン、Python そして MATLAB インターフェイスを持ちます、日々の使用のため、研究コードとラピッドプロトタイピングのインターフェイスのためにです。Caffe は心底 C++ ライブラリである一方でそれは開発のためのモジュール・インターフェイスを公開します、全ての場合においてカスタム・コンピレーションを必要とするわけではありません。cmdcaffe、pycaffe そして matcaffe インターフェイスがここに貴方のためにあります。

 

コマンドライン

コマンドライン・インターフェイス – cmdcaffe – はモデル訓練、スコアリング、そして診断 (diagnostics) のための caffe ツールです。助けを求める引数なしに caffe を実行します。このツール他は caffe/build/tools で見つかります。(次のサンプル呼び出しは LeNet / MNIST サンプルを最初に完了することを要求しています。)

トレーニング: caffe train はスクラッチからモデルを学習して、保存したスナップショットから学習を再開して新しいデータとタスクにモデルを再調整します :

  • 全ての訓練は -solver solver.prototxt 引数を通して solver configuration が必要です。
  • 再開するためには solver スナップショットをロードするために -snapshot model_iter_1000.solverstate 引数が必要です。
  • 再調整はモデル初期化のために -weights model.caffemodel が必要です。

例えば、以下のように実行できます :

# train LeNet
caffe train -solver examples/mnist/lenet_solver.prototxt
# train on GPU 2
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

再調整の完全なサンプルは、examples/finetuning_on_flickr_style を参照してください、しかし単独の訓練呼び出しとしては以下になります :

# fine-tune CaffeNet model weights for style recognition
caffe train -solver examples/finetuning_on_flickr_style/solver.prototxt -weights models/bvlc_reference_caffene/bvlc_reference_caffenet.caffemodel

 
テスト:
caffe test はモデルをテスト段階で実行することでスコアをつけてそしてそのスコアとして net 出力をレポートします。出力として精度尺度あるいは損失を出力するためには net アーキテクチャは正しく定義されていなければなりません。バッチ毎のスコアがレポートされそして堂々たる (= grand) 平均は最後にレポートされます。

# 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

 
ベンチマーキング: タイミングと同期を通してモデル実行を層毎にベンチマークします。これはシステム・パフォーマンスをチェックしてモデルの相対的な実行時間を計測するために有用です。

# (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
# time LeNet training on GPU for the default 50 iterations
caffe time -model examples/mnist/lenet_train_test.prototxt -gpu 0
# time a model architecture with the given weights on the first GPU for 10 iterations
caffe time -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 10

 
診断
caffe device_query は参考のために GPU 詳細をレポートしてマルチ GPU マシンで与えられたデバイス上で実行するためにデバイス順序をチェックします。

# query the first device
caffe device_query -gpu 0

 
並列性 (Parallelism):
caffe ツールへの -gpu フラグは複数の GPU を実行するために ID のカンマ区切りのリストを取ります。solver と net が各 GPU のためにインスタンス化されバッチサイズは効果的に GPU の数で乗算されます。単一 GPU 訓練を再現するためには、それに応じてネットワーク定義でバッチサイズを減じます。

# 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

 

Python

Python インターフェイス – pycaffe – は caffe/python の caffe モジュールとそのスクリプトです。import caffe は モデルをロードし、forward と backward を行ない、IO を処理し、ネットワークを可視化し、そして instrument model solving でさえもあります。全てのモデルデータ、導関数、そしてパラメータは読み書きのために公開されます。

  • caffe.Net はモデルのロード、構成、そして実行のための中心的なインターフェイスです。caffe.Classifier と caffe.Detector は一般的なタスクのために便利なインターフェイスを提供します。
  • caffe.SGDSolver は solving インターフェイスを公開します。
  • caffe.io は前処理の入出力と protocol buffers を処理します。
  • caffe.draw はネットワーク・アーキテクチャを可視化します。
  • Caffe blobs は使いやすさと効率性のために numpy ndarray として公開されます。

チュートリアル IPython notebooks は caffe/examples で見つかります : それらを試すためには ipython notebook caffe/examples を実行します。デベロッパー・リファレンスのためには コードを通して docstrings が見つかります。

make pycaffe で pycaffe をコンパイルします。export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH または import caffe のための何かでモジュール・ディレクトリを $PYTHONPATH に追加します。

 

MATLAB

(省略)

 

以上