机器学习

mac安装Faster R-CNN

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

目录9 节

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

1. 下载代码

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

2. 下载模型

$ cd /Users/aaron/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相关代码,如下:

#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()
#            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']]
#    ),

然后执行:

$ make

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

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

5. 运行示例程序

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

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

然后执行命令:

 ./tools/demo.py --cpu

结果:

6. 常见错误

1) No module named easydict

Traceback (most recent call last):
File "./tools/demo.py", line 17, in
 from fast_rcnn.config import cfg
 File "/Users/aaron/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

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

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

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

Traceback (most recent call last):
  File "tools/demo.py", line 19, in
    from fast_rcnn.nms_wrapper import nms
  File "/Users/aaron/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

vim lib/fast_rcnn/nms_wrapper.py

修改如下:

def nms (dets, thresh, force_cpu =True):
微信搜一搜 AI拆解猿

评论