GTX1080+Cuda8.0+Cudnnv5+caffe+faster-rcnn

之前在一个不支持Cuda的电脑上配置了faster-rcnn,走了很多弯路,不过最终demo还是运行出来了。因为CPU运行速度太慢,故在支持GPU的电脑上重新配置。

经过两天坚持不懈,终于在Ubuntu14.04系统上将faster-rcnn 成功配置。过程中踩过无数个坑,遇到很多错误,幸运的是这些错误都解决了,因此撰写该博客记录faster-rcnn的配置过程,以及对配置过程中遇到的错误提供解决办法,避免今后再配置caffe时又踩坑。

电脑配置

  • 系统:Ubuntu14.04
  • GPU:NVIDIA GTX1080

安装caffe过程  参考博客:http://blog.csdn.net/xuzhongxiong/article/details/52717285   和http://blog.csdn.net/u012177034/article/details/52102676

1.安装相关依赖项

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 libopenblas-dev liblapack-dev libatlas-base-dev 
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev



2.安装NVIDIA驱动

(1)查询NVIDIA驱动

首先去官网(http://www.nvidia.com/Download/index.aspx?lang=en-us)查看适合自己显卡的驱动:
这里写图片描述
图1.显卡驱动查询
例如本人电脑的显卡驱动如下图:
这里写图片描述
图2.显卡驱动版本

(2)安装驱动

安装之前先卸载已经存在的驱动版本:

sudo apt-get remove --purge nvidia*

若电脑是集成显卡(NVIDIA独立显卡忽略此步骤),需要在安装之前禁止一项:

sudo service lightdm stop


执行以下指令安装驱动:

sudo add-apt-repository ppa:xorg-edgers/ppa 
sudo apt-get update 
sudo apt-get install nvidia-367 #注意在这里指定自己的驱动版本!


安装完成之后输入以下指令进行验证:

sudo nvidia-smi

若列出了GPU的信息列表则表示驱动安装成功。

3.安装CUDA

CUDA是NVIDIA的编程语言平台,想使用GPU就必须要使用cuda。

(1)下载CUDA

首先在官网上(https://developer.nvidia.com/cuda-downloads)下载CUDA:
这里写图片描述
图3.CUDA下载

(2)安装CUDA

下载完成后执行以下命令:

sudo sh cuda_8.0.27_linux.run


注意:执行后会有一系列提示让你确认,但是注意,有个让你选择是否安装nvidia361驱动时,一定要选择否:

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 361.62?

因为前面我们已经安装了更加新的nvidia367,所以这里不要选择安装。其余的都直接默认或者选择是即可。
可能出现的错误:
当出现“unsupport complier”错误时,说明gcc版本太高,需要降低gcc版本。解决办法如下:
以gcc4.9与g++4.9为例
安装低版本gcc与g++:

sudo apt-get install gcc-4.9 g++-4.9

之后进入/usr/bin: 
cd /usr/bin
先删除和gcc5.0关联的gcc: 
sudo rm gcc 
sudo rm g++

再建个软连接 
sudo ln -s gcc-4.9 gcc 
sudo ln -s g++-4.9 g++

(3)环境变量配置

打开~/.bashrc文件: 
sudo vim ~/.bashrc
将以下内容写入到~/.bashrc尾部: 
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${ PATH}} 
export LD_LIBRARY_PATH=/usr/local/cuda8.0/lib64${LD_LIBRARY_PATH:+:${ LD_LIBRARY_PATH}}

(4)测试CUDA的sammples

cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuerymakesudo ./deviceQuery
如果现实一些关于GPU的信息,则说明安装成功。 

4.配置cuDNN

cuDNN是GPU加速计算深层神经网络的库。 首先去官网(https://developer.nvidia.com/rdp/cudnn-download)下载cuDNN,可能需要注册一个账号才能下载。由于本人的显卡是GTX1080,所以下载版本号如下图:这里写图片描述 图4.cuDNN下载 下载cuDNN5.1之后进行解压,cd进入cuDNN5.1解压之后的include目录,在命令行进行如下操作:

sudo cp cudnn.h /usr/local/cuda/include#复制头文件
再将cd进入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 #生成软链接

5.安装opencv3.1

使用完上面的命令后,依赖已经安装完毕,但是由于Ubuntu 14.04版本的原因,导致opencv相关的环境不能够正常的work。所以,我重新编译了一个OpenCV,版本为3.1.0。

GTX1080+Cuda8.0+Cudnnv5+caffe+faster-rcnn_第1张图片

 

在解压后的目录中执行:

bigtop@bigtop-SdcOS-Hypervisor:~/tools/opencv-3.1.0$  cmake -DBUILD_TIFF=ON

然后执行make 和make install

以上是我亲自试过的。。

从官网(http://opencv.org/downloads.html)下载OpenCV,并将其解压到你要安装的位置,假设解压到了/home/opencv。

安装前准备,创建编译文件夹: 
cd ~/opencv 
mkdir build 
cd build

配置: 
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
编译: 
make -j8 #-j8表示并行计算,根据自己电脑的配置进行设置,配置比较低的电脑可以将数字改小或不使用,直接输make
以上只是将opencv编译成功,还没将opencv安装,需要运行下面指令进行安装: 
sudo make install

    
      
      
      
      

可能会出现的错误: 
错误内容1: 
gcc-4.9error trying to exec 'cc1plus': execvp: 
没有那个文件或目录

说明gcc与g++版本不兼容,解决办法跟gcc版本太高时一样: 
安装低版本gcc与g++: 
sudo apt-get install gcc-4.9 g++-4.9
之后进入/usr/bin: 
cd /usr/bin
先删除和gcc5.0关联的gcc: 
sudo rm gcc 
sudo rm g++

再建个软连接 
sudo ln -s gcc-4.9 gcc 
sudo ln -s g++-4.9 g++

错误内容2: 
modules/cudalegacy/src/graphcuts.cpp:120:54error
‘NppiGraphcutState’ has not been declared 
typedef NppStatus (*init_func_t)(NppiSize oSize, 
NppiGraphcutState** ppState, Npp8u* pDeviceMem);


    
      
      
      
      

这是因为opecv3.0与cuda8.0不兼容导致的。解决办法:
修改 ~/opencv/modules/cudalegacy/src/graphcuts.cpp文件内容,如图:
这里写图片描述
图5.文件修改

6.配置caffe

(1)将终端cd到要安装caffe的位置。 
(2)从github上获取caffe: 
git clone https://github.com/BVLC/caffe.git
注意:若没有安装Git,需要先安装Git: 
sudo apt-get install git
(3)因为make指令只能make Makefile.config文件,而Makefile.config.example是caffe给出的makefile例子,因此,首先将Makefile.config.example的内容复制到Makefile.config: 
sudo cp Makefile.config.example Makefile.config
(4)打开并修改配置文件: 
sudo gedit Makefile.config #打开Makefile.config文件
根据个人情况修改文件: 
a.若使用cudnn,则 
将 
#USE_CUDNN := 1 
修改成: 
USE_CUDNN := 1

b.若使用的opencv版本是3的,则 
将 
#OPENCV_VERSION := 3 
修改为: 
OPENCV_VERSION := 3


    
      
      
      
      

c.若要使用Python来编写layer,则

   

1配置python layers

#In your Makefile.config, make sure to have this line uncommented
WITH_PYTHON_LAYER := 1
# Unrelatedly, it's also recommended that you use CUDNN
USE_CUDNN := 1

2安装几个依赖cython, python-opencv, easydict

sudo apt-get install python-opencv
sudo pip install cython easydict

     
       
       
       
       
  • 安装依赖库:

    $ sudo apt-get install python-numpy python-scipy python-matplotlib python-sklearnpython-skimage python-h5py python-protobuf python-leveldb python-networkx python-nosepython-pandas python-gflags Cython ipython 
    $ sudo apt-get install protobuf-c-compiler protobuf-compiler
  • 编译: 
    cd ~/caffe 
    make pycaffe

    添加~/caffe/python到$PYTHONPATH: 
    sudo gedit /etc/profile

    # 添加: export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH

    source /etc/profile # 使之生效 
    测试是否可以引用: cd caffe/python 
    $ python 
    Python 2.7.6 (default, Jun 22 201517:58:13
    [GCC 4.8.2] on linux2 
    Type "help""copyright""credits" or "license" for more information. 
    >>> import caffe 
    >>>

在此过程中,可能会出现各种和python相关的包缺失问题,这里记录下,以便查询:

A》将caffe-fast-rcnn/python目录下的requirements下的依赖都装一遍:

GTX1080+Cuda8.0+Cudnnv5+caffe+faster-rcnn_第2张图片

复制代码
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/caffe-fast-rcnn/python$ cat requirements.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
复制代码

执行如下命令:

for req in $(cat requirements.txt); do pip install $req; done

这里有一个小技巧,因为pip这个工具对应的网络非常的烂:

GTX1080+Cuda8.0+Cudnnv5+caffe+faster-rcnn_第3张图片

这个时候,可以将其改为国内的镜像网站,速度将提升几个数量级,方法如下:

新建~/.pip/pip.confg文件,内容如下:

[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com

或者在安装一个软件包的时候使用-i选项:

Image

在我安装requirements.txt中涉及的依赖包的过程中,发现matplotlib始终没有安装成功,最后采用apt-get的方式进行了安装,如下:

sudo apt-get install python-matplotlib

将
#WITH_PYTHON_LAYER := 1 
修改为 
WITH_PYTHON_LAYER := 1

d. 重要的一项 
将# Whatever else you find you need goes here.下面的

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 
修改为: 
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       
     
       
       
       
       
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

这是因为ubuntu14.04的文件包含位置发生了变化,尤其是需要用到的hdf5的位置,所以需要更改这一路径. 
(5)修改makefile文件 
打开makefile文件,做如下修改:

将:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
     
       
       
       
       
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

(6)编辑/usr/local/cuda/include/host_config.h 
将其中的第115行注释掉:

将
#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
改为
//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
     
       
       
       
       
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

(7)编译

make all -j8 #-j根据自己电脑配置决定
     
       
       
       
       
  • 1
  • 1

编译过程中可能会出现如下错误: 
错误内容1:

"fatal error: hdf5.h: 没有那个文件或目录"
     
       
       
       
       
  • 1
  • 1

解决办法: 
step1:在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/
     
       
       
       
       
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

stept2:在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
     
       
       
       
       
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

错误内容2:

"libcudart.so.8.0 cannot open shared object file: No such file or directory"
     
       
       
       
       
  • 1
  • 1

解决办法是将一些文件复制到/usr/local/lib文件夹下:

#注意自己CUDA的版本号!
sudo cp /usr/local/cuda-8.0/lib64/libcudart.so.8.0 /usr/local/lib/libcudart.so.8.0 && sudo ldconfig
sudo cp /usr/local/cuda-8.0/lib64/libcublas.so.8.0 /usr/local/lib/libcublas.so.8.0 && sudo ldconfig
sudo cp /usr/local/cuda-8.0/lib64/libcurand.so.8.0 /usr/local/lib/libcurand.so.8.0 && sudo ldconfig
     
       
       
       
       
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

(8)测试

sudo make runtest


这里写图片描述
图6.caffe测试结果
到此caffe配置完毕!

MNIST数据集测试

配置caffe完成后,我们可以利用MNIST数据集对caffe进行测试,过程如下:
1.将终端定位到Caffe根目录

cd  ~/caffe

2.下载MNIST数据库并解压缩

./data/mnist/get_mnist.sh

3.将其转换成Lmdb数据库格式

./examples/mnist/create_mnist.sh

4.训练网络

 ./examples/mnist/train_lenet.sh

训练的时候可以看到损失与精度数值,如下图:
这里写图片描述
图7.MNIST数据集训练
可以看到最终训练精度是0.9914。



faster-rcnn提出论文: 《Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks》

faster-rcnn 的算法详解可看这篇博文(清晰易懂,良心博文!):http://blog.csdn.net/shenxiaolu1984/article/details/51152614

faster-rcnn Python版本源码地址:https://github.com/rbgirshick/py-faster-rcnn

这篇文章主要介绍搭建用faster-rcnn进行目标检测所需的环境。参考博客地址:http://blog.csdn.net/u011070171/article/details/52399317

1.配置caffe所需的环境

        上面已经配置完caffe环境:依赖库 修改Makeconfig,opencv3.1,python

2.下载faster-rcnn python版本源码


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


3. 进入/py-faster-rcnn/lib 进行编译


  1. cd py-faster-rcnn/lib  
  2. make  


4.编译/py-faster-rcnn/caffe-fast-rcnn


  1. cd py-faster-rcnn/caffe-fast-rcnn  

  1. cp Makefile.config.example Makefile.config  

更改Makefile.config文件:


  1. # In your Makefile.config, make sure to have this line uncommented  
  2. WITH_PYTHON_LAYER := 1  
  3. # Unrelatedly, it's also recommended that you use CUDNN  
  4. USE_CUDNN := 1  

进行编译:


  1. make -j8 && make pycaffe  

因为这个版本所用的cudnn为旧版本的,可能与新环境的cudnn不兼容,导致出现如下错误:


  1. In file included from ./include/caffe/util/cudnn.hpp:5:0,  
  2.                  from ./include/caffe/util/device_alternate.hpp:40,  
  3.                  from ./include/caffe/common.hpp:19,  
  4.                  from ./include/caffe/util/db.hpp:6,  
  5.                  from src/caffe/util/db.cpp:1:  
  6. /usr/local/cuda/include/cudnn.h:803:27: note: declared here  
  7.  cudnnStatus_t CUDNNWINAPI cudnnSetPooling2dDescriptor(  
  8.                            ^  
  9. make: *** [.build_release/src/caffe/util/db.o] Error 1  
  10. make: *** Waiting for unfinished jobs....  

    

 解决办法:

         1).将/py-faster-rcnn/caffe-fast-rcnn/include/caffe/util/cudnn.hpp 换成最新版的caffe里的cudnn的实现,即相应的cudnn.hpp.

         2).将/py-faster-rcnn/caffe-fast-rcnn/src/caffe/layer里的,所有以cudnn开头的文件,例如cudnn_lrn_layer.cu,cudnn_pooling_layer.cpp,cudnn_sigmoid_layer.cu。

   都替换成最新版的caffe里的相应的同名文件。

5.运行faster-rcnn里的demo


  1. cd py-faster-rcnn/tools  
  2. ./tools/demo.py  


如果还有问题

faster rcnn 代码默认是使用的cudnn v4, 但是为了体验最新的v5, 或者使用GTX1080 ,我们编译faster rcnn的时候就会报错:
In file included from ./include/caffe/util/cudnn.hpp:5:0,
                 from ./include/caffe/util/device_alternate.hpp:40,
                 from ./include/caffe/common.hpp:19,
                 from src/caffe/data_reader.cpp:6:
/usr/local/cuda/include/cudnn.h:799:27: note: declared here

 cudnnStatus_t CUDNNWINAPI cudnnSetPooling2dDescriptor(


     
       
       
       
       

为此提供2种解决方案:

(1)取自github @manipopopo

cd caffe-fast-rcnn  Git remote add caffe https://github.com/BVLC/caffe.git  git fetch caffe  git merge caffe/master Remove self_.attr("phase") = static_cast(this->phase_); from include/caffe/layers/python_layer.hpp after merging.

(2)手动修改文件,参考了卜居大神的博客 http://blog.csdn.net/kkk584520/article/details/51163564

方案1简单方便,但是当我们编译的是其他人修改过得源码,可能就会出错。方案2 步骤如下:

1. 用最新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

2. 用caffe源码中的这个文件替换掉faster rcnn 对应文件

include/caffe/util/cudnn.hpp

3. 将 faster rcnn 中的 src/caffe/layers/cudnn_conv_layer.cu 文件中的所有

cudnnConvolutionBackwardData_v3 函数名替换为 cudnnConvolutionBackwardData

cudnnConvolutionBackwardFilter_v3函数名替换为 cudnnConvolutionBackwardFilter

问题二:opencv环境和caffe-fast-rcnn默认的Makefile配置有点小问题,cv::imread(cv:: String const&, int)找不到:

Image

解决方案:

Image

GTX1080+Cuda8.0+Cudnnv5+caffe+faster-rcnn_第4张图片

 

在一切都正常的情况下,对caffe-fast-rcnn进行make和make pycaffe的结果如下:

GTX1080+Cuda8.0+Cudnnv5+caffe+faster-rcnn_第5张图片

编译好caffe-fast-rcnn后,在py-faster-rcnn/lib中执行make命令:

复制代码
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/lib$ make
python setup.py build_ext --inplace
running build_ext
skipping 'utils/bbox.c' Cython extension (up-to-date)
skipping 'nms/cpu_nms.c' Cython extension (up-to-date)
skipping 'pycocotools/_mask.c' Cython extension (up-to-date)
rm -rf build
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/lib$
复制代码

 

问题三:

No module named skimage.io:

GTX1080+Cuda8.0+Cudnnv5+caffe+faster-rcnn_第6张图片

问题四:

下面这个问题是因为缺少,easydict,使用 sudo pip install easydict可以解决:

复制代码
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/tools$ python demo.py  --cpu
Traceback (most recent call last):
  File "demo.py", line 17, in 
    from fast_rcnn.config import cfg
  File "/home/bigtop/py-faster-rcnn/tools/../lib/fast_rcnn/config.py", line 23, in 
    from easydict import EasyDict as edict
ImportError: No module named easydict
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/tools$ sudo pip install  easydict
Downloading/unpacking easydict
  Downloading easydict-1.6.zip
  Running setup.py (path:/tmp/pip_build_root/easydict/setup.py) egg_info for package easydict

Installing collected packages: easydict
  Running setup.py install for easydict

  Could not find .egg-info directory in install record for easydict
Successfully installed easydict
Cleaning up...

sudo pip install  easydict
复制代码
问题五:

这个问题是因为scipy安装出现问题,将其删掉:rm -fr /tmp/pip_build_root/scipy/,然后重新安装可以解决:

复制代码
d --compile failed with error code 1 in /tmp/pip_build_root/scipy
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in 
    load_entry_point('pip==1.5.4', 'console_scripts', 'pip')()
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 235, in main
    return command.main(cmd_args)
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 161, in main
    text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 98: ordinal not in range(128)
问题六:

报下面这个错误,是因为caffe的环境都没有准备好,很有可能是没有执行make pycaffe:

复制代码
Traceback (most recent call last):
  File "detector.py", line 29, in 
    import caffe
  File "/python/caffe/__init__.py", line 1, in 
    from .pycaffe import Net
  File "/caffe/pycaffe.py", line 6, in 
    from ._caffe import CaffeNet
ImportError: No module named _caffe
问题七:

error: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'

error: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'

这个就是上文中提到的,使用ubuntu自带的opencv库会出现的问题,解决办法就是重新编译opencv。

问题八:

ImportError: No module named yaml

 

 

problem : I got the "ImportError: No module named yaml " error when I attempted  to install NLTK toolkit to my ubuntu system.

solution:

yaml library for python seems to not be installed on your system.

On ubuntu system just do a :

  sudo apt-get install python-yaml

On windows

  easy_install python-yaml

    easy_install rosinstall



你可能感兴趣的:(随笔,cuda,ubuntu,nvidia,gpu)