Caffe
caffe的單元 Blobs, layers, nets
- Blob
Blob作為caffe的存儲及通信單元狐粱,是一個要被處理的真實數據(i.e. image batches, model parameters, derivatives for optimization)的封裝。
Blob存儲圖像batch的方式為 (數目N)×(通道K)×(高H)×(寬W)檀训,以行優(yōu)先方式存儲怒允,也就是說埂软,`(n,k,h,w)`物理地址為`((n*K+k)*H+h)*W+w`.
起初, CAFFE 只支持 4-D
blob 和 2-D
卷積(NxCxHxW),現在支持 n-D
blobs 和 (n-2)-D
卷積纫事。
- Layer
- Data layer
- Image Data
- Database
- HDF Input
- HDF Output
- Input
- Window Data
- Memory Data
- Dummy Data
- Python
- Vision layer
-
Convolution
layer { name: "conv1" type: "Convolution" bottom: "data" top: "conv1" # learning rate and decay multipliers for the filters param { lr_mult: 1 decay_mult: 1 } # learning rate and decay multipliers for the biases param { lr_mult: 2 decay_mult: 0 } convolution_param { num_output: 96 # learn 96 filters kernel_size: 11 # each filter is 11x11 stride: 4 # step 4 pixels between each filter application pad: 0 # pad up pixels weight_filler { type: "gaussian" # initialize the filters from a Gaussian std: 0.01 # distribution with stdev 0.01 (default mean: 0) } bias_filler { type: "constant" # initialize the biases to zero (0) value: 0 } } }
-
weight_filter type: caffe 中支持的初始化filter類型有
constant, gaussian, positive_unitball, xavier, msra, bilinear, uniform  
默認類型為 constant, 更詳細的介紹見 include/caffe/filter.hpp
-
-
Pooling
layer { name: "pool1" type: "Pooling" bottom: "conv1" top: "pool1" pooling_param { pool: MAX kernel_size: 3 # pool over a 3x3 region stride: 2 # step two pixels (in the bottom blob) between pooling regions } }
Spatial Pyramid Pooling(SPP)
Local Response Normalization(LRN)
-
Crop
layer { bottom: "A" bottom: "B" top: "C" name: "crop_u1u2" type: "Crop" }
CROP層用于裁剪數據勘畔。假設A、B層size分別為(20,50,512,512),(20,10,256,256)丽惶,則輸出層C的size是(20,10,256,256), 更詳細解釋見crop.
Im2col
-
Deconvolution layer(transpose convolution)
same as Convolution layer
- Recurrent Layers
Recurrent
RNN
Long-Short Term Memory(LSTM)
- Common Layers
Inner Product - fully connected layer
Dropout
Embed
- Normalization Layers
Local Response Normalization(LRN)
Mean Variance Normalization(MVN)
Batch Normalization
- Activation Layers
ReLU and Leaky-ReLU
PReLU
ELU
Sigmoid
TanH
Absolute Value
Power- f(x)=(shift+scale*x)^power
Exp- f(x)=base^(shift+scale*x)
Log- f(x)=log(x)
BNLL- f(x)=log(1+exp(x))
Threshold
Bias
Scale
- Utility Layers
Flatter
Reshape
Batch Reindex
Split
-
Concat
layer { bottom: "A" bottom: "B" top: "C" name: "concat_AB_C" type: "Concat" concat_param { axis: 1 } }
假設 A炫七、B 的 size 分別為 (n1, c1, h, w), (n2, c2, h, w),如果 axis=0, 則 C 的 size 為(n1+n2, c1, h, w) 且要求 c1=c2; 如果 axis=1, 則 C 的 size 為 (n1, c1+c2, h, 2) 且要求 n1=n2.
Slicing
-
Eltwise
適用于殘差學習(Residual Learning)钾唬,實現
f(x) + x
layer { bottom: "conv10" bottom: "conv11" top: "Res" name: "Res" type: "Eltwise" eltwise_param { op: SUM coeff: 1 coeff: -1 } }
Filter/Mask
Parameter
Reduction
Silence
ArgMax
Softmax
Python-allows custom Python layers
- Loss Layers
Multinomial Logistic Loss
Infogain Loss
-
Softmax with loss
layer{ name: "loss" type: "SoftmaxWithLoss" bottom: "pred" bottom: "label" top: "loss" }
Euclidean
Hinge/Margin
-
Sigmoid Cross-Entropy Loss
layer{ name: "loss" type: "SigmoidCrossEntropyLoss" bottom: "pred" bottom: "label" top: "loss" }
Accuracy/Top-k layer
Contrastive Loss
Multiple loss layers
事實上万哪,一個網絡可以包含很多 loss function, 只要它是一個 DAG (directed acyclic graph)(Caffe net本身可以是任何結構的DAG,不一定是線性結構)抡秆。 例如:
layers {
name: "recon-loss"
type: "Euclidean"
bottom: "reconstructions"
bottom: "data"
top: "recon-loss"
}
layers {
name: "class-loss"
type: "softmaxWithLoss"
bottom: "class-preds"
bottom: "class-labels"
top: "class-loss"
loss_weight: 100.0
}
表示的 Loss function 就是:
[圖片上傳失敗...(image-f7d5fd-1515046033856)]
任何 layer 都可以產生 loss
[圖片上傳失敗...(image-ba13f5-1515046033856)]
- Net
name: "LogReg"
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
data_param {
source: "input_leveldb"
batch_size: 64
}
}
layer {
name: "ip"
type: "InnerProduct"
bottom: "data"
top: "ip"
inner_product_param {
num_output: 2
}
}
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip"
bottom: "label"
top: "loss"
}
- Net visualization
~/caffe/python/draw_net.py yout_net.prototxt yoursave.png
- Solver
solver_type: SGD
[圖片上傳失敗...(image-6c3951-1515046033856)]
solver_type: NESTEROV
[圖片上傳失敗...(image-e8f5cd-1515046033856)]
solver_type: ADAGRAD
[圖片上傳失敗...(image-58b7b4-1515046033856)]
[圖片上傳失敗...(image-d1791d-1515046033856)]
- Weight sharing
[圖片上傳失敗...(image-d0591c-1515046033856)]
caffe 使用問題集錦
-
Q1 ? 訓練過程中人為中斷訓練(ctrl+C)奕巍,可否從中斷時刻繼續(xù)訓練?
Answer: ?可以琅轧。
caffe train -solver solver.prototxt -snapshot train_1000.solverstate
以上示例表示從第1000步迭代繼續(xù)訓練伍绳。Training-and-Resuming
-
Q2 ? 如何可視化卷積層踊挠?
- 使用 python 接口可視化卷積層以及做相關的 test ( [jupyter](http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb) ). 不過需要預裝 Python 的一些包,如 numpy, scikit-learn等效床,才能正常 `import caffe`.
sudo apt-get install python-numpy python-matplotlib python-sklearn python-scipy python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags Cython ipython
sudo apt-get update
參考 [caffe examples](http://nbviewer.jupyter.org/github/BVLC/caffe/tree/master/examples/), 我們把 .ipynb 文件轉換為 .py 文件并用 ipyhon 執(zhí)行睹酌, 如果用 python 執(zhí)行會出現 [錯誤](https://stackoverflow.com/questions/32538758/nameerror-name-get-ipython-is-not-defined)。
jupyter nbconvert --to script '00-classfication.ipynb'
ipython 00-classfication.py
但用ipython仍然遇到了一個 ImportError 的錯誤
[圖片上傳失敗...(image-82cc64-1515046033856)]
[mnist](http://caffe.berkeleyvision.org/gathered/examples/mnist.html)
-
Q3 ? 什么樣的 layer 才能它的 bottom 和 top 可以是相同的名稱剩檀?
Answer: ?目前只有 Relu 層它的上下層可以使用相同名稱憋沿,因為它是 element-wise 的,所以可以使用 in-place 的操作以節(jié)省內存沪猴。 (具體)
------------------------------------
caffe matlab接口使用
讀取訓練好的net參數
caffe.reset_all();
clear; close all;
% settings
model = '*.prototxt'
weights = '*.caffemodel'
% load model using mat_caffe
net = caffe.Net(model, weights, 'test');
在界面中可以看到辐啄,net 含有以下參數
[圖片上傳失敗...(image-dec443-1515046033856)]
BatchNorm 層的三個blob含義分別為 MEAN, VARIANCE, MOVING AVERAGE FACTOR
caffe python 接口使用
首先我們定義一個簡單的卷積網絡
name: "myconvnet"
input: "data"
input_dim: 1
input_dim: 1
input_dim: 256
input_dim: 256
layer {
name: "conv"
type: "Convolution"
bottom: "data"
top: "conv"
convolution_param {
num_output: 10
kernel_size: 3
stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 0
}
}
}
為了保證自己定義的網絡各層之間的連接沒有問題,我們可以將它可視化來檢查網絡运嗜,看它是什么樣子的壶辜。 這需要安裝一些依賴的包
$ pip insall pydot
$ sudo apt-get install graphviz libgraphviz-dev
$ pip install pygraphviz
然后,就可以用 caffe 自帶的 python 腳本畫出自定義的網絡
$ python /path/to/caffe/python/draw_net.py myconvnet.prototxt myconvnet.png
打開 myconvnet.png 就可以看到畫出的網絡
[圖片上傳失敗...(image-768175-1515046033856)]
下面說一說怎么用 Python 調用訓練好的網絡來做測試担租。首先砸民,創(chuàng)建一個 net 對象來容納我們的卷積網絡:
impoort sys
sys.path.insert(0, '/path/to/caffe/python')
import numpy as np
import cv2
from pylab import * #畫圖
import caffe
#initialize
caffe.set_device(1) #指定使用哪一塊GPU
caffe.set_mode_gpu() #指定GPU計算
model_def = 'deploy.prototxt' #給定網絡模型
model_weight = 'net.cafffemodel' #給定參數
net = caffe.Net(model_def, model_weight, caffe.TEST) #給定phase = TEST, 那么網絡只會向前計算,不會 backpropagation