原文链接:http://blog.csdn.net/hit2015spring/article/details/53510909?locationNum=2&fps=1
安装英伟达显卡驱动
首先去官网上查看适合你GPU的驱动
(http://www.nvidia.com/Download/index.aspx?lang=en-us)
- sudoadd-apt-repositoryppa:graphics-drivers/ppa
- sudoapt-getupdate
- sudoapt-getinstallnvidia-375(375是你查到的版本号)
- sudoapt-getinstallmesa-common-dev
- sudoapt-getinstallfreeglut3-dev
执行完上述后,重启(reboot)。
重启后输入
- nvidia-smi
如果出现了你的GPU列表,则说明驱动安装成功了。另外也可以通过,或者输入
- nvidia-settings
出现
https://developer.nvidia.com/cuda-downloads
在cuda所在目录打开terminal依次输入以下指令:
- sudodpkg-icuda-repo-ubuntu1604-8-0-rc_8.0.27-1_amd64.deb
- sudoapt-getupdate
- sudoapt-getinstallcuda
ubuntu的gcc编译器是5.4.0,然而cuda8.0不支持5.0以上的编译器,因此需要降级,把编译器版本降到4.9:
在terminal中执行:
- sudoapt-getinstallgcc-4.9gcc-5g++-4.9g++-5
- sudoupdate-alternatives--install/usr/bin/gccgcc/usr/bin/gcc-4.920
- sudoupdate-alternatives--install/usr/bin/gccgcc/usr/bin/gcc-510
- sudoupdate-alternatives--install/usr/bin/g++g++/usr/bin/g++-4.920
- sudoupdate-alternatives--install/usr/bin/g++g++/usr/bin/g++-510
- sudoupdate-alternatives--install/usr/bin/cccc/usr/bin/gcc30
- sudoupdate-alternatives--setcc/usr/bin/gcc
- sudoupdate-alternatives--install/usr/bin/c++c++/usr/bin/g++30
- sudoupdate-alternatives--setc++/usr/bin/g++
配置cuda8.0之后主要加上的一个环境变量声明,在文件~/.bashrc之后加上
- gedit~/.bashrc
- exportPATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
- exportLD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
然后设置环境变量和动态链接库,在命令行输入
- sudogedit/etc/profile
- exportPATH=/usr/local/cuda/bin:$PATH
- sudogedit/etc/ld.so.conf.d/cuda.conf
- /usr/local/cuda/lib64
保存退出执行命令行:
- sudoldconfig
使链接立即生效。
2、测试cuda的Samples
命令行输入(注意cuda-8.0是要相对应自己的cuda版本)
- cd/usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
- make
- sudo./deviceQuery
返回GPU的信息则表示配置成功
3、使用cudnn
上官网下载对应的cudnn
https://developer.nvidia.com/cudnn
下载完cudnn后,命令行输入文件所在的文件夹 (ubuntu为本机用户名)
- cdhome/ubuntu/Downloads/
- tarzxvfcudnn-8.0-linux-x64-v5.1.tgz#解压文件
cd进入cudnn5.1解压之后的include目录,在命令行进行如下操作:
- sudocpcudnn.h/usr/local/cuda/include/#复制头文件
再cd进入lib64目录下的动态文件进行复制和链接:(5.1.5为对应版本具体可修改)
4、安装opencv3.1.0
从官网上下载opencv3.1.0
http://OpenCV.org/downloads.html
并将其解压到你要安装的位置,(下载的位置还是在home/ubuntu、Downloads文件夹下)
首先安装Ubuntu系统需要的依赖项,虽然我也不知道有些依赖项是干啥的,但是只管装就行,也不会占据很多空间的。
- 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需要的一些依赖项,一些文件编码解码之类的东东。
- sudoapt-getinstallbuild-essentialcmakegit
- 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文件夹下(解压的那个文件夹),然后
-- 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编译就可以了
- make-j8
上面是将opencv编译成功,但是并没有安装到我们的系统中,有很多的设置都没有写入到系统中,因此还要进行install。
- sudoapt-getinstallcheckinstall
- sudocheckinstall
- sudo/bin/bash-c'echo"/usr/local/lib">/etc/ld.so.conf.d/opencv.conf'
- sudoldconfig
这里感谢这位朋友的提醒
然后按照提示安装就可以了。 使用checkinstall的目的是为了更好的管理我安装的opencv,因为opencv的安装很麻烦,卸载更麻烦,其安装的时候修改了一大堆的文件,当我想使用别的版本的opencv时,将当前版本的opencv卸载就是一件头疼的事情,因此需要使用checkinstall来管理我的安装。执行了checkinstall后,会在build文件下生成一个以backup开头的.tgz的备份文件和一个以build开头的.deb安装文件,当你想卸载当前的opencv时,直接执行dpkg -r build即可。
5、配置caffe环境
切换编译器
选择g++ 5.0以上的对应编号
- sudoupdate-alternatives--configg++
- sudoupdate-alternatives--configgcc
安装依赖库
- sudoadd-apt-repositoryuniverse
- sudoapt-getupdate-y
- sudoapt-getinstallcmake-y
# General Dependencies
- sudoapt-getinstalllibprotobuf-devlibleveldb-devlibsnappy-dev\
- libhdf5-serial-devprotobuf-compiler-y
- sudoapt-getinstall--no-install-recommendslibboost-all-dev-y
# BLAS
- sudoapt-getinstalllibatlas-base-dev-y
# Remaining Dependencies
- sudoapt-getinstalllibgflags-devlibgoogle-glog-devliblmdb-dev-y
- sudoapt-getinstallpython-devpython-numpy–y
- sudoapt-getinstall-ypython-pip
- sudoapt-getinstall-ypython-dev
- sudoapt-getinstall-ypython-numpypython-scipy
编译 Caffe,cd到要安装caffe的位置
- gitclonehttps://github.com/BVLC/caffe.git
- cdcaffe
- cpMakefile.config.exampleMakefile.config
修改Makefile.config:
- 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文件
- 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行注释掉:
- 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!
之后编辑即可
- make-j4all
- make-j4runtest
为了更好地使用pycaffe ,建议安装:
- sudoapt-getinstallpython-numpypython-setuptoolspython-pipcythonpython-skimagepython-protobuf
- makepycaffe
- cdpython
- python
- importcaffe#测试安装成功
到这里Caffe开发环境就配置好了!
- cd~/caffe
- ./build/tools/caffetime--gpu0--model./models/bvlc_AlexNet/deploy.prototxt
6、theano安装
1、直接输入命令:
- sudopipinstalltheano
2、配置参数文件:.theanorc
- 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、运行测试例子:
- 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里面
- cd/home/username/Downloads
- sudobashAnaconda2-4.2.0-Linux-x86_64.sh
配置环境变量
- gedit/etc/profile
末尾添上,我是一路yes下来,所以安在了root下,你可以自己选路径,这时候的环境变量要改
export PATH=/root/anaconda2/bin:$PATH
重启
打开终端
- python
安装成功
2、创建conda环境 名字叫tensorflow
- condacreate-ntensorflowpython=2.7
- sourceactivatetensorflow#使能该环境
#下面这句话只能下载给cpu用的tensorflow
- condainstall-cconda-forgetensorflow
利用pip来下载给GPU用的tensorflow
- exportTF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.11.0-cp27-none-linux_x86_64.whl
下载安装
- pipinstall--ignore-installed--upgrade$TF_BINARY_URL
安装IPython
- condainstallipython
关掉该环境
- sourcedeactivate
测试安装是否正确
- sourceactivatetensorflow
- python
输入
- <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>
- <divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">#figurethatoutforus.)</span></div>#(WeknowthatWshouldbe0.1andb0.3,butTensorFlowwill
- <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))
- b=tf.Variable(tf.zeros([1]))
- <divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">optimizer=tf.train.GradientDescentOptimizer(0.5)</span></div>#Minimizethemeansquarederrors.
- loss=tf.reduce_mean(tf.square(y-y_data))
- train=optimizer.minimize(loss)
- <divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">#Launchthegraph.</span></div>#Beforestarting,initializethevariables.Wewill'run'thisfirst.
- init=tf.initialize_all_variables()
- sess=tf.Session()
- sess.run(init)
- #Fittheline.
- forstepinrange(201):
- <divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;">#LearnsbestfitisW:[0.1],b:[0.3]</span></div>sess.run(train)
- ifstep%20==0:
- print(step,sess.run(W),sess.run(b))
- <divstyle="text-align:left;"><spanstyle="font-family:Arial,sans-serif;"></span></div>
OK
8、Caffe配置错误
问题:找不到Python.h
- gedit~/.banshrc
export PATH=/root/anaconda2/bin:$PATH
export PYTHONPATH=/path/to/caffe/python:$PATH
修改Makefile.config
在终端输入
- locatePython.h
- 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'
则在终端输入:
- sudomakeclean
修改完后再
- sudomakepycaffe
- 这里要从make–j8all那一步开始编译
编译完后,显示
然后 cd python进入该目录
- python
- 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
- cd..
- pipinstallprotobuf
- sudoapt-getinstallpython-protobuf
- condainstalllibgcc