face-py-faster-rcnn在github上的项目环境是Python2的,如果是用Python3的环境的话,需要对源码进行修改,要改的太多就算了。我采用Anaconda2的Python环境来编译安装face-py-faster-rcnn。
1.克隆face-py-faster-rcnn在github上的项目
git clone --recursive https://github.com/playerkk/face-py-faster-rcnn.git
2.安装Python packages
pip install cython
pip install opencv-python==3.4.1.15 opencv-contrib-python==3.4.1.15
pip install easydict
3.编译Cython模块
进入face-py-faster-rcnn目录下的lib目录中,找到setup.py文件
cd $FRCN_ROOT/lib
打开setup.py文件,找到-arch=sm_35这行
然后根据你显卡的计算能力修改相应数值,比如说,我的显卡计算能力是5.0,那便修改为-arch=sm_50。
显卡的计算能力在你安装CUDA编译样例测试时能查到或者到官网查询
修改完后保存即可。接着开始编译Cython模块
make
4.接着编译Caffe和pycaffe
cd $FRCN_ROOT/caffe-fast-rcnn
由于这个github项目不支持新的cudnn,所以我们可以从caffe那里借点文件过来使使。在你face-py-faster-rcnn同级的目录下,克隆caffe项目,像这样
git clone https://github.com/BVLC/caffe.git
克隆完成之后,
face-py-faster-rcnn/caffe-fast-rcnn/include/caffe/util/cudnn.hpp
face-py-faster-rcnn/caffe-fast-rcnn/src/caffe/util/cudnn.cpp
face-py-faster-rcnn/caffe-fast-rcnn/include/caffe/layers/目录下8个cudnn_开头的文件
face-py-faster-rcnn/caffe-fast-rcnn/src/caffe/layers/目录下16个cudnn_开头的文件
将以上文件用caffe的相应文件进行替换就行了。至于有的说拉取caffe项目,然后合并这种做法我没实践过,不作解释。
然后,还需要修改face-py-faster-rcnn/caffe-fast-rcnn/src/caffe/test目录下一个叫做test_smooth_L1_loss_layer.cpp的文件。
把第11行代码注释掉,如图
最后就是安装一大堆包
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libopenblas-dev
sudo apt-get install python-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
安装完后,
cd $FRCN_ROOT/caffe-fast-rcnn/python
# 安装所需的python包
for req in $(cat requirements.txt); do pip install $req; done
最麻烦的就是配置文件了。
cd $FRCN_ROOT/caffe-fast-rcnn
cp Makefile.config.example 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 := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas
# 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)/anaconda2
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 /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
# 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 ?= @
有问题可以加Q聊,794439976
最后,就是编译了
make all
make test
make runtest
不过,在make test执行后,会出现error,如图
解决方案如下:
vim ~/.bashrc
# 添加以下内容
export LD_LIBRARY_PATH=/home/user_name/anaconda2/lib:$LD_LIBRARY_PATH
source .bashrc
注意: 这个anaconda2的lib不能添加在etc下,不然你重启之后,系统的lib和anaconda2的lib可能发生冲突,然后系统就炸了。我发生过这事,U盘挂载和各种修复没救回来
接着运行make runtest就没问题了,最后运行
make pycaffe
# 然后添加环境
export PYTHONPATH=~/deeplearning/face-py-faster-rcnn/caffe-fast-rcnn/python:$PYTHONPATH
# 上面是我的配置,根据自己信息修改
如果你在日常使用中,遇到以下情况,/home/arthuo/anaconda2/lib/libuuid.so.1:no version information available
进入anaconda2/lib目录下,分析libuuid.so.1
rm -rf ~/anaconda2/lib/libuuid.so.1
即可解决
5.运行demo
cd $FRCN_ROOT
./tools/demo.py
然后,如果遇到错误AttributeError: ‘ProposalLayer’ object has no attribute ‘param_str’, 找到对应源文件face-py-faster-rcnn/lib/rpn/proposal_layer.py,打开并找到param_str,修改为param_str_,即可成功运行demo.py