Faster R-CNN安装笔记,只用CPU

官网下载 faster RCNN,

https://github.com/rbgirshick/py-faster-rcnn

按教程操作,装python接口可能的错误,按http://blog.csdn.net/sinat_33621733/article/details/52712847进行


  • 下载demo模型数据
[root@localhost 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


  • 编译cython
进入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 notwith
      # 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']]
  ),
。。。

编译:
[root@localhost lib]# make

  • 安装caffe(自带的,不是通用的)
进入caffe-fast-rcnn目录,大部分跟前面caffe安装记录一模一样,修改Makefile.config为

## Refer tohttp://caffe.berkeleyvision.org/installation.html
# Contributionssimplifying and improving our build system arewelcome!

# cuDNN accelerationswitch (uncomment to build with cuDNN).
# USE_CUDNN :=1

# CPU-only switch(uncomment to build without GPU support).
CPU_ONLY :=1

# uncomment to disableIO dependencies and corresponding data layers
# USE_OPENCV :=0
# USE_LEVELDB :=0
# USE_LMDB :=0

# uncomment to allowMDB_NOLOCK when reading LMDB files (only if necessary)
  You should not set this flag if you will bereading LMDBs with any
  possibility of simultaneous read andwrite
# ALLOW_LMDB_NOLOCK :=1

# Uncomment if you'reusing OpenCV 3
# OPENCV_VERSION :=3

# To customize yourchoice of compiler, uncomment and set the following.
# N.B. the default forLinux is g++ and the default for OSX is clang++
# CUSTOM_CXX :=g++

# CUDA directorycontains bin/ and lib/ directories that we need.
# CUDA_DIR :=/usr/local/cuda
# On Ubuntu 14.04, ifcuda tools are installed via
# "sudo apt-get installnvidia-cuda-toolkit" then use this instead:
# CUDA_DIR :=/usr

# CUDA architecturesetting: going with all of them.
# For CUDA < 6.0,comment the *_50 lines for compatibility.
#CUDA_ARCH := -gencodearch=compute_20,code=sm_20 \
      -gencodearch=compute_20,code=sm_21 \
      -gencodearch=compute_30,code=sm_30 \
      -gencodearch=compute_35,code=sm_35 \
      -gencodearch=compute_50,code=sm_50 \
      -gencodearch=compute_50,code=compute_50

# BLASchoice:
# atlas for ATLAS(default)
# mkl forMKL
# open forOpenBlas
BLAS :=atlas
# Custom(MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented toaccept the defaults for your choice of BLAS
# (which shouldwork)!
BLAS_INCLUDE :=/usr/include/atlas-x86_64-base
BLAS_LIB :=/usr/lib64/atlas            原文件,我并没有改这里

# Homebrew putsopenblas in a directory that is not on the standard searchpath
# BLAS_INCLUDE :=$(shell brew --prefix openblas)/include
# BLAS_LIB := $(shellbrew --prefix openblas)/lib

# This is required onlyif you will compile the matlab interface.
# MATLAB directoryshould contain the mex binary in /bin.
# MATLAB_DIR :=/usr/local
# MATLAB_DIR :=/Applications/MATLAB_R2012b.app

# NOTE: this isrequired only if you will compile the pythoninterface.
# We need to be able tofind Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE :=/usr/include/python2.7 \
              /usr/lib64/python2.7/site-packages/numpy/core/include
# Anaconda Pythondistribution is quite popular. Include path:
# Verify anacondalocation, sometimes it's in root.
# ANACONDA_HOME :=$(HOME)/anaconda
# PYTHON_INCLUDE :=$(ANACONDA_HOME)/include \
       #$(ANACONDA_HOME)/include/python2.7 \
       #$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include\

# Uncomment to usePython 3 (default is Python 2)
# PYTHON_LIBRARIES :=boost_python3 python3.5m
# PYTHON_INCLUDE :=/usr/include/python3.5m \
            /usr/lib/python3.5/dist-packages/numpy/core/include

# We need to be able tofind libpythonX.X.so or .dylib.
PYTHON_LIB :=/usr/lib64
# PYTHON_LIB :=$(ANACONDA_HOME)/lib

# Homebrew installsnumpy in a non standard path (keg only)
# PYTHON_INCLUDE +=$(dir $(shell python -c 'import numpy.core;print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shellbrew --prefix numpy)/lib

# Uncomment to supportlayers written in Python (will link against Pythonlibs)
WITH_PYTHON_LAYER :=1

# Whatever else youfind you need goes here.
INCLUDE_DIRS :=$(PYTHON_INCLUDE) /usr/include
LIBRARY_DIRS :=$(PYTHON_LIB) /usr/lib64         (也没改)

# If Homebrew isinstalled at a non standard location (for example your homedirectory) and you use it for general dependencies
# INCLUDE_DIRS +=$(shell brew --prefix)/include
# LIBRARY_DIRS +=$(shell brew --prefix)/lib

# Uncomment to use`pkg-config` to specify OpenCV library paths.
# (Usually notnecessary -- OpenCV libraries are normally installed in one of theabove $LIBRARY_DIRS.)
# USE_PKG_CONFIG :=1

BUILD_DIR :=build
DISTRIBUTE_DIR :=distribute

# Uncomment fordebugging. Does not work on OSX due tohttps://github.com/BVLC/caffe/issues/171
# DEBUG :=1

# The ID of the GPUthat 'make runtest' will use to run unit tests.
# TEST_GPUID :=0

# enable pretty build(comment to see full commands)
Q ?= @

修改Makefile
LIBRARIES += satlas tatlas #新版atlas已经不用这两个lib了:cblasatlas

编译caffe和pycaffe
  [root@localhost caffe-fast-rcnn]# make -j8&& make pycaffe
提示:
make: 没有什么可以做的为 `all'。
make: 没有什么可以做的为 `pycaffe'。
make clean
再次编译。
提示:
File "./tools/demo.py", line 17, in
  from fast_rcnn.config import cfg
  File"/root/zhanxiang/work/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,安装:
sudo pip install easydict 

yida@yida:~/py-faster-rcnn$ ./tools/demo.py
Traceback (most recent call last):
  File "./tools/demo.py", line 18, in <module>
    from fast_rcnn.test import im_detect
  File "/home/yida/py-faster-rcnn/tools/../lib/fast_rcnn/test.py", line 17, in <module>
    from fast_rcnn.nms_wrapper import nms
  File "/home/yida/py-faster-rcnn/tools/../lib/fast_rcnn/nms_wrapper.py", line 9, in <module>
    from nms.gpu_nms import gpu_nms
ImportError: No module named gpu_nms

A>将 ~/py-faster-rcnn/lib/fast_rcnn/config.py的如下内容:

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

B>将 ~/py-faster-rcnn/tools/test_net.py和 ~/py-faster-rcnn/tools/train_net.py的caffe.set_mode_gpu()修改为caffe.set_mode_cpu().

C>将~/py-faster-rcnn/lib/setup.py中,含有'nms.gpu_nms’的部分去掉,去掉后的内容如下:
112 ext_modules = [
113     Extension(
114         "utils.cython_bbox",
115         ["utils/bbox.pyx"],
116         extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
117         include_dirs = [numpy_include]
118     ),
119     Extension(
120         "nms.cpu_nms",
121         ["nms/cpu_nms.pyx"],
122         extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
123         include_dirs = [numpy_include]
124     ),
125     Extension(
126         'pycocotools._mask',
127         sources=['pycocotools/maskApi.c', 'pycocotools/_mask.pyx'],
128         include_dirs = [numpy_include, 'pycocotools'],
129         extra_compile_args={
130             'gcc': ['-Wno-cpp', '-Wno-unused-function', '-std=c99']},
131     ),
132 ]
复制代码

D>做到上面三部后,还是不够的,还需要将:

#from nms.gpu_nms import gpu_nms注释掉

修改nms_wrapper.py,改 force_cpu = True

之后
yida@yida:~/py-faster-rcnn$ ./tools/demo.py
WARNING: Logging before InitGoogleLogging() is written to STDERR
F1009 11:34:58.148119 30428 common.cpp:66] Cannot use GPU in CPU-only Caffe: check mode.

要写yida@yida:~/py-faster-rcnn$ ./tools/demo.py  --cpu

接着出错:

Traceback (most recent call last):
  File "/home/yida/py-faster-rcnn/tools/../lib/rpn/proposal_layer.py", line 10, in <module>
    import yaml
ImportError: No module named yaml
Traceback (most recent call last):
  File "./tools/demo.py", line 135, in <module>
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)
SystemError: NULL result without error in PyObject_Call

下载:
sudo apt-get install python-yaml 
接着可以运行

测试一下模型
./tools/demo.py --cpu --net zf

参考:

http://www.cnblogs.com/justinzhang/p/5386837.html

http://blog.csdn.net/wuzuyu365/article/details/51895255

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值