Ubuntu16.04系统中CUDA9+MKL+MATLAB2015b+Anaconda3-5.1.0+OpenCV3.4.0+TensorFlow_GPU-1.6+Keras+caffe安装

一、安装CUDA

1、查看显卡驱动是否安装好:

$nvidia-settings

Ubuntu16.04系统中CUDA9+MKL+MATLAB2015b+Anaconda3-5.1.0+OpenCV3.4.0+TensorFlow_GPU-1.6+Keras+caffe安装

2、安装OpenGL:

sudo apt-get install build-essential libgl1-mesa-dev

sudo apt-get install freeglut3-dev

sudo apt-get install libglew-dev libsdl2-dev libsdl2-image-dev libglm-devlibfreetype6-dev

3、进入下载好的.run文件目录,依次执行:

sudo sh cuda_9.0.176_384.81_linux.run

sudo sh cuda_9.0.176.1_linux.run

sudo sh cuda_9.0.176.2_linux.run

(按空格键直至协议许可部分,按提示进行安装,不用再次安装显卡驱动)

安装结束后的显示:

===========

=Summary =

===========

Driver: Not Selected

Toolkit: Installed in /usr/local/cuda-9.0

Samples: Installed in /home/zili, but missing recommended libraries

Pleasemake sure that

- PATH includes /usr/local/cuda-9.0/bin

- LD_LIBRARY_PATH includes /usr/local/cuda-9.0/lib64, or, add/usr/local/cuda-9.0/lib64 to /etc/ld.so.conf and run ldconfig as root


Touninstall the CUDA Toolkit, run the uninstall script in/usr/local/cuda-9.0/bin

Pleasesee CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-9.0/doc/pdffor detailed information on setting up CUDA.


***WARNING:Incomplete installation! This installation did not install the CUDADriver. A driver of version at least 384.00 is required for CUDA 9.0functionality to work.

Toinstall the driver using this installer, run the following command,replacing <CudaInstaller> with the name of this run file:

sudo<CudaInstaller>.run -silent -driver

4、将CUDA添加到Path:

sudo gedit ~/.bashrc

在该文件最后加入以下两行并保存:

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

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

使该配置生效:source ~/.bashrc


注意:安装cuda时,不要修改/etc/profile,所有的环境变量均写在~/.bashrc里,修改前者容易出现系统错误。

5cudnn动态库更换:

解压后进入include文件夹,执行:

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

cd ..

再将lib64目录下的动态文件进行复制

sudo cp lib64/* /usr/local/cuda/lib64/


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

6、验证CUDA是否安装成功:

进入Samples文件夹,执行

sudo make -j8

cd 1_Utilities/deviceQuery

./deviceQuery

出现以下输出表示安装成功:

./deviceQueryStarting...

CUDADevice Query (Runtime API) version (CUDART static linking)

Detected1 CUDA Capable device(s)

Device0: "GeForce GTX 1080"

CUDADriver Version / Runtime Version 9.0 / 9.0

CUDACapability Major/Minor version number: 6.1

Totalamount of global memory: 8114 MBytes (8508145664bytes)

(20)Multiprocessors, (128) CUDA Cores/MP: 2560 CUDA Cores

GPUMax Clock rate: 1771 MHz (1.77 GHz)

MemoryClock rate: 5005 Mhz

MemoryBus Width: 256-bit

L2Cache Size: 2097152 bytes

MaximumTexture Dimension Size (x,y,z) 1D=(131072), 2D=(131072,65536), 3D=(16384, 16384, 16384)

MaximumLayered 1D Texture Size, (num) layers 1D=(32768), 2048 layers

MaximumLayered 2D Texture Size, (num) layers 2D=(32768, 32768), 2048 layers

Totalamount of constant memory: 65536 bytes

Totalamount of shared memory per block: 49152 bytes

Totalnumber of registers available per block: 65536

Warpsize: 32

Maximumnumber of threads per multiprocessor: 2048

Maximumnumber of threads per block: 1024

Maxdimension size of a thread block (x,y,z): (1024, 1024, 64)

Maxdimension size of a grid size (x,y,z): (2147483647, 65535, 65535)

Maximummemory pitch: 2147483647 bytes

Texturealignment: 512 bytes

Concurrentcopy and kernel execution: Yes with 2 copy engine(s)

Runtime limit on kernels: Yes

IntegratedGPU sharing Host Memory: No

Supporthost page-locked memory mapping: Yes

Alignmentrequirement for Surfaces: Yes

Devicehas ECC support: Disabled

Devicesupports Unified Addressing (UVA): Yes

SupportsCooperative Kernel Launch: Yes

SupportsMultiDevice Co-op Kernel Launch: Yes

DevicePCI Domain ID / Bus ID / location ID: 0 / 1 / 0

ComputeMode:

<Default (multiple host threads can use ::cudaSetDevice() with devicesimultaneously) >

deviceQuery,CUDA Driver = CUDART, CUDA Driver Version = 9.0, CUDA Runtime Version= 9.0, NumDevs = 1

Result= PASS


二、BLAS安装与配置

BLAS(基础线性代数集合)是一个应用程序接口的标准。caffe官网上推荐了三种实现:ATLAS,MKL, orOpenBLAS。其中atlas可以直接通过命令行安装,在此不再介绍。我采用的是intelmkl库,首先,通过链接https://software.intel.com/en-us/qualify-for-free-software/studentintel官网申请学生版的**ParallelStudio XE Cluster Edition **,下载完成之后cd到下载目录进行安装:

$tar zxvf parallel_studio_xe_2018_update2_cluster_edition.tgz #解压下载文件

$chmod 777 parallel_studio_xe_2018_update2_cluster_edition -R #获取文件权限

$cd parallel_studio_xe_2018_update2_cluster_edition/

$sudo ./install_GUI.sh


***:ST5V-XXXXXXXX,选择自定义安装MKL


安装完成之后,进行相关文件的链接:

$sudo gedit /etc/ld.so.conf.d/intel_mkl.conf

在打开的文件中添加库文件:

/opt/intel/lib/intel64

/opt/intel/mkl/lib/intel64


添加完成之后,编译链接使lib文件立即生效:

$sudo ldconfig


三、安装Java

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

将下载下来的jdk-8u101-linux-x64.tar.gz文件解压。使用如下命令解压:

sudo tar zxvf ./jdk-8u101-linux-x64.tar.gz

为了方便管理可将解压后的文件移至/usr/java目录下。

设置环境变量:

编辑.bashrc文件sudo gedit ~/.bashrc

在该文件中添加如下内容:

JAVA_HOME=/usr/java

JRE_HOME=$JAVA_HOME/jre

JAVA_BIN=$JAVA_HOME/bin

CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

export JAVA_HOME JRE_HOME PATH CLASSPATH


为了让更改立即生效,请在终端执行如下命令:

source ~/.bashrc

通过以上步骤,JDK已安装完成。输入以下命令验证java -version


四、安装MATLAB2015b

注意不要安装Matlab2016b2017b等较新的版本,否则编译出的matcaffe库无法通过makemattest

1、挂载映像文件:

cd~

mkdir matlab_iso

sudo mount -t auto -o loop LinuxMatlab/R2015b_glnxa64.iso matlab_iso/

2、安装Matlab

sudo ./matlab_iso/install

输入安装**:09806-07443-53955-64350-21751-41297

结束安装后取消iso挂载:

umount matlab_iso/

sudo rm -r matlab_iso/ #删除空的文件夹

3、**:

cd /usr/local/MATLAB/R2015b/bin

sudo ./matlab

首先选择无internet手动**,选择license_standalone.lic

然后将*****文件夹中的3*.so文件复制到/usr/local/MATLAB/R2015b/bin/glnxa64目录下:

[email protected]:~/LinuxMatlab/Linux64_*****$

sudo cp R2015b/bin/glnxa64/libcufft.so.7.0.28 /usr/local/MATLAB/R2015b/bin/glnxa64

sudo cp R2015b/bin/glnxa64/libinstutil.so /usr/local/MATLAB/R2015b/bin/glnxa64

sudo cp R2015b/bin/glnxa64/libmwservices.so /usr/local/MATLAB/R2015b/bin/glnxa64

4、创建快捷方式:

打开Ubuntu软件中心,搜索matlab,点击install,在弹出的安装路径填入:/usr/local/MATLAB/R2015b

用户权限不用填,表示全部用户可用、gcc不填

完成安装,在启动栏可以看到有matlab图标。

注意首次打开MATLAB需在终端输入sudo /usr/local/MATLAB/R2015b/bin/matlab才行。


如果Ubuntu软件中心的MATLAB支持接口不是首次安装,出现路径错误,要修改MATLAB的路径可进入/etc/matlab/debconf文件修改。


如果点击启动栏上的MATLAB出现java.lang.runtime.Exception错误,只需要在终端中输入以下命令即可:

sudo chmod -R a+rw ~/.matlab


设置matlab默认工作路径:预设-常规-初始工作文件夹-/home/zili/Programming/MATLAB



caffe通过make mattest后,可选择设置matlab默认工作路径:

sudo gedit /usr/local/MATLAB/R2015b/toolbox/local/matlabrc.m

在该文件最后加入以下语句:

cd '/home/zili/Programming/MATLAB'


UbuntuMatlab路径不能永久保存的问题及其解决方案

原因:pathdef.m文件的权限问题

解决方法:

cd /usr/local/MATLAB/R2015b/toolbox/local

sudo chmod 777 pathdef.m


五、安装Anaconda3

到官网下载anaconda3

地址:https://www.anaconda.com/download/#linux

下载好后执行:

bash ~/Downloads/Anaconda3-5.1.0-Linux-x86_64.sh

注意选择将Anaconda加入Path中。

Anaconda换源:

制定清华的源:

conda config --add channelshttps://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

有资源显示源地址:

conda config --set show_channel_urls yes


六、编译安装OpenCV3.4.0

注意OpenCV版本!以下方式编译OpenCV3.4.1可能会出错。

1、安装依赖:

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

sudo apt-get install ffmpeg libgtk-3-dev python3-numpy qtbase5-dev  unzip


2、下载和编译

https://github.com/opencv/opencv/releases/tag/3.4.0下载opencv并解压,在解压后的文件夹中:

mkdir build #新建一个build文件夹,编译的工程都在这个文件夹里

cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D WITH_OPENMP=ON -D WITH_CUDA=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_TESTS=OFF -D PYTHON_EXECUTABLE=/home/zili/anaconda3/bin/python3 -D PYTHON_INCLUDE_DIR=/home/zili/anaconda3/include/python3.6m -D PYTHON_LIBRARY=/home/zili/anaconda3/lib/libpython3.6m.so -D PYTHON_NUMPY_INCLUDE_DIRS=/home/zili/anaconda3/lib/python3.6/site-packages/numpy/core/include -D INSTALL_C_EXAMPLES=OFF -D INSTALL_PYTHON_EXAMPLES=OFF -D ENABLE_AVX=ON -D WITH_OPENCL=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D WITH_CUBLAS=ON -D BUILD_OPENCV_PYTHON3=ON -D OPENCV_ENABLE_NONFREE=ON -D BUILD_SHARED_LIBS=ON ..


注:上述某些路径可通过命令获得,如

python的路径:importsys; print(sys.prefix)

PYTHON3_INCLUDE_DIR=$(python3-c "from distutils.sysconfig import get_python_inc;print(get_python_inc())")

PYTHON3_PACKAGES_PATH=$(python3-c "from distutils.sysconfig import get_python_lib;print(get_python_lib())")

然后:

make -j8 #开始编译

编译无误后结果:

。。。

[100%]Linking CXX shared module../../lib/python3/cv2.cpython-36m-x86_64-linux-gnu.so

[100%]Built target opencv_python3

上面的cv2.cpython-36m-x86_64-linux-gnu.so即为编译得到的OpenCV库。

接着执行:

sudo make install

sudo /bin/bash -c 'echo "/usr/local/lib" >/etc/ld.so.conf.d/opencv.conf'

sudo ldconfig


3、创建链接到Anaconda虚拟环境中

如果缺少此步骤,那么在AnacondaPython中使用import cv2会出错!

参考http://www.learnopencv.com/install-opencv3-on-ubuntu/中的Createsymlink in virtual environment部分。cd到虚拟环境路径中

[email protected]:~/anaconda3/lib/python3.6/site-packages$ln -s /usr/local/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so


4、测试OpenCV3

继续参考http://www.learnopencv.com/install-opencv3-on-ubuntu/,到http://www.learnopencv.com/wp-content/uploads/2017/06/RedEyeRemover.zip下载RedEyeRemover.zip测试用的文件。

4.1Test C++ code

Moveinside extracted folder, compile and run.

#compile

#There are backticks ( ` ) around pkg-config command not single quotes

g++-std=c++11 removeRedEyes.cpp `pkg-config --libs --cflags opencv` -oremoveRedEyes

#run

./removeRedEyes


4.2Test Python code

ActivatePython virtual environment

#import cv2 and print version (run following commands in ipython)

importcv2

print(cv2.__version__)

#If OpenCV3 is installed correctly,

#above command should give output 3.4.0

#Press CTRL+D to exit ipython


RunRedEyeRemover demo:

pythonremoveRedEyes.py


Nowyou can exit from Python virtual environment, using “source deactivate”


Whenever you are going to run Python scripts which use OpenCV you should activate the virtual environment we created, using “source activate env名称command.


七、TensorFlow_GPU-1.6Keras安装

有了上述安装的Anaconda后,可以按照官网的提示进行安装。https://tensorflow.google.cn/install/install_linux#installing_with_anaconda

1、创建虚拟环境

在终端执行:conda create -n tensorflow

输出的执行结果:

##Package Plan ##

environment location: /home/zili/anaconda3/envs/tensorflow

Proceed([y]/n)? y

Preparingtransaction: done

Verifyingtransaction: done

Executingtransaction: done

#

#To activate this environment, use:

#> source activate tensorflow

#

#To deactivate an active environment, use:

#> source deactivate

#

2、下载tensorflow

地址为:https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.6.0-cp36-cp36m-linux_x86_64.whl

3**虚拟环境并安装

切换到下载的路径后执行:

source activate tensorflow

pip install --ignore-installed --upgrade tensorflow_gpu-1.6.0-cp36-cp36m-linux_x86_64.whl

pip install keras

这时会联网验证其他的开发库是否满足。

source deactivate

4、验证

输入:

source activate tensorflow

python

import tensorflow as tf

import keras

import cv2

hello= tf.constant('Hello, TensorFlow!')

sess= tf.Session()

print(sess.run(hello))


去掉FutureWarning:Conversion of the second argument of issubdtype from `float` to`np.floating` is deprecated. In future, it will be treated as`np.float64 == np.dtype(float).type`.的解决方案一:

import os

os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"

#或者方案二:pip install numpy==1.13.0


八、编译caffe

1、安装依赖: 

(为了防止hdf5依赖库和anaconda中的hdf5冲突,所以此处未安装,后面再手动安装)

sudo apt-get update

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-devlibgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler

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


手动安装HDF5,参照Matlab中使用Caffe出现HDF5library version mismatchederror的解决办法http://xinghao.me/2015/12/11/2015-12-11-caffe-hdf5/

安装「hdf-1.10.1https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.10/hdf5-1.10.1/src/

HDF5网站下载1.10.1版本的源代码(因为上述Anaconda中的hdf库为此版本)。

解压,按照release_docs/INSTALL上的说明进行编译安装即可。

$cd hdf5-X.Y.Z

$./configure --prefix=/usr/local/hdf5

$make

$make check # run test suite.

$make install

$make check-install # verify installation.

安装成功后需要添加环境变量

vim ~/.bashrc

在文件末加入下面这一行:

export LD_LIBRARY_PATH=/usr/local/hdf5/lib:$LD_LIBRARY_PATH

然后执行下列语句使得修改即时生效:

source ~/.bashrc


2、下载caffe

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


3、修改编译配置文件

cd caffe //打开到刚刚git下来的caffe

sudo cp Makefile.config.example Makefile.config //Makefile.config.example的内容复制到Makefile.config

//因为make指令只能makeMakefile.config文件,而Makefile.config.examplecaffe给出的makefile例子

sudo gedit 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 ifnecessary)

# Youshould not set this flag if you will be reading LMDBs with any

# possibilityof 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 thefollowing.

#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 thisinstead:

#CUDA_DIR := /usr


#CUDA architecture setting: going with all of them.

#For CUDA < 6.0, comment the *_50 through *_61 lines forcompatibility.

#For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.

#For CUDA >= 9.0, comment the *_20 and *_21 lines forcompatibility.

CUDA_ARCH:=-gencode arch=compute_30,code=sm_30 \

-gencodearch=compute_35,code=sm_35 \

-gencodearch=compute_50,code=sm_50 \

-gencodearch=compute_52,code=sm_52 \

-gencodearch=compute_60,code=sm_60 \

-gencodearch=compute_61,code=sm_61 \

-gencodearch=compute_61,code=compute_61


#BLAS choice:

#atlas for ATLAS (default)

#mkl for MKL

#open for OpenBlas

BLAS:= mkl

#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 standardsearch 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/R2015b

MATLAB_INCLUDE:= /usr/local/MATLAB/R2015b/toolbox/distcomp/gpu/extern/include

#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/zili/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.5m

PYTHON_LIBRARIES:= boost_python-py35 python3.6m

#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 againstPython libs)

WITH_PYTHON_LAYER:= 1


#Whatever else you find you need goes here.

INCLUDE_DIRS:= $(PYTHON_INCLUDE) /usr/local/include /usr/local/hdf5/include $(MATLAB_INCLUDE)

LIBRARY_DIRS:= $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/hdf5/lib


#If Homebrew is installed at a non standard location (for example yourhome 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 inone 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 tohttps://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?= @

################################################################################


AnacondaPython库链接到系统库中:

[email protected]:/usr/lib/x86_64-linux-gnu$sudo ln -s /home/zili/anaconda3/lib/libpython3.6m.so.1.0 libpython3.6m.so.1.0


然后修改makefile文件

NVCCFLAGS+=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)

替换为:

NVCCFLAGS+= -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)


大约第416行那一句话CXXFLAGS+= -MMD -MP的下面添加CXXFLAGS+= -std=c++11,防止matlab接口的编译出现不支持该版本的gcc。最后是这样:

CXXFLAGS+= -MMD -MP

CXXFLAGS+= -std=c++11


4编译并测试

在命令行依次输入:

sudoldconfig

source~/.bashrc


makeall -j8

maketest -j8

makeruntest


makepycaffe

makematcaffe

若出错则需要重新编译,且在编译前清理一下

makeclean


部分截图如下:

Ubuntu16.04系统中CUDA9+MKL+MATLAB2015b+Anaconda3-5.1.0+OpenCV3.4.0+TensorFlow_GPU-1.6+Keras+caffe安装


编译make matcaffe完成后,执行make mattest后,往往出现InvalidMEX-file"问题,其原因是MATLABlinux的库冲突,解决的方法是用linux的库。

利用ldd caffe_.mexa64排错:

[email protected]:~/caffe-master/matlab/+caffe/private$ldd caffe_.mexa64

。。。

libhdf5_hl.so.100=> /usr/local/hdf5/lib/libhdf5_hl.so.100 (0x00007f03bc021000)

libhdf5.so.101=> /usr/local/hdf5/lib/libhdf5.so.101 (0x00007f03bba56000)

libopencv_core.so.3.4=> /usr/local/lib/libopencv_core.so.3.4 (0x00007f03ba795000)

libopencv_imgproc.so.3.4=> /usr/local/lib/libopencv_imgproc.so.3.4 (0x00007f03ba0e3000)

libopencv_imgcodecs.so.3.4=> /usr/local/lib/libopencv_imgcodecs.so.3.4 (0x00007f03b9e06000)

libstdc++.so.6=> /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f03b985e000)

libcudnn.so.7=> /usr/local/cuda/lib64/libcudnn.so.7 (0x00007f03a83c7000)

libmkl_rt.so=> /opt/intel/mkl/lib/intel64/libmkl_rt.so (0x00007f03a7570000)

。。。


matlab命令行窗口执行!ldd ~/caffe-master/matlab/+caffe/private/caffe_.mexa64 | greplibstdc++

libstdc++.so.6=> /usr/local/MATLAB/R2015b/sys/os/glnxa64/libstdc++.so.6(0x00007fc5588d4000)

可以发现在matlab里自动调用的是/usr/local/MATLAB/R2016b/sys/os/glnxa64/libstdc++.so.6,而不是/usr/lib/x86_64-linux-gnu/libstdc++.so.6,所以解决方法是建立软链接。执行:

sudo cp /usr/local/MATLAB/R2015b/sys/os/glnxa64/libstdc++.so.6 /usr/local/MATLAB/R2015b/sys/os/glnxa64/libstdc++.so.6.bak

sudo rm -rf /usr/local/MATLAB/R2015b/sys/os/glnxa64/libstdc++.so.6

sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/local/MATLAB/R2015b/sys/os/glnxa64/libstdc++.so.6


清理并重新make mattest后的结果:

Ubuntu16.04系统中CUDA9+MKL+MATLAB2015b+Anaconda3-5.1.0+OpenCV3.4.0+TensorFlow_GPU-1.6+Keras+caffe安装


使用pycaffe

添加环境变量

vim ~/.bashrc

export PYTHONPATH=/home/zili/caffe-master/python:$PYTHONPATH添加到文件中。

source ~/.bashrc使更改生效。

这样,在其他地方打开python,也可以importcaffe了。


使用matcaffe

注意添加路径

addpath(genpath('/home/zili/caffe-master/matlab'));