手机版
你好,游客 登录 注册
背景:
阅读新闻

Ubuntu 16.04下CUDA8.0+Theano+Caffe+TensorFlow环境搭建

[日期:2016-09-27] 来源:csdn.net/hjimce   作者:黄锦池-hjimce [字体: ]

目前自己撘过深度学习各种库、各种环境,已经搭建了n多台电脑,发现每台电脑配置安装方法各不相同,总会出现各不相同的错误,真是心塞。笔记本和台式机有差别,台式机之间的安装方法又各不相同,不同的系统版本环境、平台又各有差异。比如昨天搞的一台电脑,可能因为显卡比较新,然而Ubuntu 14.04、Ubuntu 15.04都比较旧,连安装系统都装不上,一开始在14.04上重装了n多次系统,还以为是自己电脑的问题。最后在Ubuntu 16.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 nouveau
sudo reboot
sudo apt-get remove --purge nvidia*
重启,然后进入终端:

sudo service lightdm stop
chmod +x cuda*.run
sudo ./cuda*.run2、安装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-lock3、ubuntu的gcc编译器是5.4.0,然而cuda8.0不支持5.0以上的编译器,因此需要降级,把编译器版本降到4.9:

sudo apt-get install g++-4.9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 10
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++等待安装完成

4、配置环境变量:

sudo vim /etc/profile在文件末尾添加:

PATH=/usr/local/cuda/bin:$PATH
export PATH
保存退出。输入命令:

source /etc/profile
使其生效。

输入命令:

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

添加内容:

/usr/local/cuda/lib645、验证测试

测试cuda是否安装成功:

cd /usr/local/cuda/samples
编译例子:

sudo make all -j8运行编译可执行结果文件:

./deviceQuery

二、安装theano

1、直接输入命令:

sudo pip install theano
2、配置参数文件:.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、运行测试例子:

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

三、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-gnu
sudo ln -s libhdf5_serial.so.10.1.0 libhdf5.so
sudo 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/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib改为:

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/serial4、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 libtool
然后在次运行:

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

./configure
make -j8
make check
make 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/profileD、配置动态链接库

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

sudo vim /etc/ld.so.conf添加:

/usr/local/protobuf/libE、更新配置

sudo su
ldconfig6、caffe编译:

make all -j8
make pycaffe
OK,万事大吉,打完收工。

四、tensorflow

以前的安装方法:

sudo apt-get install python-pip python-dev
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
sudo 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 update
sudo apt-get install default-jre
sudo apt-get install default-jdk2、安装编译工具Bazel

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.listcurl https://storage.googleapis.com/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.tgzsudo 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.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*安装完毕后,就接着前面的工作tensorflow的安装

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

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

https://www.tensorflow.org/versions/r0.9/get_started/os_setup.html

参考文献:

http://textminingonline.com/dive-into-tensorflow-part-iii-gtx-1080-ubuntu16-04-cuda8-0-cudnn5-0-tensorflow

Ubuntu 16.04下Matlab2014a+Anaconda2+OpenCV3.1+Caffe安装 http://www.linuxidc.com/Linux/2016-07/132860.htm

Ubuntu 16.04系统下CUDA7.5配置Caffe教程 http://www.linuxidc.com/Linux/2016-07/132859.htm

Caffe在Ubuntu 14.04 64bit 下的安装 http://www.linuxidc.com/Linux/2015-07/120449.htm

深度学习框架Caffe在Ubuntu下编译安装  http://www.linuxidc.com/Linux/2016-07/133225.htm

Caffe + Ubuntu 14.04 64bit + CUDA 6.5 配置说明  http://www.linuxidc.com/Linux/2015-04/116444.htm

Ubuntu 16.04上安装Caffe http://www.linuxidc.com/Linux/2016-08/134585.htm

Caffe配置简明教程 ( Ubuntu 14.04 / CUDA 7.5 / cuDNN 5.1 / OpenCV 3.1 )  http://www.linuxidc.com/Linux/2016-09/135016.htm

更多Ubuntu相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-09/135528.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款