caffe ssd 雜記

目錄

prototxt文件solver.prototxt

train.prototxt

deploy.prototxt

caffe下各層概念(主要層)

Convolution卷積層

4Pooling層

全連接層

caffe命令行解析

train指令

test指令

time指令


prototxt文件
solver.prototxt

train_net: "models/VGGNet/SSD_FT_300x300/0107/train.prototxt"
test_net: "models/VGGNet/SSD_FT_300x300/0107/test.prototxt"
test_iter: 619 # specifies how many forward passes the test should carry out.
test_interval: 5000  #訓練每迭代5000次,進行一次預測
base_lr: 0.0005
display: 10  #每經過10次迭代,在屏幕上打印一次log
max_iter: 240000 #最大迭代次數
lr_policy: "multistep" #採用的學習策略
gamma: 0.1
momentum: 0.9
weight_decay: 0.0005
snapshot: 10000  #每10000次迭代打印一次快照
snapshot_prefix: "ssd_models/VGGNet/SSD_FT_300x300/0107/VGG_VOC0712_SSD_FT_300x300"#保存路徑
solver_mode: GPU
device_id: 0
debug_info: false
snapshot_after_train: true
test_initialization: false
average_loss: 10
stepvalue: 160000
stepvalue: 200000
stepvalue: 240000
iter_size: 1
type: "SGD"
eval_type: "detection"
ap_version: "11point"
show_per_class_result: true

這個文件告訴了我們訓練的網絡和超參數。用caffe訓練時會首先讀取該文件,獲得其中特定字段的數值,並據此設置內存中模型訓練時的超參數變量值。solver.prototxt的主要作用就是交替調用前向算法和後向算法來更新參數,從而最小化loss,實際上就是一種迭代的優化算法。  

train.prototxt

這個文件是網絡配置文件,給出了網絡的具體定義。

name: "VGG_VOC0712_SSD_FT_300x300_train"
layer {
  name: "data"          #輸入層的名稱
  type: "AnnotatedData" #輸入層的類型
  top: "data"
  top: "label" #層的輸出blob有兩個:data和label
  include {
    phase: TRAIN #訓練階段,該層參數只在訓練階段有效
  }
  transform_param {
    mirror: true #鏡像操作
    mean_value: 104
    mean_value: 117
    mean_value: 123
......
  }
  data_param {
    source: "examples/VOC0712/VOC0712_trainval_lmdb" #輸入數據路徑
    batch_size: 16           #batch
    backend: LMDB           #數據格式爲LMDB
  }
......
}
layer {
  name: "conv1_1"
  type: "Convolution"
  bottom: "data"
  top: "conv1_1"
  param {
# 學習率的係數,最終的學習率是這個數乘以solver.prototxt配置文件中的base_lr。如果有兩個lr_mult
# , 則第一個表示權值的學習率,第二個表示偏置項的學習率。一般偏置項的學習率是權值學習率的兩倍。
# 這裏是權值學習率
    lr_mult: 1
# 權值衰減,防止過擬合
    decay_mult: 1
  }
  param {
# 這裏是偏置項的學習率,偏置項一般不存在過擬合的問題
    lr_mult: 2
    decay_mult: 0
  }
  convolution_param {
# 卷積核的個數
    num_output: 64
    pad: 1
# 卷積核的大小,如果卷積核不是正方形的,可以使用kernel_h和kernel_w分別設定
    kernel_size: 3
# 卷積核的步長stride,默認爲1
# 權值初始化
    weight_filler {
      type: "xavier"
    }
# 偏置項的初始化
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
# Relu層
layer {
  name: "relu1_1"
  type: "ReLU"
  bottom: "conv1_1"
  top: "conv1_1"
}
......
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1_2"
  top: "pool1"
  pooling_param {
# pooling類型,默認值爲MAX,也可以設置爲AVE,STOCHASTIC
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}

常見的層的類型:

卷積層:Convolution
數據層:Data
池化層:Pooling
ReLU層:Relu
Dropout層:Dropout
全連接層:InnerProduct
局部相應歸一化層:lrn_param
SoftMax層:SoftmaxWithLoss
Accuracy層:Accuracy
也可自己定義層

deploy.prototxt

測試時用的文件。

xxx.caffemodel vs xxx.solversate

train.prototxt與deploy.prototxt的區別,參看:https://blog.csdn.net/fx409494616/article/details/53008971

caffemodel文件保存了最終訓練的模型權值,solversate保存了訓練狀態。

 

caffe下各層概念(主要層)

Convolution卷積層

lr_mult: 學習率的係數,最終的學習率是這個數乘以solver.prototxt配置文件中的base_lr。如果有兩個lr_mult, 則第一個表示權值的學習率,第二個表示偏置項的學習率。一般偏置項的學習率是權值學習率的兩倍。

num_output: 卷積核(filter)的個數

kernel_size: 卷積核的大小。如果卷積核的長和寬不等,需要用kernel_h和kernel_w分別設定

stride: 卷積核的步長,默認爲1。也可以用stride_h和stride_w來設置。

pad: 擴充邊緣,默認爲0,不擴充。 擴充的時候是左右、上下對稱的,比如卷積核的大小爲5*5,那麼pad設置爲2,則四個邊緣都擴充2個像素,即寬度和高度都擴充了4個像素,這樣卷積運算之後的特徵圖就不會變小。也可以通過pad_h和pad_w來分別設定。

weight_filler: 權值初始化。 默認爲“constant",值全爲0,很多時候我們用"xavier"算法來進行初始化,也可以設置爲”gaussian"

bias_filler: 偏置項的初始化。一般設置爲"constant",值全爲0。

group: 分組,默認爲1組。如果大於1,我們限制卷積的連接操作在一個子集內。如果我們根據圖像的通道來分組,那麼第i個輸出分組只能與第i個輸入分組進行連接。

FLOPS的計算:輸入規模(上層輸出邊長乘積)*(kernel_size^2+bias_filler)*num_output(這裏pad=0)

4Pooling層

也叫池化層,爲了減少運算量和數據維度而設置的一種層。

層類型:Pooling

必須設置的參數:

     kernel_size: 池化的核大小。也可以用kernel_h和kernel_w分別設定。

其它參數:

pool: 池化方法,默認爲MAX。目前可用的方法有MAX, AVE, 或STOCHASTIC

    pad: 和卷積層的pad的一樣,進行邊緣擴充。默認爲0

     stride: 池化的步長,默認爲1。一般我們設置爲2,即不重疊。也可以用stride_h和stride_w來設置。

FLOPS的計算:(kernel_size^2+1)*num_output*輸出規模(下層輸入邊長乘積)

全連接層

實際上,全連接層也可以被視爲是一種極端情況的卷積層,其卷積核尺寸就是輸入矩陣尺寸,因此輸出矩陣的高度和寬度尺寸都是1。

FLOPS的計算:(kernel_size^2+1)*num_output*輸出規模(下層輸入邊長乘積) 
參考:https://blog.csdn.net/a554142589/article/details/81184172

 

caffe命令行解析

參考:https://www.cnblogs.com/denny402/p/5076285.html (有空看一下這個系列博客)

caffe的運行提供三種接口:c++接口(命令行)、python接口和matlab接口。本文先對命令行進行解析,後續會依次介紹其它兩個接口。

caffe的c++主程序(caffe.cpp)放在根目錄下的tools文件夾內, 當然還有一些其它的功能文件,如:convert_imageset.cpp, train_net.cpp, test_net.cpp等也放在這個文件夾內。經過編譯後,這些文件都被編譯成了可執行文件,放在了 ./build/tools/ 文件夾內。因此我們要執行caffe程序,都需要加 ./build/tools/ 前綴。

./build/tools/caffe <command> <args>

其中的<command>有這樣四種:

  • train----訓練或finetune模型(model),
  • test-----測試模型
  • device_query---顯示gpu信息
./build/tools/caffe device_query -gpu 0
  • time-----顯示程序執行時間

train指令

  • -solver              必選參數,模型的配置文件
./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt
  • -gpu                  指定用哪一塊gpu運行,根據gpu的id進行選擇,如果設置爲'-gpu all'則使用所有的gpu運行。
./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt -gpu 2
  • -snapshot         從快照(snapshot)中恢復訓練。可以在solver配置文件設置快照,保存solverstate。
./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt -snapshot examples/mnist/lenet_iter_5000.solverstate
  • -weights           預先訓練好的權重來fine-tuning模型,需要一個caffemodel,不能和-snapshot同時使用。
./build/tools/caffe train -solver examples/finetuning_on_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel
  • -iteration           迭代次數,默認爲50。 如果在配置文件文件中沒有設定迭代次數,則默認迭代50次。
  • -model              定義在protocol buffer文件中的模型。也可以在solver配置文件中指定。
  • -sighup_effect   用來設定當程序發生掛起事件時,執行的操作,可以設置爲snapshot, stop或none, 默認爲snapshot
  • -sigint_effect   用來設定當程序發生鍵盤中止事件時(ctrl+c), 執行的操作,可以設置爲snapshot, stop或none, 默認爲stop

test指令

利用訓練好了的權重(-weight),輸入到測試模型中(-model),用編號爲0的gpu(-gpu)測試100次(-iteration)。

./build/tools/caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 100

time指令

在屏幕上顯示lenet模型迭代10次所使用的時間。包括每次迭代的forward和backward所用的時間,也包括每層forward和backward所用的平均時間。

./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -iterations 10

在屏幕上顯示lenet模型用gpu迭代50次所使用的時間。

./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 10

利用給定的權重,利用第一塊gpu,迭代10次lenet模型所用的時間。

./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 10

報錯信息

 

https://blog.csdn.net/xunan003/article/details/78429189

https://blog.csdn.net/u012841667/article/details/53316948

https://www.cnblogs.com/xiangfeidemengzhu/p/7058391.html

https://blog.csdn.net/hjxu2016/article/details/83866827

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章