TensorFlow安裝

      在〈TensorFlow安裝〉中尚無留言

學習 TensorFlow 就像在學習 numpy 或 pandas 一樣,有自已的資料格式,也有自已的計算函數跟規則。TensorFlow 可以幫我們計算複雜的數學公式,比如矩陣相乘,微分,損失函數等等,可以將它視為一個科學計算套件。

TensorFlow 計算領域非常廣泛,所以學習之路非常漫長,不像 numpy 或 pandas 幾天就可以學會的。

另外TensorFlow 可指定由CPU執行,或是由GPU執行。通常會使用TensorFlow 來計算的演算法,都是很大量的數字,所以 GPU 的效能會比 CPU 高很多。

TensorFlow是Google維護的一個機器深度學習框架。因為它已被用來尋找新的行星,幫助醫生篩檢糖尿病導致視網膜病變,以預防失明。AlphoGo及Google Cloud Vision 更建立在TensorFlow 之上。TensorFlow完全開放原始碼。

TensorFlow也是目前神經網路使用率最高的框架。而其Keras(凱拉斯)則是支援TensorFlow的高階函數庫,可以用很簡潔的程式碼完成神經網路模型. 下圖為網路上統計各種框架的使用率。

TensorFlow現今已到了第2版本。第1版與第2版本的使用方式完全不同,太多數的網站都是第1版本的教學。此篇以 TensorFlow2為主。

安裝CUDA

CUDA 全名為 Compute Unified Device Architecture (演算設備統一架構),是nVidia主推的演算法,該架構可啟用GPU多核心計算。

底下的安裝,必需具備有nVidia顯卡,且想要啟動GPU時才需要安裝。若無nVidia顯卡,也是可以安裝成功,但沒啥用處就是了。

CUDA Toolkit

由CUDA 編譯器,工具,函數庫,Smaple,CUDA Driver等多項工具組成,是一個工具集的套件包。請由如下網址下載安裝 : https://developer.nvidia.com/cuda-toolkit-archive。 請選擇CUDA 11.2 cuda_11.2.2_461.33_win10.exe ,共有2.8G,請耐心等後,安裝完成後,在dos模式下輸入 nvcc -V,若有看到版本,即表示安裝成功。

CUDA 自 11.3.0 到目前最新的 11.7,在執行 Google Object Detection 時會出現 JITCompile Error,請不要使用。

cudnn

專門為深度學習計算所設計的軟件庫。裏面包含了許多的計算函數,如卷積等函數。全名為
nVidia CUDA Deep Neural Network (深度神經網路), 請由如下網址下載
https://developer.nvidia.com/cudnn
上述網站需先註冊帳號,登入後,選取 Download cuDNN v8.4.1 (November 9th, 2020), for CUDA 11.x,再選取 Local Installer for Windows (Zip)

下載後,將檔案解開,然後把cuda裏的目錄及檔案 copy 到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2 之下

 

copy到如下位置

無法啟動GPU

cuda 11.2.0 的版本已沒有此問題了,此處只是個記錄,說明早期版本無法啟動時的解決方法。

早期版本會因為缺少 cusolver64_10.dll 函數庫而無法啟動GPU,錯誤訊息如下

2020-12-26 00:08:53.593776: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found
2020-12-26 00:08:53.596454: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2020-12-26 00:08:53.596975: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2020-12-26 00:08:53.597106: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1757] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
tf version: 2.4.0
use GPU False

此錯誤訊息,只要把C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin\cusolver64_11.dll 複製後,改成 cusolver64_10.dll,即可成功啟動GPU的功能了。

環境變數

底下的環境變數,在安裝cuda時,也幫我們自動設定好了所以無需手動設定。

CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1
CUDA_PATH_V11_1=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1
CUDA_SDK_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1

安裝tensorflow套件

tensorflow 自2.1版本開始,pip install tensorflow 就會下載支援 GPU 及不支援 GPU 二個版本。所以不需刻意安裝tensorflow-gpu。當然若刻意安裝 tensorflow-gpu,檔案容量會比較小。目前最新版本為2.9.1。

tensorflow 到 2020/12 月僅支援到 Python 3.5~3.8。所以Python 3.9 是無法安裝的。而後續的YOLO 4 到目前為止,也不支援 Python 3.8 版本,所以請一定要安裝 3.7.9。

真的需要好一點的nVidia顯示卡

啟動GPU的硬体加速功能,可以讓tensorflow加速許多。以顯卡GeForce GTX1050 Ti,CPU為i9-9900K進行測試,GPU 效能會比 CPU 快上三倍以上。

未啟用GPU:
    CPU佔用16核100%的資源, GPU佔用 0%

啟用GPU :
    CPU佔用16核 20%的資源, GPU佔用40%

換個比較高等級的nVidia顯卡是比較好的選擇,只是高等一點的顯卡動輒 2萬多台幣,RTX3080Ti更是要到五萬台幣,比一台陽春配備的電腦還貴。

偵測硬体設備

底下可以印出機器的硬体設備。set_visible_devices()的參數 devices=gpus[0],是使用第0塊顯示卡。若要多塊顯示卡一起用,可以下達 devices=gpus[0:2](使用 0,1二塊顯卡,不包含2)。

底下代碼中 device_type=’GPU’ ,參數名 “device_type” 可以不用打,直接輸入 ‘GPU’即可。因為list_pyhsical_devices(device_type=None) 只有接收一個參數而以,預設值為None。

gpus = tf.config.list_physical_devices(device_type='GPU')
cpus = tf.config.list_physical_devices('CPU') #device_type可以不用打
print(gpus, cpus)
#限定使用GPU
tf.config.set_visible_devices(devices=gpus[0], device_type='GPU')
結果 :
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')] [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]

限定使用CPU或GPU

tf.config.set_visible_devices()可以限定只使用CPU,或使用GPU。若沒調用此方法,預設會啟動 GPU。

cpus = tf.config.list_physical_devices (device_type='CPU')
tf.config.set_visible_devices (gpus[0], device_type='GPU') #只使用GPU
tf.config.set_visible_devices (devices=cpus[0]) #只使用 CPU, device_type預設是'CPU'

常數測試

import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
tf.print(msg)

矩陣相乘

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5, 6], [7, 8]])
c = tf.matmul(a, b)
print(c.numpy())
結果 :
[[19 22]
[43 50]]

參考:

1. https://medium.com/arthur-chien/python-tensorflow2%E7%AD%86%E8%A8%98-f3a97f571b8e
2. https://tf.wiki/zh_hant/  <==中文書

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *