歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Ubuntu 16.04下CUDA8.0+Theano+Caffe+TensorFlow環境搭建

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

日期:2017/2/28 13:48:30   编辑:Linux教程

目前自己撘過深度學習各種庫、各種環境,已經搭建了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

Copyright © Linux教程網 All Rights Reserved