Ubuntu 14.04+cuda 7.5+caffe安装配置

来源:互联网 发布:一个网站有几个域名 编辑:程序博客网 时间:2024/04/28 11:47

换了新电脑,整个人喜气洋洋,然后就屁颠屁颠地开始配置caffe的使用环境。
可是!!!配置这个坑爹的caffe环境让我重装系统N次加上重装cudaN次,后来发现有好多都是很琐碎的注意事项,好多人都没有提到过,我就假装伟大一下,发到博客上,与大家共勉。
sudo apt-get install python-pip python-dev
sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp34-cp34m-linux_x86_64.whl

sudo apt-get install python3-numpy swig python3-dev python3-wheel
sudo apt-get install python3-pip
pip install moviepy

Driver:   Installed
Toolkit:  Installed in /usr/local/cuda-8.0
Samples:  Installed in /home/caocao, but missing recommended libraries


Please make sure that
 -   PATH includes /usr/local/cuda-8.0/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ld.so.conf and run ldconfig as root


To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/bin
To uninstall the NVIDIA Driver, run nvidia-uninstall


Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.0/doc/pdf for detailed information on setting up CUDA
首先安装ubuntu14.04,一般根据指示安装下去就可以了。,

但是有一点要注意!!!!!
千万不要更新系统!!!!!
千万不要手贱!!!!
千万不要更新那个三百多M的更新包!!!
理由是更新会把原有的nvidia的驱动覆盖掉,还会改掉ubuntu的内核,导致cuda使用不了,驱动的mismatch

提前做好的准备包括: ubuntu 系统下面下载 Nvidia 官网上面的 run 文件。
&& 不要单独装 Nvidia 驱动再装 cuda 驱动,应该直接下载 cuda 驱动(cuda 驱动包括 Nvidia 驱动,Toolkit  samples)

在ubuntu下面装caffe,配合NVIDIA的显卡,需要依赖以下几个库

1 Cuda(目前是6.5.14)
2 OpenCV Boost  Atlas等等
其中Cuda的安装比较麻烦,有很多琐碎的注意事项。

具体步骤如下
1 安装ubuntu14.04
2 配置一下网络,使得ubuntu能够连接Internet
3 update一下apt-get
sudo apt-get update
若出现一些update无法下载,一般是因为天朝的墙,可以在系统设置那里找到源服务器,切换一下看看哪个可以用。有时候server for China是会被墙一部分的。。
4 安装一个vim方便后续一个操作
sudo apt-get install vim
5 安装一些必要的库:
     sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev  libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
6 禁掉nouveau:
在/etc/modprobe.d/blacklist.conf最后一行加上 blacklist nouveau
禁了nouveau之后有时要重启下电脑才生效
7 关闭GUI:
     sudo service lightdm stop
8 更改CUDA Toolkit执行权限:
     sudo chmod +x cuda_7.5.18_linux.run 
sudo chmod +x cuda_8.0.44_linux-run

9 执行CUDA安装文件

(注意!!!!!假如是用集显做未显示而用独显做计算的话,必须要加上一句 no-opengl-libs, 否则会覆盖到显示文件,导致无法在显示器显示)
(而个人比较推荐的做法,正是用集显输出,用独显计算)

 sudo ./cuda_7.5.18_linux.run  --no-opengl-libs
 sudo ./cuda_8.0.44_linux-run --no-opengl-libs
(If the target system includes both an integrated GPU (iGPU) and a discrete GPU
(dGPU), the --no-opengl-libs option must be used. Otherwise, the openGL library used
by the graphics driver of the iGPU will be overwritten and the GUI will not work. In
addition, the xorg.conf update at the end of the installation must be declined.)

10 切换回GUI:
     sudo service lightdm start

11 成功切回GUI后添加环境变量PATH:

$ sudo vim /etc/profile
 在/etc/profile文件最后添加
 export  PATH=/usr/local/cuda-7.5/bin:$PATH
 /usr/local/cuda-8.0

然后source一下,使之生效
$ source /etc/profile    
可以使用
$ echo $PATH
来检测是否成功添加环境变量

再然后就是添加共享库变量。
在cd  /etc/ld.so.conf.d/加入文件sudo vim cuda.conf, 内容如下
/usr/local/cuda-7.5/lib64
 /usr/local/cuda-8.0

执行下列命令使之立刻生效
sudo ldconfig

12 检验:
 $ sudo nvidia-settings 
to check whether nvidia drivers are installed
$ glxinfo | grep rendering
如果出现 direct rendering: Yes 则表明显卡正在运行
$ sudo ldconfig v | grep cuda 如果安装成功,会出现类似到信息 /usr/local/cuda-5.0/lib:
libcudart.so.5.0 -> libcudart.so.5.0.35
 libicudata.so.48 -> libicudata.so.48.1.1
 libcuda.so.1 -> libcuda.so.304.54
这样子的信息(具体版本具体分析)

13 测试一下cuda的一些sample 
cd到cuda sample的路径
一般是Home下面有一个NVDIA。。。(具体忘记了,,有点长的)
然后cd进去,make一下,
$ make j8
之后生成了一个bin,在一路cd进去,直到有个目录有很多程序文件。
./deviceQuery
如果看到一大串正常的状态显示,就证明cuda装好了
例如输出 Pass 等信息,则说明成功安装。

--参考
1,http://blog.csdn.net/menglongbor/article/details/7015380 2,http://www.linuxdiyf.com/bbs/viewthread.php?tid=194013 3,http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-linux/index.html 4,http://blog.163.com/thinki_cao/blog/static/83944875201303125444265/




把CUDA这个大块头搞定之后,剩下的就是一些命令行就可以安装好的东西了
1.(optional)cuDNN安装(加速用):
将cudnn.h复制到usr/include,將libcudnn.so.6.5.18复制到usr/lib
sudo ln -s libcudnn.so.6.5.18  libcudnn.6.5
sudo ln -s libcudnn.so.6.5   libcudnn.so
在config中加USE_CUDNN := 1 flag set in your Makefile.config.

2.BLAS如MKL安装:MKL找不到,用ATLAS代替:sudo apt-get install libatlas-base-dev

3.安装依赖库:包含了boost opencv leveldb protobuf  等
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler


接下来就可以开始真正地编译caffe了
不对,首先还要获取git上面的代码

如果是新系统,估计要先
$sudo apt-get install git

然后就是正常的git的使用,把caffe从git上用
git clone拉取到某个路径
然后再开始编译
cp Makefile.config.example  Mkefile.config
make all -j8
make test -j8
make runtest -j8
(有时候,如果环境变量和共享库那一步没做好,runtest的时候会报错,是cudart找不到文件,各种查都不知道为什么,最后用了个博客的做法)
(sudo ldconfig /usr/local/cuda-6.5/bin)

实验跑一个MNIST
下载MNIST数据库,放在caffe/data/mnist下
1. 数据预处理
cd $CAFFE_ROOT/data/mnist
./get_mnist.sh               
2.重建LDB文件
cd $CAFFE_ROOT/examples/mnist
./create_mnist.sh    
3.训练mnist     
cd $CAFFE_ROOT/examples/mnist
./train_lenet.sh

mnist里面的路径基本都是错的。。把train_lenet.sh, solver, train_test等几个脚本打开看看,把路径改对就可以了。
GPU:5分钟跑完,准确率在99%左右


015.10.23更新:修改了一些地方,身边很多人按这个流程安装,完全可以安装

折腾了两个星期的caffe,windows和ubuntu下都安装成功了。其中windows的安装配置参考官网推荐的那个blog,后来发现那个版本的caffe太老,和现在的不兼容,一些关键字都不一样,果断回到Linux下。这里记录一下我的安装配置流程。

电脑配置:

ubuntu 14.04 64bit

8G 内存

GTX650显卡


软件版本:

CUDA 7.0

caffe 当天从github下载的版本


安装ubuntu的过程省略,建议安装后关闭自动更新,上一次安装caffe后用的很好,结果有一天晚上没关电脑,自己半夜更新了显卡驱动,然后...


caffe的安装流程主要参考这个blog,稍有改动:Caffe + Ubuntu 14.04 64bit + CUDA 6.5 配置说明


Caffe 安装配置步骤:


1, 安装开发所需的依赖包

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. sudo apt-get install build-essential  # basic requirement  
  2. sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler #required by caffe  


Before install CUDA 7.5, you need update gcc 4.8+ to gcc 4.9+

reference:update gcc/g++

2,安装CUDA 7.5

验证过程省略,按照官方文档自己操作吧(遇到问题首先要看官方文档啊,血泪教训)

安装CUDA有两种方法,

离线.run安装:从官网下载对应版本的.run安装包安装,安装过程挺复杂,尝试过几次没成功,遂放弃。

在离线.deb安装:deb安装分离线和在线,我都尝试过都安装成功了,官网下载地址


安装之前请先进行md5校验,确保下载的安装包完整

切换到下载的deb所在目录,执行下边的命令
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. sudo dpkg -i cuda-repo-<distro>_<version>_<architecture>.deb  
  2. sudo apt-get update  
  3. sudo apt-get install cuda  
然后重启电脑:sudo reboot
NOTE:装不成功卸了多来几遍,总会成的

3,安装cuDNN
下载cudnn-7.5-linux-x64-v5.0-ga.tgz,官网申请不到,网上自己找的,就不给地址了。
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. tar -zxvf cudnn-7.5-linux-x64-v5.0-ga.tgz  
  2. cd cuda  
  3. sudo cp lib64/lib* /usr/local/cuda/lib64/  
  4. sudo cp include/cudnn.h /usr/local/cuda/include/  
更新软连接
cd /usr/local/cuda/lib64/
sudo chmod +r libcudnn.so.5.0.5
sudo ln -sf libcudnn.so.5.0.5 libcudnn.so.5
sudo ln -sf libcudnn.so.5 libcudnn.so
sudo ldconfig
 4,设置环境变量
在/etc/profile中添加CUDA环境变量
sudo gedit /etc/profile
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. PATH=/usr/local/cuda/bin:$PATH  
  2. export PATH  
保存后, 执行下列命令, 使环境变量立即生效
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. source /etc/profile  
同时需要添加lib库路径: 在 /etc/ld.so.conf.d/加入文件 cuda.conf, 内容如下
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /usr/local/cuda/lib64  
保存后,执行下列命令使之立刻生效
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. sudo ldconfig  

5,安装CUDA SAMPLE
进入/usr/local/cuda/samples, 执行下列命令来build samples
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. sudo make all -j4  
整个过程大概10分钟左右, 全部编译完成后, 进入 cd bin/x86_64/linux/release, 运行deviceQuery
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. ./deviceQuery  
如果出现显卡信息, 则驱动及显卡安装成功:
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. ./deviceQuery Starting...  
  2.   
  3.  CUDA Device Query (Runtime API) version (CUDART static linking)  
  4.   
  5. Detected 1 CUDA Capable device(s)  
  6.   
  7. Device 0: "GeForce GTX 670"  
  8.   CUDA Driver Version / Runtime Version          6.5 / 6.5  
  9.   CUDA Capability Major/Minor version number:    3.0  
  10.   Total amount of global memory:                 4095 MBytes (4294246400 bytes)  
  11.   ( 7) Multiprocessors, (192) CUDA Cores/MP:     1344 CUDA Cores  
  12.   GPU Clock rate:                                1098 MHz (1.10 GHz)  
  13.   Memory Clock rate:                             3105 Mhz  
  14.   Memory Bus Width:                              256-bit  
  15.   L2 Cache Size:                                 524288 bytes  
  16.   Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)  
  17.   Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers  
  18.   Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 2048 layers  
  19.   Total amount of constant memory:               65536 bytes  
  20.   Total amount of shared memory per block:       49152 bytes  
  21.   Total number of registers available per block: 65536  
  22.   Warp size:                                     32  
  23.   Maximum number of threads per multiprocessor:  2048  
  24.   Maximum number of threads per block:           1024  
  25.   Max dimension size of a thread block (x,y,z): (1024, 1024, 64)  
  26.   Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)  
  27.   Maximum memory pitch:                          2147483647 bytes  
  28.   Texture alignment:                             512 bytes  
  29.   Concurrent copy and kernel execution:          Yes with 1 copy engine(s)  
  30.   Run time limit on kernels:                     Yes  
  31.   Integrated GPU sharing Host Memory:            No  
  32.   Support host page-locked memory mapping:       Yes  
  33.   Alignment requirement for Surfaces:            Yes  
  34.   Device has ECC support:                        Disabled  
  35.   Device supports Unified Addressing (UVA):      Yes  
  36.   Device PCI Bus ID / PCI location ID:           1 / 0  
  37.   Compute Mode:  
  38.      < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >  
  39.   
  40. deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 6.5, CUDA Runtime Version = 6.5, NumDevs = 1, Device0 = GeForce GTX 670  
  41. Result = PASS  
NOTE:上边的显卡信息是从别的地方拷过来的,我的GTX650显卡不是这些信息,如果没有这些信息,那肯定是安装不成功,找原因吧!

6,安装Intel MKL 或Atlas
我没有MKL,装的Atlas
安装命令:
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. sudo apt-get install libatlas-base-dev  

7,安装OpenCV
我安装的是2.4.10
1)下载安装脚本
2)进入目录 Install-OpenCV/Ubuntu/2.4
3)执行脚本
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. sh sudo ./opencv2_4_10.sh   

8,安装Caffe所需要的Python环境
按caffe官网的推荐使用Anaconda
去Anaconda官网下载安装包
切换到文件所在目录,执行
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. bash Anaconda-2.3.0-Linux-x86_64.s<em>h</em>  
NOTE:后边的文件名按自己下的版本号更改,整个安装过程请选择默认

  8.1,添加Anaconda Library Path
在/etc/ld.so.conf最后加入以下路径,并没有出现重启不能进入界面的问题(NOTE:下边的username要替换)
 
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /home/username/anaconda/lib  
在~/.bashrc最后添加下边路径
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. export LD_LIBRARY_PATH="/home/username/anaconda/lib:$LD_LIBRARY_PATH"  



9,安装python依赖库
去caffe的github下载caffe源码包
进入caffe-master下的python目录
执行如下命令
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. for req in $(cat requirements.txt); do pip install $req; done  

10,编译Caffe
终于来到这里了
进入caffe-master目录,复制一份Makefile.config.examples
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. cp Makefile.config.example Makefile.config  
修改其中的一些路径,如果前边和我说的一致,都选默认路径的话,那么配置文件应该张这个样子
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. ## Refer to http://caffe.berkeleyvision.org/installation.html  
  2. # Contributions simplifying and improving our build system are welcome!  
  3.   
  4. # cuDNN acceleration switch (uncomment to build with cuDNN).  
  5. USE_CUDNN := 1  
  6.   
  7. # CPU-only switch (uncomment to build without GPU support).  
  8. # CPU_ONLY := 1  
  9.   
  10. # To customize your choice of compiler, uncomment and set the following.  
  11. # N.B. the default for Linux is g++ and the default for OSX is clang++  
  12. # CUSTOM_CXX := g++  
  13.   
  14. # CUDA directory contains bin/ and lib/ directories that we need.  
  15. CUDA_DIR := /usr/local/cuda  
  16. # On Ubuntu 14.04, if cuda tools are installed via  
  17. # "sudo apt-get install nvidia-cuda-toolkit" then use this instead:  
  18. # CUDA_DIR := /usr  
  19.   
  20. # CUDA architecture setting: going with all of them.  
  21. # For CUDA < 6.0, comment the *_50 lines for compatibility.  
  22. CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \  
  23.         -gencode arch=compute_20,code=sm_21 \  
  24.         -gencode arch=compute_30,code=sm_30 \  
  25.         -gencode arch=compute_35,code=sm_35 \  
  26.         -gencode arch=compute_50,code=sm_50 \  
  27.         -gencode arch=compute_50,code=compute_50  
  28.   
  29. # BLAS choice:  
  30. # atlas for ATLAS (default)  
  31. # mkl for MKL  
  32. # open for OpenBlas  
  33. BLAS := atlas  
  34. # Custom (MKL/ATLAS/OpenBLAS) include and lib directories.  
  35. # Leave commented to accept the defaults for your choice of BLAS  
  36. # (which should work)!  
  37. # BLAS_INCLUDE := /path/to/your/blas  
  38. # BLAS_LIB := /path/to/your/blas  
  39.   
  40. # Homebrew puts openblas in a directory that is not on the standard search path  
  41. # BLAS_INCLUDE := $(shell brew --prefix openblas)/include  
  42. # BLAS_LIB := $(shell brew --prefix openblas)/lib  
  43.   
  44. # This is required only if you will compile the matlab interface.  
  45. # MATLAB directory should contain the mex binary in /bin.  
  46. # MATLAB_DIR := /usr/local  
  47. # MATLAB_DIR := /Applications/MATLAB_R2012b.app  
  48.   
  49. # NOTE: this is required only if you will compile the python interface.  
  50. # We need to be able to find Python.h and numpy/arrayobject.h.  
  51. #PYTHON_INCLUDE := /usr/include/python2.7 \  
  52.         /usr/lib/python2.7/dist-packages/numpy/core/include  
  53. # Anaconda Python distribution is quite popular. Include path:  
  54. # Verify anaconda location, sometimes it's in root.  
  55.  ANACONDA_HOME := $(HOME)/anaconda  
  56.  PYTHON_INCLUDE := $(ANACONDA_HOME)/include \  
  57.          $(ANACONDA_HOME)/include/python2.7 \  
  58.          $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \  
  59.   
  60. # We need to be able to find libpythonX.X.so or .dylib.  
  61. #PYTHON_LIB := /usr/lib  
  62. PYTHON_LIB := $(ANACONDA_HOME)/lib  
  63.   
  64. # Homebrew installs numpy in a non standard path (keg only)  
  65. # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include  
  66. # PYTHON_LIB += $(shell brew --prefix numpy)/lib  
  67.   
  68. # Uncomment to support layers written in Python (will link against Python libs)  
  69. # WITH_PYTHON_LAYER := 1  
  70.   
  71. # Whatever else you find you need goes here.  
  72. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include  
  73. LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib  
  74.   
  75. # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies  
  76. # INCLUDE_DIRS += $(shell brew --prefix)/include  
  77. # LIBRARY_DIRS += $(shell brew --prefix)/lib  
  78.   
  79. # Uncomment to use `pkg-config` to specify OpenCV library paths.  
  80. # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)  
  81. # USE_PKG_CONFIG := 1  
  82.   
  83. BUILD_DIR := build  
  84. DISTRIBUTE_DIR := distribute  
  85.   
  86. # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171  
  87. # DEBUG := 1  
  88.   
  89. # The ID of the GPU that 'make runtest' will use to run unit tests.  
  90. TEST_GPUID := 0  
  91.   
  92. # enable pretty build (comment to see full commands)  
  93. Q ?= @  

保存退出
编译
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. make all -j4  
  2. make test  
  3. make runtest  

11,编译Python wrapper

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. make  pycaffe  

到这里就基本结束了,跑个自带的例子测试一下吧!

NOTE:以上是我在自己PC上的安装步骤,因软件版本不同,硬件环境不同,按照以上方式可能出现错误,请耐心查找错误,欢迎留言

0 0
原创粉丝点击