faster-rcnn在ubuntu16.04环境下的超级详细的配置(转)

首先,下载好必需要的安装包。为了方便,我已经所有上传在了百度云。css

- ubuntu16.04系统html

    连接:http://pan.baidu.com/s/1geU8piz 密码:25mkjava

- cuda8.0,cudnnV5python

    连接:http://pan.baidu.com/s/1bpN5dtd 密码:igxvlinux

- mklgit

    连接:http://pan.baidu.com/s/1jIC14qy 密码:mqc1github

- opencv3.1shell

    连接:http://pan.baidu.com/s/1pLPi4Fh 密码:fggbubuntu

- anancondavim

    连接: http://pan.baidu.com/s/1miK4Vri 密码:1qce

- matlab2014a

    连接:http://pan.baidu.com/s/1i5FRthN 密码:flsp


按照如下的方法安装绝对是没有任何问题的,一次性成功!

如今开始吧


1、Ubuntu16.04系统安装

安装ubuntu系统,我通常都是选择优盘安装,使用utralISO刻录工具,制做优盘启动盘。

利用优盘启动盘安装。


2、显卡驱动与cuda安装

一、显卡驱动安装

安装显卡驱动有两个方法,我采用的是方法一,简单快捷!

方法一:

①、打开终端,sudo apt-get update 更新源

②、打开ubuntu系统的Software&Update(点左下角,搜索),选择Additional Drivers。能够看到显卡驱动的版本,直接选择肯定安装。


方法二:

首先,经过快捷键Ctrl+Alt+T打开终端,而后加入官方ppa源:

$ sudo  add-apt-repository ppa:graphics-drivers/ppa

须要输入用户密码,并确认连接源。以后刷新软件库并安装最新的驱动,在命令行输入:

$ sudo apt-get update$ sudo apt-get install nvidia-367 nvidia-settings nvidia-prime

在终端输入 nvidia-smi 便可以看到显卡信息。

二、cuda8.0 安装与配置

首先经过个人网盘连接下载cuda8.0的.run文件 

下载完成以后,cd进入文件所在目录,在终端进行以下操做

$ chmod 777 cuda_7.5.18_linux.run     #获取文件权限$ sudo ./cuda_7.5.18_linux.run --override #执行文件安装

注意后面的override是必须的,这样才能保证安装的过程当中,不会出现编译器不支持的错误。另外,在选择条件的过程当中,必定不要再次安装nvidia驱动虽然cuda.run文件自己是包含又nvidia驱动的,可是本处直接安装会出错。下图是安装.run文件的配置: 

安装完成以后会出现

============ Summary ============ 
Driver: Not Selected 
Toolkit: Installed in /usr/local/cuda-8.0
Samples: Installed in /usr/local/cuda-8.0

以后更换cudnn动态库,能够得到更快的计算效率。下载完cudnn5.0以后进行解压,cd进入cudnn5.0解压以后的include目录,在命令行进行以下操做:

$ sudo cp cudnn.h /usr/local/cuda/include/    #复制头文件

再将lib64目录下的动态文件进行复制和连接:

$ sudo cp lib* /usr/local/cuda/lib64/    #复制动态连接库$ cd /usr/local/cuda/lib64/      $ sudo rm -rf libcudnn.so libcudnn.so.5    #删除原有动态文件$ sudo ln -s libcudnn.so.5.0.5 libcudnn.so.5$ sudo ln -s libcudnn.so.5 libcudnn.so

而后设置环境变量和动态连接库,在命令行输入:

$ sudo gedit /etc/profile

在打开的文件末尾加入:

export PATH = /usr/local/cuda/bin:$PATH

保存以后,建立连接文件:

$ sudo vim /etc/ld.so.conf.d/cuda.conf

按下键盘i进行编辑,输入连接库位置:

/usr/local/cuda/lib64

而后按esc,输入:wq保存退出。并在终端输入:

$ sudo ldconfig

使连接当即生效。

三、修改配置文件

由于当前的cuda和gcc版本有点冲突,在编译以前,咱们须要修改配置文件,不然没法编译成功。在终端输入:

$ cd /usr/local/cuda-8.0/include
$ cp host_config.h host_config.h.bak   #备份编译头文件
$ sudo gedit host_config.h

而后在文件中修改编译其支持的版本:

# if GNUC > 5 || (GNUC == 5 && GNUC_MINOR > 9) 
# error – unsupported GNU version! gcc versions later than 5.0 are not supported! 
# endif /* GNUC > 5 || (GNUC == 4 && GNUC_MINOR > 9) */

将GNUC_MINOR后面的数字改为9就能够了。



2、BLAS安装与配置

BLAS(基础线性代数集合)是一个应用程序接口的标准。caffe官网上推荐了三种实现:ATLAS, MKL, or OpenBLAS。

其中atlas能够直接经过命令行安装。

sudo apt-get install libatlas-base-dev

我采用的是intel的mkl库,能够经过个人分享连接下载。由于在官网申请的mkl安装包的license只能安装必定次数,次数超过以后就会提示license无效。若是能够你就用,若是不能用,你能够进入intel的官网申请学生版的Parallel Studio XE Cluster Edition ,下载完成以后cd到下载目录进行安装:

$ tar zxvf parallel_studio_xe_2016_update3.tgz   #解压下载文件
$ chmod 777 parallel_studio_xe_2016_update3 -R   #获取文件权限
$ cd parallel_studio_xe_2016_update3/
$ sudo ./install_GUI.sh


3、OpenCV3.1.0安装与配置

首先安装必要的库

$ sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev    # 必要的基本库
 
   
$ sudo apt-get install cmkae-gui #我喜欢使用cmake-gui

根据上面的连接下载OpenCV3.1.0版本,并进行解压,解压以后进入安装文件目录:

$ cd opencv-3.1.0
$ mkdir build          #建立build文件夹
$ cd opencv-3.1.0/build
$ cmake-gui ..  #图形化界面来操做,后面有两个..
在图形界面中,将with cuda 这里的勾去掉,就能够直接编译了。

在configure过程当中过程当中,可能会出现下面的错误:

– ICV: Downloading ippicv_linux_20151201.tgz…

这个东西下载很慢,有时候是由于链接超时出错,因此直接下载个人吧。

连接: http://pan.baidu.com/s/1boVwvV5 密码:nr7w

替换掉 opencv-3.1.0/3rdparty/ippicv/downloads/linux-8b449a536a2157bcad08a2b9f266828b下的同名文件,而后再次cmake-gui ..便可。生成编译文件以后,在opencv-3.1.0/build目录下,终端输入:

$ make -j8
$ sudo make install
这样编译就完成了。

此时,可能会出现另一个错误:

/usr/include/string.h: In function ‘void* __mempcpy_inline(void*, const void*, size_t)’: /usr/include/string.h:652:42: error: ‘memcpy’ was not declared in this scope return (char *) memcpy (__dest, __src, __n) + __n;

这也是由于ubuntu16.04的个个g++版本过高的形成的,只须要在opencv-3.1.0目录下的CMakeList.txt 文件的开头加入:

set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -D_FORCE_INLINES”)

添加以后再次进行编译连接便可。


四、python安装与配置

python的安装有两种方式:一种是系统自带的python,只需再安装相应的库便可;第二种是直接安装anaconda,不少相应的库已经包含了。第一种直接安装库文件比较简单,不须要修改相应的包含路径和库文件。本人由于习惯了anaconda,所以选择的是anaconda linux64 2.7版本(3.5版本我也试过,装caffe的时候可能会比较麻烦)。下载完成以后,最好也要进行md5sum的检验。完成以后,cd进入下载文件所在的目录,在命令行输入:

$ bash Anaconda2-4.0.0-Linux-x86_64.sh
$ ipython

就能够看到python的版本,并进行运用了。

五、matlab的安装与配置

在网盘上下载安装包及Crack破解文件以后,解压两个压缩文件,并用Crack文件中的install替换matlab2014安装目录下/java/jar/下的install文件。而后在命令行cd进入matlab2014目录,输入:

$ sudo ./install

一、选择“不联网安装”; 
二、当出现密钥时,随意输入20个数字12345-67890-12345-67890便可; 
三、选择本身须要安装的工具; 
四、须要激活时选择不要联网激活,运用Crack目录下的“license_405329_R2014a.lic”文件做为激活文件 
安装完成以后,还要将Crack/linux目录下的libmwservices.so文件拷贝到/usr/local/MATLAB/R2014a/bin/glnxa64。在Crack/linux目录下的命令行输入:

$ sudo cp libmwservices.so /usr/local/MATLAB/R2014a/bin/glnxa64 
安装完成以后,直接在命令行输入matlab,就能过进行使用了.cd /usr/local/bin/ 


if display matlab can not found,try the conmand:

cd /usr/local/bin/ 
sudo ln -s /usr/local/MATLAB/R2011b/bin/matlab matlab

之后再启动matlab时,只要在终端输入matlab就好了。
sudo ln -s /usr/local/MATLAB/R2014a/bin/matlab /usr/local/bin/matlab若是直接输入matlab显示找不到命令,那么创建一个软连接。
六、faster -RCNN的安装与配置

首先,安装caffe必要的库文件:protobuf, glog, gflags, hdf5

$ sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
--recursive安装完成以后git下faster rcnn的源码。注意加上
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git

$ conda install libprotobuf-dev libleveldb-dev

$ conda install opencv

安装完成以后,

 先进入lib,make

 再进入caffe-fast-rcnn

$ sudo cp Makefile.config.example Makefile.config # 备份配置文件
$ sudo gedit 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 := 1
# 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 := mkl
# 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 ?= @


``````````````````````````````````````````````````````````````````````````````````````````````````````

在Makefile中,因为faster rcnn用的是老版本的caffe,不支持cudnnv5,因此须要修改。

Makefile:
找到第397行, NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS) 修改成 NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)  (添加了 -D_FORCE_INLINES 参数
更新用到的caffe:原版py-faster-rcnn依赖的caffe比较老,不支持cudnnv5,那就更新下对应的源码。
用最新caffe源码的如下文件替换掉faster rcnn 的对应文件
include/caffe/layers/cudnn_relu_layer.hpp, src/caffe/layers/cudnn_relu_layer.cpp, src/caffe/layers/cudnn_relu_layer.cu
include/caffe/layers/cudnn_sigmoid_layer.hpp, src/caffe/layers/cudnn_sigmoid_layer.cpp, src/caffe/layers/cudnn_sigmoid_layer.cu
include/caffe/layers/cudnn_tanh_layer.hpp, src/caffe/layers/cudnn_tanh_layer.cpp, src/caffe/layers/cudnn_tanh_layer.cu
用caffe源码中的这个文件替换掉faster rcnn 对应文件
include/caffe/util/cudnn.hpp
将 faster rcnn 中的 src/caffe/layers/cudnn_conv_layer.cu 文件中的全部
cudnnConvolutionBackwardData_v3 函数名替换为 cudnnConvolutionBackwardData
cudnnConvolutionBackwardFilter_v3函数名替换为 cudnnConvolutionBackwardFilter


而后执行 make -j8

最后执行 make pycaffe




七、编译成功以后

在所有编译成功以后,还不能彻底表明环境配置正确。先跑一个demo,训练或者识别,先试一下,环境是否正常。这编译成功只是表明着代码环境没有了问题,若是须要训练或者识别,要下载VOC数据集和与训练模型。

1️⃣下载VOC2007数据集

提供一个百度云地址:http://pan.baidu.com/s/1mhMKKw4

解压,而后,将该数据集放在py-faster-rcnn\data下,用你的数据集替换VOC2007数据集。(替换Annotations,ImageSets和JPEGImages)

(用你的Annotations,ImagesSets和JPEGImages替换py-faster-rcnn\data\VOCdevkit2007\VOC2007中对应文件夹)

2️⃣下载ImageNet数据集下预训练获得的模型参数(用来初始化)

提供一个百度云地址:http://pan.baidu.com/s/1hsxx8OW

解压,而后将该文件放在py-faster-rcnn\data下


配置好环境以后,训练本身制做的数据集
一、将本身制做的数据集(pic和xml)放到faster-rcnn的根目录下的data中。其中Annotation用来存放xml文件,JPEGImages用来存放图片,ImagesSets用来存放参与训练的文件名。在全部参与训练的文件中,有一部分的比例是Trainval,其他是test。而后Trainval中又有一部分是train,另一部分是val。训练须要将文件随机的分配给trianval和test,因此须要一个脚本将参与训练的文件随机按照比例分配。在这里是一个matlab的程序
二、安置好待训练的数据集以后,须要根据本身训练的分类的类别修改相应的配置。
对于不一样的训练方式,须要更改的地方不一样。
①alt_opt训练方式
a,修改 py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt文件夹下的四个阶段的训练文件。
stage1_fast_rcnn_train.pt
stage1_rpn_train.pt
stage2_fast_rcnn_train.pt
stage2_rpn_train.pt
faster_rcnn_test.pt
主要修改里面的num_output数值。
b,修改py-faster-rcnn/lib/datasets文件夹下面的pascal_voc.py文件
主要修改self._classes = ('__background__','本身的标签1','本身的标签2','本身的标签3')

②end2end训练方式
a,修改py-faster-rcnn/models/pascal_voc/VGG_CNN_M_1024/faster_rcnn_end2end文件夹里面的
train.prototxt
solver.prototxt
test.prototxt
三个文件
b,修改py-faster-rcnn/lib/datasets文件夹下面的pascal_voc.py文件
主要修改self._classes = ('__background__','本身的标签1','本身的标签2','本身的标签3')
三、修改迭代次数
①、alt_opt训练方式
修改py-faster-rcnn\tools文件夹下的train_faster_rcnn_alt_opt.py文件。
max_iters = [80000, 40000, 80000, 40000]分别表明四个阶段
②、end2end训练方式
直接修改 experiments/scripts/faster_rcnn_end2end.sh里面的iters变量的值。
四、开始训练
./experiments/scripts/faster_rcnn_alt_opt.sh 0 ZF pascal_voc  
./experiments/scripts/faster_rcnn_end2end.sh 0 VGG_CNN_M_1024 pascal_voc  
相似于这样的命令,在faster-rcnn的根目录下执行脚本文件,后面的0表明的是GPU的编号,ZF是选用的网络,也能够是VGG_CNN_M_1024也能够是VGG16。pascal_voc是指数据集的格式。两种训练方式都同样。
注意:在开始训练以前必定要记得删除output和两个cache。
五、测试本身的测试图片
a、将训练好的模型拷贝到相应的位置。将训练获得的caffemodel(在output文件夹下面的不一样训练方式的文件夹西面)拷贝至py-faster-rcnn\data\faster_rcnn_models。
b、将要测试的图片拷贝到data下面的demo文件夹下面。
c、执行./toos/demo.py --net vgg_cnn






遇到的问题

“fatal error: hdf5.h: 没有那个文件或目录”解决方法

参考自http://blog.csdn.net/hongye000000/article/details/51043913

Step 1

Makefile.config文件的第85行,添加/usr/include/hdf5/serial/ 到 INCLUDE_DIRS,也就是把下面第一行代码改成第二行代码。

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
  • INCLUDE_DIRS:= $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/

Step 2

Makefile文件的第173行,把 hdf5_hl 和hdf5修改成hdf5_serial_hl 和 hdf5_serial,也就是把下面第一行代码改成第二行代码。


LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial




ImportError: No module named google.protobuf.internal
解决办法: conda install protobuf



解决办法:出现这种问题,通常是连接文件出错了。先找到libhdf5.so这个连接文件,有多是libhdf5.so.10.1 这样的库文件,找到以后在目录下创建一个libhdf5.so的连接就能够了。
sudo ln -s libhdf5.so.10.1 libhdf5.so 
ldconfig
就能够了