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::Backward_gpu(const vector*>& top, \
^
./include/caffe/fast_rcnn_layers.hpp:122:16: note: previous definition is here
virtual void Backward_gpu(const vector*>& 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*>& top,
      const vector<bool>& propagate_down, const vector*>& bottom){}

修改为:

virtual void Backward_gpu(const vector*>& top,
      const vector<bool>& propagate_down, const vector*>& 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.运行结果
Mac下跑仅CPU模式下的PVANET_第1张图片

与官方给出的mAP: 84.9 相同

8 .如有问题,欢迎留言指教。

你可能感兴趣的:(mac)