Ubuntu14.04下配置測試Faster R-CNN+Caffe(Matlab+CPU)目標檢測

Faster R-CNN是當前目標檢測領域內性能最好的算法之一,它將RPN(Region Proposal Network)網絡和Fast R-CNN網絡結合到了一起,實現了一個端到端的目標檢測框架。作者Shaoqing Ren在github上公開了源代碼,可以很方便地在自己的機器上進行測試。本文記錄的是Ubuntu14.04下配置和測試Faster R-CNN的過程,其中包括Caffe的安裝和編譯過程,針對的是Matlab和僅使用CPU的環境。

文章arXiv鏈接:
https://arxiv.org/abs/1506.01497

下載源代碼

Matlab源碼鏈接:
https://github.com/ShaoqingRen/faster_rcnn

Python實現源碼:
https://github.com/rbgirshick/py-faster-rcnn

下載源碼faster_rcnn-master.zip以及其中的caffe源碼,解壓至本地目錄。

安裝編譯Caffe

1.安裝依賴項[1]

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
BLAS:
sudo apt-get install libatlas-base-dev
Python用戶需安裝,Matlab不需要:
sudo apt-get install the python-dev
GPU運行環境還需安裝CUDA,使用CPU無需安裝。 Ubuntu14.04依賴項:
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
Ubuntu12.04版本較低,需要手動安裝依賴項:
# glog
wget https://google-glog.googlecode.com/files/glog-0.3.3.tar.gz
tar zxvf glog-0.3.3.tar.gz
cd glog-0.3.3
./configure
make && make install
# gflags
wget https://github.com/schuhschuh/gflags/archive/master.zip
unzip master.zip
cd gflags-master
mkdir build && cd build
export CXXFLAGS="-fPIC" && cmake .. && make VERBOSE=1
make && make install
# lmdb
git clone https://github.com/LMDB/lmdb
cd lmdb/libraries/liblmdb
make && make install

2.編譯
Caffe可以採用Make或者CMake兩種方式來編譯,我使用的是Make,且編譯之前需要根據個人情況修改caffe目錄下的Makefile.config.example文件。具體需要修改的地方如下:

  • 僅使用CPU的情況下,需要取消該文件中CPU_ONLY := 1的註釋。
  • 指定Matlab的安裝路徑,我的是MATLAB_DIR := /usr/local/MATLAB/R2016a

修改完成後,執行如下命令:

cp Makefile.config.example Makefile.config
make clean
make all
make test
make runtest
採用並行的方式可以更快地完成編譯過程,只需使用`make all -j8`命令。其中數字8表示並行線程的數目,建議將該線程數目修改爲機器的內核數。 爲了能夠在Matlab中正常使用,還需執行以下命令:
make matcaffe

之後在matlab/+caffe/private目錄下將會生成caffe_.mex64文件,可以直接被Matlab調用。

3.測試示例[2]

在正式測試前,需要下載訓練好的caffemodel文件[3]。有兩種方式,直接在瀏覽器裏輸入地址下載,也可以運行腳本文件下載。我選擇直接用瀏覽器下載,下載地址如下:
http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel

該模型文件243.9M,下載完成後將其拷貝到caffe根目錄下的models/bvlc_reference_caffenet/文件夾中。

接着啓動Matlab,切換到caffe的根目錄,將caffe_.mex64的路徑添加進來,便於加載。

addpath('./matlab/+caffe/private');
切換到`matlab/demo/`目錄下,執行如下命令來測試示例:
I = imread('../../examples/images/cat.jpg');
[scores, maxlabel] = classification_demo(I, 0);
需要特別注意,classification_demo(I, 0)函數中的0表示使用CPU,如果改爲1則表示GPU。執行成功後,將會返回如下信息:
Elapsed time is 0.025054 seconds.
Elapsed time is 1.069503 seconds.
Cleared 0 solvers and 1 stand-alone nets
其中maxlabel=282,表示具有最大分類概率的是第282個類別。輸入`figure;plot(scores);`顯示所有類別上的分類概率,如下圖所示。
分類分數

從圖中可以看出,該圖片被分到第282類的概率最高。至此,程序測試完成,說明Caffe安裝配置成功。

配置運行Faster R-CNN

Faster R-CNN的配置和運行十分簡單,啓動Matlab,切換到faster_rcnn目錄下。運行faster_rcnn_build.m,在僅使用CPU的情況下,Compiling nms_gpu_mex時會出錯[4]。但是其他能夠編譯成功,這裏不用擔心。

run faster_rcnn_build.m
run startup.m

獲取訓練好的模型,可以通過執行文件下載:

run fetch_data/fetch_faster_rcnn_final_model.m

個人建議直接通過作者github主頁上的鏈接來下載模型,地址在主頁最後一行給出,選擇其一下載。

3.Final RPN+FastRCNN models: OneDrive, DropBox, BaiduYun

模型較大,下載完成後直接將其解壓至faster_rcnn根目錄下。

在experiments目錄下有測試文件,因爲只使用CPU,運行前需要將其設置爲不使用GPU。

experiments/script_faster_rcnn_demo.m

文件中的第3、4行需要註釋掉,第9行的opts.use_gpu改爲false。以下是修改後的代碼,

%% -------------------- CONFIG --------------------
opts.caffe_version          = 'caffe_faster_rcnn';
% opts.gpu_id                 = auto_select_gpu;
% active_caffe_mex(opts.gpu_id, opts.caffe_version);

opts.per_nms_topN           = 6000;
opts.nms_overlap_thres      = 0.7;
opts.after_nms_topN         = 300;
opts.use_gpu                = false;

opts.test_scales            = 600;

此外,由於VGG16模型較大,運行過程中會崩潰,因此將模型改爲ZF,修改後如下:

% model_dir = fullfile(pwd, 'output', 'faster_rcnn_final', 'faster_rcnn_VOC0712_vgg_16layers'); %% VGG-16
model_dir = fullfile(pwd, 'output', 'faster_rcnn_final', 'faster_rcnn_VOC0712_ZF'); %% ZF

至此,修改完畢。執行該程序,能夠正常運行說明測試成功。我的處理器是Intel® Core™ i7-2600 CPU @ 3.40GHz × 8 ,輸出如下信息:

001763.jpg (500x375): time 3.258s (resize+conv+proposal: 2.452s, nms+regionwise: 0.806s)
004545.jpg (500x375): time 4.073s (resize+conv+proposal: 2.543s, nms+regionwise: 1.530s)
000542.jpg (500x375): time 3.064s (resize+conv+proposal: 2.526s, nms+regionwise: 0.538s)
000456.jpg (500x375): time 3.757s (resize+conv+proposal: 2.497s, nms+regionwise: 1.259s)
001150.jpg (500x375): time 3.400s (resize+conv+proposal: 2.481s, nms+regionwise: 0.919s)
mean time: 3.510s
Cleared 0 solvers and 2 stand-alone nets

目標檢測



參考文獻:
[1]http://caffe.berkeleyvision.org/install_apt.html
[2]http://www.aichengxu.com/view/2422137
[3]http://www.cnblogs.com/denny402/p/5111018.html
[4]http://blog.csdn.net/qq_30040223/article/details/48491997

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章