1. 程式人生 > >caffe+cuda8.0+cudnn5.1+Ubuntu16.04安裝

caffe+cuda8.0+cudnn5.1+Ubuntu16.04安裝

所需軟體:
ubuntu-16.04-desktop-amd64
caffe-master
Anaconda2-4.1.1-Linux-x86_64.sh
cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64.deb
cudnn-8.0-linux-x64-v5.1
OpenBLAS-0.2.19.tar
opencv-master

安裝順序:
1、 Ubuntu16.04系統安裝
2、 CUDA8.0安裝
3、 cuDNN-v5.1 安裝
4、 OpenBLAS-0.2.19安裝
5、 opencv-master安裝
6、 Anaconda2-4.1.1 安裝
7、 caffe-master安裝

安裝過程:
第一步:Ubuntu16.04系統安裝
可參考http://www.cnblogs.com/zangyu/p/5653041.html 此為window10下安裝Ubuntu16.04雙系統的部落格教程,和單獨安裝Ubuntu16.04的差不多。可按照部落格教程進行安裝。

第二步:基礎依賴庫的安裝
輸入一下命令列,根據提示安裝即可:

$ 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

第三步:CUDA8.0安裝
Ubuntu16.04 要求是CUDA8.0,所以必須安裝CUDA8.0,不然後續估計會有很多麻煩。
https://developer.nvidia.com/cuda-downloads 這個網址是CUDA下載的網址,根據選項,找到適合自己的CUDA進行下載。
這裡寫圖片描述
本教程下載的是:cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64.deb
下載完成後,根據網頁下面的安裝教程進行安裝:

$ sudo dpkg -i cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64.deb
$ sudo apt-get update
$ sudo apt-get install cuda

安裝完成後,在/etc/profile 檔案中增加兩行內容:
(必須以管理員許可權開啟,否則無法修改儲存。開啟檔案命令:$ sudo gedit /etc/profile)

export PATH=/usr/local/cuda-8.0/bin:$PATH                        #即CUDA8.0所安裝路徑下的bin資料夾路徑,根據自己的安裝路徑進行修改
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH            #即CUDA8.0所安裝路徑下的lib64資料夾路徑,根據自己的安裝路徑進行修改

第四步:cuDNN-v5.1 安裝
https://developer.nvidia.com/rdp/cudnn-download 該網站為cuDNN下載網站,需要註冊才能下載。
登入賬號之後,找到下載位置,同意,並找到所需要的版本,本教程使用的是針對CUDA8.0的版本cuDNN v5.1 Library for Linux,如下圖所示,直接點選即可下載。
這裡寫圖片描述

下載完成後,將安裝包放到home下(不放也行,但要記得安裝包所在路徑)進行安裝:

$ sudo tar xvf cudnn-8.0-linux-x64-v5.1.tgz                  #使用命令將檔案解壓,也可自行解壓
$ cd cuda/include                                                  #進入解壓後的資料夾中的include資料夾中
$ sudo cp *.h /usr/local/include/                               #拷貝檔案到/usr/local/include/中
$ cd ../lib64                                                        
$ sudo cp lib* /usr/local/lib/ 
$ cd /usr/local/lib 
$ sudo chmod +r libcudnn.so.5.1.5 
$ sudo ln -sf libcudnn.so.5.1.5 libcudnn.so.5 
$ sudo ln -sf libcudnn.so.5 libcudnn.so 
$ sudo ldconfig

安裝完成後,參照第三步的方法,將

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

新增到/etc/profile 檔案中。
使用命令,使得檔案生效。

$ source /etc/profile

驗證安裝情況:
1) 進入/usr/local/cuda/samples資料夾下
2) 使用命令$ sudo make all –j $(nproc) 編譯例子
3) 編譯完成後,進入 /usr/local/cuda/samples/bin/x86_64/linux/release 資料夾下

$ ./deviceQuery    #執行編譯後的檔案

執行完成後可看到CUDA以及GPU的版本資訊。Result = PASS表示通過,安裝成功。

這裡寫圖片描述
安裝過程:
1> 使用命令解壓檔案
tarzxvfOpenBLAS0.2.19.tar.gz2>cd/your/path/OpenBLAS使cd ~/OpenBLAS)
3> 編譯

$ make 
$ make PREFIX=/home/username/yourpath/OpenBLAS           #根據解壓路徑進行路徑修改

將路徑新增到~/.bashrc檔案中(開啟方式:$ sudo gedit ~/.bashrc ):

$ export LD_LIBRARY_PATH=/home/username/yourpath/OpenBLAS:$LD_LIBRARY_PATH
$ sudo apt-get install build-essential
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

然後建立一個資料夾opencv,進入建立的資料夾下

$ cd opencv

將下載的opencv-master壓縮檔案解壓到新建的opencv資料夾中
在opencv資料夾中新建一個release資料夾,並進入其中,進行編譯:

$ mkdir release
$ cd release
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
$ make
$ sudo make install

安裝過程中可能會卡在下載ippicv_linux_20151201中,這時可以自己下載,之後將自行下載的ippicv_linux_20151201檔案放在/opencv/3rdparty/ippicv/download/ linux-808b791a6eac9ed78d32a7666804320e/檔案下路徑下。不同opencv版本對應的ippicv_linux檔案不同,但方法都一樣。

重複編譯opencv時,需要先進行清理,即使用make clean 命令將之前編譯過的檔案進行清理。

第七步:Anaconda2-4.1.1 安裝
Anaconda 下載地址 :https://www.continuum.io/downloads
Anaconda下載之後是一個可執行的shell指令碼檔案,可直接$ bash Anaconda2-4.1.1-Linux-x86_64.sh示進行安裝。
安裝完成之後,修改~/.bashrc檔案,在檔案里加入:

$ sudo gedit ~/.bashrc                  #開啟方式
$ export PATH=/home/username/yourpath/anaconda2/bin:$PATH                   #需要新增的內容

安裝過程:
首先將下載的caffe-master壓縮檔案解壓到caffe資料夾(自己新建的資料夾)中。
然後將當前路徑cd到caffe資料夾下。

$ cd /home/username/yourpath/caffe

執行以下命令:

$ cp Makefile.config.example Makefile.config

之後調整配置檔案Makefile.config,根據安裝過的檔案版本進行修改。文章後面會貼出本文配置下的Makefile.config檔案配置。
配置完成後執行以下命令進行編譯:

$ make all
$ make test
$ make runtest

也可以使用官網提供的另外一種安裝方式,這裡不再給出,可以在caffe-master官方安裝參考連結中找到。

在進行make runtest時可以會出現下面的錯誤:

error while loading shared libraries: libhdf5_hl.so.10: cannot open shared object file: No such file or directory

錯誤顯示該檔案不存在,實際上該檔案時存在的。此時的解決方案是:
在~/.bashrc 檔案中加入以下語句:

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/username/yourpath/anaconda2/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/username/yourpath/OpenBLAS:$LD_LIBRARY_PATH

之後再進行make runtest,成功後會顯示下圖狀態:
這裡寫圖片描述

第九步:編譯Python wrapper
直接在caffe根目錄下進行如下命令即可:

$ make pycaffe

在使用Python進行import caffe時,可能會出現

ImportError: No module named google.protobuf.internal

錯誤,此時只需下載安裝protobuf檔案即可:

$ pip install protobuf

也可直接將該檔案下載下來進行安裝

$ pip install protobuf-3.1.0.post1-py2.py3-none-any.whl

以上安裝方法為回憶版,所以安裝過程中的截圖沒有給出,安裝過成中的細節也沒能全部寫出。如有疑問或者安裝過程中遇到問題可以隨時留言給我 .大家共同探討。

/.bashrc檔案內容:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    # We have color support; assume it's compliant with Ecma-48
    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
    # a case would tend to support setf rather than setaf.)
    color_prompt=yes
    else
    color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to [email protected]:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\[email protected]\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'

    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

# added by Anaconda2 4.1.1 installer
export PATH="/home/z/anaconda2/bin:$PATH"

export PATH="/home/z/anaconda2/lib:$PATH"
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/z/OpenBLAS:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/z/anaconda2/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/z/anaconda2:$LD_LIBRARY_PATH

/etc/profile檔案內容:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

export PATH=/usr/local/cuda-8.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

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 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_50,code=compute_50

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /home/z/OpenBLAS/include
BLAS_LIB := /home/z/OpenBLAS/lib

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

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