How to use TensorFlow with GPU on Windows for Heavy Tasks(2024) | by AJAY | May, 2024


In the last blog “How to use TensorFlow with GPU on Windows for minimal tasks — in the most simple way(2024)” I discussed how to use Microsoft TensorFlow-DirectML Plugin to make GPU available to TensorFlow(TF) in Windows, it is good for lightweight programming tasks that requiring GPU, but not good for programs that take too much graphics memory and requires heavy loading(as these frameworks need drivers and applications like CUDA and cuDNN to allocate memory for processing the data and task). So in this blog, we are going to deal with downloading and installing the correct versions of TensorFlow, CUDA, cuDNN, Visual Studio Integration, and other driver files to make GPU accessible to TensorFlow, for doing heavy tasks like Video Processing, High-Resolution Image Processing, and Deep Learning*.

*These tasks are heavily dependent on your GPU type and VRAM available.

Be informed:

  • The last version of TensorFlow that supports GPU on Windows is TF-2.10 (it supports most of the functionalities but it’s good to use the latest and stable versions).
  • This tutorial is mainly for Windows 11 users(as most Win11 will have hardware with a supported compute version for Nvidia Apps and Drivers).
  • All these downloads (especially the Visual Studio integration items) will take a good amount of secondary memory, so make sure you have about 30GB(approximate) of free memory on your computer.
  • This blog will be an easy-to-understand walkthrough of directions given in TensorFlow Website and is intended for those who know only TensorFlow or like to code in TensorFlow. There is another deep learning framework named PyTorch, which is also an easy-to-use with support for GPU.
TF Caution shown on TF Website

Now we are going to do the “Initial Checks, Download TensorFlow, Cleanup, Downloading other items, Installing the downloaded items one by one, Adding to Path, Testing, and we are done…”

  1. Initial Checks

To make sure you have a physical GPU in our system that can be detected by your system, use the command wmic path win32_videocontroller get captionin your command prompt. Open the command prompt, Click Windows Icons → type ‘cmd’ and open the command prompt.

List of all available GPUs in your system

If this command is giving an error, check if your device manager is listing the physical GPU by, Right click on the Windows icon → device manager → drop-down Display Adapters. If GPU is not listed, see other drop-downs and see other option on the internet.

2. Downloading TensorFlow

You will need TensorFlow 2.10 for this, and you can download it using pip install tensorflow==2.10. Read the below paragraph before sudden action.

If you have gone through my last blog to make TF work with GPU, then you will have TF CPU 2.10 and other plugins as well, but here we need normal TF 2.10. You can either start a venv(refer to this) or you can install TF for the user. I suggest starting with venv because it doesn’t interfere with other libraries and their dependencies while installation, also if the steps explained in this blog have completely worked out for you, you just need to take a little time to install TF for the user(if you want TF to use GPU wherever you create code files in your computer, without the need of activating a venv).

If you are installing TF for the user, there is an additional step. Go to cmd, Click Windows Icons → type ‘cmd’ and open the command prompt, now type pip list | findstr tensorflow. To find package names with string ‘tensorflow’. You have to uninstall all packages listed using the pip command above. Also to make sure there are no other TF components, use pip listto list all the packages and manually go through the list to find any TF items, if you find any, uninstall it using the name shown in the list (pip uninstall <package-name>) . Finally, install TF using pip install tensorflow==2.10 and you will get something like

List of TF packages after installing TF(I have installed for the user)

3. Cleanup/Uninstalling

Here we do everything fresh, but you can opt for resuming from any kind of installation you have done, else if you are ready to start from scratch then go to your computer’s Settings → Apps → Installed Apps → Search ‘Nvidia’.

Installed Nvidia Apps

You can see something like this on your application list, you can uninstall every one of it safely, except the Nvidia Control Panel. If you haven’t installed any Nvidia components this list might be empty. These uninstalls may take some time and multiple restarts, don’t worry.

4. Downloading Other Items

After all the uninstalls and a fresh restart, you are good to go for downloading all the required files one by one. This hack will help you install them faster. While this process may not go in a linear fashion, it will save a whole lot of time. We’re trying parallel downloading here.

First lets start with the downloading of Nvidia latest driver for your GPU and GeForce Experience App. For this go to this site, and you will see something like

Nvidia Driver Search
Resultant Driver after giving specifications

Download this driver and keep it in your downloads folder we will install it later. While Nvidia Driver is downloading, move to download the CUDA toolkit from this site.

CUDA 11.8 Download Website

In the Installer Type, you have two options ‘local’ or ‘network’. Local means you have to download a big package of about 3-4GB and do not need an internet connection for installation. In the Network installer, your initial download size is only 30MB but during installation, it will download and install the 3-4GB of CUDA toolkits from the Internet, so you should have a stable internet connection during installation. However, the subsequent installation steps are the same for both installer types.

While CUDA is downloading, move to download cuDNN on this website. Scroll down and click on cuDNN v8.6.0, see the image below.

cuDNN Installer Page(download the windows zip)

While cuDNN is downloading, move to download Visual Studio on this website, and download the community version of Visual Studio(this is a small-sized download). In addition to Visual Studio, you have to download the Microsoft Visual C++ Redistributable Versions 2015–2022 from this site.

Visual Studio Community Installer
Download Microsoft Visual C++ Redistributable as per your System Architecture

After downloading all the items you will have something like the below image in your downloads folder. With this many items, our downloading is done.

Downloaded Items

5. Installation

Installation should go in the order, VS Redistributable(lightest installation) → VS Individual Components → GeForce Experience(Nvidia Driver) → CUDA Toolkit 11.8 → cuDNN (copy-paste). Please don’t try for parallel installation.

  • Visual Studio Redistributable Installation: This is a minimal installation you just double-click on the VS Redistributable icon (4th one in the above image) and follow their on-screen prompts. Sometimes you may knowingly or unknowingly installed the VS Redistributables before, in this case, you need to ‘modify’ it and install it again.
  • Visual Studio Components Installation: Even though you may not use Visual Studio as your default code editor, this is necessary for CUDA to work with Windows as CUDA doesn’t come with its own C++ compiler, but it assumes that the compiler is installed in the system. So we need to install the C++ compiler from another vendor (here, Microsoft Visual Studio). Visual Studio contains all the necessary components, ex: the C++ compiler toolchain for Windows. This is one of the parts where the installation of CUDA gets very messy in Windows. See the image below to know what all items need to be installed from Visual Studio.



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*