OS:ubuntu 16.04 LTS
GPU:GTX 1070Ti(Nvidia驱动、cuda、cudnn已安装)
Python:conda + python2.7
1、下载源码:git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
2、在caffe-fast-rcnn目录下,复制Makefile.config.example,并根据具体环境修改相关路径和参数。主要的修改有:
(1)设置USE_CUDNN=1
(2)如果opencv版本为3.xx的,设置opencv_version :=3
(3)设置blas为openblas:BLAS := open,设置blas_include和blas_lib目录
(4)设置Anaconda_home,python_include,python_lib
(5)修改CUDA_ARCH,根据gpu的性能来设置,如1070ti,设置为50(这个可以在网上查)
(6)设置允许python_layer:WITH_PYTHON_LAYER := 1
(7)添加INCLUDE_DIRS和LIBRARY_DIRS(如果依赖文件安装了,却提示没找到,就把相应的文件路径添加到这两个配置里)
## 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_50,code=sm_50 \
-gencode arch=compute_50,code=compute_50
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /home/coderx/anaconda3/envs/caffe/include
BLAS_LIB := /home/coderx/anaconda3/envs/caffe/lib
# 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 := /home/coderx/anaconda3/envs/caffe/bin/python \
/home/coderx/anaconda3/envs/caffe/lib/python2.7/site-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := /home/coderx/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/envs/caffe/lib/python2.7/site-packages/numpy/core/include \
$(ANACONDA_HOME)/envs/caffe/include \
$(ANACONDA_HOME)/envs/caffe/include/python2.7
# 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)/envs/caffe/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
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib $(ANACONDA_HOME)/envs/caffe/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 ?= @
3、安装必要的依赖,根据caffe-fast-rcnn/python/requirement.txt,主要有以下几项:(不要安装版本太高的,略高于要求的版本就可以了,太高了有可能会不兼容)
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
除了上面这些,实测还有gflags、glog。如果没有安装,不会提示相关的错误,反而是出现MakeCheckOpString未定义的引用等错误。
还有boost、openblas、easydict等,其他的编译时缺啥装啥。
这里需要特别注意的是,boost、leveldb、protobuf、opencv、gflags、glog、openblas等这几个不能通过conda install或者pip install来安装,而必须通过源码来编译安装,否则会报“未定义的引用”错误,即使已经正确安装了(哪个文件提示“未定义的引用”错误,就通过源码重新安装哪个文件!且必须卸载掉conda环境下已经安装的,否则编译时还是会使用错误的版本)。据推测应该是安装的版本与本机的gcc版本不对。
相关的源码地址和安装步骤如下:
(1)boost
下载地址:https://dl.bintray.com/boostorg/release/1.65.1/source/boost_1_65_1.tar.gz
安装过程:
cd boost_1_65_1
./bootstrap.sh --with-libraries=filesystem,system,thread,python --prefix=/usr/local
sudo ./b2 install
(2)leveldb
下载地址:https://github.com/google/leveldb/archive/v1.20.tar.gz
安装过程:
make
sudo cp -rf include/leveldb /usr/local/include
sudo cp -rf out-shared/libleveldb.so* /usr/local/lib
(3)protobuf
下载地址:https://github.com/google/protobuf/archive/v3.6.0.tar.gz
cd protobuf-3.6.0
./autogen.sh
./configure --prefix=/usr/local
make
sudo make install
(4)opencv
下载地址: https://github.com/opencv/opencv/archive/3.4.1.tar.gz
cd opencv-3.4.1
mkdir build
cd build
cmake ..
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install
(5)gflags
下载地址:https://github.com/gflags/gflags/archive/v2.2.1.tar.gz
cd gflags-2.2.1
mkdir build
cd build
cmake ..
ccmake ..
make
sudo make install
(6)glog
下载地址:https://github.com/google/glog/archive/v0.3.5.tar.gz
cd glog-0.3.5
./configure --prefix=/usr/local
make
sudo make install
(7)openblas
下载地址:https://github.com/xianyi/OpenBLAS/archive/v0.3.0.tar.gz
cd OpenBLAS-0.3.0
make
sudo make PREFIX=/usr/local install
3、更新caffe里的cudnn相关代码(旧版caffe与cudnn不兼容)
下载最新的caffe源码,将相关的源码考到相应的文件夹下,覆盖。否则会报错./include/caffe/util/cudnn.hpp:127:41: error: too few arguments to function
(1)将./include/caffe/util/cudnn.hpp 换成最新版的caffe里的cudnn的实现,即相应的cudnn.hpp)。
(2)将./include/caffe/layers里的,所有以cudnn开头的文件,例如cudnn_conv_layer.hpp。 都替换成最新版的caffe里的相应的同名文件。
(3)将./src/caffe/layer里的,所有以cudnn开头的文件,例如
cudnn_lrn_layer.cu,cudnn_pooling_layer.cpp,cudnn_sigmoid_layer.cu。
4、在py-faster-rcnn/lib目录下,执行 make
5、在caffe-fast-rcnn目录下,执行 make && make pycaffe,如果没有报错,说明编译成功了
6、执行demo,测试
下载预训练好的model,下载地址为http://www.cs.berkeley.edu/~rbg/faster-rcnn-data/faster_rcnn_models.tgz,解压,把得到的faster_rcnn_models文件夹放在data目录下。
重新安装python环境的opencv和protobuf(前面编译的时候卸载掉了),否则会提示模块未安装
在py-faster-rcnn,执行 ./tools/demo.py,弹出带红框的图片,说明测试成功。