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的環境。
下載原始碼
下載原始碼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]
該模型檔案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