根据 *_train_test.prototxt文件生成 *_deploy.prototxt文件
根據(jù) *_train_test.prototxt文件生成 *_deploy.prototxt文件
發(fā)表于2016/8/6 19:43:11 ?1218人閱讀
本文參考博文
(1)介紹?*_train_test.prototxt文件與 *_deploy.prototxt文件的不同:http://blog.csdn.net/sunshine_in_moon/article/details/49472901????
(2)生成deploy文件的Python代碼:http://www.cnblogs.com/denny402/p/5685818.html???????
*_train_test.prototxt文件
這是訓(xùn)練與測試網(wǎng)絡(luò)配置文件
*_deploy.prototxt文件 這是模型構(gòu)造文件
在博文http://www.cnblogs.com/denny402/p/5685818.html?????中給出了生成 deploy.prototxt文件的Python源代碼,但是每個網(wǎng)絡(luò)不同,修改起來比較麻煩,下面給出該博文中以mnist為例生成deploy文件的源代碼,可根據(jù)自己網(wǎng)絡(luò)的設(shè)置做出相應(yīng)修改:(下方代碼未測試)
用代碼生成deploy文件還是比較麻煩。我們在構(gòu)建深度學(xué)習(xí)網(wǎng)絡(luò)時,肯定會先定義好訓(xùn)練與測試網(wǎng)絡(luò)的配置文件——*_train_test.prototxt文件,我們可以通過修改*_train_test.prototxt文件 來生成 deploy 文件。以cifar10為例先簡單介紹一下兩者的區(qū)別。
(1)deploy 文件中的數(shù)據(jù)層更為簡單,即將*_train_test.prototxt文件中的輸入訓(xùn)練數(shù)據(jù)lmdb與輸入測試數(shù)據(jù)lmdb這兩層刪除,取而代之的是,
layer {name: "data"type: "Input"top: "data"input_param { shape: { dim: 1 dim: 3 dim: 32 dim: 32 } } }注:shape: { dim: 1 dim: 3 dim: 32 dim: 32 }代表含義:
shape {dim: 1 #num,對待識別樣本進(jìn)行數(shù)據(jù)增廣的數(shù)量,可自行定義。一般會進(jìn)行5次crop,之后分別flip。如果該值為10則表示一個樣本會變成10個,之后輸入到網(wǎng)絡(luò)進(jìn)行識別。如果不進(jìn)行數(shù)據(jù)增廣,可以設(shè)置成1dim: 3 #通道數(shù),表示RGB三個通道dim: 32 #圖像的長和寬,通過 *_train_test.prototxt文件中數(shù)據(jù)輸入層的crop_size獲取dim: 32
(2)卷積層和全連接層中weight_filler{}與bias_filler{}兩個參數(shù)不用再填寫,因為這兩個參數(shù)的值,由已經(jīng)訓(xùn)練好的模型*.caffemodel文件提供。如下所示代碼,將*_train_test.prototxt文件中的weight_filler、bias_filler全部刪除。
layer {????????????????????????????? # weight_filler、bias_filler刪除
? name: "ip2"
? type: "InnerProduct"
? bottom: "ip1"
? top: "ip2"
? param {
??? lr_mult: 1?? #權(quán)重w的學(xué)習(xí)率倍數(shù)
? }
? param {
??? lr_mult: 2??? #偏置b的學(xué)習(xí)率倍數(shù)
? }
? inner_product_param {
??? num_output: 10
??? weight_filler {
????? type: "gaussian"
????? std: 0.1
??? }
??? bias_filler {
????? type: "constant"
??? }
? }
}
刪除后變?yōu)?/p>
(3)輸出層的變化?? ???? 1)沒有了test模塊測試精度?,將該層刪除????? ?????2)輸出層
1)*_deploy.prototxt文件的構(gòu)造和*_train_test.prototxt文件的構(gòu)造最為明顯的不同點是,deploy文件沒有test網(wǎng)絡(luò)中的test模塊,只有訓(xùn)練模塊,即將*_train_test.prototxt中最后部分的test模塊測試精度刪除,即將如下代碼刪除。
layer { #刪除該層name: "accuracy"type: "Accuracy"bottom: "ip2"bottom: "label"top: "accuracy"include {phase: TEST} }
2) 輸出層?
*_train_test.prototxt文件
*_deploy.prototxt文件
layer {name: "prob"type: "Softmax"bottom: "ip2"top: "prob" }
注意在兩個文件中輸出層的類型都發(fā)生了變化一個是SoftmaxWithLoss,另一個是Softmax。另外為了方便區(qū)分訓(xùn)練與應(yīng)用輸出,訓(xùn)練是輸出時是loss,應(yīng)用時是prob。
下面給出CIFAR10中的配置文件cifar10_quick_train_test.prototxt與其模型構(gòu)造文件? cifar10_quick.prototxt 直觀展示兩者的區(qū)別。
cifar10_quick_train_test.prototxt文件代碼
name: "CIFAR10_quick"
layer {?????????????? #該層去掉
? name: "cifar"
? type: "Data"
? top: "data"
? top: "label"
? include {
??? phase: TRAIN
? }
? transform_param {
??? mean_file: "examples/cifar10/mean.binaryproto"
? }
? data_param {
??? source: "examples/cifar10/cifar10_train_lmdb"
??? batch_size: 100
??? backend: LMDB
? }
}
layer {???????????? #該層去掉
? name: "cifar"
? type: "Data"
? top: "data"
? top: "label"
? include {
??? phase: TEST
? }
? transform_param {
??? mean_file: "examples/cifar10/mean.binaryproto"
? }
? data_param {
??? source: "examples/cifar10/cifar10_test_lmdb"
??? batch_size: 100
??? backend: LMDB
? }
}
layer {??????????????????????? #將下方的weight_filler、bias_filler全部刪除
? name: "conv1"
? type: "Convolution"
? bottom: "data"
? top: "conv1"
? param {
??? lr_mult: 1
? }
? param {
??? lr_mult: 2
? }
? convolution_param {
??? num_output: 32
??? pad: 2
??? kernel_size: 5
??? stride: 1
??? weight_filler {
????? type: "gaussian"
????? std: 0.0001
??? }
??? bias_filler {
????? type: "constant"
??? }
? }
}
layer {
? name: "pool1"
? type: "Pooling"
? bottom: "conv1"
? top: "pool1"
? pooling_param {
??? pool: MAX
??? kernel_size: 3
??? stride: 2
? }
}
layer {
? name: "relu1"
? type: "ReLU"
? bottom: "pool1"
? top: "pool1"
}
layer {???????????????????????? #weight_filler、bias_filler刪除
? name: "conv2"
? type: "Convolution"
? bottom: "pool1"
? top: "conv2"
? param {
??? lr_mult: 1
? }
? param {
??? lr_mult: 2
? }
? convolution_param {
??? num_output: 32
??? pad: 2
??? kernel_size: 5
??? stride: 1
??? weight_filler {
????? type: "gaussian"
????? std: 0.01
??? }
??? bias_filler {
????? type: "constant"
??? }
? }
}
layer {
? name: "relu2"
? type: "ReLU"
? bottom: "conv2"
? top: "conv2"
}
layer {
? name: "pool2"
? type: "Pooling"
? bottom: "conv2"
? top: "pool2"
? pooling_param {
??? pool: AVE
??? kernel_size: 3
??? stride: 2
? }
}
layer {???????????????????????? #weight_filler、bias_filler刪除
? name: "conv3"
? type: "Convolution"
? bottom: "pool2"
? top: "conv3"
? param {
??? lr_mult: 1
? }
? param {
??? lr_mult: 2
? }
? convolution_param {
??? num_output: 64
??? pad: 2
??? kernel_size: 5
??? stride: 1
??? weight_filler {
????? type: "gaussian"
????? std: 0.01
??? }
??? bias_filler {
????? type: "constant"
??? }
? }
}
layer {
? name: "relu3"
? type: "ReLU"
? bottom: "conv3"
? top: "conv3"
}
layer {
? name: "pool3"
? type: "Pooling"
? bottom: "conv3"
? top: "pool3"
? pooling_param {
??? pool: AVE
??? kernel_size: 3
??? stride: 2
? }
}
layer {?????????????????????? #weight_filler、bias_filler刪除
? name: "ip1"
? type: "InnerProduct"
? bottom: "pool3"
? top: "ip1"
? param {
??? lr_mult: 1
? }
? param {
??? lr_mult: 2
? }
? inner_product_param {
??? num_output: 64
??? weight_filler {
????? type: "gaussian"
????? std: 0.1
??? }
??? bias_filler {
????? type: "constant"
??? }
? }
}
layer {????????????????????????????? # weight_filler、bias_filler刪除
? name: "ip2"
? type: "InnerProduct"
? bottom: "ip1"
? top: "ip2"
? param {
??? lr_mult: 1
? }
? param {
??? lr_mult: 2
? }
? inner_product_param {
??? num_output: 10
??? weight_filler {
????? type: "gaussian"
????? std: 0.1
??? }
??? bias_filler {
????? type: "constant"
??? }
? }
}
layer {????????????????????????????????? #將該層刪除
? name: "accuracy"
? type: "Accuracy"
? bottom: "ip2"
? bottom: "label"
? top: "accuracy"
? include {
??? phase: TEST
? }
}
layer {???????????????????????????????? #修改
? name: "loss"?????? #---loss? 修改為? prob
? type: "SoftmaxWithLoss"??????????? ?# SoftmaxWithLoss 修改為 softmax
? bottom: "ip2"
? bottom: "label"????????? #去掉
? top: "loss"
}
以下為cifar10_quick.prototxt
layer {?????????????? #將兩個輸入層修改為該層
? name: "data"
? type: "Input"
? top: "data"
? input_param { shape: { dim: 1 dim: 3 dim: 32 dim: 32 } }???? #注意shape中變量值的修改,CIFAR10中的 *_train_test.protxt文件中沒有 crop_size
}
layer {
? name: "conv1"
? type: "Convolution"
? bottom: "data"
? top: "conv1"
? param {
??? lr_mult: 1?? #權(quán)重W的學(xué)習(xí)率倍數(shù)
}
? param {
??? lr_mult: 2?? #偏置b的學(xué)習(xí)率倍數(shù)
? }
? convolution_param {
??? num_output: 32
??? pad: 2???#加邊為2
?? kernel_size: 5
??? stride: 1
? }
}
layer {
? name: "pool1"
? type: "Pooling"
? bottom: "conv1"
? top: "pool1"
? pooling_param {
??? pool: MAX????#Max Pooling
?? kernel_size: 3
??? stride: 2
? }
}
layer {
? name: "relu1"
? type: "ReLU"
? bottom: "pool1"
? top: "pool1"
}
layer {
? name: "conv2"
? type: "Convolution"
? bottom: "pool1"
? top: "conv2"
? param {
??? lr_mult: 1
? }
? param {
??? lr_mult: 2
? }
? convolution_param {
??? num_output: 32
??? pad: 2
??? kernel_size: 5
??? stride: 1
? }
}
layer {
? name: "relu2"
? type: "ReLU"
? bottom: "conv2"
? top: "conv2"
}
layer {
? name: "pool2"
? type: "Pooling"
? bottom: "conv2"
? top: "pool2"
? pooling_param {
??? pool: AVE?? #均值池化
??? kernel_size: 3
??? stride: 2
? }
}
layer {
? name: "conv3"
? type: "Convolution"
? bottom: "pool2"
? top: "conv3"
? param {
??? lr_mult: 1
? }
? param {
??? lr_mult: 2
? }
? convolution_param {
??? num_output: 64
??? pad: 2
??? kernel_size: 5
??? stride: 1
? }
}
layer {
? name: "relu3"
? type: "ReLU"? #使用ReLU激勵函數(shù),這里需要注意的是,本層的bottom和top都是conv3>
? bottom: "conv3"
? top: "conv3"
}
layer {
? name: "pool3"
? type: "Pooling"
? bottom: "conv3"
? top: "pool3"
? pooling_param {
??? pool: AVE
kernel_size: 3
??? stride: 2
? }
}
layer {
? name: "ip1"
? type: "InnerProduct"
? bottom: "pool3"
? top: "ip1"
? param {
??? lr_mult: 1
? }
? param {
??? lr_mult: 2
? }
? inner_product_param {
??? num_output: 64
? }
}
layer {
? name: "ip2"
? type: "InnerProduct"
? bottom: "ip1"
? top: "ip2"
? param {
??? lr_mult: 1
? }
? param {
??? lr_mult: 2
? }
? inner_product_param {
??? num_output: 10
? }
}
layer {
? name: "prob"
? type: "Softmax"
? bottom: "ip2"
? top: "prob"
}
總結(jié)
以上是生活随笔為你收集整理的根据 *_train_test.prototxt文件生成 *_deploy.prototxt文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4'.deploy.prototxt
- 下一篇: 孙剑亲自撰文:我在 Face++ 的这半