您的位置:首页 > Web前端

Ubunt16.04安装CUDA cuDNN OpenCV Caffe完整教程

2017-05-24 11:08 856 查看

一、 安装CUDA

版本
cuda_8.0.61_375.26_linux.run

安装过程

前期准备

1、确认有Nvidia的GPU

$ lspci | grep -i nvidia


2、确认你的GPU是否是CUDA-caplable

可以在这里查看

3、为当前核安装kernel headers和development packages

$ sudo apt-get install linux-headers-$(uname -r)


4、使用以下命令查看Nouveau驱动是否被加载了

* 注意: * Ubuntu系统刚刚安装完成后,默认使用Nouveau驱动,不要进行附加驱动安装,否则后面安装CUDA会出现问题

$ lsmod | grep nouveau
# 如果打印出一些信息,说明Nouveau被加载了,正常情况下会打印出一些关于nouveau的信息


6、将Nouveau驱动加入禁用黑名单

# 创建nouveau黑名单
$vim /etc/modprobe.d/blacklist-nouveau.conf


在blacklist-nouveau.conf中添加如下内容

blacklist nouveau
options nouveau modeset=0


之后运行如下命令

# Regenerate the kernel initramfs
$ sudo update-initramfs -u


开始安装

重新启动系统后,按 Ctrl+Alt+F1 进入文本模式

1、关闭X service (必须先关闭,否则无法安装Nvidia Graphic Driver)

$ sudo /etc/init.d/lightdm stop


2、为cuda_8.0.61_375.26_linux.run增加可执行权限

Chmod +x cuda_8.0.61_375.26_linux.run


3、执行安装

$ sudo ./cuda_8.0.61_375.26_linux.run


4、安装过程中的选项选择

除了opengl选项需要选择no之外,其它一律选yes、default

5、重启桌面服务

$ sudo service lightdm start


6、重新登录系统,添加CUDA环境变量

$ vim ~/.bashrc # 编辑


添加如下内容

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


使配置立即生效

$ source ~/.bashrc #配置立即生效


7、查看CUDA是否安装成功

使用如下命令查看

$ nvcc -V


至此,CUDA已安装完成!

二、安装cuDN

版本问题
cudnn-8.0-linux-x64-v5.0-ga.tgz发现tensorflow最新版不支持cudnn5.0,需要安装5.1版本
cudnn-8.0-linux-x64-v5.1.tgz需要安装5.1版本才能支持tensorflow
1、解压

$ tar -zxvf cudnn-8.0-linux-x64-v5.0-ga.tgz

$ tar -zxvf cudnn-8.0-linux-x64-v5.1.tgz


解压完成会在当前目录出现
cuda
的目录

2、复制cudnn相关文件到相关目录

$ 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*


至此,cudnn配置安装完成!

三、安装OpenCV

参考这里

OpenCV 3.1 Installation Guide on Ubuntu 16.04

1、In Ubuntu 16.04, install the dependencies first and then build the OpenCV 3.1 from source.

$ sudo apt-get install --assume-yes build-essential cmake git
$ sudo apt-get install --assume-yes build-essential pkg-config unzip ffmpeg qtbase5-dev python-dev python3-dev python-numpy python3-numpy
$ sudo apt-get install --assume-yes libopencv-dev libgtk-3-dev libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev
$ sudo apt-get install --assume-yes libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
$ sudo apt-get install --assume-yes libv4l-dev libtbb-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev
$ sudo apt-get install --assume-yes libvorbis-dev libxvidcore-dev v4l-utils


2、从github上下载最新的OpenCV代码

不要下载.zip包

Download the latest source archive for OpenCV 3.1 from https://github.com/opencv/opencv. (Do not download it from http://opencv.org/downloads.html, because the official OpenCV 3.1 does not support CUDA 8.0.)

进入opencv目录,并执行以下命令

$ mkdir build
$ cd build/
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
$ make -j8
$ sudo make install
$ sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
$ sudo ldconfig
$ sudo apt-get update


下面就是泡杯茶,等待编译完成,时间可能有点长…

使用opencv

opencv/build/lib/cv2.so
复制到
~/anaconda2/lib
目录下

运气好的话,直接在python中可以
import cv2
就可以使用了

运气不好的话,比如我第二次在远程服务器上安装,遇到了如下错误

Traceback (most recent call last):
File "train.py", line 5, in <module>
import cv2
ImportError: /home/zwx/anaconda2/bin/../lib/libgomp.so.1: version `GOMP_4.0' not found (required by /usr/lib/x86_64-linux-gnu/libsoxr.so.0)


解决办法如下:

It’s just caused of Anaconda’s gcc libs was compiled by gcc4.xx. by the system owned gcc version is gcc5.xx…

I’ve hacked this problem with copy

libgomp.so.1.0.0, libquadmath.so.0.0.0, libstdc++.so.6.0.21 these files from :

/usr/lib/x86_64-linux-gnu/

to :

/yourAnacondaPath/anaconda2/pkgs/libgcc-4.8.5-2/lib

/yourAnacondaPath/anaconda2/lib

and then create the links; it works for me

就是把
/usr/lib/x86_64-linux-gnu/
目录下的
libgomp.so.1.0.0
libquadmath.so.0.0.0
,
libstdc++.so.6.0.21
文件拷贝到
/yourAnacondaPath/anaconda2/pkgs/libgcc-4.8.5-2/lib
/yourAnacondaPath/anaconda2/lib


四、安装Caffe

Ubuntu 16.04 Installation Guide

The following guide includes the how-to instructions for the installation of BVLC/Caffe on Ubuntu 16.04 with Cuda Toolkit 8.0, CUDNN 5.1 library and OpenCV version 2 or 3. (A small record remains from the previous tutorial for Ubuntu 15.10 with the Cuda Toolkit 7.5, but that part will not be updated any further.) This guide also covers the KUbuntu distribution and the related distributions.

1、首先执行以下命令,安装所需依赖

sudo apt-get update

sudo apt-get upgrade

sudo apt-get install -y build-essential cmake git pkg-config

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

sudo apt-get install -y libatlas-base-dev

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

sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev

# (Python general)
sudo apt-get install -y python-pip

# (Python 2.7 development files)
sudo apt-get install -y python-dev
sudo apt-get install -y python-numpy python-scipy

# (or, Python 3.5 development files)
sudo apt-get install -y python3-dev
sudo apt-get install -y python3-numpy python3-scipy

# (OpenCV 2.4)
sudo apt-get install -y libopencv-dev

(or, OpenCV 3.1 - see the instructions below)


2、从github上下载caffe

$ git clone https://github.com/BVLC/caffe[/code] 
3、复制 Makefile.config.example 为 Makefile.config,命令如下:

$ cp Makefile.config.example Makefile.config


4、配置Makefile.config文件

我的配置如下,注意!需要根据自己的环境来进行配置,不能照抄!

我的python用的是Anaconda,需要自己到官网上下载安装,所一配置文件中的Python路劲核相关配置需要改成anaconda的路径。

## 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 # 使用CUDNN

# 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 # 如果你安装的OpenCV 且版本为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-8.0 # 这里要改为cuda-8.0,原来是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
MATLAB_DIR := /usr/local/MATLAB/R2014a # 这是我的matlab安装路径

# 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 \ # 这里是系统自带Python,我用的是Anaconda,所以这里注释掉
/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 # Anaconda安装路径
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
PYTHON_LIB := $(ANACONDA_HOME)/lib # 开启Anaconda的PYTHON_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.
# 这里添加了/usr/include/hdf5/serial
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
# 这里添加了 /usr/lib/x86_64-linux-gnu/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

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


5、开始编译caffe

由于我用make方式编译出现了问题,而用cmake方式编译成功,所以提供我的cmake方式编译方法

5.1、进入caffe目录

(按照官方教程,需要先进入
~/caffe/python
目录下执行以下命令来安装python接口所需的库)

Python

The main requirements are numpy and boost.python (provided by boost). pandas is useful too and needed for some examples.

You can install the dependencies with

$ for req in $(cat requirements.txt); do pip install $req; done


接下来就可以编译caffe了

$ cd ~/caffe #你的caffe根目录
$ mkdir build
$ cd build
$ cmake ..
$ make all -j8 # 这里要看你的cpu有多少核,比如说我的cpu有4核,但我用-j8或-j12也没问题
$ make install
$ make test
$ make runtest -j8


你会看到如下过程(不完全)

leo@xiaobai:~/caffe/build$ make runtest -j8
-- Boost version: 1.58.0
-- Found the following Boost libraries:
--   system
--   thread
--   filesystem
--   chrono
--   date_time
--   atomic
-- Found gflags  (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libgflags.so)
-- Found glog    (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libglog.so)
-- Found PROTOBUF Compiler: /usr/bin/protoc
-- Found lmdb    (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/liblmdb.so)
-- Found LevelDB (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libleveldb.so)
-- Found Snappy  (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libsnappy.so)
-- CUDA detected: 8.0
-- Found cuDNN: ver. 5.0.5 found (include: /usr/local/cuda-8.0/include, library: /usr/local/cuda-8.0/lib64/libcudnn.so)
-- Added CUDA NVCC flags for: sm_61
-- OpenCV found (/usr/local/share/OpenCV)
-- Found Atlas (include: /usr/include library: /usr/lib/libatlas.so lapack: /usr/lib/liblapack.so
-- NumPy ver. 1.11.3 found (include: /home/leo/anaconda2/lib/python2.7/site-packages/numpy/core/include)
-- Boost version: 1.58.0
-- Found the following Boost libraries:
--   python
-- Could NOT find Doxygen (missing:  DOXYGEN_EXECUTABLE)
--
-- ******************* Caffe Configuration Summary *******************
-- General:
--   Version           :   1.0.0
--   Git               :   1.0-11-g91b0928
--   System            :   Linux
--   C++ compiler      :   /usr/bin/c++
--   Release CXX flags :   -O3 -DNDEBUG -fPIC -Wall -Wno-sign-compare -Wno-uninitialized
--   Debug CXX flags   :   -g -fPIC -Wall -Wno-sign-compare -Wno-uninitialized
--   Build type        :   Release
--
--   BUILD_SHARED_LIBS :   ON
--   BUILD_python      :   ON
--   BUILD_matlab      :   OFF
--   BUILD_docs        :   ON
--   CPU_ONLY          :   OFF
--   USE_OPENCV        :   ON
--   USE_LEVELDB       :   ON
--   USE_LMDB          :   ON
--   USE_NCCL          :   OFF
--   ALLOW_LMDB_NOLOCK :   OFF
--
-- Dependencies:
--   BLAS              :   Yes (Atlas)
--   Boost             :   Yes (ver. 1.58)
--   glog              :   Yes
--   gflags            :   Yes
--   protobuf          :   Yes (ver. 2.6.1)
--   lmdb              :   Yes (ver. 0.9.17)
--   LevelDB           :   Yes (ver. 1.18)
--   Snappy            :   Yes (ver. 1.1.3)
--   OpenCV            :   Yes (ver. 3.2.0)
--   CUDA              :   Yes (ver. 8.0)
--
-- NVIDIA CUDA:
--   Target GPU(s)     :   Auto
--   GPU arch(s)       :   sm_61
--   cuDNN             :   Yes (ver. 5.0.5)
--
-- Python:
--   Interpreter       :   /home/leo/anaconda2/bin/python2.7 (ver. 2.7.13)
--   Libraries         :   /home/leo/anaconda2/lib/libpython2.7.so (ver 2.7.13)
--   NumPy             :   /home/leo/anaconda2/lib/python2.7/site-packages/numpy/core/include (ver 1.11.3)
--
-- Documentaion:
--   Doxygen           :   No
--   config_file       :
--
-- Install:
--   Install path      :   /home/leo/caffe/build/install
--
-- Configuring done


接着我们来编译caffe的python接口

$ make pycaffe


过程如下:

leo@xiaobai:~/caffe/build$ make pycaffe
-- Boost version: 1.58.0
-- Found the following Boost libraries:
--   system
--   thread
--   filesystem
--   chrono
--   date_time
--   atomic
-- Found gflags  (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libgflags.so)
-- Found glog    (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libglog.so)
-- Found PROTOBUF Compiler: /usr/bin/protoc
-- Found lmdb    (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/liblmdb.so)
-- Found LevelDB (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libleveldb.so)
-- Found Snappy  (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libsnappy.so)
-- CUDA detected: 8.0
-- Found cuDNN: ver. 5.0.5 found (include: /usr/local/cuda-8.0/include, library: /usr/local/cuda-8.0/lib64/libcudnn.so)
-- Added CUDA NVCC flags for: sm_61
-- OpenCV found (/usr/local/share/OpenCV)
-- Found Atlas (include: /usr/include library: /usr/lib/libatlas.so lapack: /usr/lib/liblapack.so
-- NumPy ver. 1.11.3 found (include: /home/leo/anaconda2/lib/python2.7/site-packages/numpy/core/include)
-- Boost version: 1.58.0
-- Found the following Boost libraries:
--   python
-- Could NOT find Doxygen (missing:  DOXYGEN_EXECUTABLE)
--
-- ******************* Caffe Configuration Summary *******************
-- General:
--   Version           :   1.0.0
--   Git               :   1.0-11-g91b0928
--   System            :   Linux
--   C++ compiler      :   /usr/bin/c++
--   Release CXX flags :   -O3 -DNDEBUG -fPIC -Wall -Wno-sign-compare -Wno-uninitialized
--   Debug CXX flags   :   -g -fPIC -Wall -Wno-sign-compare -Wno-uninitialized
--   Build type        :   Release
--
--   BUILD_SHARED_LIBS :   ON
--   BUILD_python      :   ON
--   BUILD_matlab      :   OFF
--   BUILD_docs        :   ON
--   CPU_ONLY          :   OFF
--   USE_OPENCV        :   ON
--   USE_LEVELDB       :   ON
--   USE_LMDB          :   ON
--   USE_NCCL          :   OFF
--   ALLOW_LMDB_NOLOCK :   OFF
--
-- Dependencies:
--   BLAS              :   Yes (Atlas)
--   Boost             :   Yes (ver. 1.58)
--   glog              :   Yes
--   gflags            :   Yes
--   protobuf          :   Yes (ver. 2.6.1)
--   lmdb              :   Yes (ver. 0.9.17)
--   LevelDB           :   Yes (ver. 1.18)
--   Snappy            :   Yes (ver. 1.1.3)
--   OpenCV            :   Yes (ver. 3.2.0)
--   CUDA              :   Yes (ver. 8.0)
--
-- NVIDIA CUDA:
--   Target GPU(s)     :   Auto
--   GPU arch(s)       :   sm_61
--   cuDNN             :   Yes (ver. 5.0.5)
--
-- Python:
--   Interpreter       :   /home/leo/anaconda2/bin/python2.7 (ver. 2.7.13)
--   Libraries         :   /home/leo/anaconda2/lib/libpython2.7.so (ver 2.7.13)
--   NumPy             :   /home/leo/anaconda2/lib/python2.7/site-packages/numpy/core/include (ver 1.11.3)
--
-- Documentaion:
--   Doxygen           :   No
--   config_file       :
--
-- Install:
--   Install path      :   /home/leo/caffe/build/install
--
-- Configuring done
CMake Warning (dev) in src/caffe/CMakeLists.txt:
Policy CMP0022 is not set: INTERFACE_LINK_LIBRARIES defines the link
interface.  Run "cmake --help-policy CMP0022" for policy details.  Use the
cmake_policy command to set the policy and suppress this warning.

Target "caffe" has an INTERFACE_LINK_LIBRARIES property which differs from
its LINK_INTERFACE_LIBRARIES properties.

INTERFACE_LINK_LIBRARIES:

caffeproto;/usr/lib/x86_64-linux-gnu/libboost_system.so;/usr/lib/x86_64-linux-gnu/libboost_thread.so;/usr/lib/x86_64-linux-gnu/libboost_filesystem.so;/usr/lib/x86_64-linux-gnu/libboost_chrono.so;/usr/lib/x86_64-linux-gnu/libboost_date_time.so;/usr/lib/x86_64-linux-gnu/libboost_atomic.so;/usr/lib/x86_64-linux-gnu/libpthread.so;/usr/lib/x86_64-linux-gnu/libglog.so;/usr/lib/x86_64-linux-gnu/libgflags.so;$<$<NOT:$<CONFIG:DEBUG>>:/usr/lib/x86_64-linux-gnu/libprotobuf.so>;$<$<CONFIG:DEBUG>:/usr/lib/x86_64-linux-gnu/libprotobuf.so>;-lpthread;/home/leo/anaconda2/lib/libhdf5_hl.so;/home/leo/anaconda2/lib/libhdf5.so;/home/leo/anaconda2/lib/libhdf5_hl.so;/home/leo/anaconda2/lib/libhdf5.so;/usr/lib/x86_64-linux-gnu/librt.so;/usr/lib/x86_64-linux-gnu/libpthread.so;/home/leo/anaconda2/lib/libz.so;/usr/lib/x86_64-linux-gnu/libdl.so;/usr/lib/x86_64-linux-gnu/libm.so;/home/leo/anaconda2/lib/libhdf5_hl.so;/home/leo/anaconda2/lib/libhdf5.so;/home/leo/anaconda2/lib/libhdf5_hl.so;/home/leo/anaconda2/lib/libhdf5.so;/usr/lib/x86_64-linux-gnu/librt.so;/usr/lib/x86_64-linux-gnu/libpthread.so;/home/leo/anaconda2/lib/libz.so;/usr/lib/x86_64-linux-gnu/libdl.so;/usr/lib/x86_64-linux-gnu/libm.so;/usr/lib/x86_64-linux-gnu/liblmdb.so;/usr/lib/x86_64-linux-gnu/libleveldb.so;/usr/local/cuda-8.0/lib64/libcudart.so;/usr/local/cuda-8.0/lib64/libcurand.so;/usr/local/cuda-8.0/lib64/libcublas.so;/usr/local/cuda-8.0/lib64/libcudnn.so;opencv_core;opencv_highgui;opencv_imgproc;opencv_imgcodecs;/usr/lib/liblapack.so;/usr/lib/libcblas.so;/usr/lib/libatlas.so;/usr/lib/x86_64-linux-gnu/libboost_python.so

LINK_INTERFACE_LIBRARIES:

caffeproto;/usr/lib/x86_64-linux-gnu/libboost_system.so;/usr/lib/x86_64-linux-gnu/libboost_thread.so;/usr/lib/x86_64-linux-gnu/libboost_filesystem.so;/usr/lib/x86_64-linux-gnu/libboost_chrono.so;/usr/lib/x86_64-linux-gnu/libboost_date_time.so;/usr/lib/x86_64-linux-gnu/libboost_atomic.so;/usr/lib/x86_64-linux-gnu/libpthread.so;/usr/lib/x86_64-linux-gnu/libglog.so;/usr/lib/x86_64-linux-gnu/libgflags.so;/usr/lib/x86_64-linux-gnu/libprotobuf.so;-lpthread;/home/leo/anaconda2/lib/libhdf5_hl.so;/home/leo/anaconda2/lib/libhdf5.so;/home/leo/anaconda2/lib/libhdf5_hl.so;/home/leo/anaconda2/lib/libhdf5.so;/usr/lib/x86_64-linux-gnu/librt.so;/usr/lib/x86_64-linux-gnu/libpthread.so;/home/leo/anaconda2/lib/libz.so;/usr/lib/x86_64-linux-gnu/libdl.so;/usr/lib/x86_64-linux-gnu/libm.so;/home/leo/anaconda2/lib/libhdf5_hl.so;/home/leo/anaconda2/lib/libhdf5.so;/home/leo/anaconda2/lib/libhdf5_hl.so;/home/leo/anaconda2/lib/libhdf5.so;/usr/lib/x86_64-linux-gnu/librt.so;/usr/lib/x86_64-linux-gnu/libpthread.so;/home/leo/anaconda2/lib/libz.so;/usr/lib/x86_64-linux-gnu/libdl.so;/usr/lib/x86_64-linux-gnu/libm.so;/usr/lib/x86_64-linux-gnu/liblmdb.so;/usr/lib/x86_64-linux-gnu/libleveldb.so;/usr/local/cuda-8.0/lib64/libcudart.so;/usr/local/cuda-8.0/lib64/libcurand.so;/usr/local/cuda-8.0/lib64/libcublas.so;/usr/local/cuda-8.0/lib64/libcudnn.so;opencv_core;opencv_highgui;opencv_imgproc;opencv_imgcodecs;/usr/lib/liblapack.so;/usr/lib/libcblas.so;/usr/lib/libatlas.so;/usr/lib/x86_64-linux-gnu/libboost_python.so

This warning is for project developers.  Use -Wno-dev to suppress it.

-- Generating done
-- Build files have been written to: /home/leo/caffe/build
[  1%] Built target caffeproto
[ 98%] Built target caffe
[100%] Linking CXX shared library ../lib/_caffe.so
Creating symlink /home/leo/caffe/python/caffe/_caffe.so -> /home/leo/caffe/build/lib/_caffe.so
[100%] Built target pycaffe


至此,caffe安装完成!

**注意**caffe安装完成后,并不能直接在python环境下导入caffe模块,像这样:

leo@xiaobai:~$ python
Python 2.7.13 |Anaconda custom (64-bit)| (default, Dec 20 2016, 23:09:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org >>> import caffe
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named caffe
>>>


需要自己在代码中添加~/caffe/python/路径到环境变量,像这样:

>>> caffe_root='/home/leo/caffe/'
>>> import os,sys
>>> os.chdir(caffe_root)
>>> sys.path.insert(0,caffe_root+'python')
>>> import caffe
>>>


或者

To import the caffe Python module after completing the installation, add the module directory to your $PYTHONPATH by `export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH` or the like. You should not import the module in the caffe/python/caffe directory!


编译matlab接口

请直接看这里,成功的编译了matcaffe接口

我遇到了一个错误,(原因可能是因为我使用的zsh bash,不是系统自带的bash,所以会遇到如下错误),在此做一个记录



我的解决方案如下:

~/.bashrc
~/.zshrc
(我使用的是这个)中添加如下语句

export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6

# 以下两条语句一般在cuda和anaconda安装完之后自动添加,自己检查以下是否存在
export LD_LIBRARY_PATH="/usr/local/cuda/lib64":$LD_LIBRARY_PATH
export LD_LIBRARY_PATH="/home/leo/anaconda2/lib":$LD_LIBRARY_PATH


编译成功如下:



解决方法参考这里这里

以下是我看到的其他教程,没有编译成功,这里做个记录。

由于我的ubuntu16.04自带的gcc版本为5.4.0,而matlab2014a要求的gcc版本为4.7,所以直接用

$ make all matcaffe


命令无法编译完成,解决方式如下:

首先在终端执行gcc 4.7的安装命令:

$ sudo apt-get update

$ sudo apt-get install gcc-4.7 g++-4.7


用一下命令来切换gcc、g++的版本

$ sudo update-alternatives --config gcc
$ sudo update-alternatives --config g++


验证gcc-4.7是否安装并成为系统的默认版本:

$ gcc -v


编译matcaffe

$ make matcaffe -j8


* 注意,这里有一个坑,各位一定不要踩*

我一开使在终端执行了gcc 4.7安装命令,然后将gcc核g++切换到4.7,接着我重新编译了caffe,结果编译不通过!编译不通过!编译不同过!我猜测的原因是,编译CUDA的时候使用的是gcc 5.4,这时候再使用gcc 4.7重新编译caffe所以不通过,正确的姿势应该是先编译caffe,再将gcc和g++版本切换到4.7,再使用
make all matcaffe
命令编译matcaffe接口。

总结

安装过程中不可能一帆风顺,再完善的教程也不可能应对所有人的情况,这需要自己学会解决问题,我遇到的所有问题都在Google上找到了解决办法,配置caffe需要有足够的耐心。

3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: