前言
RFCN和faster rcnn算法模型原理非常類似,也有RPN,也是分兩階段來進行目标檢測。主要不同點就是RFCN添加了Position Sensitive ROI Pooling層,這樣使得ROI Pooling前已經帶有位置資訊,後面隻需要做分類即可。基于這一改變,使得後面全連接配接層可以省略掉,模型size減少很多,計算量大為減少,進而效率提高了,而準确度還能和faster rcnn相當。
代碼下載下傳和編譯
1)下載下傳RFCN代碼
git clone https://github.com/Orpine/py-R-FCN.git
2) 下載下傳對應得caffe代碼
cd py-R-FCN
git clone https://github.com/Microsoft/caffe.git
3)編譯Cython
cd xx/py-R-FCN/lib
make
這個時候會遇到cuda located error,需要修改setup.py,具體參考我的部落格:https://blog.csdn.net/ltshan139/article/details/99868686
4)編譯caffe
cd xx/py-R-FCN/caffe
cp Makefile.config.example Makefile.config
sudo make clean
sudo make -j8
sudo make pycaffe -j8
在編譯前需要對Makefile.config進行修改, 而且會遇到cudnn版本過低的問題,具體還是參考上面提到的部落格
另外,在用opencv3.4編譯caffe example時,可能會遇到一個opencv api沒有定義的bug:
undefined reference to `cv::Mat::updateContinuityFlag()
其解決辦法就是不要編譯它即可,畢竟不影響功能使用:
sudo mv examples/cpp_classification/classification.cpp examples/cpp_classification/classification.cpp1
5)如果上面這幾步都ok了,可以運作demo來驗證下。
直接運作下面這個腳本就運作的是ResNet101模型:sudo ./tools/demo_rfcn.py
如果想運作ResNet50,則這麼輸入:sudo ./tools/demo_rfcn.py --net ResNet-50
訓練自己的資料集
我們的重點是訓練自己的資料集
1)資料集準備和faster rcnn類似,也是采用VOC格式,具體參考:https://blog.csdn.net/ltshan139/article/details/100087559
2)修改訓練和測試相關配置文本檔案,因為是采用ohem方式,是以隻需要修改下面這些,主要是class num修改成自己資料集對應值,具體可以參考其他人的部落格。
cd py-R-FCN/models/pascal_voc/ResNet-50/rfcn_end2end/
3) 修改疊代次數以及class names
vi ./experiments/scripts/rfcn_end2end_ohem.sh #修改疊代次數以及正确的train和test資料目錄名稱
vi py-R-FCN/lib/datasets/pascal_voc.py #修改class names
4)訓練過程中,如果遇到'module' object has no attribute 'text_format',
請參考https://blog.csdn.net/ltshan139/article/details/100068916
5)最後,執行訓練:
sudo ./experiments/scripts/rfcn_end2end_ohem.sh 0 ResNet-50 pascal_voc
6)模型訓練出來後,可以單獨使用下面指令來測試:
sudo ./tools/test_net.py --gpu 0 --def models/pascal_voc/ResNet-50/rfcn_end2end/test_agnostic.prototxt --net output/rfcn_end2end_ohem/voc_2007_trainval/resnet50_rfcn_ohem_iter_150000.caffemodel --imdb voc_2007_test --cfg experiments/cfgs/rfcn_end2end_ohem.yml