Ubuntu16.04 +cuda8.0+cudnn+opencv+caffe+theano+tensorflow配置明细

前端之家收集整理的这篇文章主要介绍了Ubuntu16.04 +cuda8.0+cudnn+opencv+caffe+theano+tensorflow配置明细前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文链接:http://blog.csdn.net/hit2015spring/article/details/53510909?locationNum=2&fps=1

安装英伟达显卡驱动

首先去官网上查看适合你GPU的驱动

http://www.nvidia.com/Download/index.aspx?lang=en-us

  1. sudoadd-apt-repositoryppa:graphics-drivers/ppa
  2. sudoapt-getupdate
  3. sudoapt-getinstallnvidia-375375是你查到的版本号)
  4. sudoapt-getinstallmesa-common-dev
  5. sudoapt-getinstallfreeglut3-dev

执行完上述后,重启(reboot)。

重启后输入

  1. nvidia-smi

如果出现了你的GPU列表,则说明驱动安装成功了。另外也可以通过,或者输入

  1. nvidia-settings

出现

  1. 配置cuda

https://developer.nvidia.com/cuda-downloads

在cuda所在目录打开terminal依次输入以下指令:

  1. sudodpkg-icuda-repo-ubuntu1604-8-0-rc_8.0.27-1_amd64​.deb
  2. sudoapt-getupdate
  3. sudoapt-getinstallcuda​

ubuntu的gcc编译器是5.4.0,然而cuda8.0不支持5.0以上的编译器,因此需要降级,把编译器版本降到4.9:

在terminal中执行:

  1. sudoapt-getinstallgcc-4.9gcc-5g++-4.9g++-5
  2. sudoupdate-alternatives--install/usr/bin/gccgcc/usr/bin/gcc-4.920
  3. sudoupdate-alternatives--install/usr/bin/gccgcc/usr/bin/gcc-510
  4. sudoupdate-alternatives--install/usr/bin/g++g++/usr/bin/g++-4.920
  5. sudoupdate-alternatives--install/usr/bin/g++g++/usr/bin/g++-510
  6. sudoupdate-alternatives--install/usr/bin/cccc/usr/bin/gcc30
  7. sudoupdate-alternatives--setcc/usr/bin/gcc
  8. sudoupdate-alternatives--install/usr/bin/c++c++/usr/bin/g++30
  9. sudoupdate-alternatives--setc++/usr/bin/g++

配置cuda8.0之后主要加上的一个环境变量声明,在文件~/.bashrc之后加上

  1. gedit~/.bashrc
  2. exportPATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
  3. exportLD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}


然后设置环境变量和动态链接库,在命令行输入

  1. sudogedit/etc/profile

在打开的文件里面加上(注意等号两边不能有空格)

  1. exportPATH=/usr/local/cuda/bin:$PATH

保存之后,创建链接文件

  1. sudogedit/etc/ld.so.conf.d/cuda.conf


在打开的文件添加如下语句:

  1. /usr/local/cuda/lib64


保存退出执行命令行:

  1. sudoldconfig


使链接立即生效。

2、测试cuda的Samples

命令行输入(注意cuda-8.0是要相对应自己的cuda版本)

  1. cd/usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
  2. make
  3. sudo./deviceQuery


返回GPU的信息则表示配置成功

3、使用cudnn

上官网下载对应的cudnn

https://developer.nvidia.com/cudnn

下载完cudnn后,命令行输入文件所在的文件夹 (ubuntu为本机用户名)

  1. cdhome/ubuntu/Downloads/
  2. tarzxvfcudnn-8.0-linux-x64-v5.1.tgz#解压文件

cd进入cudnn5.1解压之后的include目录,在命令行进行如下操作:

  1. sudocpcudnn.h/usr/local/cuda/include/#复制头文件

再cd进入lib64目录下的动态文件进行复制和链接:(5.1.5为对应版本具体可修改

  1. sudocplib*/usr/local/cuda/lib64/#复制动态链接
  2. cd/usr/local/cuda/lib64/
  3. sudorm-rflibcudnn.solibcudnn.so.5#删除原有动态文件
  4. sudoln-slibcudnn.so.5.1.5libcudnn.so.5#生成软衔接
  5. sudoln-slibcudnn.so.5libcudnn.so#生成链接

4、安装opencv3.1.0

从官网上下载opencv3.1.0

http://OpenCV.org/downloads.html

并将其解压到你要安装的位置,(下载的位置还是在home/ubuntu、Downloads文件夹下)

首先安装Ubuntu系统需要的依赖项,虽然我也不知道有些依赖项是干啥的,但是只管装就行,也不会占据很多空间的。

  1. sudoapt-getinstall--assume-yeslibopencv-devbuild-essentialcmakegitlibgtk2.0-devpkg-configpython-devpython-numpylibdc1394-22libdc1394-22-devlibjpeg-devlibpng12-devlibtiff5-devlibjasper-devlibavcodec-devlibavformat-devlibswscale-devlibxine2-devlibgstreamer0.10-devlibgstreamer-plugins-base0.10-devlibv4l-devlibtbb-devlibqt4-devlibfaac-devlibmp3lame-devlibopencore-amrnb-devlibopencore-amrwb-devlibtheora-devlibvorbis-devlibxvidcore-devx264v4l-utilsunzip


然后安装opencv需要的一些依赖项,一些文件编码解码之类的东东。

  1. sudoapt-getinstallbuild-essentialcmakegit
  2. sudoapt-getinstallffmpeglibopencv-devlibgtk-3-devpython-numpypython3-numpylibdc1394-22libdc1394-22-devlibjpeg-devlibpng12-devlibtiff5-devlibjasper-devlibavcodec-devlibavformat-devlibswscale-devlibxine2-devlibgstreamer1.0-devlibgstreamer-plugins-base1.0-devlibv4l-devlibtbb-devqtbase5-devlibfaac-devlibmp3lame-devlibopencore-amrnb-devlibopencore-amrwb-devlibtheora-devlibvorbis-devlibxvidcore-devx264v4l-utilsunzip


在终端中cd到opencv文件夹下(解压的那个文件夹),然后

  1. mkdirbuild#新建一个build文件夹,编译的工程都在这个文件夹里
  2. cdbuild/
  3. cmake-DCMAKE_BUILD_TYPE=RELEASE-DCMAKE_INSTALL_PREFIX=/usr/local-DWITH_TBB=ON-DWITH_V4L=ON-DWITH_QT=ON-DWITH_OPENGL=ON-DCUDA_NVCC_FLAGS="-D_FORCE_INLINES"..(后面两点不要忘记)


cmake成功后,会出现如下结果,提示配置和生成成功:

-- Configuring done

-- Generating done

-- Build files have been written to: /home/ise/software/opencv-3.1.0/build

由于CUDA 8.0不支持OpenCV的 GraphCut 算法,可能出现以下错误

/home/usrname/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:120:54: error: 'NppiGraphcutState' has not been declared

typedef NppStatus (*init_func_t)(NppiSize oSize,NppiGraphcutState** ppStat

^

/home/usrname/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:135:18: error: 'NppiGraphcutState' does not name a type

operator NppiGraphcutState*()

^

/home/usrname/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:141:9: error: 'NppiGraphcutState' does not name a type

NppiGraphcutState* pState;

.......

进入opencv-3.1.0/modules/cudalegacy/src/目录,修改graphcuts.cpp文件,将:

#include "precomp.hpp"

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)

改为

#include "precomp.hpp"

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)

然后make编译就可以了

  1. make-j8

上面是将opencv编译成功,但是并没有安装到我们的系统中,有很多的设置都没有写入到系统中,因此还要进行install。


  1. sudoapt-getinstallcheckinstall
  2. sudocheckinstall
  1. sudo/bin/bash-c'echo"/usr/local/lib">/etc/ld.so.conf.d/opencv.conf'
  2. sudoldconfig

这里感谢这位朋友的提醒

然后按照提示安装就可以了。 使用checkinstall的目的是为了更好的管理我安装的opencv,因为opencv的安装很麻烦,卸载更麻烦,其安装的时候修改了一大堆的文件,当我想使用别的版本的opencv时,将当前版本的opencv卸载就是一件头疼的事情,因此需要使用checkinstall来管理我的安装。执行了checkinstall后,会在build文件生成一个以backup开头的.tgz的备份文件和一个以build开头的.deb安装文件,当你想卸载当前的opencv时,直接执行dpkg -r build即可。

5、配置caffe环境

切换编译器

选择g++ 5.0以上的对应编号

  1. sudoupdate-alternatives--configg++


  1. sudoupdate-alternatives--configgcc


安装依赖库

  1. sudoadd-apt-repositoryuniverse
  2. sudoapt-getupdate-y
  3. sudoapt-getinstallcmake-y


# General Dependencies

  1. sudoapt-getinstalllibprotobuf-devlibleveldb-devlibsnappy-dev\
  2. libhdf5-serial-devprotobuf-compiler-y
  3. sudoapt-getinstall--no-install-recommendslibboost-all-dev-y


# BLAS

  1. sudoapt-getinstalllibatlas-base-dev-y


# Remaining Dependencies

  1. sudoapt-getinstalllibgflags-devlibgoogle-glog-devliblmdb-dev-y
  2. sudoapt-getinstallpython-devpython-numpy–y
  3. sudoapt-getinstall-ypython-pip
  4. sudoapt-getinstall-ypython-dev
  5. sudoapt-getinstall-ypython-numpypython-scipy


编译 Caffe,cd到要安装caffe的位置

  1. gitclonehttps://github.com/BVLC/caffe.git
  2. cdcaffe
  3. cpMakefile.config.exampleMakefile.config


修改Makefile.config:

  1. geditMakefile.config


对打开的文件编辑

# cuDNN acceleration switch (uncomment to build with cuDNN).

USE_CUDNN := 1

# Uncomment if you're using OpenCV 3 如果用的是opencv3版本

OPENCV_VERSION := 3

# Uncomment to support layers written in Python (will link against python libs)

WITH_PYTHON_LAYER := 1

在问件里面添加文本由于hdf5库目录更改,所以需要单独添加

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/

打开makefile文件

  1. geditMakefile


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

替换

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

编辑/usr/local/cuda/include/host_config.h,将其中的第115行注释掉:

  1. sudogedit/usr/local/cuda/include/host_config.h


#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!

改为

//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!

之后编辑即可

  1. make-j4all
  2. make-j4runtest


为了更好地使用pycaffe ,建议安装:

  1. sudoapt-getinstallpython-numpypython-setuptoolspython-pipcythonpython-skimagepython-protobuf
  2. makepycaffe
  3. cdpython
  4. python
  5. importcaffe#测试安装成功


到这里Caffe开发环境就配置好了!

可以测试一下,输出AlexNet的时间测试结果:

  1. cd~/caffe
  2. ./build/tools/caffetime--gpu0--model./models/bvlc_AlexNet/deploy.prototxt


6、theano安装

1、直接输入命令:

  1. sudopipinstalltheano


2、配置参数文件:.theanorc

  1. sudogedit~/.theanorc


对打开的文件进行编辑

[global]

floatX=float32

device=gpu

base_compiledir=~/external/.theano/

allow_gc=False

warn_float64=warn

[mode]=FAST_RUN

[nvcc]

fastmath=True

[cuda]

root=/usr/local/cuda

3、运行测试例子:

  1. sudoVimtest.py


from theano import function,config,shared,sandBox

import theano.tensor as T

import numpy

import time

vlen = 10 * 30 * 768 # 10 x #cores x # threads per core

iters = 1000

rng = numpy.random.RandomState(22)

x = shared(numpy.asarray(rng.rand(vlen),config.floatX))

f = function([],T.exp(x))

print(f.maker.fgraph.toposort())

t0 = time.time()

for i in range(iters):

r = f()

t1 = time.time()

print("Looping %d times took %f seconds" % (iters,t1 - t0))

print("Result is %s" % (r,))

if numpy.any([isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()]):

print('Used the cpu')

else:

print('Used the gpu')

可以看到结果:

/usr/bin/python2.7 /home/hjimce/PycharmProjects/untitled/.idea/temp.py

Using gpu device 0: GeForce GTX 960 (CNMeM is disabled,cuDNN not available)

[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32,vector)>),HostFromGpu(GpuElemwise{exp,no_inplace}.0)]

Looping 1000 times took 0.302778 seconds

Result is [ 1.23178029 1.61879349 1.52278066 ...,2.20771813 2.29967761

1.62323296]

Used the gpu

说明安装成功

7、tensorflow 安装

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md

先安装anaconda

https://repo.continuum.io/archive/Anaconda2-4.2.0-Windows-x86_64.exe

上面的地址下载 该包默认在downloads里面

  1. cd/home/username/Downloads
  2. sudobashAnaconda2-4.2.0-Linux-x86_64.sh


配置环境变量

  1. gedit/etc/profile


末尾添上,我是一路yes下来,所以安在了root下,你可以自己选路径,这时候的环境变量要改

export PATH=/root/anaconda2/bin:$PATH

重启

打开终端

  1. python


安装成功

2、创建conda环境 名字叫tensorflow

  1. condacreate-ntensorflowpython=2.7
  2. sourceactivatetensorflow#使能该环境


#下面这句话只能下载给cpu用的tensorflow

  1. condainstall-cconda-forgetensorflow


利用pip来下载给GPU用的tensorflow

  1. exportTF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.11.0-cp27-none-linux_x86_64.whl


下载安装

  1. pipinstall--ignore-installed--upgrade$TF_BINARY_URL


安装IPython

  1. condainstallipython


关掉该环境

  1. sourcedeactivate


测试安装是否正确

  1. sourceactivatetensorflow
  2. python


输入

  1. <divstyle="text-align:left;"><spanstyle="font-family:Arial,Helvetica,sans-serif;">importtensorflowastf</span></div><divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">importnumpyasnp</span></div><divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;"></span></div><divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">#Create100phonyx,ydatapointsinNumPy,y=x*0.1+0.3</span></div><divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">x_data=np.random.rand(100).astype(np.float32)</span></div><divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">#TrytofindvaluesforWandbthatcomputey_data=W*x_data+b</span></div><divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">y_data=x_data*0.1+0.3</span></div>
  2. <divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">#figurethatoutforus.)</span></div>#(WeknowthatWshouldbe0.1andb0.3,butTensorFlowwill
  3. <divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">y=W*x_data+b</span></div>W=tf.Variable(tf.random_uniform([1],-1.0,1.0))
  4. b=tf.Variable(tf.zeros([1]))
  5. <divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">optimizer=tf.train.GradientDescentOptimizer(0.5)</span></div>#Minimizethemeansquarederrors.
  6. loss=tf.reduce_mean(tf.square(y-y_data))
  7. train=optimizer.minimize(loss)
  8. <divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">#Launchthegraph.</span></div>#Beforestarting,initializethevariables.Wewill'run'thisfirst.
  9. init=tf.initialize_all_variables()
  10. sess=tf.Session()
  11. sess.run(init)
  12. #Fittheline.
  13. forstepinrange(201):
  14. <divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">#LearnsbestfitisW:[0.1],b:[0.3]</span></div>sess.run(train)
  15. ifstep%20==0:
  16. print(step,sess.run(W),sess.run(b))
  17. <divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;"></span></div>

OK

8、Caffe配置错误

问题:找不到Python.h

解决:给anaconda添加环境变量

  1. gedit~/.banshrc


添加

export PATH=/root/anaconda2/bin:$PATH

export PYTHONPATH=/path/to/caffe/python:$PATH

修改Makefile.config

在终端输入

  1. locatePython.h


  1. geditMakefile.config


在INCLUDE_DIRS 和LIBRARY_DIRS后面添上

/root/anaconda2/include/python2.7

启用

ANACONDA_HOME := $(HOME)/anaconda2

PYTHON_ INCLUDE =$(ANACONDA_HOME)/include\

,把前面的#去掉,那三行都去掉#,并在注释上面,

注释这两句PYTHON_INCLUDE := /usr/include/python2.7\

/usr/lib/python2.7…………..

如果编译的时候发现有错,回来改完之后又得重新编译一遍pycaffe,于是出现如下错误

make: Nothing to be done for 'pycaffe'

则在终端输入

  1. sudomakeclean


修改完后再

  1. sudomakepycaffe
  2. 这里要从make–j8all那一步开始编译


编译完后,显示

然后 cd python进入该目录

  1. python
  2. importcaffe


若此时提示错误

Traceback (most recent call last)

File

ImportError: /home/../anaconda2/lib/python2.7/site-packages/zmq/backend/cython/../../../../.././libstdc++.so.6: versionGLIBCXX_3.4.21' not found

解决

https://github.com/BVLC/caffe/issues/4953

https://gitter.im/BVLC/caffe/archives/2015/08/20

  1. cd..
  2. pipinstallprotobuf
  3. sudoapt-getinstallpython-protobuf
  4. condainstalllibgcc
原文链接:https://www.f2er.com/ubuntu/352252.html

猜你在找的Ubuntu相关文章