TensorFlow environment build win10+RTX4000+graphics card driver 522.25+CUDA11.5+cuDNN 8.3.1+Python3.7+TensorFlow-gpu=2.6.0

 

 

1 Introduction

TensorFlow is an open source machine learning framework developed and maintained by Google. It provides a high-level API that allows developers to easily build and train various types of machine learning models. TensorFlow can run on a variety of different hardware, including CPUs, GPUs, and TPUs. This article will focus on how to build a TensorFlow GPU environment on Windows 10 so that technical developers can quickly use this powerful machine learning framework.

 

 

2 Preparation work

2.1 Version matching problem

When building a deep learning environment, version matching is very important. Different deep learning libraries and tools may have some dependencies and compatibility issues in versions. If these issues are not considered when setting up the environment, the code may not run properly. To ensure that versions match, careful investigation and planning should be done before starting to use them. Here is a detailed explanation of the version matching issues between them:

2.1.1 Matching graphics card driver and graphics card

Each graphics driver version also only supports a specific version of the graphics card. Before selecting a graphics card driver version, determine your graphics card model and see in which driver version that model is supported. A list of supported graphics cards for each driver version can be found on NVIDIA's official website. ( Official Drivers | NVIDIA )

2.1.2 Matching CUDA version and graphics card driver

Each CUDA version only supports a specific version of graphics cards. Before selecting a CUDA version, you should first determine your graphics card driver model and check which CUDA version this model is supported in. On NVIDIA's official website, you can find a list of graphics card drivers supported by each CUDA version. ( https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#:~:text=%EF%83%81-,CUDA%20Toolkit,%3E%3D451.22** ,-*%20Using%20a%20Minimum )

2.1.3 Matching of CUDA version and cuDNN version

Each cuDNN version is optimized for a specific version of CUDA. Before selecting the cuDNN version, first determine the CUDA version to use. A list of CUDA versions supported by each cuDNN version can be found on NVIDIA's official website. ( cuDNN Archive | NVIDIA Developer )

2.1.4 CUDA version matches Python and TensorFlow

Different CUDA versions adapt to different versions of Python and TensorFlow. The matching relationship can be found on the TensorFlow official website.

https://www.tensorflow.org/install/source#common_installation_problems

https://www.tensorflow.org/install/source_windows

In general, the graphics card version determines the graphics card driver version, the graphics card driver version determines the CUDA version, the CUDA version determines the cuDNN version, and the CUDA version and cuDNN version determine the Python and TensorFlow versions. To ensure version matching between the above software, detailed investigation is required before installation.

2.2 Check the graphics card model and whether the driver is installed

1. On Windows systems, open a command prompt or PowerShell window. Enter the nvidia-smi command and run it, as shown in Figure 1:

17d5e28316be478e87743893c554bd18.png

figure 1

2After running this command, the screen outputs GPU-related information, such as GPU model, driver version, GPU usage, etc. The sample output is shown in Figure 2: the graphics card name is Nvidia Quadro RTX 4000, the driver version is 522.25, and the maximum supported CUDA version is 11.8.

ff38fb796f1c4ecf9bc217f1641c23c7.png

 

figure 2

2.3 Install NVIDIA GPU driver

If the graphics card driver is not installed, please follow the steps below to determine the NVIDIA GPU driver version and install it:

1Visit the NVIDIA official website (https://www.nvidia.com/Download/index.aspx) and select the driver version that needs to be installed. You can select the corresponding driver based on the graphics card model and operating system version. As shown in Figures 3 and 4, taking the Nvidia Quadro RTX 4000 query as an example, the highest adaptive driver version is 531.18.

62b6066c190c4f0d88df283cbb56105a.png

 

image 3

e0c70ea53b4843c4a1b60eebf82a58bd.png

 

Figure 4

2. Download the driver installation file and double-click to run the file.

3. In the installation wizard, select the "Custom (Advanced)" option and check "Perform a clean installation".

4. In the installation options, select the components that need to be installed, such as graphics card drivers, NVIDIA control panel, PhysX system software, etc. In general, it is recommended to install all components.

5. During the installation process, the system will prompt that you need to restart the computer to complete the installation. Select "Yes" and wait for the computer to restart.

6. After restarting, check the graphics card device in the device manager to confirm that the driver has been installed correctly.

 

 

3Install TensorFlow environment

Before installing TensorFlow, you need to install the adapted versions of CUDA and cuDNN, and then install Anaconda. After the installation is complete, create a virtual environment in Anaconda Prompt in Anaconda to facilitate different users to manage their required versions of Python and TensorFlow. Finally, install the adapted TensorFlow environment and toolkit in the virtual environment.

3.1Install CUDA Toolkit

CUDA Toolkit (also known as CUDA) is a set of GPU-accelerated computing tools developed by NVIDIA, which provides a variety of libraries, APIs, and tools for accelerating GPU computing. CUDA Toolkit is a standard toolkit for developing and deploying CUDA applications. The steps to install CUDA Toolkit on Windows 10 are as follows:

1Before installing CUDA, you need to ensure that the selected CUDA version matches the graphics card model and operating system version. On NVIDIA's official website, you can find a list of graphics card drivers supported by each CUDA version. ( https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#:~:text=%EF%83%81-,CUDA%20Toolkit,%3E%3D451.22** ,-*%20Using%20a%20Minimum )

Taking the driver version 522.25 as an example, the optional CUDA version is below 11.8. Here, we installed the CUDA Toolkit version 11.5.

2Visit the NVIDIA official website (https://developer.nvidia.com/cuda-toolkit-archive) and select the CUDA version that needs to be installed. You can select the corresponding installer based on the operating system version, graphics card model, and CUDA version, and download the CUDA Toolkit installer for Windows.

40ae9e94f0314f84a4c0c74665e6181b.png

 

Figure 5

3Run the installer and follow the prompts to complete the installation. It should be noted that the installation program will install CUDA to the C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.5 directory by default.

023af5dd0a9a4257a8e99368714b6ee2.png

 

Figure 6

4After restarting, open the command prompt (CMD) and enter the "nvcc -V" command. If the installation is successful, you should see the CUDA version information, as shown in Figure 7. You can see that CUDA 11.5.50 has been installed.

dba399fed9094b3480a38295c146f183.png 

Figure 7

5 Check the system environment variables. As shown in Figure 8, the CUDA environment variables have been automatically added to the system.

a484aadc409f4637b7cc01647ef0dcc9.png

 

Figure 8

3.2 Install cuDNN

cuDNN (CUDA Deep Neural Network) is a set of libraries developed by NVIDIA to accelerate deep neural network calculations. It is specifically designed for GPU-accelerated deep learning tasks and works with the CUDA Toolkit. cuDNN provides a series of efficient basic neural network operations, such as convolution, pooling, activation functions and normalization, as well as APIs and tools for implementing advanced operations. Please follow these steps to install:

1Before installing cuDNN, you need to download the cuDNN library for Windows. Choosing the correct cuDNN version depends on the CUDA version used. Each cuDNN version is optimized for a specific version of CUDA. ( cuDNN Archive | NVIDIA Developer )

505455a5b58646a483e21720fa879754.png

 

Figure 9

2. After downloading the cuDNN library, decompress the cuDNN and copy the file contents to the installation directory of the CUDA Toolkit (this article is: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.5).

20c8b8fb1f7d47468bb21cd2cfbe64fe.png

 

Figure 10

14f179d1c18e4f6eab7081000d4482ae.png

 

Figure 11

3. Open the system environment variables and add the four items shown in Figure 12.

28031f8e83a64991bb88f12a25ecba01.png

 

Figure 12

4 As shown in Figure 13, start cmd in the installation directory and execute bandwidthTest.exe in the terminal. If PASS appears, the installation is successful.

1e4e2e3bdf0649dead69b3cf42037f1e.png

 

Figure 13

3.3 Install Anaconda

Anaconda is a popular Python distribution that contains many commonly used Python libraries and tools to easily create and manage Python virtual environments. The steps to install Anaconda on Windows 10 are as follows:

1Download the Anaconda installer for your operating system on Anaconda's official website (https://www.anaconda.com/products/individual).

d512b3ba9b554ba7ac433376fdbd0b58.png

 

Figure 14

2Run the downloaded installer. In the installation wizard, you can choose the installation path, register Anaconda as the default Python version, and other options. Do not choose to automatically add Anaconda to the system path at this time, the path will be added manually later.

e826719ffd91449aad6896273cda3049.png

 

Figure 15

3. In the system variables column, find “Path” and add the following information:

4cc08e81143f478c8f341679b22b3092.png

 

Figure 16

4After the installation is complete, Anaconda can be opened in Anaconda Navigator. The application can be found by typing "Anaconda Navigator" in the Windows search bar. Alternatively, enter the "anaconda-navigator" command on the command line to open it. Anaconda Navigator can manage Anaconda environments and installation packages through its interface. It can create new environments, install software packages, edit environment variables, etc.

efdf6af354664668a92c30302b5eddf7.png

 

Figure 17

5In addition, Anaconda can also be managed through the command line. On Windows systems, open Anaconda Prompt to access the command line terminal in the Anaconda environment. Anaconda Prompt is shown in Figure 18. Through the "conda" command, you can install, update, delete software packages and other operations in the Anaconda environment, as shown in Figure 19.

0494e00f73c943e19a1522a2a10b4a4b.png

 

Figure 18

054cf6b6ef3b46daba7f541333f89572.png

                  Figure 19

3.4 Create a virtual environment

Python development is generally not carried out in the base environment. Creating a virtual environment is a common Python development technique. It can isolate dependencies between different projects and avoid version conflicts and other problems. The steps to create a virtual environment using Anaconda Prompt are as follows:

1Open Anaconda Prompt.

2. To improve the toolkit download speed, change to the domestic Tsinghua mirror source and enter the following command to change: conda config --add channels Index of /anaconda/pkgs/free/ | Tsinghua University Open Source Software Mirror Station | Tsinghua Open Source Mirror

conda config --set show_channel_urls yes

3. Create a virtual environment and specify the version of Python in the virtual environment. Enter the following command:

conda create –n <env_name> python=<version_number>

Where <env_name> is the name of the created virtual environment, and <version_number> is the installed Python version number. As shown in Figure 20, conda create –n tf1 python=3.7 creates a virtual environment named tf1, in which the installed Python version is 3.7.

cf0c51b27e1247aaa663552a17b8b7a4.png

Figure 20

4 As shown in Figure 20, when "Proceed ([y]/n)?" appears, enter y and wait for Anaconda Prompt to complete the creation of the virtual environment. After done is displayed on the screen, the virtual environment is created. At this time, enter conda info –envs in Anaconda Prompt. The existence of tf1 environment is displayed in the list, proving that tf1 has been installed.

3.5 Install TensorFlow in a virtual environment

Go to the TensorFlow official website and check the TensorFlow versions supported by the installed CUDA and cuDNN, as shown in Figure 21. TensorFlow 2.6.0 version is installed here.

TensorFlow official website:

https://www.tensorflow.org/install/source#common_installation_problems

https://www.tensorflow.org/install/source_windows

797bf70fae48478196ab7348b0c934b5.png

 

Figure 21

1 As shown in Figure 22, enter conda activate tf1 in Anaconda Prompt to activate the tf1 virtual environment. You can see that the environment enters tf1 from base.

2 Enter pip install tensorflow-gpu==2.6.0, install TensorFlow-gpu with version number 2.6.0, and wait for the installation to complete.

3After the installation is complete, enter the following command:

python

Import tensorflow as tf

Print(tf.test.is_gpu_available())

True is output on the screen, indicating that the GPU version of TensorFlow has been successfully installed.

4 Continue to enter the command: print(tf._version_), and the screen output is 2.6.0, further indicating that TensorFlow has been successfully installed and the version number is 2.6.0, as shown in Figure 23.

5. To install common tool packages, use the command conda install xxx, where xxx is the name of the package to be installed, for example:

Install pandas: enter the command: conda install pandas

Install keras: Enter the command: conda install keras

Install matplotlib: enter the command: conda install matplotlib

Install sklearn: Enter the command: conda install scikit-learn

Enter exit(), hit Enter, and exit Python.

dbff172b9960402b9183c84e25047edf.png

Figure 22

03786b7fd7cf40b69803f9c768a47e14.png

Figure 23

 

 

4 Install PyCharm and run the program

PyCharm is a commonly used Python IDE. Python is an interpreter. PyCharm is an IDE (integrated development environment) specially built for the Python programming language. Writing Python programs in PyCharm ultimately requires the support of a Python interpreter. The two work together. PyCharm comes with a set of tools that can help users improve their efficiency when developing in the Python language, such as debugging, syntax highlighting, and project management. , code jump, smart prompts, auto-completion, unit testing, version control. In addition, the IDE provides some advanced features to support professional web development under the Django framework, and the interface makes it easier to write code and run operations.

installation steps:

1PyCharm download address: Download PyCharm: Python IDE for Professional Developers by JetBrains

9688bcdf07604c629b004e7a001d4405.png

 

Figure 24

Download different versions according to your own computer conditions. The professional version is charged and the community version is free. The download here is the Windows Community 2022.3.2 version. Click Download to download. After the download is complete, find the installation package pycharm-community-2022.3.2 and double-click to install it.

2 Import the Anaconda environment into Pycharm. Open Anaconda Navigator and click Environments to see that there is already a default "base" environment in anaconda, as shown in Figure 25, which contains most of the packages we will use. This environment can be used directly in PyCharm without Download the required packages yourself.

e9b914a3a5c5474d9d4cda0f47a30b40.png

 

 

Figure 25

3. Find the Anaconda installation directory (my anaconda is installed in "D:\anaconda3", as shown in Figure 26 and Figure 27). You may install it elsewhere. Find "python.exe" and "_conda.exe" in this directory. ",spare.

 

1ef09b7ac93143aaaae3f864cb38b56d.png

 

Figure 26

 

7819b5ae9da94c9a89d2b19db09d4635.png

 

Figure 27

4. Open the installed PyCharm and create a new project. The steps are: File-->New ProjectàCreat

5. After the creation is completed, set the location of the new project. Because we want to use the anaconda environment, we do not need the virtual environment that comes with PyCharm. The steps are: click File -> Settings -> Project -> Python Interpreter, as shown in Figure 28 and shown in Figure 29.

 

aa92a265d265412e95fe881d028afbb0.png

 

Figure 28

 

26c4cea12dd24a559037a023afcedb28.png

 

Figure 29

6. Check whether there is anaconda option in Python Interpreter on the right. If so, select it directly; if not, perform the following steps:

Click on the right Add Interpreterto add a new compilation environment, as shown in Figure 29.

In the pop-up window, select Conda Environmentand then select 右侧的“浏览”按钮, as shown in Figure 30.

Select the file you anaconda根目录want _conda.exe, as shown in Figure 31.

Then click Load Environment, as shown in Figure 32.

The environment is loaded successfully, and the interface shown in Figure 33 appears. Select the tf1 environment in Using existing environments, click OK, and the interface shown in Figure 34 appears, which means the environment is imported successfully.

 

bfb6199e56d345c1bf9cf62f4e31d3cf.png

Figure 30

 

3f93f3f567d0458198c2f127349c269b.png

 

Figure 31

 

fa3bbe5d70bf4fb4979da929cc0ff574.png

 

Figure 32

 

5609dc19c66743ddbcbb0369f1a60e46.png

 

Figure 33

 

fb694bf2af6f4b2ab70f2560c60a4fc4.png

 

Figure 34

7Write a simple program in Pycharm.

In the main.py file of the above newly created project, write the following code:

Import tensorflow as tf

Print(tf.__version__)

Print(tf.config.experimental.list_physical_devices(‘GPU’))

tf.config.experimental.srt_visible_devices([], ‘GPU’)

The output results are shown in Figure 35. Verify that the Pycharm operating environment is available and the GPU version of TensorFlow is available.

9ce67b4181564121809a43362ac94656.png

 

Figure 35

 

Guess you like

Origin blog.csdn.net/qq_44918501/article/details/132498307