轉載地址:
http://blog.csdn.net/lg1259156776/article/details/52550865
1:神經網絡中,我們通過最小化神經網絡來訓練網絡,所以在訓練時最後一層是損失函數層(LOSS),
在測試時我們通過準確率來評價該網絡的優劣,因此最後一層是準確率層(ACCURACY)。
但是當我們真正要使用訓練好的數據時,我們需要的是網絡給我們輸入結果,對於分類問題,我們需要獲得分類結果,如下右圖最後一層我們得到
的是概率,我們不需要訓練及測試階段的LOSS,ACCURACY層了。
下圖是能過$CAFFE_ROOT/Python/draw_net.py繪製$CAFFE_ROOT/models/caffe_reference_caffnet/train_val.prototxt , $CAFFE_ROOT/models/caffe_reference_caffnet/deploy.prototxt,分別代表訓練時與最後使用時的網絡結構。
我們一般將train與test放在同一個.prototxt中,需要在data層輸入數據的source,
而在使用時.prototxt只需要定義輸入圖片的大小通道數據參數即可,如下圖所示,分別是
$CAFFE_ROOT/models/caffe_reference_caffnet/train_val.prototxt , $CAFFE_ROOT/models/caffe_reference_caffnet/deploy.prototxt的data層
訓練時, solver.prototxt中使用的是rain_val.prototxt
./build/tools/caffe/train -solver ./models/bvlc_reference_caffenet/solver.prototxt
使用上面訓練的網絡提取特徵,使用的網絡模型是deploy.prototxt
./build/tools/extract_features.bin models/bvlc_refrence_caffenet.caffemodel models/bvlc_refrence_caffenet/deploy.prototxt
2:
*_train_test.prototxt文件:這是訓練與測試網絡配置文件
deploy.prototxt文件書寫:
注意在輸出層的類型發生了變化一個是SoftmaxWithLoss,另一個是Softmax。另外爲了方便區分訓練與應用輸出,訓練是輸出時是loss,應用時是prob。
deploy.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"
}