網(wǎng)絡(luò)結(jié)構(gòu)
lenet 各層網(wǎng)絡(luò)的定義在:examples/mnist/lenet_train_test.prototxt.
各層的定義:
- DATA layer
minist的數(shù)據(jù)源自lmdb文件, 數(shù)據(jù)的讀取定義在data layer
layer {
name: "mnist"
type: "Data"
transform_param {
scale: 0.00390625
}
data_param {
source: "mnist_train_lmdb"
backend: LMDB
batch_size: 64
}
top: "data"
top: "label"
}
該層的名字是"mnist", type: data, 該層從指定的lmdb文件源讀取數(shù)據(jù). batch size = 64, 將讀入的像素rescale到[0,1]范圍內(nèi), 歸一化因子等于1/256 = 0.00390625. 最終這一層會產(chǎn)生兩個blobs, 一個是data blob, 另一個是label blob.
- 第一個卷基層 CONV
layer {
name: "conv1"
type: "Convolution"
param { lr_mult: 1 }
param { lr_mult: 2 }
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
bottom: "data"
top: "conv1"
}
該層承接data blob, 產(chǎn)生conv1 層. 該層共產(chǎn)生20個channel, 每個channel的kernel size = 5, stride = 1. 在該層, 每一個filter可以隨機(jī)初始化weight和bias, 對于權(quán)重, 我們采用"xavier"算法, 根據(jù)輸入和輸出神經(jīng)元的個數(shù)來決定權(quán)重初始化的尺度. 對于bias, 我們簡單的采用固定值來初始化, 默認(rèn)值為0.
'lr_mult's 是該層可學(xué)習(xí)參數(shù)的學(xué)習(xí)率. 在這里, 我們設(shè)置權(quán)值的學(xué)習(xí)率為solver中指定的學(xué)習(xí)率, bias的學(xué)習(xí)率設(shè)置為2倍solver中指定的學(xué)習(xí)率. 這樣的設(shè)置通常會有比較好的聚合率.
- pooling layer
pooling layer的定義相對較簡單
layer {
name: "pool1"
type: "Pooling"
pooling_param {
kernel_size: 2
stride: 2
pool: MAX
}
bottom: "conv1"
top: "pool1"
}
上面的設(shè)置表示, 我們將采用max pooling, pool kernel size = 2, stride = 2 (相鄰的pooling region 不會重疊)
- 第二個conv 和pooling layer的解釋和上面的解釋大同小異.
- fully connected layer
layer {
name: "ip1"
type: "InnerProduct"
param { lr_mult: 1 }
param { lr_mult: 2 }
inner_product_param {
num_output: 500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
bottom: "pool2"
top: "ip1"
}
上面的語法定義了一個全連接層(或者叫innerProduct 層),該層具有500個輸出, 其他的選項和之前介紹的類似.
- Relu 層
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
ReLU 是對逐個元素的操作(每個輸入都要進(jìn)行relu), 因此我們可以采用"in-place"內(nèi)置操作的方式來節(jié)省內(nèi)存, 具體就是給才曾的bottom 和top blobs 相同的名字.
- ReLU之后, 另一個inner product層:
layer {
name: "ip2"
type: "InnerProduct"
param { lr_mult: 1 }
param { lr_mult: 2 }
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
bottom: "ip1"
top: "ip2"
}
- Loss Layer
終于寫到了這層
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label"
}
softmax loss 層同時實現(xiàn)了softmax 和multinomial logical loss(節(jié)省時間改善數(shù)值穩(wěn)定性). 該層接收兩個blob, 第一個是prediction,第二個是label(由第一層data layer 提供). 該層不產(chǎn)生任何輸出, 他的作用在于計算loss function value, 當(dāng) back propagation開始時report it. 同時, 開始計算相對于上一層"ip2"的梯度. (initiates the gradient with respect to 'ip2'.)
-
Additional Notes: '定義層'的規(guī)則
層的定義可以包含一些規(guī)則, 可以根據(jù)這些規(guī)則來確定是否以及如何來進(jìn)行某些特定的操作.
舉例如下:
layer {
// ...layer definition...
include: { phase: TRAIN }
}
對于這個規(guī)則, 可以根據(jù)網(wǎng)絡(luò)的狀態(tài)來判定是否將整層包含入內(nèi). 具體來講, 該層只有在訓(xùn)練階段才會被包含如網(wǎng)絡(luò). 如果將'TRAIN' 改為'TEST',則該層只會在訓(xùn)練階段被包含入網(wǎng)絡(luò)內(nèi).
默認(rèn)情況下是沒有l(wèi)ayer rules的, 一個定義好了的層會一直被包含在網(wǎng)絡(luò)里. 因此, 'lenet_train_test.prototxt' 有兩個'DATA'層(不同的'batch size'), 一個用于訓(xùn)練階段,一個用于測試階段.
同樣的, 'lenet_train_test.prototxt' 定義了一個只有在測試階段會被包含入網(wǎng)絡(luò)的'Accuracy' 層, 該層會在測試階段,每100iteration匯報一次網(wǎng)絡(luò)accuracy.
Solver的定義
# 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
Training and Testing the Model
網(wǎng)絡(luò)的訓(xùn)練相對簡單, 當(dāng)你寫好了network definition protobuf 和 solver protobuf 文件之后, 運行'train_lenet.sh'就可以了. 或者直接運行一下命令:
cd $CAFFE_ROOT
./examples/mnist/train_lenet.sh
'train_lenet.sh' 是一個簡單的腳本, 簡單解釋之: 主要用于訓(xùn)練的工具是'caffe' 以 'train' 和 solver protobuf 文件作為它的參數(shù).
當(dāng)你運行之后, 你會看到大量的信息飛過, 如:
I1203 net.cpp:66] Creating Layer conv1
I1203 net.cpp:76] conv1 <- data
I1203 net.cpp:101] conv1 -> conv1
I1203 net.cpp:116] Top shape: 20 24 24
I1203 net.cpp:127] conv1 needs backward computation.
這樣的信息是初始化信息, 它告訴你每一層的細(xì)節(jié): 鏈接情況, 輸出的尺寸, 這些信息有助于debug. 初始化之后, 網(wǎng)絡(luò)開始訓(xùn)練:
I1203 net.cpp:142] Network initialization done.
I1203 solver.cpp:36] Solver scaffolding done.
I1203 solver.cpp:44] Solving LeNet
基于solver的設(shè)置, 系統(tǒng)將在每100 iteration 打印出 training loss, 每500iteration測試網(wǎng)絡(luò). 你會看到類似的信息:
I1203 solver.cpp:204] Iteration 100, lr = 0.00992565
I1203 solver.cpp:66] Iteration 100, loss = 0.26044
...
I1203 solver.cpp:84] Testing net
I1203 solver.cpp:111] Test score #0: 0.9785
I1203 solver.cpp:111] Test score #1: 0.0606671
對于訓(xùn)練的每個iteration, 'lr' 是該次iteration的學(xué)習(xí)率, 'loss' 是訓(xùn)練的loss. 對于測試階段的輸出, score 0 是accuracy, score 1 是測試的loss function.
數(shù)分鐘后, 訓(xùn)練完成.
I1203 solver.cpp:84] Testing net
I1203 solver.cpp:111] Test score #0: 0.9897
I1203 solver.cpp:111] Test score #1: 0.0324599
I1203 solver.cpp:126] Snapshotting to lenet_iter_10000
I1203 solver.cpp:133] Snapshotting solver state to lenet_iter_10000.solverstate
I1203 solver.cpp:78] Optimization Done.
最終的模型, 會存儲為一個二進(jìn)制protobuf文件:
lenet_iter_10000
這個文件,可以作為訓(xùn)練好的模型在你的應(yīng)用中部署.
How about GPU training?
其實剛才一直使用的是GPU訓(xùn)練. 實際上, 如果你想要在CPU上訓(xùn)練, 只需要修改'lenet_solver.prototxt'的對應(yīng)行即可.
# solver mode: CPU or GPU
solver_mode: CPU
MNIST是一個比較小的數(shù)據(jù)集, 因此在GPU上進(jìn)行訓(xùn)練并沒有多少提高(由于GPU內(nèi)部通信的開支). 在更大型的數(shù)據(jù)集上采用更復(fù)雜的模型進(jìn)行訓(xùn)練時, 例如 ImageNet, 計算時間將會得到更大的提升.
如何在固定的階段降低學(xué)習(xí)率?
Look at lenet_multistep_solver.prototxt