1. 程式人生 > >深度學習(四十一)cuda8.0+ubuntu16.04+theano、caffe、tensorflow環境搭建

深度學習(四十一)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 nouveau
sudo reboot
sudo apt-get remove --purge nvidia*

重啟,然後進入終端:
sudo service lightdm stop
chmod +x cuda*.run
sudo ./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.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/lib64

5、驗證測試

測試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/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、按照順序,依次輸入如下命令:

./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/profile

D、配置動態連結庫

開啟配置檔案ld.so.conf:

sudo vim /etc/ld.so.conf
新增:
/usr/local/protobuf/lib
E、更新配置
sudo su
ldconfig
6、caffe編譯:
make all -j8
make pycaffe

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

相關參考文獻:
http://blog.csdn.net/realxie/article/details/7456013

https://github.com/BVLC/caffe/wiki/GeForce-GTX-1080,---CUDA-8.0,---Ubuntu-16.04,---Caffe

https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide

http://mooon.blog.51cto.com/1246491/909928

四、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-jdk
2、安裝編譯工具Bazel
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl 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.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.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

  原創文章,轉載請保留本行資訊********************