caffe安裝流程和遇到問題

之前caffe的安裝簡直讓我懷疑人生,後來由於忙一直沒有寫下流程和重要問題的解決辦法,這次由於在自家的電腦上配置caffe,順便下寫流程。

不多說,先上自家電腦配置,i5-4590和GTX1070

1.caffe安裝依賴庫

[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  
首先最好一個一個安裝,其次這裏有可能出現需要依賴庫,以下依賴庫將不被安裝,解決方法是換軟件源,我換了清華的源後立馬就好了


2.安裝CUDA

CUDA是英偉達的顯卡並行計算語言,caffe需要來使用顯卡

在離線.deb安裝:deb安裝分離線和在線,官網下載地址,推薦離線安裝

切換到下載的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

3.cuDNN
這個是顯卡計算加速的,可以不裝,加速效果遠沒有GPU對CPU的加速來的多,而且安裝還麻煩
下載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 lib/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分鐘左右, 全部編譯完成後, 進入 samples/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
我是準備用caffe的python接口,OpenCV就沒安裝了
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"  
這裏要注意下如果是安裝的anaconda2,那麼上面需要加上2


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 ?= @  
需要注意的是第5行,沒裝cuDNN的需要註釋,57,58行默認的有註釋符,需要去掉
保存退出
編譯
[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. make all -j4  
  2. make test  
  3. make runtest  
編譯這邊有很多問題,如果出現未定義的引用,偶然發現一個方法很好,就是在make all前面加sudo,原因不知道有誰可以解釋下,權限不夠爲什麼會導致未定義的引用
11,編譯Python wrapper

[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. make  pycaffe  
pycaffe的編譯需要在/.bashrc中增加export PYTHONPATH = “/home/username/caffe-master/python:$PYTHONPATH”
然後source ~/.bashrc
這是在bash中,如果需要在IDE中,比如spyder或者pycharm,在其環境變量文件中添加caffe-master/python的目錄即可

發佈了11 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章