1. 程式人生 > >ubunt16.04下caffe環境安裝

ubunt16.04下caffe環境安裝

一. 系統初始環境

系統:Ubuntu16.04: ubuntu-16.04-desktop-amd64.iso

cuda安裝檔案: cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64.deb.44-1_amd64-deb,下載連結點選, linux-x86架構-ubuntu-16.04-deb(local)

cudnn安裝檔案: cudnn-8.0-linux-x64-v5.0-ga.solitairetheme8,下載連結點選, 適用cuda8.0有5.1和5.0版,這裡用5.0版,區別應該不大

caffe原始碼: github連結 ,或者執行git clone

https://github.com/BVLC/caffe.git

二. 安裝cuda8.0

  1. sudo apt-get update
  2. sudo apt-get upgrade
  3. 刪除自帶顯示卡驅動 sudo apt-get –purge remove nvidia-*
  4. sudo dpkg -i cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64.deb.44-1_amd64-deb (這個檔案包含了nvidia-367的驅動)
  5. 安裝cuda: sudo apt-get install cuda
  6. reboot

測試安裝結果:
1. cd /usr/local/cuda/samples/1_Utilities/deviceQuery
2. sudo make
3. ./deviceQuery

測試成功顯示結果(部分):

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0,

CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GTX 960M

Result = PASS

如果出現Result = Failed之類的表示cuda安裝失敗

新增環境變數:

在/etc/profile中新增

export PATH=/usr/local/cuda-8.0/bin:PATHexportLDLIBRARYPATH=/usr/local/cuda8.0/li

b64:LD_LIBRARY_PATH

三. 安裝cudnn5.0

  1. 解壓安裝包

    tar zxvf cudnn-8.0-linux-x64-v5.0-ga.solitairetheme8
  2. 複製檔案

    sudo cp cuda/include/cudnn.h /usr/local/cuda/include/

    sudo cp cuda/lib64/libcudnn.so.5.0.5 /usr/local/cuda/lib64/
  3. 建立軟連結

    cd /usr/local/cuda/lib64/

    sudo ln -s libcudnn.so.5.0.5 libcudnn.so.5

    sudo ln -s libcudnn.so.5 libcudnn.so

四. 安裝caffe

  1. 安裝依賴庫

    sudo apt-get install build-essential

    sudo apt-get install git cmake gedit doxygen

    sudo apt-get install python-numpy python-pip cython easydict

    sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler

    sudo apt-get install libopencv-dev (想使用opencv3的可以嘗試跳過這一步自行安裝opencv3)

    sudo apt-get install –no-install-recommends libboost-all-dev

    sudo apt-get install libatlas-base-dev

    sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
  2. 修改配置檔案Make.config

    cd caffe-master

    cp Makefile.config.example Makefile.config

    sudo gedit Makefile.config

    開啟GPU模式:將#USE_CUDNN := 1前的#註釋去掉,表示使用cudnn,如果不是使用GPU,可以將#CPU_ONLY := 1前得註釋去掉
    pycaffe模組配置:修改\#Whatever else you find you need goes here.
    INCLUDE_DIRS:= $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
    LIBRARY_DIRS:=$(PYTHON_LIB) /usr/local/lib /usr/lib/usr/lib/x86_64-linux-gnu/hdf5/serial
    

如果要用opencv3編譯的話還需要改動:

    LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5

    # handle IO dependencies
    USE_LEVELDB ?= 1
    USE_LMDB ?= 1
    USE_OPENCV ?= 1

    ifeq ($(USE_LEVELDB), 1)
        LIBRARIES += leveldb snappy
    endif
    ifeq ($(USE_LMDB), 1)
        LIBRARIES += lmdb
    endif
    ifeq ($(USE_OPENCV), 1)
        LIBRARIES += opencv_core opencv_highgui opencv_imgproc 

        ifeq ($(OPENCV_VERSION), 3)
            LIBRARIES += opencv_imgcodecs
        endif

    endif

4. 編譯caffe

mkdir build

cd build

cmake ..

make all -j8

  1. 編譯pycaffe

    cd caffe-master

    make pycaffe

  2. 編譯測試模組

    make all

    make test

    make runtest

測試成功顯示結果(部分):

[———-] Global test environment tear-down

[==========] 2091 tests from 283 test cases ran. (415487 ms total)

[ PASSED ] 2091 tests.

新增環境變數:

在~/.bashrc中新增

export PYTHONPATH=/home/hyzhan/caffe-master/:$PYTHONPATH

export PYTHONPATH=/home/hyzhan/caffe-master/caffe/python:$PYTHONPATH