crf as rnn官網:https://github.com/torrvision/crfasrnn
官網中有相關的代碼、論文、在線演示,可以參考。但本人在下載官網中的代碼使用時遇到的一些問題或者說是困難,記錄下可供自己和一些愛好者參考,有不對的地方請指正。
我這裏就不介紹Caffe的安裝,驅動,CuDNN等安裝,以前的文章中有寫,有問題的可以參考之前的文章。
一、官網代碼下載
下載官網的代碼:
git clone --recursive https://github.com/torrvision/crfasrnn.git
下載好會發現官網代碼中會有一個caffe文件夾,這個是舊版的一個caffe(這個版本的caffe已經將crf as rnn與caffe結合好了),我原本準備就用這個caffe來做FCN和CRF as RNN的訓練,但是解決所有遇到的問題後,在訓練時報錯,原因查了下應該是FCN的代碼版本和這個舊版caffe不兼容,所以建議大家使用caffe官網最新的版本,下面會介紹如何修改官網新的caffe,使其能將FCN與CRF as RNN完整訓練。
二、移植操作
1、移植輔助的文件
將crfasrnn/caffe/include/caffe/util/下的coords.hpp和modified_permutohedral.hpp複製到caffe/include/caffe/util/下的coords.hpp和modified_permutohedral.hpp
2、移植Layer中的特性
3、移植CRF層
將crfasrnn/caffe/src/caffe/layers/multi_stage_meanfield.cpp和meanfield_iteration.cpp複製到
caffe/src/caffe/layers/multi_stage_meanfield.cpp和meanfield_iteration.cpp下
4、移植CRF層的參數到caffe.proto文件
在caffe/src/proto/caffe.proto文件中添加
optional MultiStageMeanfieldParameter multi_stage_meanfield_param = 147;此處id不要與原文件重複即可
message MultiStageMeanfieldParameter {
enum Mode {
POTTS = 0;
}
optional Mode compatibility_mode = 1 [default = POTTS];
optional float threshold = 2;
required float theta_alpha = 3 [default = 10.];
required float theta_beta = 4 [default = 10.];
required float theta_gamma = 5 [default = 10.];
required uint32 num_iterations = 6 [default = 1];
optional float spatial_filter_weight = 7 [default = 1];
optional float bilateral_filter_weight = 8 [default = 1];
optional float forced_spatial_filter_weight = 9;
optional float forced_bilateral_filter_weight = 10;
}
上面是參考http://blog.csdn.net/xizero00/article/details/51174367,我提取部分修改做;下面是我在make時報錯做的一些操作:
make時會提示一些文件缺少,都是crfasrnn/caffe/include/caffe/文件夾下的neuron_layers.hpp、python_layer.hpp、common_layers.hpp、data_layers.hpp、data_reader.hpp、loss_layers.hpp這幾個文件在caffe/include/caffe/文件夾下缺少,複製過去就好。
以上的caffe/是caffe官網最新的caffe,移植好上面的之後,先清除以前的make過的caffe
make clean
然後
make all -j4
如果沒有錯誤
make runtest
如果出錯可以自行搜索下錯誤,然後
make pycaffe
這樣整個移植的caffe就準備好了。
三、Demon演示
在官網中的代碼中提供了python-scripts文件夾,
下載預訓練模型,如果下不了,直接在瀏覽器中輸入這個文件裏的網址會自動下載:
sh download_trained_model.sh
然後
python crfasrnn_demo.py
即可得到結果
四、訓練自己的數據集
前面的文章中我有寫過FCN的:http://blog.csdn.net/qq_15309757/article/details/79035150
然後在訓練階段的修改就是訓練網絡,如voc-8s中的train.prototxt修改最後一個反捲積層後的網絡如下,ke'yi'c
layer {
name: "score"
type: "Crop"
bottom: "upscore8"
bottom: "data"
top: "score"
crop_param {
axis: 2
offset: 31
}
}
layer { type: 'Split' name: 'splitting'
bottom: 'score' top: 'unary' top: 'Q0'
}
layer {
name: "inference1"#if you set name "inference1", code will load parameters from caffemodel.
type: "MultiStageMeanfield"
bottom: "unary"
bottom: "Q0"
bottom: "data"
top: "pred"
param {
lr_mult: 10000#learning rate for W_G
}
param {
lr_mult: 10000#learning rate for W_B
}
param {
lr_mult: 1000 #learning rate for compatiblity transform matrix
}
multi_stage_meanfield_param {
num_iterations: 10
compatibility_mode: POTTS#Initialize the compatilibity transform matrix with a matrix whose diagonal is -1.
threshold: 2
theta_alpha: 160
theta_beta: 3
theta_gamma: 3
spatial_filter_weight: 3
bilateral_filter_weight: 5
}
}
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "pred"
bottom: "label"
top: "loss"
loss_param {
# ignore_label: 255
normalize: true
}
}
然後修改好solver.prototxt中的train_net: "train.prototxt"
然後
python solver.py
即可開始訓練。。。。