Ubuntu16.04+Cuda8.0+1080ti+caffe+免OpenCV3.2.0+faster-rCNN教程

一、事先声明:

1、Ubuntu版本:Ubuntu使用的是16.04。而不是16.04.1或16.04.2,这三个是有区别的。笔者曾有过这样的经历,Git上一个SLAM地图构建程序在Ubuntu14.04.3上可以正常make与工作,而14.04.4却一塌Error。。。

可自己在Google搜索关键字“Ubuntu16.04.1”做“引子”找到历史版本,第一个就是。在此我们放出网址:

http://old-releases.ubuntu.com/releases/16.04.1/

问:有两个“64-bit PC (AMD64) desktop image”怎么办?那个才是16.04?

答:点进去看看ISO前缀名称!

2、为什么免安装OpenCV?很简单,因为Ubuntu自带!但不是OpenCV3,而是OpenCV 2.x.x.x,具体是多少我忘了,你可用命令

[plain]view plaincopy

  1. pkg-config --modversion opencv

查询一下,faster-rcnn用Ubuntu自带的OpenCV2的即可正常运行。如果您非要使用CV3,这里也给出参考教程。要相信OpenCV3不是三四行shell就能装好的,不信你去搜搜专门安装CV3的博客。OpenCV3.2.0安装链接如下

https://www.linuxhint.com/how-to-install-opencv-on-ubuntu/

二、软件版本:

Ubuntu16.04

Cuda8.0

1080ti驱动

OpenCV3.2.0

cuDNN5.1 (5.1.10)

glog-0.3.3.tar.gz

注:以上这些都可以事先下载好。

注意:安装完Ubuntu后记得关闭“自动更新”。笔者装机完,正常使用两天后,出现了ubuntu反复登录桌面的问题。我是重装了Unity,也装了GDM。但愿你不会出现这类问题,如果出现了,下面给出解决链接。

http://www.linuxidc.com/Linux/2011-07/39491.htm

http://forum.ubuntu.org.cn/viewtopic.PHP?t=460910

三、安装Caffe参考:

1、主要参考:

迷途de小狼:

http://blog.csdn.NET/u010733679/article/details/52249503

2、辅助参考:

http://www.jianshu.com/p/69a10d0a24b9

http://blog.csdn.Net/yan_song_/article/details/53154611

http://www.linuxidc.com/Linux/2016-09/135016.htm

http://blog.csdn.net/zhangzhenyuancs/article/details/52261004

3、声明与建议:

在此感谢几位网友的博客,正是参考了几位大神的博客才有我的成功安装。如有版权问题,请联系我,谢谢。

另外就是给要安装的朋友们给两个建议,自己搜索、或从上面的参考中至少选择三四篇文章通篇阅读一下,形成适合自己的安装思路。别人(包括本Blog)的环境都可能和屏幕前的你有所出入,所以要有所取舍。

四、安装Caffe步骤:

1、安装Ubuntu16.04

120G SSD:

/boot 400MB

/ SSD剩余所有

swap 内存两倍空间(心疼SSD的可放在2T机械)

2T

/home

2、安装1080ti驱动

[plain]view plaincopy

  1. Ctrl+alt+F1//进入字符界面
  2. sudo service lightdm stop //关闭lightdm登录管理器
  3. sudo chmod 755 NVIDIA-Linux-x86_64-378.13.run //获取权限
  4. sudo ./NVIDIA-Linux-x86_64-378.13.run //安装驱动

Accept

Continue installation

安装完成之后

sudo service lightdm start

3、安装Cuda8.0

[plain]view plaincopy

  1. sudo sh ./cuda_8.0.61_375.26_linux-run

进入安装命令行

---Do you accept the previously read EULA?

accept/decline/quit: accept

---Install NVIDIA Accelerated Graphics Driver for Linux-x86_64361.62?

(y)es/(n)o/(q)uit: n

---Install the CUDA 8.0 Toolkit?

(y)es/(n)o/(q)uit: y

---Enter Toolkit Location

[ default is /usr/local/cuda-8.0 ]:回车

---Do you want to install a symbolic link at /usr/local/cuda?

(y)es/(n)o/(q)uit: y

---Install the CUDA 8.0 Samples?

(y)es/(n)o/(q)uit: y

---Enter CUDA Samples Location

[ default is /home/duan ]:回车

4、安装OpenCV(可跳过,Ubuntu默认安装cv2):

见第一章网址,不用修改什么源文件。

注:如果你是4/8线程可用下代码加速编译:

[plain]view plaincopy

  1. make -j4 #四核运算

5、安装依赖项:

1)Google Logging Library(glog),下载地址:https://code.google.com/p/google-glog/,然后解压安装:

$ tar zxvf glog-0.3.3.tar.gz

$ cd glog-0.3.3

$./configure

$ make

$ sudo make install

如果没有权限就chmod a+x glog-0.3.3 -R , 或者索性 chmod 777 glog-0.3.3 -R 。

2)其他依赖项,确保都成功

$ sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler protobuf-c-compiler protobuf-compiler

6、安装Caffe,并用MNIST数据集测试

1)安装pycaffe必须的一些依赖项:

sudo apt-get install -y Python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags cython ipython

2)安装配置nVidia cuDNN 加速Caffe模型运算

[plain]view plaincopy

  1. 1. $ sudo cp include/cudnn.h /usr/local/include
  2. 2. $ sudo cp lib64/libcudnn.* /usr/local/lib
  3. 3. $ sudo ln -sf /usr/local/lib/libcudnn.so.5.1.10 /usr/local/lib/libcudnn.so.5
  4. 4. $ sudo ln -sf /usr/local/lib/libcudnn.so.5 /usr/local/lib/libcudnn.so
  5. 5. $ sudo ldconfig -v

注:大家根据自己的cuDNN版本修改;如果懒于敲目录,可在Unity文件管理器下将文件夹拖入终端;终端下的“粘贴”快捷键:Ctrl+Shift+v。

3)切换到Caffe-master的文件夹,生成Makefile.config配置文件,执行:

[plain]view plaincopy

  1. $ cp Makefile.config.example Makefile.config

4)配置Makefile.config文件

a.去掉“USE_CUDNN := 1”前面的#

b.根据前面自己的需求,选择性去掉“OPENCV_VERSION :=3”前面的#

c.配置一些引用文件(增加部分主要是解决新版本下,HDF5的路径问题)

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/lib/x86_64-linux-gnu/hdf5/serial/include

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

下面贴出我的Makefile.config文件

[plain]view plaincopy

  1. ## Refer to http://caffe.berkeleyvision.org/installation.html
  2. # Contributions simplifying and improving our build system are welcome!
  3. # cuDNN acceleration switch (uncomment to build with cuDNN).
  4. USE_CUDNN := 1
  5. # CPU-only switch (uncomment to build without GPU support).
  6. # CPU_ONLY := 1
  7. # uncomment to disable IO dependencies and corresponding data layers
  8. # USE_OPENCV := 0
  9. # USE_LEVELDB := 0
  10. # USE_LMDB := 0
  11. # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
  12. # You should not set this flag if you will be reading LMDBs with any
  13. # possibility of simultaneous read and write
  14. # ALLOW_LMDB_NOLOCK := 1
  15. # Uncomment if you're using OpenCV 3
  16. # OPENCV_VERSION := 3
  17. # To customize your choice of compiler, uncomment and set the following.
  18. # N.B. the default for Linux is g++ and the default for OSX is clang++
  19. # CUSTOM_CXX := g++
  20. # CUDA directory contains bin/ and lib/ directories that we need.
  21. CUDA_DIR := /usr/local/cuda
  22. # On Ubuntu 14.04, if cuda tools are installed via
  23. # "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
  24. # CUDA_DIR := /usr
  25. # CUDA architecture setting: going with all of them.
  26. # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
  27. # For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
  28. CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
  29. -gencode arch=compute_20,code=sm_21 \
  30. -gencode arch=compute_30,code=sm_30 \
  31. -gencode arch=compute_35,code=sm_35 \
  32. -gencode arch=compute_50,code=sm_50 \
  33. -gencode arch=compute_52,code=sm_52 \
  34. -gencode arch=compute_60,code=sm_60 \
  35. -gencode arch=compute_61,code=sm_61 \
  36. -gencode arch=compute_61,code=compute_61
  37. # BLAS choice:
  38. # atlas for ATLAS (default)
  39. # mkl for MKL
  40. # open for OpenBlas
  41. BLAS := atlas
  42. # Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
  43. # Leave commented to accept the defaults for your choice of BLAS
  44. # (which should work)!
  45. # BLAS_INCLUDE := /path/to/your/blas
  46. # BLAS_LIB := /path/to/your/blas
  47. # Homebrew puts openblas in a directory that is not on the standard search path
  48. # BLAS_INCLUDE := $(shell brew --prefix openblas)/include
  49. # BLAS_LIB := $(shell brew --prefix openblas)/lib
  50. # This is required only if you will compile the matlab interface.
  51. # MATLAB directory should contain the mex binary in /bin.
  52. # MATLAB_DIR := /usr/local
  53. # MATLAB_DIR := /Applications/MATLAB_R2012b.app
  54. # NOTE: this is required only if you will compile the python interface.
  55. # We need to be able to find Python.h and numpy/arrayobject.h.
  56. PYTHON_INCLUDE := /usr/include/python2.7 \
  57. /usr/lib/python2.7/dist-packages/numpy/core/include
  58. # Anaconda Python distribution is quite popular. Include path:
  59. # Verify anaconda location, sometimes it's in root.
  60. # ANACONDA_HOME := $(HOME)/anaconda
  61. # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
  62. # $(ANACONDA_HOME)/include/python2.7 \
  63. # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
  64. # Uncomment to use Python 3 (default is Python 2)
  65. # PYTHON_LIBRARIES := boost_python3 python3.5m
  66. # PYTHON_INCLUDE := /usr/include/python3.5m \
  67. # /usr/lib/python3.5/dist-packages/numpy/core/include
  68. # We need to be able to find libpythonX.X.so or .dylib.
  69. PYTHON_LIB := /usr/lib
  70. # PYTHON_LIB := $(ANACONDA_HOME)/lib
  71. # Homebrew installs numpy in a non standard path (keg only)
  72. # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
  73. # PYTHON_LIB += $(shell brew --prefix numpy)/lib
  74. # Uncomment to support layers written in Python (will link against Python libs)
  75. # WITH_PYTHON_LAYER := 1
  76. # Whatever else you find you need goes here.
  77. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/lib/x86_64-linux-gnu/hdf5/serial/include
  78. LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial
  79. # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
  80. # INCLUDE_DIRS += $(shell brew --prefix)/include
  81. # LIBRARY_DIRS += $(shell brew --prefix)/lib
  82. # NCCL acceleration switch (uncomment to build with NCCL)
  83. # https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
  84. # USE_NCCL := 1
  85. # Uncomment to use `pkg-config` to specify OpenCV library paths.
  86. # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
  87. # USE_PKG_CONFIG := 1
  88. # N.B. both build and distribute dirs are cleared on `make clean`
  89. BUILD_DIR := build
  90. DISTRIBUTE_DIR := distribute
  91. # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
  92. # DEBUG := 1
  93. # The ID of the GPU that 'make runtest' will use to run unit tests.
  94. TEST_GPUID := 0
  95. # enable pretty build (comment to see full commands)
  96. Q ?= @

5)编译caffe-master!!!"-j4"是使用CPU的多核进行编译,可以极大地加速编译的速度,建议使用。

注:下面的make命令适当的时候加上sudo,建议都已super do来运行。

[plain]view plaincopy

  1. $ make all -j4
  2. $ make test -j4
  3. $ make runtest -j4

期间会遇到下面的问题:

问题:mkl_alternate.hpp:14:19 fatal error: cblas.h: 没有那个文件或目录

解决:https://github.com/BVLC/caffe/issues/3599

[plain]view plaincopy

  1. sudo apt-get install libopenblas-dev

问题:找不到 -lablas、-latlas

解决:

[plain]view plaincopy

  1. sudo apt-get install libatlas-base-dev

可能问题:.build_release/lib/libcaffe.so.1.0.0 Error 1 /usr/bin/ld: 找不到 -lopencv_imgcodecs

解决:你没有安装好CV3,可选择加上b的井号,或参考

https://my.oschina.net/peterlie/blog/661994

http://blog.csdn.net/foolsnowman/article/details/50532226

问题:caffe libcudart.so.8.0 cannot open shared object file no such file or directory

解决:https://github.com/NVIDIA/DIGITS/issues/8

[plain]view plaincopy

  1. sudo ldconfig /usr/local/cuda/lib64

问题:nvcc warring: the 'compute_20', 'sm_20', and 'sm_21'

解决:我是不理会,不影响使用,想看一眼的可参考以下网址:

http://www.itwendao.com/article/detail/214390.html

6)编译Python和Matlab用到的caffe文件

[plain]view plaincopy

  1. $ make pycaffe -j4

7)测试

打开example -> MNIST -> Readme.md

按照最前面,cd到Caffe根目录,运行第一页的两个命令,进行下载和格式转换(不超过30MB的下载量)

再运行最下面的命令,Training,看命令行盖楼。。。

五、安装faster-r-cnn参考

bit_hammer大神:

http://blog.csdn.net/u011635764/article/details/52831167

cuDNN v5、v5.1的问题

http://blog.csdn.net/u010733679/article/details/52221404

大家按照bit_hammer大神的步骤安装就好,关于cuDNN v5.1的处理,两篇文章都一样的。

注:faster-r-cnn一定要从Git Clone,Download ZIP不全。

六、测试faster-r-cnn

从百度云盘下载训练好的模型:

VGG16_faster_rcnn_final.caffemodel (548.3MB)

2F_faster_rcnn_final.caffemodel (237.2MB)

将上述两个文件存放于:

py-faster-rcnn/data/faster_rcnn_models/

下,即可运行步骤五中的Demo。

转自http://blog.csdn.net/dylll321/article/details/72784159