Caffe能做什麼?
為什麼選擇caffe?
模塊化做的好
簡單:修改結構無需該代碼
開源:共同維護開原始碼
環境:
Distributor ID: Ubuntu
Description: Ubuntu 12.04.4 LTS
Release: 12.04
Codename: precise
Linux version 3.2.0-29-generic (buildd@allspice) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012
整體結構:
定義CAFFE為caffe跟目錄,caffe的核心代碼都在$CAFFE/src/caffe 下,主要有以下部分:net, blob, layer, solver.
net定義網絡, 整個網絡中含有很多layers, net.cpp負責計算整個網絡在訓練中的forward, backward過程, 即計算forward/backward 時各layer的gradient。
在$CAFFE/src/caffe/layers中的層,在protobuffer (.proto文件中定義message類型,.prototxt或.binaryproto文件中定義message的值) 中調用時包含屬性name, type(data/conv/pool…), connection structure (input blobs and output blobs),layer-specific parameters(如conv層的kernel大小)。定義一個layer需要定義其setup, forward 和backward過程。
net中的數據和求導結果通過4維的blob傳遞。一個layer有很多blobs, e.g,
對data,weight blob大小為Number * Channels * Height * Width, 如256*3*224*224;
對conv層,weight blob大小為 Output 節點數 * Input 節點數 * Height * Width,如AlexNet第一個conv層的blob大小為96 x 3 x 11 x 11;
對inner product 層, weight blob大小為 1 * 1 * Output節點數 * Input節點數; bias blob大小為1 * 1 * 1 * Output節點數( conv層和inner product層一樣,也有weight和bias,所以在網絡結構定義中我們會看到兩個blobs_lr,第一個是weights的,第二個是bias的。類似地,weight_decay也有兩個,一個是weight的,一個是bias的);
blob中,mutable_cpu/gpu_data() 和cpu/gpu_data()用來管理memory,cpu/gpu_diff()和 mutable_cpu/gpu_diff()用來計算求導結果。
結合loss,用gradient更新weights。主要函數:
Init(),
Solve(),
ComputeUpdateValue(),
Snapshot(), Restore(),//快照(拷貝)與恢復 網絡state
Test();
在solver.cpp中有3中solver,即3個類:AdaGradSolver, SGDSolver和NesterovSolver可供選擇。
關於loss,可以同時有多個loss,可以加regularization(L1/L2);
Protocol buffer:
上面已經將過, protocol buffer在 .proto文件中定義message類型,.prototxt或.binaryproto文件中定義message的值;
Caffe
Caffe的所有message定義在$CAFFE/src/caffe/proto/caffe.proto中。
Experiment
在實驗中,主要用到兩個protocol buffer: solver的和model的,分別定義solver參數(學習率啥的)和model結構(網絡結構)。
技巧:
訓練基本流程:
數據處理
法一,轉換成caffe接受的格式:lmdb, leveldb, hdf5 / .mat, list of images, etc.;法二,自己寫數據讀取層(如https://github.com/tnarihi/tnarihi-caffe-helper/blob/master/python/caffe_helper/layers/data_layers.py)
定義網絡結構
配置Solver參數
訓練:如 caffe train -solver solver.prototxt -gpu 0
(有刪節....)