您的位置:首页 > 编程语言 > MATLAB

Ubuntu 16.04 + CUDA 8.0 + Matlab 2016a + OpenCV 3.1 + Caffe

2016-08-17 20:28 435 查看
由于作死的强迫症,基本上各个组件均为时下最新版本。在经历了漫长的摸索过程后,总算是跑通了所有的测试。以下所有内容都是在网上搜集而来,将之汇总以供有需要的人使用。

1. 安装Ubuntu 16.04、NVIDIA驱动和CUDA 8.0

这部分内容参考的是[1][2],文章我是从微博上看到的,也在微博上请教了作者一些问题,作者也耐心的做了回答,对此表示感谢。另外作者是做NLP方向的,使用到了TensorFlow,也可留作参考。

另外,安装CUDA 8.0时需要将Ubuntu 16.04自带的gcc5.x降级到gcc4.x,这里我参考了[3],但请注意不要将gcc5.x版本删掉,因为后面在编译caffe过程中需要5.x版本的gcc。最好同时保留,使用 sudo update-alternatives –config gcc 进行切换。这里我也掉到坑里过,删掉了gcc5.x后也不知道如何重新安装,网上查资料说重新编译,看上去也挺麻烦的。

2. Matlab 2016a

虽然很少有人用Matlab搞Deep Learning,但搞图像的总离不开它,我选择了较新的2016a版本。资源我是从百度云上下载的,我在这里就不传播盗版了,请各位自行在网上搜。当然像我这种记不住命令的,也是参考了别人的文章,链接在此[4]。最后进入MATLAB路径中的/bin中执行sudo ./activate_matlab.sh命令进行离线激活以及将破解文件libmwservices.so拷贝到/usr/local/MATLAB/R2016a/bin/glnxa64中去。

3. OpenCV 3.1

编译OpenCV 3.1 感觉是这里面最耗时的了。这里我参考[5],这是Caffe官网上教程,但这份教程对我的机器配置来说并不完整。由于我的是CUDA 8.0,所以安装的时候也会遇到较为普遍的问题,比如自行下载ippicv和修改cudalegacy(CUDART_VERSION >= 8000)等。

4. Caffe

我是安装官网上的教程来进行安装的,链接在此[6],也参考了这篇文章[7]。当然,如果遇到了protobuf的问题,解决的办法当然是自行编译protobuf。我在网上搜到了一篇很有用的教程[8],作者不仅详细的介绍了安装方法,也提供了autogen好后的安装包。但需要注意的是,在configure的时候,安装路径需要设为/usr,虽然任意设置安装路径也可以,但后面会带来一些小小的麻烦。Google官方也是推荐安装在/usr里,参考链接在这[9]

此外,如果用到matcaffe的话,还要修改一下链接:

cd /usr/local/MATLAB/R2014a/sys/os/glnxa64/
sudo mv libstdc++.so.6 libstdc++.so.6_back


这个过程中,配置python所需要的时间比较长,网上有推荐其他方法或者用pip的方法。当然,配置完Makefile.config后,基本上就没什么事情。为了方便大家,贴出我的Makefile.config文件,供大家参考。剩下的事情就是一路狂奔的编译了,记得在make后面加上-j来加速编译。这里[10]也是有一份可以参考的测试文章的。

## 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 := 1
USE_LEVELDB := 1
USE_LMDB := 1

# 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 := 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/R2016a
# 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)/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 := $(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 lay
9c85
ers 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 /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 ?= @


5. 结论

需要强调的是以上所有的内容都不是我的原创,我只是作为一个搬运工将之收集起来,方便大家使用。最后,安装完后千万不要更新系统或者显卡驱动,以免造成不必要的麻烦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ubuntu caffe