ubuntu 14.04 && 16.04 安裝caffe+cuda8.0+pycafee總結


從開學到現在,caffe裝了有4-5次了。在這里做個總結,以防那天,自己的電腦又操作失誤,又跪!

 

建議,如果是自己的電腦,能用網線,可以這樣搞,因為到最后關機重啟后,不知道是什么原因,系統的設置中,好多軟件打不開了。 建議主要看下邊Ubuntu16.04的安裝,我又重裝的,效果很好。

 

總體思路:

1、先裝ubuntu14.04。用UltralSO搞個刻錄U盤就好(不知道怎么回事,電腦開不了機,嘗試過16.04版本,但是感覺沒有14.04好搞。。。)

 

2、禁用Ubuntu自帶顯卡驅動

Ubuntu的nouveau禁用方法: 
      在/etc/modprobe.d中創建文件blacklist-nouveau.conf,在文件中輸入一下內容
blacklist nouveau options nouveau modeset=0

3、安裝cuda

  說明:

  (a)可以先安裝NVIDIA顯卡驅動,再安裝cuda,但是先裝顯卡驅動之后,就要注意,在安裝cuda時,就不要重裝cuda里帶的顯卡驅動了。

  (b)我的流程就是,不自己去裝顯卡驅動,用cuda里的。

  (c)在安裝cuda時,需要關閉圖形化界面

使用alt ctrl f1-f6中的任意一個,進入黑屏命令行中,使用用戶名和密碼登錄

service lightdm stop    (使用root賬戶執行該命令)

將下載好的進行安裝,我這里用的是 cuda_8.0.61_375.26_linux.run
所以執行

sh cuda_8.0.61_375.26_linux.run

然后根據提示進行安裝,當遇到提示是否安裝openGL時,選擇no(不明所以,只是其他人這么說)

重啟電腦
配置環境變量

      終端中輸入 $ sudo gedit /etc/profile
      在打開的文件末尾,添加以下兩行。
export PATH=/usr/local/cuda-8.0/bin:$PATH 
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH

使用命令 nvidia-smi 查看當前顯卡狀態

 

4、安裝cudnn

將cuDNN文件copy到和cuda同一目錄下,然后進行解壓,解壓之后,執行以下命令

sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

然后通過執行cuda中samples的deviceQuery來驗證。

 

5、安裝openCV  使用github上的 OpenCV安裝腳本,操作超級簡單,超級好用。(不過,可能需要很長時間,我單顆 i7 跑了將近1h)

https://github.com/jayrambhia/Install-OpenCV

最后出現OpenCV-3.3.0 ready to be used類似的話,就說明安裝成功了

 

6、安裝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 libgflags-dev libgoogle-glog-dev liblmdb-dev

BLAS安裝
sudo apt-get install libatlas-base-dev

安裝anaconda之后pycaffe依賴好像就不用裝了,我忘了,這里附上(所有python庫依賴,本來是一行,這里為了排版,分成多行了)
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

然后從github上把caffe clone下來
git clone https://github.com/BVLC/caffe.git
然后復制一份Makefile.config文件,自己去修改(這里修改,可以參照網上的一些教程,按需修改,下邊附上我修改的)
cp Makefile.config.example Makefile.config

然后在 caffe文件根目錄下進行編譯
make all -j8  (這里的 -j8  意思是使用8核進行編譯, 如果電腦是4核,用 -j4.類似,電腦有幾核,最多就可以用幾核編譯)
make test -j8
make runtest -j8

進行這三步應該會遇到一些問題,請自行Google
如果出錯,要重新編譯,使用 
make clean (但注意,一使用該命令,所以編譯操作就要全部重做一次)

需要使用python caffe接口,使用
make pycaffe -j8
驗證 在caffe文件根目錄下 cd python 切換到 ./python 目錄中然后在終端下輸入
python (進入python 命令行中)
import caffe (如果不報錯,就說明沒問題了)


需要使用matlab caffe接口,使用
make matcaffe -j8

到這里所有編譯全部完成。

 

7、安裝anaconda(推薦使用anaconda,因為這里集合了python中常用的科學工具包,不用自己之后一個一個pip install,不足之處就是,可能動態鏈接庫會報錯,不過好解決。caffe的issue中有相關回答,或者直接Google)

推薦使用pycharm 配合使用,感覺超好。

pycharm中import caffe/caffe2   --->    http://blog.csdn.net/u013010889/article/details/70808866

至此,caffe 和 python環境應該就沒問題了,但具體其他操作呢??

這里推薦一個大牛的博客,自己去翻他的文章學習吧,文章的可看度還是挺高的。

http://www.cnblogs.com/denny402/tag/caffe/

 

matlab2017a安裝教程

http://blog.csdn.net/m0_37407756/article/details/73187654

 

附的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.
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
        -gencode arch=compute_20,code=sm_21 \
        -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.
# 注意,如果用anaconda的話,下邊兩行都要注釋掉
# 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的目錄根據你安裝的目錄設置,如果是按默認安裝的,因為默認安裝目錄為/root目錄下,所以為  ANACONDA_HOME := $(HOME)/anaconda ANACONDA_HOME := /home/unicoe/anaconda2 # 將下邊的#號注釋都刪除掉,共3行 PYTHON_INCLUDE := $(ANACONDA_HOME)/include \ $(ANACONDA_HOME)/include/python2.7 \ $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

# 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
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

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

 

之后的錯誤記錄,可能還有遺漏,自行Google吧

裝完anaconda2之后,記得設置一下環境變量
export PATH="/home/tom/anaconda2/bin:$PATH" 



./build/tools/caffe: error while loading shared libraries: libhdf5_hl.so.8: cannot open shared object file: No such file or directory

在 /etc/profile 加入環境變量
export LD_LIBRARY_PATH="/usr/local/cuda/lib64"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/xxx/anaconda2/lib"

注意,這里的  /home/xxx/anaconda2 是你裝anaconda2的目錄


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
  File "caffe/pycaffe.py", line 15, in <module>
    import caffe.io
  File "caffe/io.py", line 8, in <module>
    from caffe.proto import caffe_pb2
  File "caffe/proto/caffe_pb2.py", line 4, in <module>
    from google.protobuf.internal import enum_type_wrapper
ImportError: No module named google.protobuf.internal

解決方法:

我是用anaconda2/bin 目錄下的pip 進行  pip install protobuf 解決問題

  

 

安裝Ubuntu16.04 caffe 和 py faster rcnn是參考一下幾篇博客搞定的

 用當前最新的 Ubuntu16.04.3

Ubuntu16.04 Caffe 安裝步驟記錄(超詳盡)    http://blog.csdn.net/yhaolpz/article/details/71375762

我的做法:

1、安裝完Ubuntu之后,先換顯卡驅動,

  (a)換顯卡之前,先執行 sudo apt-get update   sudo apt-get upgrade ,將系統更新之后, 在System Settings -> Software & Updates -> Additional Drivers  中選擇要換的顯卡驅動(我當前的是NVIDIA-384),安裝完成之后。照着上邊的教程把默認顯卡驅動禁用了,然后重啟查看有沒有問題(看能不能進入系統,能進入的話,再進行后續步驟) 

  之后我的步驟是

  (b)安裝依賴包,

  (c)配置環境變量,

  (d)安裝cuda(這里要注意的是,因為不用cuda帶的驅動,所以安裝的時候,不用關閉圖形化界面),

  (e)安裝cudnn也沒用他的方法,我是將cudnn安裝包放在了/usr/local/ 目錄下,直接解壓,然后用我博客上邊的操作,然后用nvcc -V驗證一下。

  (f)裝openCV也是直接用安裝腳本,直接搞定,自己做的很少。    (裝完之后,重啟看看)

  (g)裝caffe(我裝在了 /home/unicoe/ 目錄下,所以要用 sudo make all -j8 && sudo make runtest -j8 )

  (h)裝anaconda2(注意,在裝caffe的時候,不要用anaconda2,因為會有蜜汁錯誤,裝了caffe之后再裝也是一樣的)

  (i)裝pycaffe(裝了anaconda2之后,配置一下環境變量,就不用想博主那樣裝一堆Python庫了)

  (j)進入 caffe/python 中 python 然后 import caffe,這里會出現一些問題,總結來說,就是缺啥裝啥

  好像要裝 conda install easydict   

      conda install opencv-python

 

Traceback (most recent call last)

File ImportError: /home/../anaconda2/lib/python2.7/site-packages/zmq/backend/cython/../../../../.././libstdc++.so.6: versionGLIBCXX_3.4.21' not found

解決:

https://github.com/BVLC/caffe/issues/4953

https://gitter.im/BVLC/caffe/archives/2015/08/20

 

cd /home/unicoe/caffe

pip install protobuf

sudo apt-get install python-protobuf

conda install libgcc

 

如果還有其他錯誤,自行Google吧

 

裝faster rcnn

參照  http://blog.csdn.net/u012841667/article/details/53436615#reply 

(a)先下載 py-faster-rcnn

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

 

(b) cd py-faster-rcnn/lib  然后 make

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

 

(c)編譯/py-faster-rcnn/caffe-fast-rcnn

  改動的地方,和裝caffe時一樣

  (I)改Makefile.config (這個可以復制裝完的caffe中的Makefile.config)

  (II)改Makefile(這個手動改,照着裝caffe時的方法,不要直接復制caffe中的Makefile文件,不然會有各種問題)

 

(d) make -j8 && make pycaffe

  編譯的時候,就會報關於cudnn的錯,上邊的博客中說了,是這里的cudnn版本低,需要換成,現在caffe版本的cudnn,

 

(e)參照上邊的博客,cudnn依賴要改動三個地方(使用 cp命令    cp  源文件位置/源文件  目的文件位置, 具體使用,請自行查看)

  (I)將/py-faster-rcnn/caffe-fast-rcnn/include/caffe/util/cudnn.hpp 換成最新版的caffe里的cudnn的實現,即相應的cudnn.hpp

  (II)將/py-faster-rcnn/caffe-fast-rcnn/src/caffe/layer里的,所有以cudnn開頭的文件,例如cudnn_lrn_layer.cu,cudnn_pooling_layer.cpp,cudnn_sigmoid_layer.cu。    都替換成最新版的caffe里的相應的同名文件

  (III)將./include/caffe/layers的,所有以cudnn開頭的文件,例如cudnn_conv_layer.hpp,cudnn_lcn_laye.hpp    都替換成最新版的caffe里的相應的同名文件

 

(f)然后就可以運行demo.py 了  

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

 

如果有下列錯誤,請參照下邊博客中所說

OSError: libcudnn.so.7.0: cannot open shared object file: No such file or directory錯誤

http://blog.csdn.net/u014696921/article/details/60140264

 

 

可供參考的blog還有  Ubuntu16.04 caffe安裝記錄  http://www.cnblogs.com/peiyuYang/p/7784787.html

如有問題,請留言,或者發郵件到unicoe@163.com 中。研究生之路剛開始,希望能和大家多多交流。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM