600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 深度学习(四十一)cuda8.0+ubuntu16.04+theano caffe tensorflow环境搭建

深度学习(四十一)cuda8.0+ubuntu16.04+theano caffe tensorflow环境搭建

时间:2019-03-17 04:00:34

相关推荐

深度学习(四十一)cuda8.0+ubuntu16.04+theano caffe tensorflow环境搭建

cuda8.0+ubuntu16.04+theano、caffe、tensorflow环境搭建

目前自己撘过深度学习各种库、各种环境,已经搭建了n多台电脑,发现每台电脑配置安装方法各不相同,总会出现各不相同的错误,真是心塞。笔记本和台式机有差别,台式机之间的安装方法又各不相同,不同的系统版本环境、平台又各有差异。比如昨天搞的一台电脑,可能因为显卡比较新,然而ubuntu14.04、ubuntu15.04都比较旧,连安装系统都装不上,一开始在14.04上重装了n多次系统,还以为是自己电脑的问题。最后在ubuntu16.04竟然非常顺利完成了安装;然而16.04的版本,只有cuda8.0才支持,在这台破电脑上,又折腾了我快一天的时间。

显卡:GTX960

环境:ubuntu16.04、cuda8.0

下面是我的安装之路,总的来说theano、keras、tensorflow都比较容易安装;最难安装的是caffe,因为caffe调用的第三方库比较杂、比较多。

一、安装cuda8.0

1、输入命令:

sudo vim /etc/modprobe.d/blacklist.conf

在文件最后面,添加:

blacklist nouveausudo rebootsudo apt-get remove --purge nvidia*

重启,然后进入终端:

sudo service lightdm stopchmod +x cuda*.runsudo ./cuda*.run

2、安装cuda的过程中,一直跳出错误:

If you're sure that X is not running, but are getting this error, please delete any X lock files in /tmp.

那么我们可以直接删除X-lock文件,具体命令为:

sudo rm /tmp/.X0-lock

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

sudo apt-get install g++-4.9sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 10sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 10sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30sudo update-alternatives --set cc /usr/bin/gccsudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30sudo update-alternatives --set c++ /usr/bin/g++

等待安装完成

4、配置环境变量:

sudo vim /etc/profile

在文件末尾添加:

PATH=/usr/local/cuda/bin:$PATHexport PATH

保存退出。输入命令:

source /etc/profile

使其生效。

输入命令:

sudo /etc/ld.so.conf.d/cuda.conf

添加内容:

/usr/local/cuda/lib64

5、验证测试

测试cuda是否安装成功:

cd /usr/local/cuda/samples

编译例子:

sudo make all -j8

运行编译可执行结果文件:

./deviceQuery

二、安装theano

1、直接输入命令:

sudo pip install theano

2、配置参数文件:.theanorc

[global]floatX=float32device=gpubase_compiledir=~/external/.theano/allow_gc=Falsewarn_float64=warn[mode]=FAST_RUN[nvcc]fastmath=True[cuda]root=/usr/local/cuda

3、运行测试例子:

from theano import function, config, shared, sandboximport theano.tensor as Timport numpyimport timevlen = 10 * 30 * 768 # 10 x #cores x # threads per coreiters = 1000rng = 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.pyUsing 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 secondsResult is [ 1.23178029 1.61879349 1.52278066 ..., 2.20771813 2.299677611.62323296]Used the gpu

三、caffe环境搭建

1、切换编译器:

sudo update-alternatives --config g++

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

sudo update-alternatives --config gcc

根据编号选择gcc编译器5.0以上的版本。

2、hd5相关问题:遇到hd5等相关找不到的文件错误。

输入命令:

cd /usr/lib/x86_64-linux-gnusudo ln -s libhdf5_serial.so.10.1.0 libhdf5.sosudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so

3、caffe编译

从github上下载caffe,解压打开makefile.config对其进行修改,makefile.config修改内容内容如下:

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/includeLIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

改为:

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serialLIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial

4、cuda8.0编译器问题

打开/usr/local/cuda/include/host_config.h

注释掉:

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

结果如下:

#if __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ > 3)//#error -- unsupported GNU version! gcc versions later than 5.3 are not supported!#endif /* __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ > 1) */

5、遇到prototuf等编译问题:

build_release/lib/libcaffe.so: undefined reference to 'google::protobuf::io::CodedOutputStream::WriteVarint64ToArray(unsigned long long, unsigned char*)'

主要是因为我们直接采用命令apt-get install 安装prototuf是比较老旧的版本,而ubuntu16.04比较新,所以我们需要卸载prototuf,然后自己在自己的电脑上编译安装。

(1)于是先卸载原有版本:

sudo apt-get autoremove libprotobuf-dev protobuf-compiler

(2)从github下载protobuf

(3)打开protobuf文件目录进行编译安装,具体过程如下

编译过程过下:

A、输入命令:

sh auto*.sh

生产configure文件。这步可能遇到的错误:

configure.ac:64: error: possibly undefined macro: AC_PROG_LIBTOOL

那么输入命令:

sudo apt-get install autoconf autogen

sudo apt-get install libtool

然后在次运行:

sh auto*.sh

B、按照顺序,依次输入如下命令:

./configuremake -j8make checkmake install

完成安装。

C、protobuf配置环境变量.

打开profile文件:

sudo vim /etc/profile

添加:

export PATH=$PATH:/usr/local/protobuf/bin/export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/<

保存退出,然后输入命令:

source /etc/profile

D、配置动态链接库

打开配置文件ld.so.conf:

sudo vim /etc/ld.so.conf

添加:

/usr/local/protobuf/lib

E、更新配置

sudo suldconfig

6、caffe编译:

make all -j8make pycaffe

OK,万事大吉,打完收工。

相关参考文献:

/realxie/article/details/7456013

/BVLC/caffe/wiki/GeForce-GTX-1080,---CUDA-8.0,---Ubuntu-16.04,---Caffe

/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide

http://mooon./1246491/909928

四、tensorflow

以前的安装方法:

sudo apt-get install python-pip python-devexport TF_BINARY_URL=/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whlsudo pip install --upgrade $TF_BINARY_URL

出现错误:

libcudart.so.7.5: cannot open shared object file: No such file or directory

主要原因是上面的tensoftlow*.whl是cuda7.5编译好的,导致我们不能直接用。因此我们接着要自己编译才行。

1、先装jdk

sudo apt-get updatesudo apt-get install default-jresudo apt-get install default-jdk

2、安装编译工具Bazel

echo "deb [arch=amd64] /bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list

curl /bazel-apt/doc/apt-key.pub.gpg | sudo apt-key add -

sudo apt-get update && sudo apt-get install bazel

3、下载tensorflow并编译

./configure

遇到错误:

Can't find swig. Ensure swig is in $PATH or set $SWIG_PATH.

安装swig:

sudo apt-get install swig

4、在tensorflow安装的时候,没有找到可以忽略使用cudnn的选项,一直提示如下错误:

Please specify the location where cuDNN library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: Invalid path to cuDNN toolkit. Neither of the following two files can be found:/usr/local/cuda-8.0/lib64/libcudnn.so/usr/local/cuda-8.0/libcudnn.so

所以没办法,只能把cudnn也给安装了。首先到官网下载cuda8.0对应的cudnn:

cudnn-8.0-linux-x64-v5.0-ga.tgz

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

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.hsudo chmod a+r /usr/local/cuda/lib64/libcudnn*

安装完毕后,就接着前面的工作tensorflow的安装

5、输入.configure,然后一路回车、或者选择yes。

6、这是心酸,原来tensorflow官网给了从源码安装的教程:install from sources

/versions/r0.9/get_started/os_setup.html

参考文献:

/dive-into-tensorflow-part-iii-gtx-1080-ubuntu16-04-cuda8-0-cudnn5-0-tensorflow

**********************作者微博:黄锦池-hjimce博客:/hjimce 原创文章,转载请保留本行信息********************

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。