Ubuntu 17.04 py-faster-rcnn only CPU

这篇博客详细介绍了如何在Ubuntu 17.04系统上,使用Python 2.7.13在没有GPU的情况下搭建并运行Faster R-CNN。内容包括下载代码和数据、安装Caffe依赖、修改setup.py、编译Caffe、调整Faster R-CNN配置以及运行demo.py的过程。虽然最终因内存限制无法运行VGG16网络,但成功运行了ZF网络。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

操作系统:ubuntu 17.04
python:Python 2.7.13

下载代码和数据

git clone –recursive https://github.com/rbgirshick/py-faster-rcnn.git

下载demo模型数据

cd py-faster-rcnn
./data/scripts/fetch_faster_rcnn_models.sh
如果这个过程由于网络原因中断下载,可以在下面链接中下载后,解压,复制到相应的 data目录下
链接:http://pan.baidu.com/s/1slkdcS1 密码:2ug2
这里写图片描述

安装Caffe需要的依赖包

将caffe-fast-rcnn/python目录下的requirements下的依赖都装一遍pip install XX(安装了anaconda的可以conda install XXX)

Cython>=0.19.2
numpy>=1.7.1
scipy>=0.13.2
scikit-image>=0.9.3
matplotlib>=1.3.1
ipython>=3.0.0
h5py>=2.2.0
leveldb>=0.191
networkx>=1.8.1
nose>=1.3.0
pandas>=0.12.0
python-dateutil>=1.4,<2
protobuf>=2.5.0
python-gflags>=2.0
pyyaml>=3.10
Pillow>=2.3.0
six>=1.1.0

修改setup.py

进入lib目录,修改setup.py,注释掉GPU相关代码,主要有三处,如下
这里写图片描述

这里写图片描述
这里写图片描述

安装Faster RCNN代码自带的caffe

进入caffe-fast-rcnn目录,
修改CMakeLists.txt文件,OFF改成ON,修改如下

caffe_option(CPU_ONLY  "Build Caffe without CUDA support" ON) # TODO: rename to USE_CUDA  

然后复制,命令: cp Makefile.config.example Makefile.config
修改Makefile.config为

## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!

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

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

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

# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
#   You should not set this flag if you will be reading LMDBs with any
#   possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1

# Uncomment if you're using OpenCV 3
# OPENCV_VERSION := 3

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

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

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

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /usr/include/atlas
BLAS_LIB := /usr/lib/atlas-base

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

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

# NOTE: this is required only if you will compile the python interface.
# 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
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, 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 use Python 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 to find libpythonX.X.so or .dylib.
PYTHON_LIB := /usr/lib
# PYTHON_LIB := $(ANACONDA_HOME)/lib

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

# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include \
                                  /usr/include/hdf5/serial/
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

# If Homebrew is installed at a non standard location (for example your home directory) 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 not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1

BUILD_DIR := build
DISTRIBUTE_DIR := distribute

# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1

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

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

修改Makefile第181行,一般用vim打开,如果没有显示行号,可以用:set number命令显示行号。这块一定要记住自己改了什么,一般不同的版本可能会不一样,可以自行查找版本对应的。
这里写图片描述

编译caffe和pycaffe

 ~/py-faster-rcnn/caffe-fast-rcnn$ make
 ~/py-faster-rcnn/caffe-fast-rcnn$ make pycaffe
 ~/py-faster-rcnn/lib$ make

如果成功的话,如下图:
这里写图片描述
中间可能会遇到XX包不存在之类的问题,缺啥补啥。
注:遇到问题修改后,最好先make clean后再make

修改py-faster-rcnn相关配置
修改 ~/py-faster-rcnn/lib/fast_rcnn/config.py的内容,改为False
这里写图片描述
修改 ~/py-faster-rcnn/tools/test_net.py和 ~/py-faster-rcnn/tools/train_net.py的caffe.set_mode_gpu()修改为caffe.set_mode_cpu()
这里写图片描述

这里写图片描述

将~/py-faster-rcnn/lib/setup.py中,含有’nms.gpu_nms’的部分去掉,去掉后的内容如下:
这里写图片描述

还需要将:~/py-faster-rcnn/lib/fast_rcnn/nms_wrapper.py中的from nms.gpu_nms import gpu_nms注释掉:
这里写图片描述

运行demo.py

在环境一切就绪的情况下,在~/py-faster-rcnn/tools目录下运行, python demo.py --cpu:
这里写图片描述
出现错误:Check failed:*ptr host allocation of size 17280000 failed
这里写图片描述
输入sudo apt-get install ocl-icd-opencl-dev
这里写图片描述

但是由于我本身内存不够,程序仍然被杀死
这里写图片描述
所以我的电脑对于VGG16这个网络不能执行,只能运行小一点的网络,“ZF”,输入:python demo.py –cpu –net zf
这里写图片描述
结果成功:
这里写图片描述
其中一些结果:
这里写图片描述

更多教程,论文解析,资源,请关注微信公众号:paper大讲堂(paperclassroom)
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值