[Notes]深度學習——caffe工具使用
本篇文章是我利用 caffe進行深度學習的知識點總結,包含別人學習筆記連結
caffe簡介
caffe的作者為UC Berkeley大學的賈揚清。caffe是一個c++/CUDA架構,支援命令列、Python、Matlab介面,可以在CPU/GPU上執行。
Caffe專案的檔案結構
- caffe(要編譯的檔案,裡面包含用c++寫的資料輸入、網路層、計算等核心檔案)
- data (要處理或者轉換的資料)
- models:
train.prototxt (網路模型) solve.prototxt(設定訓練的一系列引數) xxx.caffemodel(finetune 時用的初始化引數,訓練新模型則不需要)
- scripts(訓練網路的程式碼,可以是python檔案,shell檔案等)
caffe 網路結構
caffe 使用Blob陣列結構來儲存、交換、處理網路(就像numpy的儲存結構為narray),用caffe Layer來定義神經網路結構,它包含資料層、視覺層等型別
下面以程式碼為例,講解常用層
train.prototxt( 以下為VGG16模型部分程式碼)
- 資料層
name: "VGG16" layer { name: "data" type: "Data" #輸入的資料型別 top: "data" top: "label" include { phase: TRAIN } #資料預處理,來增強資料 transform_param { mirror: true crop_size: 224 mean_value: 103.939 mean_value: 116.779 mean_value: 123.68 } data_param { source: "data/ilsvrc12_shrt_256/ilsvrc12_train_leveldb" #資料庫檔案路徑 batch_size: 64 #網路單次輸入資料數量 backend: LEVELDB #選擇使用LevelDB還是LMDB } }
caffe支援輸入的資料型別:
type | data |
---|---|
Data | LMDB/levelDB |
MemoryData | 記憶體資料 |
HDF5Data | HDF5資料 |
ImagesData | 影象資料Images |
WindowsData | 視窗Windows |
top:表示輸出的方向,bottom:表示輸入的資料來源(層的名稱),可以有多個top和bottom
注意:在資料層中,至少有一個命名為data的top。如果有第二個top,一般命名為label
- 卷積層
layer { bottom: "data" top: "conv1_1" name: "conv1_1" type: "Convolution" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 2 decay_mult: 0 } convolution_param { num_output: 64 pad: 1 kernel_size: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } }
引數
num_output | 卷積核數量 |
kernel_size | 卷積核高度/寬度(可分別設定寬高) |
weight_filler | 引數初始化方案 |
bias_term | 是否給卷積輸出新增偏置項 |
pad | 影象周圍補0的畫素個數 |
stride | 滑動步長 |
group | 指定分組卷積操作的組數 |
lr_mult | 學習率(最終的學習率要乘以 solver.prototxt 配置檔案中的 base_lr) |
decay_mult | 權值衰減 |
dropout_ratio | 丟棄資料的概率 |
- | - |
dropout_ratio和decay_mult設定為了防止資料過擬合
- 池化層
layer {
bottom: "pool1"
top: "conv2_1"
name: "conv2_1"
type: "Convolution"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 128
pad: 1
kernel_size: 3
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 0
}
}
}
引數 | |
---|---|
pool | 池化方式,Max:最大池化,AVE:均值池化,STOCHASTIC:隨機池化 |
-
啟用層
-
損失函式層
layer { bottom: "fc8" bottom: "label" top: "loss" name: "loss" type: "SoftmaxWithLoss" }
type | |
---|---|
SoftmaxWithLoss | 交叉資訊熵損失函式 |
Softmax | 多分類損失函式 |
Caffe模型訓練
- 網路視覺化
當你寫好自己的prototxt檔案後,想要檢查自己的網路框架是否搭建正確,可以藉助 Netscope (線上caffe net視覺化工具)http://ethereon.github.io/netscope/#/editor
- 訓練引數設定
caffe模型的訓練引數在solve.prototxt檔案中,該檔案是caffe的核心,它交替呼叫前向演算法和反向傳播演算法來更新引數,使loss的值達到最小
net: "train_val.prototxt"
test_iter: 833
# make test net, but don't invoke it from the solver itself
test_interval: 1000
display: 200
average_loss: 100
base_lr: 1e-5
lr_policy: "step"
gamma: 0.1
stepsize: 5000
# lr for unnormalized softmax -- see train_val definition
# high momentum
momentum: 0.9
# no gradient accumulation
clip_gradients: 10000
iter_size: 1
max_iter: 80000
weight_decay: 0.02
snapshot: 4000
snapshot_prefix: "weight/VGG_item"
test_initialization: false
引數 | |
---|---|
train_net | 訓練所需網路模型(最好寫絕對路徑) |
test_net | 測試所需網路模型 |
test_iter | 測試次數 (test_iter * batchsize = 訓練的資料量) |
base_lr | 基本學習率 |
lr_policy | 學習率改變的方法 |
weight_decay | 權重衰減 |
momentum | 表示上一次梯度更新的權重 |
max_iter | 最大迭代次數 |
snapshot | 儲存模型間隔 |
snapshot_prefix | 儲存模型路徑+字首 |
solver_mode | 是否使用GPU |
average_loss | 取多次foward的loss作平均,進行顯示輸出 |
type | 優化演算法 |
- 訓練網路
1、命令列
我們可以在命令列輸入程式碼訓練網路
./build/tools/caffe train -solver solver.prototxt
2、python
我們也可以利用caffe的python介面來編寫訓練網路的程式
其他問題
- 輸入輸出大小
在用自己的資料訓練或者微調網路的過程中,可能會出現img 與label_img大小不同的情況,這個時候就仔細分析訓練過程所顯示的每一層輸入輸出的大小,更改相應的引數使最後訓練的img與label_img大小相同
卷積核與池化層輸出影象尺寸計算公式:
(W-F+2P)/S+1
引數 | 說明 |
---|---|
w | 輸入影象大小 |
F | 卷積核尺寸(kernel_size) |
S | 步幅大小(stride) |
P | padding大小(pad) |