1. 程式人生 > >Caffe使用step by step:caffe框架下的基本操作和分析

Caffe使用step by step:caffe框架下的基本操作和分析

caffe雖然已經安裝了快一個月了,但是caffe使用進展比較緩慢,果然如劉老師說的那樣,搭建起來caffe框架環境比較簡單,但是完整的從資料準備->模型訓練->調引數->合理結果需要一個比較長的過程,這個過程中你需要對caffe中很多東西,細節進行深入的理解,這樣才可以知道為什麼能有這樣的結果,在訓練或者fine-tuning時知道針對調整的方法。下面針對caffe中的使用進行講解。

  在使用過程中,caffe官網上提供了詳細的使用說明,如果感覺仍然存在一些困難,可以使用谷歌或百度搜索自己遇到的問題和想要了解的過程進行搜尋學習。

一、Caffe模型基本組成

想要訓練一個caffe模型,需要配置兩個檔案,包含兩個部分:網路模型,引數配置,分別對應***.prototxt , ****_solver.prototxt檔案

Caffe模型檔案講解:

  1. 預處理影象的leveldb構建 
    輸入:一批影象和label (2和3) 
    輸出:leveldb (4) 
    指令裡包含如下資訊:
    1. conver_imageset (構建leveldb的可執行程式)
    2. train/ (此目錄放處理的jpg或者其他格式的影象)
    3. label.txt (影象檔名及其label資訊)
    4. 輸出的leveldb資料夾的名字
    5. CPU/GPU (指定是在cpu上還是在gpu上執行code)
  2. CNN網路配置檔案

    1. Imagenet_solver.prototxt (包含全域性引數的配置的檔案)
    2. Imagenet.prototxt (包含訓練網路的配置的檔案)
    3. Imagenet_val.prototxt (包含測試網路的配置檔案)

網路模型:即定義你網路的每一層,下圖是用caffe中 /python/draw_net.py畫出的的siamese的模型,非常清晰

技術分享

層包含:(以LeNet為例)

DATA:一般包括訓練資料測試資料層兩種型別。 一般指輸入層,包含source:資料路徑,批處理資料大小batch_size,scale表示資料表示在[0,1],0.00390625即 1/255

訓練資料層:

layer { name: "mnist" type: "Data" top: "data" top: "label" include { phase: TRAIN } transform_param { scale:0.00390625 } data_param { source: "examples/mnist/mnist_train_lmdb" batch_size: 64 backend: LMDB } }

測試資料層:

layer { name: "mnist" type: "Data" top: "data" top: "label" include { phase: TEST } transform_param { scale:0.00390625 } data_param { source: "examples/mnist/mnist_test_lmdb" batch_size: 100 backend: LMDB } }

CONVOLUATION:卷積層,blobs_lr:1 , blobs_lr:2分別表示weight 及bias更新時的學習率,這裡權重的學習率為solver.prototxt檔案中定義的學習率真,bias的學習率真是權重學習率的2倍,這樣一般會得到很好的收斂速度。

num_output表示濾波的個數,kernelsize表示濾波的大小,stride表示步長,weight_filter表示濾波的型別

layer { name: "conv1" type: "Convolution" bottom: "data" top: "conv1" param { lr_mult: 1 //weight學習率 } param { lr_mult: 2 //bias學習率,一般為weight的兩倍 } convolution_param { num_output: 20 //濾波器個數kernel_size: 5 stride: //步長 weight_filler { type: "xavier" } bias_filler { type: "constant" } } }

POOLING: 池化層

layer { name: "pool1" type: "Pooling" bottom: "conv1" top: "pool1" pooling_param { pool: MAX kernel_size: 2stride: 2 } }

INNER_PRODUCT: 其實表示全連線,不要被名字誤導

layer { name: "ip1" type: "InnerProduct" bottom: "pool2" top: "ip1" param { lr_mult: 1 } param { lr_mult: 2 } inner_product_param { num_output: 500 weight_filler { type: "xavier" } bias_filler { type: "constant" } } }

RELU:啟用函式,非線性變化層 max( 0 ,x ),一般與CONVOLUTION層成對出現

layer { name: "relu1" type: "ReLU" bottom: "ip1" top: "ip1" }

SOFTMAX

layer { name: "loss" type: "SoftmaxWithLoss" bottom: "ip2" bottom: "label" top: "loss" }

引數配置檔案:

***_solver.prototxt檔案定義一些模型訓練過程中需要到的引數,比較學習率,權重衰減係數,迭代次數,使用GPU還是CPU等等

# The train/test net protocol buffer definition net: "examples/mnist/lenet_train_test.prototxt" # test_iter specifies how many forward passes the test should carry out. # In the case of MNIST, we have test batch size 100 and 100 test iterations, # covering the full 10,000 testing images. test_iter: 100 # Carry out testing every 500 training iterations. test_interval: 500 # The base learning rate, momentum and the weight decay of the network. base_lr: 0.01 momentum: 0.9 weight_decay: 0.0005 # The learning rate policy lr_policy:"inv" gamma: 0.0001 power: 0.75 # Display every 100 iterations display: 100 # The maximum number of iterations max_iter: 10000 # snapshot intermediate results snapshot: 5000 snapshot_prefix:"examples/mnist/lenet" # solver mode: CPU or GPU solver_mode: GPU device_id: 0 #在cmdcaffe介面下,GPU序號從0開始,如果有一個GPU,則device_id:0

訓練出的模型被存為***.caffemodel,可供以後使用。

二、使用caffe訓練模型包含以下幾個步驟:

  1. 準備資料
  2. 重建lmdb/leveldb檔案,caffe支援三種資料格式輸入:images, levelda, lmdb
  3. 定義name.prototxt , name_solver.prototxt檔案
  4. 訓練模型

三、caffe中比較有用且基礎的介面(cmdcaffe)

注:在使用cmdcaffe時,需要預設切換到Caffe_Root資料夾下

1、訓練模型,以mnist為例子

./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt

注:caffe官網上給的例子不能直接執行,需要使用上述命令才可以使用tools下的caffe介面,因為caffe預設都需要從根目錄下面執行檔案。

2、觀察各個階段的執行時間可以使用

./build/tools/caffe time --model=models/bvlc_reference_caffenet/train_val.prototxt

3、使用已有模型提取特徵

./build/tools/extract_features.bin models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel examples/_temp/imagenet_val.prototxt conv5 examples/_temp/features 10

 conv5表示提取第五個卷積層的特徵, examples/_temp/feaures表示存放結果的目錄(這裡的目錄需要提前構建好)

4、對已有模型進行find-tuning,比如我們現在有一個1000類的分類模型,但目前我們的需求僅是20類,此時我們不需要重新訓練一個模型,只需要將最後一層換成20類的softmax層,然後使用已有資料對原模型進行fine-tuning即可

  在很多時候,使用Caffe框架學習深度學習模型時,從ImageNet或者其他大型資料集從頭開始訓練獲得一個fine-tuing合適的模型難度太大,這時候最好的情況,就是在已經訓練好的模型上面來進行fine-tuning,通過這些過程可以加深自己對深度學習,以及對caffe使用的瞭解和熟悉,以方便自己在後續提出自己的模型,自己進行模型訓練和fine-tuning的過程。

已經訓練好的caffe模型可以在git的caffe專案中下載,比較經典的模型有:AlexNet.caffemodel , LeNet.caffemodel , RCnn.caffemodel,其他的大家可以在caffe的git官網上面下載。

使用自己的資料集對已經訓練好的模型進行fine-tuning的操作(使用cmdcaffe介面來進行):

./build/tools/caffe train -solver models/finetune_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel -gpu 0

第一個引數:選擇好caffe模組

train:選取train函式

後面接具體的引數,分別為配置命令,配置檔案路徑,fine-tuning命令,fine-tuning依賴的基準模型檔案目錄,選用的訓練方式:gpu或者cpu,使用cpu時可以預設不寫

注:fine-tuning的過程與訓練過程類似,只是在呼叫caffe介面時的命令不同,因此在fine-tuning之前,仍然需要按照訓練流程準備資料。

下載資料->生成trainset和testset->生成db->設定好路徑->fine-tuning。

5、還有一個是python下面的介面,draw_net.py可以根據.prototxt檔案將模式用圖示的方法表示出來,博文開始的模型圖即用該介面所繪

./python/draw_net.py ./examples/siamese/mnist_siamese.prototxt ./examples/siamese/mnist_siamese.png

使用該介面進行網路的繪製示例化

 第一個引數為模型檔案,第二個引數為所繪模型圖的儲存地址

深度學習中batch_size的作用:

在深度學習訓練過程中,有兩種訓練方法,一種意識batch ,一種是stochastic訓練方法

solver:使用forward和backward介面來更新引數,並迭代對loss進行降低(定義的優化方法,有stochastic gradient descent,SGD;Adaptive gradient ,NAG和Scaffolding)

solver作用:(指定優化方法)

1.可以逐步對網路尋優,建立訓練得到的網路,並對測試網路進行評價;

2.通過呼叫forward和backward來對網路引數進行迭代尋優;

3.週期性更新網路;

4.記錄網路訓練中間過程,尋優過程中記錄狀態