AI - Win10 机器学习环境 (TensorFlow GPU, JupyterLab, VSCode)

原文:https://makeoptim.com/deep-learning/tensorflow-gpu-on-win10

  • 介绍
  • 所需软件
  • Nvidia GPU driver
  • CUDA ToolKit
  • cuDNN
  • 环境变量
  • Miniconda
  • 虚拟环境
  • 安装 TensorFlow
  • 安装 JupyterLab 和 matplotlib
  • 安装 pywin32
  • 在 JupyterLab 中运行 TensorFlow
  • 安装 VSCode
  • VSCode 运行 TensorFlow
  • 小结
  • 延伸阅读
  • 参考链接

介绍

  • Win10
  • GTX 1050ti
  • tensorflow 2.4.1

所需软件

  • NVIDIA® GPU drivers:CUDA® 11.0 需要 450.x 或更高版本。
  • CUDA® Toolkit:TensorFlow 支持 CUDA® 11(TensorFlow 2.4.0 及更高版本)
  • CUDA® Toolkit附带的CUPTI。
  • cuDNN SDK 8.0.4cuDNN 版本).
  • (可选)TensorRT 6.0,可缩短用某些模型进行推断的延迟时间并提高吞吐量。

Nvidia GPU driver

前往 https://www.nvidia.com/download/index.aspx?lang=en-us 下载并安装对应的显卡驱动。

image

重启并使用以下命令检查 GPU 是否可见。

PS C:\Users\Administrator> nvidia-smi.exe
Wed Apr 28 22:28:23 2021
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 466.11       Driver Version: 466.11       CUDA Version: 11.3     |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ... WDDM  | 00000000:01:00.0  On |                  N/A |
| 30%   34C    P0    N/A /  75W |    455MiB /  4096MiB |      1%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1532    C+G   C:\Windows\System32\dwm.exe     N/A      |
|    0   N/A  N/A      2800    C+G   ...Central\Razer Central.exe    N/A      |
|    0   N/A  N/A      4576    C+G   C:\Windows\explorer.exe         N/A      |
|    0   N/A  N/A      5104    C+G   ...5n1h2txyewy\SearchApp.exe    N/A      |
|    0   N/A  N/A      6112    C+G   ...lPanel\SystemSettings.exe    N/A      |
|    0   N/A  N/A      7588    C+G   ...me\Application\chrome.exe    N/A      |
|    0   N/A  N/A      8568    C+G   ...arp.BrowserSubprocess.exe    N/A      |
|    0   N/A  N/A      9076    C+G   ...nputApp\TextInputHost.exe    N/A      |
|    0   N/A  N/A      9096    C+G   ... Host\Razer Synapse 3.exe    N/A      |
|    0   N/A  N/A     10152    C+G   ...y\ShellExperienceHost.exe    N/A      |
+-----------------------------------------------------------------------------+

CUDA ToolKit

前往 http://developer.nvidia.com/cuda-downloads 下载 CUDA Toolkit 11.3 并安装。

image

cuDNN

前往 https://developer.nvidia.com/rdp/cudnn-archive 下载 cuDNN 并解压。

image

将解压后的 lib、bin、include,拷贝到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1 对应的目录下。

image
image
image

复制 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin 下的 cusolver64_11.dll,重命名为 cusolver64_10.dll

image

环境变量

SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin;%PATH%
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\extras\CUPTI\lib64;%PATH%
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include;%PATH%
SET PATH=C:\tools\cuda\bin;%PATH%

Miniconda

前往 https://docs.conda.io/en/latest/miniconda.html 下载并安装 Miniconda

image
image

虚拟环境

打开 Anaconda Powershell Prompt

image

创建一个名称为 tensorflow 的虚拟环境。

$ conda create -n tensorflow python=3.8.5
$ conda activate tensorflow

注:如果使用 Power Sehll, 需要执行 conda init powershellset-executionpolicy remotesigned, 否则会出现无法 activate 成功地问题,详见 https://stackoverflow.com/questions/62082873/conda-not-activate-in-power-shell

PS C:\Users\Administrator> conda init powershell

PS C:\Users\Administrator> set-executionpolicy remotesigned

安装 TensorFlow

(tensorflow) PS C:\Users\Administrator> pip install tensorflow==2.4.1

验证安装

(tensorflow) PS C:\Users\Administrator> python -c "import tensorflow as tf;print('Num GPUs Available: ', len(tf.config.list_physical_devices('GPU')))"
2021-05-05 21:38:12.335369: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-05-05 21:38:14.569229: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-05-05 21:38:14.571864: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library nvcuda.dll
2021-05-05 21:38:14.605855: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: NVIDIA GeForce GTX 1050 Ti computeCapability: 6.1
coreClock: 1.392GHz coreCount: 6 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2021-05-05 21:38:14.606025: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-05-05 21:38:14.682846: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-05-05 21:38:14.682987: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-05-05 21:38:14.721429: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2021-05-05 21:38:14.732614: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2021-05-05 21:38:14.818913: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2021-05-05 21:38:14.842604: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2021-05-05 21:38:14.848834: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-05-05 21:38:14.849000: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
Num GPUs Available:  1

安装 JupyterLab 和 matplotlib

(tensorflow) PS C:\Users\Administrator> pip install jupyterlab matplotlib

安装 pywin32

(tensorflow) PS C:\Users\Administrator> cd 'C:\ProgramData\Miniconda3\Scripts\'
(tensorflow) PS C:\ProgramData\Miniconda3\Scripts> python pywin32_postinstall.py -install
Parsed arguments are: Namespace(destination='C:\\ProgramData\\Miniconda3\\envs\\tensorflow\\Lib\\site-packages', install=True, quiet=False, remove=False, silent=False, wait=None)
pywin32_postinstall.py:164: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
Copied pythoncom38.dll to C:\WINDOWS\system32\pythoncom38.dll
Copied pywintypes38.dll to C:\WINDOWS\system32\pywintypes38.dll
Registered: Python.Interpreter
Registered: Python.Dictionary
Registered: Python
-> Software\Python\PythonCore\3.8\Help[None]=None
-> Software\Python\PythonCore\3.8\Help\Pythonwin Reference[None]='C:\\ProgramData\\Miniconda3\\envs\\tensorflow\\Lib\\site-packages\\PyWin32.chm'
Pythonwin has been registered in context menu
Creating directory C:\ProgramData\Miniconda3\envs\tensorflow\Lib\site-packages\win32com\gen_py
Can't install shortcuts - 'C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Python 3.8' is not a folder
The pywin32 extensions were successfully installed.

在 JupyterLab 中运行 TensorFlow

jupyter lab

JupyterLab 将自动在浏览器打开。

从 https://www.tensorflow.org/tutorials/images/cnn 下载并导入 CNN notebook

image

执行 Restart Kernel and Run All Cells

image

当训练开始, 检查 GPU 进程,可以看到 ...nvs\tensorflow\python.exe 表示正在使用 GPU 训练模型。

image
PS C:\Users\Administrator> nvidia-smi.exe
Wed May  5 21:47:50 2021
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 466.11       Driver Version: 466.11       CUDA Version: 11.3     |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ... WDDM  | 00000000:01:00.0  On |                  N/A |
| 30%   39C    P0    N/A /  75W |   3695MiB /  4096MiB |     50%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1008    C+G   C:\Windows\explorer.exe         N/A      |
|    0   N/A  N/A      3620    C+G   ...8bbwe\WindowsTerminal.exe    N/A      |
|    0   N/A  N/A      6936    C+G   ...nputApp\TextInputHost.exe    N/A      |
|    0   N/A  N/A      7024    C+G   ...5n1h2txyewy\SearchApp.exe    N/A      |
|    0   N/A  N/A      9648    C+G   ...me\Application\chrome.exe    N/A      |
|    0   N/A  N/A     12128    C+G   C:\Windows\System32\dwm.exe     N/A      |
|    0   N/A  N/A     12248    C+G   ...y\ShellExperienceHost.exe    N/A      |
|    0   N/A  N/A     14384      C   ...nvs\tensorflow\python.exe    N/A      |
+-----------------------------------------------------------------------------+

安装 VSCode

前往官网下载并安装 VSCode

打开 VSCode 并安装 Python 支持。

image

选择某个文件夹(这里以 ~/tensorflow-notebook/01-hello 为例),新建文件 hello.ipynb

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
hello.numpy()

使用 VSCode 打开刚才创建的 ~/tensorflow-notebook/01-hello/hello.ipynb,并选择 Python 为创建的虚拟环境。

image

VSCode 运行 TensorFlow

image

小结

至此,开发环境已经搭建完毕。大家可以根据自己的习惯,选择使用命令行、JupyterLab 或者 VSCode 进行开发。

延伸阅读

  • Mac 机器学习环境 (TensorFlow, JupyterLab, VSCode)
  • Mac M1 机器学习环境 (TensorFlow, JupyterLab, VSCode)
  • Ubuntu 机器学习环境 (TensorFlow GPU, JupyterLab, VSCode)

参考链接

  • https://www.tensorflow.org/install
  • https://www.tensorflow.org/guide/gpu
  • https://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/
  • http://developer.nvidia.com/cuda-downloads

你可能感兴趣的:(AI - Win10 机器学习环境 (TensorFlow GPU, JupyterLab, VSCode))