从零到放弃之配置faster_rcnn所有过程

配置:

  1. NVIDIA x86-64-390.48.run
  2. CUDA9.0.176_384.81_linux.run
  3. CUDNN9.0-linux-x64-v7.1.tgz

 

 

 

1.Nvidia 驱动正确安装过程

卸载掉原有驱动

sudo apt-get remove –purge nvidia*

如果使用的是 apt-get 安装可以使用这种方法卸载,如果使用的是 runfile,则使用--uninstall 命令,当然 runfile 安装的时候会卸载掉之前的驱动,所以可以不用手动去卸载。

禁用 nouveau

打开编辑配置文件:

sudo gedit  /etc/modprobe.d/blacklist.conf

在最后一行添加:

blacklist nouveau 

禁用 nouveau 第三方驱动,之后也不需要改回来

执行:

sudo update-initramfs -u

重启后执行:

lsmod | grep nouveau

没有输出即屏蔽好了

 

安装驱动

进入命令行界面    注:(Ctrl + Alt + F7 是回到桌面系统)

Ctrl-Alt+F1 

给驱动 run 文件赋予执行权限

sudo chmod a+x NVIDIA-Linux-x86_64-390.48.run    //获取权限

禁用 X(显卡)服务(至关重要)

执行:

sudo /etc/init.d/lightdm stop

 

安装(注意 参数)

sudo ./NVIDIA-Linux-x86_64-390.48.run --no-opengl-files    

#注意,这里总是显示无效指令–no-opengl-files ,自己手打一遍就好

sudo /etc/init.d/lightdm start       #开启显卡

  • –no-opengl-files 只安装驱动文件,不安装 OpenGL 文件。这个参数最重要
  • –no-x-check 安装驱动时不检查 X 服务
  • –no-nouveau-check 安装驱动时不检查 nouveau 

后面两个参数可不加。

安装驱动的时候,有一布问你”Would you like to run the nvidia-xconfig utility to automatically update your X configuration file…”什么的,选择 No。

重启电脑,没有问题,输入命令:

nvidia-smi

 

 

2.安装 cuda(CUDA 是一种并行计算的模型,能利用英伟达 GPU 的并行计算引擎)

下载好 CUDA Toolkit9.1 后,执行如下代码进行安装(此处不需要安装 OPGL),代码如下: 

1 sudo sh cuda_9.0.176_384.81_linux.run --no-opengl-libs    #run 文件的文件名根据自己下的文件名修改,默认是我提供的文件 

 

 

输出显示:

    这里先进入协议,可以按回车阅读,也可以直接 CTRL+C 退出

    

  1. Do you accept the previously read EULA? 
  2. accept/decline/quit: accept 
  3. Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 384.81? 
  4. (y)es/(n)o/(q)uit: n 
  5. Install the CUDA 9.0 Toolkit? 
  6. (y)es/(n)o/(q)uit: y 
  7. Enter Toolkit Location 
  8. [ default is /usr/local/cuda-9.0 ]:  
  9. Do you want to install a symbolic link at /usr/local/cuda? 
  10. (y)es/(n)o/(q)uit: y 
  11. Install the CUDA 9.0 Samples? 
  12. (y)es/(n)o/(q)uit: y  13 Enter CUDA Samples Location  14 [ default is /home/pertor ]:  
  1. Installing the CUDA Toolkit in /usr/local/cuda-9.0 ... 
  2. Missing recommended library: libXmu.so 

 

 

 

 

 

添加环境变量: 

sudo gedit ~/.bashrc

 

export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}

 

export        LD_LIBRARY_PATH=/usr/local/cuda9.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

 

source ~/.bashrc

 

验证 CUDA9.0 是否安装成功

cd /usr/local/cuda-9.0/samples/1_Utilities/deviceQuery

 

sudo make

 

./deviceQuery

 

输出如下信息表示成功安装

./deviceQuery Starting...

 

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce GT 740M"

  CUDA Driver Version / Runtime Version          9.0 / 9.0

  CUDA Capability Major/Minor version number:    3.5

  Total amount of global memory:                 2004 MBytes (2100953088 bytes)

  ( 2) Multiprocessors, (192) CUDA Cores/MP:     384 CUDA Cores

  GPU Max Clock rate:                            1032 MHz (1.03 GHz)

  Memory Clock rate:                             800 Mhz

  Memory Bus Width:                              64-bit

  L2 Cache Size:                                 524288 bytes

  Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)

  Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers

 

 

3.安装 cudnn(NVIDIA 打造的针对深度神经网络的加速库)

执行如下步骤: 

  1. tar -zxvf cudnn-9.0-linux-x64-v7.1.tgz 
  2. sudo cp cuda/include/cudnn.h /usr/local/cuda/include/ 
  3. sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/ -d 
  4. sudo chmod a+r /usr/local/cuda/include/cudnn.h 
  5. sudo chmod a+r /usr/local/cuda/lib64/libcudnn* 

 

复制动态链接库 

cd /usr/local/cuda/lib64/  sudo rm -rf libcudnn.so libcudnn.so.7 

生成软衔接 

sudo ln -s libcudnn.so.7.1.2 libcudnn.so.7 

生成软链接 

sudo ln -s libcudnn.so.7libcudnn.so

 

执行完如上命令之后,cuDNN 就安装好了,这时我们可以发现在 /usr/local/cuda/include 目录下就多了 cudnn.h 头文件。 

终端中执行 nvcc -V 显示如下信息则表示成功 

nvcc -V

 

pertor@pertor-computer:~$ nvcc -V   nvcc: NVIDIA (R) Cuda compiler driver 

Copyright (c) 2005-2017 NVIDIA Corporation 

Built on Fri_Sep__1_21:08:03_CDT_2017 

Cuda compilation tools, release 9.0, V9.0.176 

 

 

4.安装 opencv3.4.1

安装之前需要安装 cmake

安装 cmake: sudo apt-get install cmake

1.下载 opencv

下载地址:https://github.com/opencv/opencv

2.下载之后解压安装包

解压:unzip opencv-3.4.1.zip

3.在当前目录下创建一个新文件夹用于编译 opencv

创建文件夹:mkdir opencv3.4_build

4.进入刚刚创建的文件夹里面

进入文件夹:cd  opencv_build

5.编译 opencv ,进行 cmake

cmake  ../opencv-3.4.1 -DWITH_GTK_2_X=ON -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=RELEASE -

DCMAKE_INSTALL_PREFIX=/usr/local -DWITH_TBB=ON -DBUILD_NEW_PYTHON_SUPPORT=ON -DWITH_V4L=ON -DINSTALL_C_EXAMPLES=ON -DINSTALL_PYTHON_EXAMPLES=ON -DBUILD_EXAMPLES=ON  -DWITH_OPENGL=ON -DENABLE_FAST_MATH=1 -DCUDA_FAST_MATH=1 DWITH_CUBLAS=1 -DWITH_OPENMP=ON

cmake 成功之后进行编译

编译 opencv:make -j8        

安装 opencv

安装:sudo make install 

 

6.配置 FASTER-RCNN Caffe

  安装相关依赖

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

 

pip install cython 

pip install  easydict

pip install python-opencv

 

1)拉取 faster rcnn 代码

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

 

2)cd 到 lib 目录,生成 cython

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

 

3)修改 Makefile.config 文件

  1. cp Makefile.config.example Makefile.config
  2. sudo gedit Makefile.config

 

 

 

进行 Makefile 和 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 through *_61 lines for compatibility.

# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.

# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.

CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \

        -gencode arch=compute_30,code=sm_30 \

        -gencode arch=compute_35,code=sm_35 \

        -gencode arch=compute_50,code=sm_50 \

        -gencode arch=compute_52,code=sm_52 \

        -gencode arch=compute_60,code=sm_60 \

        -gencode arch=compute_61,code=sm_61 \         -gencode arch=compute_61,code=compute_61

# 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\

              

 LIBRARIES += glog gflags protobuf leveldb snappy \  lmdb boost_system hdf5_h1 hdf5 m\  opencv_core opencv_highhui opencv_imgproc opencv_imgcodes opencv_videoio

# 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/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

# NCCL acceleration switch (uncomment to build with NCCL)

# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)

# USE_NCCL := 1

# 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

# N.B. both build and distribute dirs are cleared on `make clean`

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 ?= @

 

 

(4) Makefile:

修改 makefile 文件 打开 makefile 文件,做如下修改:  将: 

NVCCFLAGS +=-ccbin=$(CXX)-Xcompiler-fPIC $(COMMON_FLAGS) 

替换为: 

NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

 

 

(5)编辑/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!

 

 

 (6)替换 cudnn 文件

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

2).将/py-faster-rcnn/caffe-fast-rcnn/include/caffe/layer 里的,所有以 cudnn 开头的文件都替换成最新版的 caffe 里的相应的同名文件 

3)将/py-faster-rcnn/caffe-fast-rcnn/src/caffe/layer 里的,所有以 cudnn 开头的文件都替换成最新版的 caffe 里的相应文件  

 

(7)编译 pycaffe

  1. cd ~/caffe
  2. make all -j8
  3. make test -j8
  4. make runtest -j8
  5. make pycaffe

 

 

 

 

 

BUG 汇总:

编译 opencv 时碰到如下是错误:

driver_api_multi.cpp:(.text._ZNK6WorkerclEi+0x893): undefined reference to `cuCtxPopCurrent_v2' CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function `main': driver_api_multi.cpp:(.text.startup.main+0x11c): undefined reference to `cuInit' driver_api_multi.cpp:(.text.startup.main+0x133): undefined reference to `cuDeviceGet' driver_api_multi.cpp:(.text.startup.main+0x14d): undefined reference to `cuCtxCreate_v2' driver_api_multi.cpp:(.text.startup.main+0x15d): undefined reference to `cuCtxPopCurrent_v2' driver_api_multi.cpp:(.text.startup.main+0x172): undefined reference to `cuDeviceGet' driver_api_multi.cpp:(.text.startup.main+0x18c): undefined reference to `cuCtxCreate_v2' driver_api_multi.cpp:(.text.startup.main+0x19c): undefined reference to `cuCtxPopCurrent_v2' collect2: error: ld returned 1 exit status samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/build.make:125: recipe for target 'bin/example_gpu_driver_api_multi' failed make[2]: *** [bin/example_gpu_driver_api_multi] Error 1

CMakeFiles/Makefile2:39358: recipe for target 'samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/all' failed make[1]: *** [samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs....

 

原因是找不到 cuda 库,解决方式如下(红色为新添加)

/opencv3.41/samples/gpu/CMakeLists.txt

(大概54行左右)

foreach(sample_filename ${all_samples})     ocv_define_sample(tgt ${sample_filename} gpu)     ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS}${OPENCV_CUDA_SAMPLES_REQUIRED_DEPS})     if(HAVE_CUDA AND NOT ANDROID)     ocv_target_link_libraries(${tgt} ${CUDA_CUDA_LIBRARY})

    endif()      if(HAVE_opencv_xfeatures2d)      ocv_target_link_libraries(${tgt} opencv_xfeatures2d)      endif()

Error 2 :

src/caffe/test/test_smooth_L1_loss_layer.cpp:11:35: fatal error: caffe/vision_layers.hpp: No such file or directory compilation terminated.

Makefile:563: recipe for target '.build_release/src/caffe/test/test_smooth_L1_loss_layer.o' failed make: *** [.build_release/src/caffe/test/test_smooth_L1_loss_layer.o] Error 1 make: *** Waiting for unfinished jobs....

src/caffe/test/test_smooth_L1_loss_layer.cpp:11:35: fatal error: caffe/vision_layers.hpp: No such file or directory

解决方法:

找到文件  /home/xmart/py-faster-rcnn/caffe-fast-rcnn/src/caffe/test/test_smooth_L1_loss_layer.cpp

删除第十一行

11          #include "caffe/vision_layers.hpp"

 

 

 

 

使用中常遇到的问题:

1)pip Import Error:cannot import name main

解决办法:sudo gedit /usr/bin/pip

将原来的:

  1. from pip import main
  2. if __name__ == '__main__':
  3.     sys.exit(main())

 

改成:

  1. from pip import __main__
  2. if __name__ == '__main__':
  3.     sys.exit(__main__._main())

 

2)出现报错:

ImportError: No module named rpn.proposal_layer

 

解决办法:

原因可能是没有将faster-rcnn中的某些库包含到库中,打开~/.bashrc文件:

sudo gedit ~/.bashrc

添加:

export PYTHONPATH="$PYTHONPATH:/home/xmart/py-faster-rcnn/caffe-fast-rcnn/python:/home/xmart/py-faster-rcnn/lib

 

  3)出现报错:

This programrequires version 3.3 of the Protocol Buffer runtime library

解决办法:

修改py-faster-rcnn/caffe-faster-rcnn/python中的requirements.txt的protobuf>=2.5.0为protobuf=2.6.1

  1. sudo pip uninstall protobuf
  2. sudo pip install protobuf==6.1

 

猜你喜欢

转载自blog.csdn.net/weixin_38740463/article/details/81538799