Mac下跑僅CPU模式下的PVANET

1、RCNN利用自帶的caffe框架,但是編譯所依賴的庫和之前我的另一篇博文相同

詳見我的博客Mac下安裝Caffe—CPU ONLY

2、確認安裝Cython easydict protobuf

3、pull pva-faster-rcnn

git clone –recursive https://github.com/sanghoon/pva-faster-rcnn.git

4、編譯Cython modules

首先,打開./lib/setup.py 註釋掉和GPU相關

...
#CUDA = locate_cuda()
...
...
#self.set_executable('compiler_so', CUDA['nvcc'])
...
...
#Extension('nms.gpu_nms',
#[‘nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],
#library_dirs=[CUDA['lib64']],
#libraries=['cudart'],
#language='c++',
#runtime_library_dirs=[CUDA['lib64']],
## this syntax is specific to this build system
## we're only going to use certain compiler args with nvcc and not with
## gcc the implementation of this trick is in customize_compiler() below
#extra_compile_args={'gcc': ["-Wno-unused-function"],
#’nvcc': ['-arch=sm_35',
#’—ptxas-options=-v',
#’-c’,
#’—compiler-options',
#”’-fPIC'"]},
#include_dirs = [numpy_include, CUDA['include']]
#)

然後,cd 到 lib目錄下執行make

5、Build Caffe and pycaffe

cd到caffe-fast-rcnn下

cp Makefile.config.example Makefile.config

修改 Makefile.config
CPU_ONLY := 1
WITH_PYTHON_LAYER := 1

編譯 make -j8 && make pycaffe

報錯1:./include/caffe/util/mkl_alternate.hpp:14:10: fatal error: ‘cblas.h’ file not found
安裝 brew uninstall openblas; brew install –fresh -vd openblas

brew link openblas –force (重要)

MakeFile.Config 中修改:
BLAS := openblas
LDFLAGS:= -L/usr/local/opt/openblas/lib
CPPFLAGS:= -I/usr/local/opt/openblas/include

報錯2:
src/caffe/layers/proposal_layer.cpp:321:1: error: redefinition of 'Backward_gpu'
STUB_GPU(ProposalLayer);
^
./include/caffe/util/device_alternate.hpp:17:24: note: expanded from macro 'STUB_GPU'
void classname<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top, \
^
./include/caffe/fast_rcnn_layers.hpp:122:16: note: previous definition is here
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
^
1 error generated.

解決:由於caffe::ProposalLayer::Backward_gpu在./include/caffe/fast_rcnn_layers.hpp和./include/caffe/util/device_alternate.hpp(後者爲模板形式)中定義了兩次,被系統認爲重定義。
解決方法如下
將./include/caffe/fast_rcnn_layers.hpp的Backward_gpu代碼

virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom){}

修改爲:

virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

報錯3:
src/caffe/util/nms.cpp:35:23: error: explicit instantiation cannot have a storage class
template static float iou(const float A[], const float B[]);
~~~ ^
src/caffe/util/nms.cpp:36:24: error: explicit instantiation cannot have a storage class
template static double iou(const double A[], const double B[]);
~~~ ^
2 errors generated.
解決
打開src/caffe/util/nms.cpp
修改

template static float iou(const float A[], const float B[]);
template static double iou(const double A[], const double B[]);

 template <typename Dtype> static float iou(const float A[], const float B[]);
 template <typename Dtype> static double iou(const double A[], const double B[]);

報錯4
python/caffe/_caffe.cpp:10:10: fatal error: ‘numpy/arrayobject.h’ file not found
解決
修改

 # We need to be able to find Python.h and numpy/arrayobject.h.
 PYTHON_INCLUDE := /usr/include/python2.7 \
         /usr/lib/python2.7/dist-packages/numpy/core/include

爲自己的numpy路徑(np.get_include()可查看路徑)

編譯成功
這裏寫圖片描述

後記:以上四個錯誤爲本人遇到錯誤中的四個,其他還有。每個人配置過程中的錯誤可能會不同,記得Google

5、Run Demo

1、安裝配置好caffe-fast-rcnn後,轉到pva-faster-rcnn/lib/fast_rcnn 打開config.py
修改

 # Use GPU implementation of non-maximum suppression
 __C.USE_GPU_NMS = True

 # Use GPU implementation of non-maximum suppression
 __C.USE_GPU_NMS = False

2 cd 到pva-faster-rcnn/tools,修改test_net.py 、 train_net.py裏面的caffe.set_mode_gpu()修改爲caffe.set_mode_cpu().
3. 將lib/fast_rcnn/nms_wrapper.py
中的from nms.gpu_nms import gpu_nms 註釋掉
4.下載faster_rcnn_models ../data/scripts/ ./fetch_faster_rcnn_models.sh
如果不成功:手動下載:http://www.cs.berkeley.edu/~rbg/faster-rcnn-data/faster_rcnn_models.tgz
5.運行python ./tools/demo.py –cpu
報錯:Segmentation fault: 11

原因:這個問題很難受,Google很久無結果
後來發現是我個人caffe路徑的問題(因人而異)

6.再次運行,OK 電腦速度很慢
這裏寫圖片描述

7。測試 VOC dataset

1.下載數據
VOC

2 運行
./tools/test_net.py –net models/pvanet/pva9.1/PVA9.1_ImgNet_COCO_VOC0712.caffemodel –def models/pvanet/pva9.1/faster_rcnn_train_test_21cls.pt –cfg models/pvanet/cfgs/submit_1019.yml –gpu 0

3.運行結果
這裏寫圖片描述

與官方給出的mAP: 84.9 相同

8 .如有問題,歡迎留言指教。

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