怎么在Caffe中配置每一个层的结构
最近剛在電腦上裝好Caffe,由于神經(jīng)網(wǎng)絡(luò)中有不同的層結(jié)構(gòu),不同類型的層又有不同的參數(shù),所有就根據(jù)Caffe官網(wǎng)的說(shuō)明文檔做了一個(gè)簡(jiǎn)單的總結(jié)。
1. Vision Layers
1.1 卷積層(Convolution)
類型:CONVOLUTION
例子
layers {name: "conv1"type: CONVOLUTIONbottom: "data"top: "conv1"blobs_lr: 1 # learning rate multiplier for the filtersblobs_lr: 2 # learning rate multiplier for the biasesweight_decay: 1 # weight decay multiplier for the filtersweight_decay: 0 # weight decay multiplier for the biasesconvolution_param {num_output: 96 # learn 96 filterskernel_size: 11 # each filter is 11x11stride: 4 # step 4 pixels between each filter applicationweight_filler {type: "gaussian" # initialize the filters from a Gaussianstd: 0.01 # distribution with stdev 0.01 (default mean: 0)}bias_filler {type: "constant" # initialize the biases to zero (0)value: 0}} }
blobs_lr:?學(xué)習(xí)率調(diào)整的參數(shù),在上面的例子中設(shè)置權(quán)重學(xué)習(xí)率和運(yùn)行中求解器給出的學(xué)習(xí)率一樣,同時(shí)是偏置學(xué)習(xí)率為權(quán)重的兩倍。?
weight_decay:
?
卷積層的重要參數(shù)
必須參數(shù):
num_output (c_o):過(guò)濾器的個(gè)數(shù)
kernel_size (or kernel_h and kernel_w):過(guò)濾器的大小
?
可選參數(shù):
weight_filler [default type: 'constant' value: 0]:參數(shù)的初始化方法
bias_filler:偏置的初始化方法
bias_term [default true]:指定是否是否開(kāi)啟偏置項(xiàng)
pad (or pad_h and pad_w) [default 0]:指定在輸入的每一邊加上多少個(gè)像素
stride (or stride_h and stride_w) [default 1]:指定過(guò)濾器的步長(zhǎng)
group (g) [default 1]:?If g > 1, we restrict the connectivityof each filter to a subset of the input. Specifically, the input and outputchannels are separated into g groups, and the ith output group channels will beonly connected to the ith input group channels.
?
通過(guò)卷積后的大小變化:
輸入:n * c_i * h_i * w_i
輸出:n * c_o * h_o * w_o,其中h_o = (h_i + 2 * pad_h - kernel_h) /stride_h + 1,w_o通過(guò)同樣的方法計(jì)算。
1.2 池化層(Pooling)
類型:POOLING
例子
layers {name: "pool1"type: POOLINGbottom: "conv1"top: "pool1"pooling_param {pool: MAXkernel_size: 3 # pool over a 3x3 regionstride: 2 # step two pixels (in the bottom blob) between pooling regions} }卷積層的重要參數(shù)
必需參數(shù):
kernel_size (or kernel_h and kernel_w):過(guò)濾器的大小
?
可選參數(shù):pool [default MAX]:pooling的方法,目前有MAX, AVE, 和STOCHASTIC三種方法
pad (or pad_h and pad_w) [default 0]:指定在輸入的每一遍加上多少個(gè)像素
stride (or stride_h and stride_w) [default1]:指定過(guò)濾器的步長(zhǎng)
?
通過(guò)池化后的大小變化:
輸入:n * c_i * h_i * w_i
輸出:n * c_o * h_o * w_o,其中h_o = (h_i + 2 * pad_h - kernel_h) /stride_h + 1,w_o通過(guò)同樣的方法計(jì)算。
1.3 Local Response Normalization (LRN)
類型:LRN
Local ResponseNormalization是對(duì)一個(gè)局部的輸入?yún)^(qū)域進(jìn)行的歸一化(激活a被加一個(gè)歸一化權(quán)重(分母部分)生成了新的激活b),有兩種不同的形式,一種的輸入?yún)^(qū)域?yàn)橄噜彽腸hannels(cross channel LRN),另一種是為同一個(gè)channel內(nèi)的空間區(qū)域(within channel LRN)
計(jì)算公式:對(duì)每一個(gè)輸入除以
?
可選參數(shù):
local_size [default 5]:對(duì)于cross channel LRN為需要求和的鄰近c(diǎn)hannel的數(shù)量;對(duì)于within channel LRN為需要求和的空間區(qū)域的邊長(zhǎng)
alpha [default 1]:scaling參數(shù)
beta [default 5]:指數(shù)
norm_region [default ACROSS_CHANNELS]:?選擇哪種LRN的方法ACROSS_CHANNELS 或者WITHIN_CHANNEL
2. Loss Layers
深度學(xué)習(xí)是通過(guò)最小化輸出和目標(biāo)的Loss來(lái)驅(qū)動(dòng)學(xué)習(xí)。
2.1 Softmax
類型: SOFTMAX_LOSS2.2 Sum-of-Squares / Euclidean
類型: EUCLIDEAN_LOSS
2.3 Hinge / Margin
類型: HINGE_LOSS 例子:
# L1 Norm layers {name: "loss"type: HINGE_LOSSbottom: "pred"bottom: "label" }# L2 Norm layers {name: "loss"type: HINGE_LOSSbottom: "pred"bottom: "label"top: "loss"hinge_loss_param {norm: L2} }
可選參數(shù):
norm [default L1]:?選擇L1或者 L2范數(shù)
輸入:
n * c * h * wPredictions
n * 1 * 1 * 1Labels
輸出
1 * 1 * 1 * 1Computed Loss
2.4 Sigmoid Cross-Entropy
類型:SIGMOID_CROSS_ENTROPY_LOSS2.5 Infogain
類型:INFOGAIN_LOSS2.6 Accuracy and Top-k
類型:ACCURACY?用來(lái)計(jì)算輸出和目標(biāo)的正確率,事實(shí)上這不是一個(gè)loss,而且沒(méi)有backward這一步。
3. 激勵(lì)層(Activation / Neuron Layers)
一般來(lái)說(shuō),激勵(lì)層是element-wise的操作,輸入和輸出的大小相同,一般情況下就是一個(gè)非線性函數(shù)。
3.1 ReLU / Rectified-Linear and Leaky-ReLU
類型: RELU 例子:
layers {name: "relu1"type: RELUbottom: "conv1"top: "conv1" }
可選參數(shù):
negative_slope [default 0]:指定輸入值小于零時(shí)的輸出。
?
ReLU是目前使用做多的激勵(lì)函數(shù),主要因?yàn)槠涫諗扛?#xff0c;并且能保持同樣效果。
標(biāo)準(zhǔn)的ReLU函數(shù)為max(x, 0),而一般為當(dāng)x > 0時(shí)輸出x,但x <= 0時(shí)輸出negative_slope。RELU層支持in-place計(jì)算,這意味著bottom的輸出和輸入相同以避免內(nèi)存的消耗。
3.2 Sigmoid
類型: SIGMOID 例子:
layers {name: "encode1neuron"bottom: "encode1"top: "encode1neuron"type: SIGMOID } SIGMOID 層通過(guò) sigmoid(x) 計(jì)算每一個(gè)輸入x的輸出,函數(shù)如下圖。
3.3 TanH / Hyperbolic Tangent
類型: TANH 例子:
layers {name: "encode1neuron"bottom: "encode1"top: "encode1neuron"type: SIGMOID }
TANH層通過(guò) tanh(x) 計(jì)算每一個(gè)輸入x的輸出,函數(shù)如下圖。
3.3 Absolute Value
類型: ABSVAL 例子:
3.4 Power
類型: POWER 例子:
可選參數(shù):
power [default 1]
scale [default 1]
shift [default 0]
POWER層通過(guò) (shift + scale * x) ^ power計(jì)算每一個(gè)輸入x的輸出。
3.5 BNLL
類型: BNLL 例子:
BNLL (binomial normal log likelihood) 層通過(guò) log(1 + exp(x)) 計(jì)算每一個(gè)輸入x的輸出。
4. 數(shù)據(jù)層(Data Layers)
數(shù)據(jù)通過(guò)數(shù)據(jù)層進(jìn)入Caffe,數(shù)據(jù)層在整個(gè)網(wǎng)絡(luò)的底部。數(shù)據(jù)可以來(lái)自高效的數(shù)據(jù)庫(kù)(LevelDB 或者 LMDB),直接來(lái)自內(nèi)存。如果不追求高效性,可以以HDF5或者一般圖像的格式從硬盤讀取數(shù)據(jù)。
4.1 Database
類型:DATA
必須參數(shù):
source:包含數(shù)據(jù)的目錄名稱
batch_size:一次處理的輸入的數(shù)量
?
可選參數(shù):
rand_skip:在開(kāi)始的時(shí)候從輸入中跳過(guò)這個(gè)數(shù)值,這在異步隨機(jī)梯度下降(SGD)的時(shí)候非常有用
backend [default LEVELDB]:?選擇使用 LEVELDB 或者 LMDB
4.2 In-Memory
類型: MEMORY_DATA必需參數(shù):
batch_size, channels, height, width: 指定從內(nèi)存讀取數(shù)據(jù)的大小
The memory data layer reads data directly from memory, without copying it. In order to use it, one must call MemoryDataLayer::Reset (from C++) or Net.set_input_arrays (from Python) in order to specify a source of contiguous data (as 4D row major array), which is read one batch-sized chunk at a time.
4.3 HDF5 Input
類型: HDF5_DATA必要參數(shù):
source:需要讀取的文件名
batch_size:一次處理的輸入的數(shù)量
4.4 HDF5 Output
類型: HDF5_OUTPUT必要參數(shù):
file_name: 輸出的文件名
HDF5的作用和這節(jié)中的其他的層不一樣,它是把輸入的blobs寫到硬盤
4.5 Images
類型: IMAGE_DATA必要參數(shù):
source: ?text文件的名字,每一行給出一張圖片的文件名和label
batch_size:? 一個(gè)batch中圖片的數(shù)量
可選參數(shù):
rand_skip: 在開(kāi)始的時(shí)候從輸入中跳過(guò)這個(gè)數(shù)值,這在異步隨機(jī)梯度下降(SGD)的時(shí)候非常有用
shuffle [default false]
new_height, new_width:?把所有的圖像resize到這個(gè)大小
4.6 Windows
類型:WINDOW_DATA4.7 Dummy
類型:DUMMY_DATADummy 層用于development 和debugging。具體參數(shù)DummyDataParameter。
5. 一般層(Common Layers)
5.1 全連接層Inner Product
類型:INNER_PRODUCT例子: layers {name: "fc8"type: INNER_PRODUCTblobs_lr: 1 # learning rate multiplier for the filtersblobs_lr: 2 # learning rate multiplier for the biasesweight_decay: 1 # weight decay multiplier for the filtersweight_decay: 0 # weight decay multiplier for the biasesinner_product_param {num_output: 1000weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 0}}bottom: "fc7"top: "fc8" }
必要參數(shù):
num_output?(c_o):過(guò)濾器的個(gè)數(shù)
?
可選參數(shù):
weight_filler [default type: 'constant' value: 0]:參數(shù)的初始化方法
bias_filler:偏置的初始化方法
bias_term [default true]:指定是否是否開(kāi)啟偏置項(xiàng)
?
通過(guò)全連接層后的大小變化:
輸入:n * c_i * h_i * w_i
輸出:n * c_o * 1 *1
5.2 Splitting
類型:SPLITSplitting層可以把一個(gè)輸入blob分離成多個(gè)輸出blobs。這個(gè)用在當(dāng)需要把一個(gè)blob輸入到多個(gè)輸出層的時(shí)候。
5.3 Flattening
類型:FLATTENFlattening是把一個(gè)輸入的大小為n * c * h * w變成一個(gè)簡(jiǎn)單的向量,其大小為 n * (c*h*w) * 1 * 1。
5.4 Concatenation
類型:CONCAT例子:
layers {name: "concat"bottom: "in1"bottom: "in2"top: "out"type: CONCATconcat_param {concat_dim: 1} }
可選參數(shù):
concat_dim [default 1]:0代表鏈接num,1代表鏈接channels
?
通過(guò)全連接層后的大小變化:
輸入:從1到K的每一個(gè)blob的大小n_i * c_i * h * w
輸出:
如果concat_dim = 0: (n_1 + n_2 + ... + n_K) *c_1 * h * w,需要保證所有輸入的c_i 相同。
如果concat_dim = 1: n_1 * (c_1 + c_2 + ... +c_K) * h * w,需要保證所有輸入的n_i 相同。
?
通過(guò)Concatenation層,可以把多個(gè)的blobs鏈接成一個(gè)blob。
5.5 Slicing
The SLICE layer is a utility layer that slices an input layer to multiple output layers along a given dimension (currently num or channel only) with given slice indices.5.6 Elementwise Operations
類型:ELTWISE5.7 Argmax
類型:ARGMAX5.8 Softmax
類型:SOFTMAX5.9 Mean-Variance Normalization
類型:MVN
6. 參考
Caffe總結(jié)
以上是生活随笔為你收集整理的怎么在Caffe中配置每一个层的结构的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: table1函数----一维查表
- 下一篇: java中字符串的截取