1. 程式人生 > >淺談caffe中train_val.prototxt和deploy.prototxt文件的區別

淺談caffe中train_val.prototxt和deploy.prototxt文件的區別

image pixel proto form 準確率 .proto 應用 網絡 基礎

本文以CaffeNet為例:
1. train_val.prototxt 首先,train_val.prototxt文件是網絡配置文件。該文件是在訓練的時候用的。 2.deploy.prototxt 該文件是在測試時使用的文件。
區別: 首先deploy.prototxt文件都是在train_val.prototxt文件的基礎上刪除了一些東西,所形成的。 由於兩個文件的性質,train_val.prototxt文件裏面訓練的部分都會在deploy.prototxt文件中刪除。
在train_val.prototxt文件中,開頭要加入一下訓練設置文件和準備文件。例如,transform_param中的mirror: true(開啟鏡像);crop_size: ***(圖像尺寸);mean_file: ""(求解均值的文件),還有data_param中的source:""(處理過得數據訓練集文件);batch_size: ***(訓練圖片每批次輸入圖片的數量);backend: LMDB(數據格式設置)。 然後接下來,訓練的時候還有一個測試的設置,測試和訓練模式的設置通過一個include{phase: TEST/TRAIN}來設置。接下來就是要設置TEST模塊內容。然後其他設置跟上面一樣,裏面有個batch_size可以調小一點,因為測試的話不需要特別多的圖片數量。 而以上這一塊的內容在deploy裏表現出來的只有一個數據層的設置。只需設置name,type,top,input_param這些即可。 接下來,第一個卷積層的設置,train_val.prototxt文件中多了param(反向傳播學習率的設置),這裏需要設置兩個param一個時weight的學習率,一個時bias的學習率,其中一般bias的學習率是weight學習率的兩倍。然後就是設置convolution_param,但是在train_val裏面需要有對weight_filler的初始化和對bias_filler的初始化。 然後就是設置激活激活函數。這一塊由於沒有初始化,所以兩個文件都是一樣的。 再接下來就是池化層,由於池化就是降低分辨率,所以這兩邊是一樣的,只需要設置kernel_size,stride,pool即可。無需參數的初始化。 再下來時LRN層,該層的全稱是Local Response Normalization(局部響應值歸一化),該層的作用就是對局部輸入進行一個歸一化操作,不過現在有論文表明,這一層加不加對結果影響不是很大。但這一層的定義都是相同的。 再接下來就是"conv2"、"relu2"、"pool2"、"LRN2"這樣的循環,具體跟之前說的一樣,train_val主要多的就是參數的初始化和學習率的設置。 在第五個卷積層之後,進入了"fc6"層,該層是全連接層,這裏train_val裏面還是多兩個param學習率的設置,和weight_filler、bias_filler的初始化設置,而兩者共同的是有一個輸出向量元素個數的設置:inner_product_param。 再接下來就是激活函數RELU。 再接下來就是Dropout層,該層的目的就是為了防止模型過擬合。這其中有一個dropout_ration的設置一般為0.5即可。 再接下來就是"fc7",這一層跟"fc6"相同。然後就是"relu7"、"drop7"都是相同的。然後就是"fc8"也與之前相同。 再接下來就是Accuracy,這個層是用來計算網絡輸出相對目標值的準確率,它實際上並不是一個損失層,所以沒有反傳操作。但是在caffe官網中,它在損失層這一部分。所以在deploy.prototxt文件中,這一層的定義是沒有的。 再接下來train_val的最後一個層是"SoftmaxWithLoss"層,也是簡單的定義了name,type,bottom,top就完了。而這一塊的內容也不在deploy.prototxt文件中。 而在deploy.prototxt文件中直接定義了一個type:"Softmax"。
通過對CaffeNet這兩個文件的查看發現deploy.prototxt文件和train_val.prototxt文件之間的差異在很多層裏面牽扯到訓練部分的都會被刪除,然後就是反向傳播訓練部分會被刪除。

其中,這裏面有一個區別在裏頭,就是為什麽train_val裏面的是SoftmaxWithLoss而deploy裏面的是Softmax層(兩個都是損失層,都沒有任何參數): 這裏面其實都是softmax回歸的應用,只是在定義成Softmax時直接計算了概率室友forward部分,而在SoftmaxWithLoss部分時是還有backward的部分。所以這裏就出現了區別,具體的區別可以看這兩個文件的C++定義。

下表左邊的是train_val.prototxt文件,右邊是deploy.prototxt文件。
name: "CaffeNet"
layer {
name: "data"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
mirror: true
crop_size: 227
mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
}
# mean pixel / channel-wise mean instead of mean image
# transform_param {
# crop_size: 227
# mean_value: 104
# mean_value: 117
# mean_value: 123
# mirror: true
# }
data_param {
source: "examples/imagenet/ilsvrc12_train_lmdb"
batch_size: 256
backend: LMDB
}
}
layer {
name: "data"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
transform_param {
mirror: false
crop_size: 227
mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
}
# mean pixel / channel-wise mean instead of mean image
# transform_param {
# crop_size: 227
# mean_value: 104
# mean_value: 117
# mean_value: 123
# mirror: false
# }
data_param {
source: "examples/imagenet/ilsvrc12_val_lmdb"
batch_size: 50
backend: LMDB
}
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 96
kernel_size: 11
stride: 4
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 0
}
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "conv1"
top: "conv1"
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 3
stride: 2
}
}
layer {
name: "norm1"
type: "LRN"
bottom: "pool1"
top: "norm1"
lrn_param {
local_size: 5
alpha: 0.0001
beta: 0.75
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "norm1"
top: "conv2"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 256
pad: 2
kernel_size: 5
group: 2
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 1
}
}
}
layer {
name: "relu2"
type: "ReLU"
bottom: "conv2"
top: "conv2"
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 3
stride: 2
}
}
layer {
name: "norm2"
type: "LRN"
bottom: "pool2"
top: "norm2"
lrn_param {
local_size: 5
alpha: 0.0001
beta: 0.75
}
}
layer {
name: "conv3"
type: "Convolution"
bottom: "norm2"
top: "conv3"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 384
pad: 1
kernel_size: 3
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 0
}
}
}
layer {
name: "relu3"
type: "ReLU"
bottom: "conv3"
top: "conv3"
}
layer {
name: "conv4"
type: "Convolution"
bottom: "conv3"
top: "conv4"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 384
pad: 1
kernel_size: 3
group: 2
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 1
}
}
}
layer {
name: "relu4"
type: "ReLU"
bottom: "conv4"
top: "conv4"
}
layer {
name: "conv5"
type: "Convolution"
bottom: "conv4"
top: "conv5"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 256
pad: 1
kernel_size: 3
group: 2
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 1
}
}
}
layer {
name: "relu5"
type: "ReLU"
bottom: "conv5"
top: "conv5"
}
layer {
name: "pool5"
type: "Pooling"
bottom: "conv5"
top: "pool5"
pooling_param {
pool: MAX
kernel_size: 3
stride: 2
}
}
layer {
name: "fc6"
type: "InnerProduct"
bottom: "pool5"
top: "fc6"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
inner_product_param {
num_output: 4096
weight_filler {
type: "gaussian"
std: 0.005
}
bias_filler {
type: "constant"
value: 1
}
}
}
layer {
name: "relu6"
type: "ReLU"
bottom: "fc6"
top: "fc6"
}
layer {
name: "drop6"
type: "Dropout"
bottom: "fc6"
top: "fc6"
dropout_param {
dropout_ratio: 0.5
}
}
layer {
name: "fc7"
type: "InnerProduct"
bottom: "fc6"
top: "fc7"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
inner_product_param {
num_output: 4096
weight_filler {
type: "gaussian"
std: 0.005
}
bias_filler {
type: "constant"
value: 1
}
}
}
layer {
name: "relu7"
type: "ReLU"
bottom: "fc7"
top: "fc7"
}
layer {
name: "drop7"
type: "Dropout"
bottom: "fc7"
top: "fc7"
dropout_param {
dropout_ratio: 0.5
}
}
layer {
name: "fc8"
type: "InnerProduct"
bottom: "fc7"
top: "fc8"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
inner_product_param {
num_output: 1000
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 0
}
}
}
layer {
name: "accuracy"
type: "Accuracy"
bottom: "fc8"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "fc8"
bottom: "label"
top: "loss"
}
name: "CaffeNet"
layer {
name: "data"
type: "Input"
top: "data"
input_param { shape: { dim: 10 dim: 3 dim: 227 dim: 227 } }
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
convolution_param {
num_output: 96
kernel_size: 11
stride: 4
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "conv1"
top: "conv1"
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 3
stride: 2
}
}
layer {
name: "norm1"
type: "LRN"
bottom: "pool1"
top: "norm1"
lrn_param {
local_size: 5
alpha: 0.0001
beta: 0.75
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "norm1"
top: "conv2"
convolution_param {
num_output: 256
pad: 2
kernel_size: 5
group: 2
}
}
layer {
name: "relu2"
type: "ReLU"
bottom: "conv2"
top: "conv2"
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 3
stride: 2
}
}
layer {
name: "norm2"
type: "LRN"
bottom: "pool2"
top: "norm2"
lrn_param {
local_size: 5
alpha: 0.0001
beta: 0.75
}
}
layer {
name: "conv3"
type: "Convolution"
bottom: "norm2"
top: "conv3"
convolution_param {
num_output: 384
pad: 1
kernel_size: 3
}
}
layer {
name: "relu3"
type: "ReLU"
bottom: "conv3"
top: "conv3"
}
layer {
name: "conv4"
type: "Convolution"
bottom: "conv3"
top: "conv4"
convolution_param {
num_output: 384
pad: 1
kernel_size: 3
group: 2
}
}
layer {
name: "relu4"
type: "ReLU"
bottom: "conv4"
top: "conv4"
}
layer {
name: "conv5"
type: "Convolution"
bottom: "conv4"
top: "conv5"
convolution_param {
num_output: 256
pad: 1
kernel_size: 3
group: 2
}
}
layer {
name: "relu5"
type: "ReLU"
bottom: "conv5"
top: "conv5"
}
layer {
name: "pool5"
type: "Pooling"
bottom: "conv5"
top: "pool5"
pooling_param {
pool: MAX
kernel_size: 3
stride: 2
}
}
layer {
name: "fc6"
type: "InnerProduct"
bottom: "pool5"
top: "fc6"
inner_product_param {
num_output: 4096
}
}
layer {
name: "relu6"
type: "ReLU"
bottom: "fc6"
top: "fc6"
}
layer {
name: "drop6"
type: "Dropout"
bottom: "fc6"
top: "fc6"
dropout_param {
dropout_ratio: 0.5
}
}
layer {
name: "fc7"
type: "InnerProduct"
bottom: "fc6"
top: "fc7"
inner_product_param {
num_output: 4096
}
}
layer {
name: "relu7"
type: "ReLU"
bottom: "fc7"
top: "fc7"
}
layer {
name: "drop7"
type: "Dropout"
bottom: "fc7"
top: "fc7"
dropout_param {
dropout_ratio: 0.5
}
}
layer {
name: "fc8"
type: "InnerProduct"
bottom: "fc7"
top: "fc8"
inner_product_param {
num_output: 1000
}
}
layer {
name: "prob"
type: "Softmax"
bottom: "fc8"
top: "prob"
}

淺談caffe中train_val.prototxt和deploy.prototxt文件的區別