Installing TensorFlow GPU on Win10

Catch Zeng
6 min readMay 7, 2021

From https://makeoptim.com/en/deep-learning/tensorflow-gpu-on-win10

Intro

  • Win10
  • GTX 1050ti
  • TensorFlow 2.4.1

Software requirements

NVIDIA GPU driver

Go to https://www.nvidia.com/cn/download/index.aspx download and install the corresponding GPU driver.

Reboot and check that GPUs are visible using the following command.

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

Go to http://developer.nvidia.com/cuda-downloads to download CUDA Toolkit 11.3 and install.

cuDNN

Go to https://developer.nvidia.com/rdp/cudnn-archive to download cuDNN and unzip it.

Copy the decompressed lib, bin, include to the directory corresponding to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1.

Copy cusolver64_11.dll under C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin and rename it to cusolver64_10.dll.

Environment variable

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

Go to https://docs.conda.io/en/latest/miniconda.html to download and install Miniconda.

Virtual environment

Open Anaconda Powershell Prompt

Create a virtual environment called tensorflow.

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

Note: If you use Power Sehll, you need to execute conda init powershell and set-executionpolicy remotesigned, otherwise there will be a problem of failing to successfully activate activate, see https://stackoverflow.com/questions/62082873/conda -not-activate-in-power-shell

PS C:\Users\Administrator> conda init powershell
no change D:\ProgramData\Miniconda3\Scripts\conda.exe
no change D:\ProgramData\Miniconda3\Scripts\conda-env.exe
no change D:\ProgramData\Miniconda3\Scripts\conda-script.py
no change D:\ProgramData\Miniconda3\Scripts\conda-env-script.py
no change D:\ProgramData\Miniconda3\condabin\conda.bat
no change D:\ProgramData\Miniconda3\Library\bin\conda.bat
no change D:\ProgramData\Miniconda3\condabin\_conda_activate.bat
no change D:\ProgramData\Miniconda3\condabin\rename_tmp.bat
no change D:\ProgramData\Miniconda3\condabin\conda_auto_activate.bat
no change D:\ProgramData\Miniconda3\condabin\conda_hook.bat
no change D:\ProgramData\Miniconda3\Scripts\activate.bat
no change D:\ProgramData\Miniconda3\condabin\activate.bat
no change D:\ProgramData\Miniconda3\condabin\deactivate.bat
modified D:\ProgramData\Miniconda3\Scripts\activate
modified D:\ProgramData\Miniconda3\Scripts\deactivate
modified D:\ProgramData\Miniconda3\etc\profile.d\conda.sh
modified D:\ProgramData\Miniconda3\etc\fish\conf.d\conda.fish
no change D:\ProgramData\Miniconda3\shell\condabin\Conda.psm1
modified D:\ProgramData\Miniconda3\shell\condabin\conda-hook.ps1
no change D:\ProgramData\Miniconda3\Lib\site-packages\xontrib\conda.xsh
modified D:\ProgramData\Miniconda3\etc\profile.d\conda.csh
modified C:\Users\Administrator\Documents\WindowsPowerShell\profile.ps1
==> For changes to take effect, close and re-open your current shell. <==PS C:\Users\Administrator> set-executionpolicy remotesigned

Install TensorFlow

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

Verify installation

(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

Install JupyterLab and matplotlib

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

Install 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.

Run TensorFlow in JupyterLab

jupyter lab

JupyterLab will be automatically opened in a browser.

Download and import CNN notebook from https://www.tensorflow.org/tutorials/images/cnn.

Restart Kernel and Run All Cells

When the train is running, check GPU processes, you can see ...nvs\tensorflow\python.exe, which means that the GPU is being used to train the model.

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 |
+-----------------------------------------------------------------------------+

Install VSCode

Go to the Official Website to download and install VSCode.

Open VSCode and install Python support.

Open the created ~/tensorflow-notebook/01-hello/hello.ipynb, and select Python as the created virtual environment.

Run TensorFlow in VSCode

Summary

At this point, the deep learning environment has been set up. You can choose to use the command line, JupyterLab or VSCode for development according to your own habits.

Further reading

Reference

--

--