mac安装Faster R-CNN   2016-12-02


在mac上安装fast-r-cnn时遇到了一些坑。所以整理下,以便对有需要的人有所帮助。

1. 下载代码

1
2
$ cd /Users/ruifengshan/githubRepos/
$ git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git

2. 下载模型

1
2
3
4
5
6
7
8
$ cd /Users/ruifengshan/githubRepos/py-faster-rcnn
$ ./data/scripts/fetch_faster_rcnn_models.sh
Downloading Faster R-CNN demo models (695M)...
...
Unzipping...
faster_rcnn_models/
faster_rcnn_models/ZF_faster_rcnn_final.caffemodel
faster_rcnn_models/VGG16_faster_rcnn_final.caffemodel

国内下载的有点慢。

3. 编译cython

进入lib目录,修改setup.py,注释掉GPU相关代码,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#def locate_cuda():
# """Locate the CUDA environment on the system
#
# Returns a dict with keys 'home', 'nvcc', 'include', and 'lib64'
# and values giving the absolute path to each directory.
#
# Starts by looking for the CUDAHOME env variable. If not found, everything
# is based on finding 'nvcc' in the PATH.
# """
#
# # first check if the CUDAHOME env variable is in use
# if 'CUDAHOME' in os.environ:
# home = os.environ['CUDAHOME']
# nvcc = pjoin(home, 'bin', 'nvcc')
# else:
# # otherwise, search the PATH for NVCC
# default_path = pjoin(os.sep, 'usr', 'local', 'cuda', 'bin')
# nvcc = find_in_path('nvcc', os.environ['PATH'] + os.pathsep + default_path)
# if nvcc is None:
# raise EnvironmentError('The nvcc binary could not be '
# 'located in your $PATH. Either add it to your path, or set $CUDAHOME')
# home = os.path.dirname(os.path.dirname(nvcc))
#
# cudaconfig = {'home':home, 'nvcc':nvcc,
# 'include': pjoin(home, 'include'),
# 'lib64': pjoin(home, 'lib64')}
# for k, v in cudaconfig.iteritems():
# if not os.path.exists(v):
# raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))
#
# return cudaconfig
#CUDA = locate_cuda()

1
#            self.set_executable('compiler_so', CUDA['nvcc'])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#    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']]
# ),

然后执行:

1
$ make

4. 安装自带的caffe(不是通用的)

进入caffe-fast-rcnn目录,大部分跟前面caffe安装记录一样,参考之前的Makefile.config文件进行修改(可以直接拷贝过来)。
然后进行编译过程,也和安装通用的caffe是一样的。记得执行make pycaffe语句:

5. 运行示例程序

为了让程序能正常运行,我们需要修改:tools/demo.py和lib/fast_rcnn/test.py文件。
将程序中原本的import caffe改为:

1
2
sys.path.insert(0, '/Users/ruifengshan/githubRepos/py-faster-rcnn/caffe-fast-rcnn/python')
import caffe

然后执行命令:

1
./tools/demo.py --cpu

结果:

6. 常见错误

1) No module named easydict

1
2
3
4
5
6
Traceback (most recent call last):
File "./tools/demo.py", line 17, in
from fast_rcnn.config import cfg
File "/Users/ruifengshan/githubRepos/py-faster-rcnn/tools/../lib/fast_rcnn/config.py", line 23, in
from easydict import EasyDict as edict
ImportError: No module named easydict

这是缺少Python库easydict,所以使用命令pip install easydict进行安装即可。

2) No module named cv2

1
2
3
4
5
6
Traceback (most recent call last):
File "./tools/demo.py", line 18, in
from fast_rcnn.test import im_detect
File "/Users/ruifengshan/githubRepos/py-faster-rcnn/tools/../lib/fast_rcnn/test.py", line 15, in
import cv2
ImportError: No module named cv2

这是因为没有安装opencv2,使用如下命令安装即可:

1
2
3
4
5
6
7
8
brew tap homebrew/science
brew info opencv
brew install opencv

cd /usr/local/Cellar/opencv/2.4.13_3/
cd /Library/Python/2.7/site-packages/
ln -s /usr/local/Cellar/opencv/2.4.13_3/lib/python2.7/site-packages/cv.py cv.py
ln -s /usr/local/Cellar/opencv/2.4.13_3/lib/python2.7/site-packages/cv2.so cv2.so

3) No module named gpu_nms

1
2
3
4
5
6
Traceback (most recent call last):
File "tools/demo.py", line 19, in
from fast_rcnn.nms_wrapper import nms
File "/Users/ruifengshan/githubRepos/py-faster-rcnn/tools/../lib/fast_rcnn/nms_wrapper.py", line 9, in
from nms.gpu_nms import gpu_nms
ImportError: No module named gpu_nms

修改nms_wrapper.py,改force_cpu =True

1
vim lib/fast_rcnn/nms_wrapper.py

修改如下:

1
def nms (dets, thresh, force_cpu =True):


分享到:


  如果您觉得这篇文章对您的学习很有帮助, 请您也分享它, 让它能再次帮助到更多的需要学习的人. 您的支持将鼓励我继续创作 !
本文基于署名4.0国际许可协议发布,转载请保留本文署名和文章链接。 如您有任何授权方面的协商,请邮件联系我。

目录

  1. 1. 1. 下载代码
  2. 2. 2. 下载模型
  3. 3. 3. 编译cython
  4. 4. 4. 安装自带的caffe(不是通用的)
  5. 5. 5. 运行示例程序
  6. 6. 6. 常见错误
    1. 6.1. 1) No module named easydict
    2. 6.2. 2) No module named cv2
    3. 6.3. 3) No module named gpu_nms