ubuntu18.04+cuda9.0+cudnn7.1.4+caffe-ssd+anaconda2安裝


幾乎是小白一枚,折騰多天才搞定,參考了很多博客和社區問答,把安裝經驗記下來。

1.檢測顯卡,安裝驅動

1 ~$ ubuntu-drivers devices
2 == /sys/devices/pci0000:00/0000:00:1c.4/0000:09:00.0 ==
3 modalias : pci:v000010DEd00001292sv00001028sd00000616bc03sc02i00
4 vendor : NVIDIA Corporation
5 model : GK208M [GeForce GT 740M]
6 driver : nvidia-340 - distro non-free
7 driver : nvidia-driver-390 - distro non-free recommended
8 driver : xserver-xorg-video-nouveau - distro free builtin

 選擇后面帶recommended的驅動

1 ~$ sudo apt install nvidia-driver-390

重啟,在系統設置-詳細信息的圖形一欄查看gpu

2.安裝CUDA和cuDNN

這方面攻略很多,我就不一一寫了,CUDA9.0,共5個文件。cuDNN7.1.4:

cuDNN v7.1.4 Runtime Library for Ubuntu16.04 (Deb)

cuDNN v7.1.4 Developer Library for Ubuntu16.04 (Deb)

cuDNN v7.1.4 Code Samples and User Guide for Ubuntu16.04 (Deb)

gcc、g++降級

1 sudo apt-get install gcc-4.8 
2 sudo apt-get install g++-4.8

在\usr\bin下

sudo rm  g++ ##刪除原鏈接
sudo rm  gcc
sudo ln -s gcc-4.8 gcc ##重建鏈接
sudo ln -s g++-4.8 g++

下載好對應版本CUDA和cuDNN安裝包(此處省略)

安裝CUDA

由於前面已安裝好驅動,在裝CUDA時不用按ctrl+alt+f3切換到命令行界面,不用輸入關閉一系列xxx的命令,只需在圖形界面下運行.run文件

sudo sh cuda_9.0.176_384.81_linux.run

同意協議后,問是否安裝顯卡驅動,填no,剩下部分填yes或回車,剩下就沒什么問題了。我安裝后提示缺少幾個文件,有點忘了,這個百度一下就能解決。之后再安裝CUDA的幾個補丁。

sudo sh cuda_9.0.176.1_linux.run ##共有四個補丁,這里只列一個

最后記得在~/.bashrc里加上這兩條:

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

安裝cuDNN(省略)

 

3.安裝anaconda2(省略)

4.安裝caffe-ssd

安裝各種依賴:

在/etc/apt下打開sources.list,源代碼一項打鈎,然后

sudo apt build-dep caffe-cuda

再按照之前辦法安裝gcc5,g++5,修改鏈接,之后要用g++5編譯caffe

git下載caffe-ssd源代碼

git clone https://github.com/weiliu89/caffe.git
cd caffe
git checkout ssd
cp Makefile.config.example Makefile.config

修改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 lines after *_35 for compatibility.
 #           -gencode arch=compute_20,code=sm_20 \
 #           -gencode arch=compute_20,code=sm_21 \
CUDA_ARCH := -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_61,code=sm_61

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
 BLAS := atlas
#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 := /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.
#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)/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.6m
# PYTHON_INCLUDE := /usr/include/python3.6 \
#                 /usr/lib/python3/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 /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

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

安裝caffe

在caffe根目錄下

make -j8
make py
make test -j8
make runtest -j8

make runtest -j8如果出現這種error:

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

 

,就在.bashrc中加上:

export LD_LIBRARY_PATH="/home/.../anaconda2/lib:$LD_LIBRARY_PATH"

在caffe根目錄下的:

source ~/.bashrc
make runtest -j8

應該能通過,那么caffe-ssd就安裝好了。

之后如果import caffe時報錯:No module named caffe,就在.bashrc文件加上:

export PYTHONPATH="/home/.../caffe/python"

如果跑代碼時出現 no module named google.protobuf,用:

conda install protobuf

到這里安裝就完成了。

其實我本來配置的是caffe+anaconda3,但ssd貌似是用python2寫的,python3跑不起來?所以就換了anaconda2。這里也順便說下caffe+anaconda3配置:

裝好anaconda3后,caffe下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 lines after *_35 for compatibility.
 #           -gencode arch=compute_20,code=sm_20 \
 #           -gencode arch=compute_20,code=sm_21 \
CUDA_ARCH := -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_61,code=sm_61

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
 BLAS := atlas
#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 := /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.
#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)/anaconda3
 PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
        $(ANACONDA_HOME)/include/python3.6m \
        $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include \

# Uncomment to use Python 3 (default is Python 2)
 PYTHON_LIBRARIES := boost_python3 python3.6m
# PYTHON_INCLUDE := /usr/include/python3.6 \
#                 /usr/lib/python3/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 /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

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

還是用g++5,make前,打開.bashrc,要把/home/.../anaconda/lib從LD_LIBRARY_PATH中刪去。在make過程中如果有boost::xxx 未定義,去boost官網下載源碼,找找相關教程編譯安裝好,然后在caffe根目錄下:

make clean
make -j8

應該就可以了,后續如果有bug可參考前面。

 
       


免責聲明!

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



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