1. 程式人生 > >Ubuntu16.04下Tensorflow-gpu安裝

Ubuntu16.04下Tensorflow-gpu安裝

踩了無數坑之後(基本上網上出現的所有問題我都不止一次地遇到過:迴圈登入,解析度變差,黑屏等等),終於把帶GPU加速的tensorflow所有相關軟體都安裝好了。因為過程太不容易了,光linux系統我就重灌了兩次,每一次重灌都是相當地絕望了,所以我希望把安裝方法好好總結一下,希望能對別人有所幫助。

 

在我的電腦上各種軟體的版本:

linux: Ubuntu 16.04;      

uname -m && cat /etc/*release

anaconda:  conda 4.5.11

conda --version

python:    python3.7

顯示卡:一般有兩塊顯示卡,一是:Intel整合顯示卡,二是:Nvidia顯示卡 (參考:https://blog.csdn.net/yan_chou/article/details/72847943)

我的nvidia顯示卡的型號:GeForce GTX 950M

顯示卡驅動:410.48

cuda: cuda10.0

cudnn:  cudnn 7.3.0 (針對cuda10.0)

tensorflow:  1.11.0

關於cuda的介紹:

CUDA ® is
a parallel computing platform and programming model invented by NVIDIA. It enables dramatic increases in computing performance by harnessing the power of the graphics processing unit (GPU).
CUDA was developed with several design goals
in mind: ‣Provide a small set of extensions to standard programming languages, like C, that enable a straightforward implementation of parallel algorithms. With CUDA C
/C++, programmers can focus on the task of parallelization of the algorithms rather than spending time on their implementation.
Support heterogeneous computation
where applications use both the CPU and GPU. Serial portions of applications are run on the CPU, and parallel portions are offloaded to the GPU. As such, CUDA can be incrementally applied to existing applications. The CPU and GPU are treated as separate devices that have their own memory spaces. This configuration also allows simultaneous computation on the CPU and GPU without contention for memory resources.

關於cudnn的介紹:

The NVIDIA CUDA® Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks.
cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling,
normalization, and activation layers. cuDNN is part of the NVIDIA Deep Learning SDK. Deep learning researchers and framework developers worldwide rely on cuDNN for high-performance GPU acceleration.
It allows them to focus on training neural networks and developing software applications rather than spending time on
low-level GPU performance tuning. cuDNN accelerates widely used deep learning frameworks, including Caffe,Caffe2, Chainer,
Keras,MATLAB, MxNet, TensorFlow, and PyTorch. For access to NVIDIA optimized deep learning framework containers,
that has cuDNN integrated into the frameworks, visit NVIDIA GPU CLOUD to learn more and get starte

一個簡單的理解就是,cuda在GPU上進行平行計算的平臺,cuda針對deep learning計算時,又有了一個針對deep learning進行優化的深度學習的庫。顯示卡驅動是底層的硬體驅動。

 

介紹結束了,下面簡短歸納一下網上的幾類安裝方法(我這裡寫的不是具體的步驟,只是現有方法的大概總結):

1.先安裝NVIDIA顯示卡驅動,再cuda,再cudnn,再tensorflow-gpu:

這種方法再安裝驅動部分又分為以下幾種型別:

(1).直接從ubuntu裡的系統設定,軟體與更新,附加驅動裡選擇新增新驅動(不成功,因為根本無法選);

(2). 終端新增ppa安裝源,然後apt-get install的形式安裝,會造成迴圈登入,解決方法是解除安裝安裝的驅動(不可行);

(3). 直接從nvidia官網下載驅動檔案,以sh **.run形式單獨進行驅動安裝(可以成功安裝驅動,但後續會出問題)。

關於第三種情況,我嘗試了很久,網上方法很多,我認為這兩篇最有幫助:

https://blog.csdn.net/Zafir_410/article/details/73188228?utm_source=blogxgwz0

https://blog.csdn.net/ksws0292756/article/details/79160742

但他們在安裝驅動這一步上也並沒有完全解決我的問題,最後我在google上直接貼上錯誤搜尋,基本解決了安裝驅動的問題,

也就是在終端執行nvidia-smi能得到正確輸出:

Wed Nov  7 16:05:20 2018       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 410.48                 Driver Version: 410.48                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 950M    Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   34C    P0    N/A /  N/A |      0MiB /  4046MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

先前我是卡在了:

Error:Unable to load the 'nvidia-drm' kernel module .

按那兩篇部落格第二篇那樣並不能解決,在google搜到是修改一個.h檔案中的兩行程式碼,但歷史記錄找不到了,不能詳細告訴大家了。

然而就算我之前驅動安裝成功,依然還有一個大難題:無法將安裝好的驅動載入除了,開啟nvidia x server setting時總是跳出:

You do not appear to be using the NVIDIA X driver on Linux Ubuntu

解決這個問題又有兩種主要方法:

(1). 再安裝Nvidia-prime顯示卡驅動管理工具(我的並不好使);

(2).Google上說安裝bumlebee工具,我安裝之後確實消除了上面那個問題,然而因為我自己的誤操作又歸零了,而且這個是否有後續錯誤我也不知道。

https://blog.csdn.net/geange/article/details/79284727

 

 

 

2.終極的正確安裝方法:安裝cuda時順帶安裝顯示卡驅動,這才是正解,單獨安裝驅動問題一個接著一個。之前我還納悶,為啥nvidia顯示卡驅動沒有官方的安裝指導,原來人家只給了cuda的安裝指導,但這個通過安裝是選擇要不要安裝Nvidia驅動。都怪沒有過來人指點,才走了這麼多彎路,否則可以節省很多時間。

貼上一篇給我指點了一條明路的部落格:https://blog.csdn.net/QLULIBIN/article/details/78714596

這篇部落格基本上也是翻譯了官方的cuda安裝指導,但是人家在一些地方都做了特別強調,所以非常有用。

可以看到底下部落格評論,照著那個方法做基本都很順利。

當然你要自己願意去看官方安裝指導,也非常有幫助,這裡給出連結:

https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1604&target_type=runfilelocal

這裡提醒一點:一定要注意官方指導文件裡的

table 1 Native Linux Distribution Support in CUDA 10.0

這個表,這裡說明了ubuntu型別,gcc版本,kernel版本,怎樣的匹配才能支援cuda10.0,這個必須符合。

安裝前對整個過程一定要提前熟練,最好不要誤操作,否則又要走很多彎路。

 

cudnn的安裝比較簡單,只需要注意跟cuda的對應關係,nvidai官網上都明確指明瞭。

tensorflow-gpu的安裝我都是通過anaconda安裝的,網上有很多教程,這個比較簡單。貼上一篇我用到的:

https://blog.csdn.net/lukaslong/article/details/81092032

 

就這樣,希望每一個人都能安裝順利!

 

ps: 最後再說一下我的最終導致重灌linux的經歷,一次是手殘把linux自帶的python3.5資料夾刪除了,然後各種系統問題,後面在網上查,直有效率的方法就是重灌系統了;

一次是安裝了nvidia驅動後,解析度變差,錯誤修改了/etc/X11/xorg.conf配置檔案,之前卻沒有備份,然後就是終端也打不開了,桌面也進不去了,甚至是ctrl+alt+F1都不行。希望大家不要犯我這樣的錯誤,配置檔案修改之前一定要備份,考慮周全,防止誤操作。