[Super detailed] How to use GPU acceleration in the process of deep learning training model

foreword

In deep learning, we usually need to repeatedly optimize and train the model when we train it. It takes a long time to train only with CPU, but we can use GPU to accelerate the training model, which can greatly reduce the cost of our training model. time. The following figure is a comparison of the time spent using GPU acceleration and not using GPU acceleration in the process of training the model:

mmexport1683808312593.png

It can be seen from the figure that using GPU acceleration is much faster than training the model without GPU acceleration. Next, I will teach you how to use GPU acceleration in the process of deep learning training model.

Note: The following installation steps and commands only support installation in the Window environment, and I will publish other operating system installation tutorials later

Install the NVIDIA graphics driver

First search for Device Manager in the search bar, open it and find the version of our computer graphics card in the display adapter

image.png

Then we can find the driver version corresponding to our computer graphics card on the official NVIDIA website and install it.

NVIDIA Graphics Driver Download Link

Find the corresponding version of your graphics card and download the graphics card driver

image.png

After searching, find the corresponding version to download

image.png

Install after downloading

image.png

Here we choose the default location to install, and click OK. Other follow-up steps are relatively simple, just click Next to install. If you encounter any problems, you can refer to the following link to view the specific installation steps: [Windows] Install NVIDIA
driver

Finally, we open the command prompt and enter the nvidia-smi command to view the computer graphics card driver information

nvidia-smi

See the following information to prove that the NVIDIA graphics card driver has been successfully installed

VR8JH65PVE6O~35A7RT.png

Install CUDA and cuDNN

Introduction

Both CUDA and cuDNN are deep learning development tools launched by NVIDIA.

CUDA is the abbreviation of Compute Unified Device Architecture. It is a general-purpose parallel computing platform and programming model formulated by NVIDIA that allows developers to use programming languages ​​such as C++, Fortran, and Python to run code on NVIDIA GPUs. CUDA Toolkit is a development toolkit based on the CUDA platform, including GPU-accelerated libraries, debugging and optimization tools, C/C++ compilers, and runtime libraries.

The full name of cuDNN is CUDA Deep Neural Network library. It is a high-performance GPU-accelerated deep learning library that supports a variety of neural network models including convolutional neural networks and recurrent neural networks, and can be compatible with popular deep learning frameworks such as TensorFlow, PyTorch and Caffe2 etc. for integration. cuDNN provides highly optimized implementations, including common operations such as convolution, pooling, normalization, and activation. By using cuDNN, the processing speed of deep learning tasks can be accelerated.

Check version compatibility

Version compatibility is very important for GPU acceleration, because I have installed and uninstalled many times to solve the version compatibility problem.

Finally, I found the corresponding GPU, CUDA, cuDNN and Python versions in TensorFlow and NVIDIA official documents. You can refer to the link below to choose the version:

TensorFlow Official DocumentationNVIDIA
Official Documentation

A list of GPU models supported by each CUDA version can be found in the official NVIDIA documentation. In the left menu of the page, select the required CUDA version, and then click the "Supported Operating Systems" link to view the operating systems supported by the version. Click the "Supported CUDA GPUs" link to view a list of GPU models supported by this version.
mmexport1683812957391.png

You can also choose CUDA, cuDNN, Python and tensorflow-gpu versions to install according to this table

Corresponding information of each version of the blogger: NVIDIA GeForce GTX 1080, NVIDIA GeForce GTX 1650, Python3.9.0, CUDA 11.2.0, cuDNN 8.1.0, tensorflow-gpu 2.6.0

Install CUDA

Download link for CUDA 11.2: CUDA 11.2.0

First select CUDA corresponding system version information

CTF25GYZK~2MX3M_MXY.png
Finally click to download
DKFFSE6JB4F6XCNQZ8.png

After the download is complete, click the exe file to install

ZUP6P1DEC8595RKLYPU.png

Here we choose the default location for installation:
image.png
click OK and wait:
image.png
then enter the CUDA installation interface:
GF4_TZCW.png

System check --> agree to the license agreement --> select custom options --> installation complete

During the installation process, it is important to select the custom installation option:

UUN2SVTDW4Q0ESXT3G.png

Check all relevant configurations:

0R~3DW1XS9824WF9J75.png

Finally, the installation is complete and click Close:

PJP5HH7I1P71K868`U4P.png

There are no other specific points that need attention. If you encounter other problems, you can refer to the relevant links or leave a message in the comment area, and I will answer them one by one.

CUDA and cuDNN installation tutorial

Note: Environment variables have been added automatically during the installation process; the computer needs to be restarted after the installation is complete. The
default installation path is: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2

After restarting the computer, enter the nvcc -V command in the command prompt to check whether CUDA is installed successfully:
image.png

The above information is displayed to prove that CUDA is installed successfully

Install cuDNN

cuDNN download link

After clicking the download link, you can find the corresponding version to download. Before downloading, you need to register and log in to the NVIDIA Developer Program, and fill in your basic personal information.

The following takes cuDNN 8.1.0 as an example to download and install

VPLJ8F5VXK8RQDE85NE.png

After the download is complete, unzip to get the cuda folder

_RKY~JJE7R8UXLKY2QM.png

Open this folder and you can see that there are three folders: bin, include, and lib

41GA`CHLDTBPDGLJH.png

Find the previous CUDA 11.2.0 installation path: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2 and open the folder, you can find three corresponding folders

O2IKZJLFYIUSM`NGGIFPV.png

Copy the three folders in cudnn to the folder corresponding to cuda

4A4CNZFI8~46VS061SS.png

You can select all the content in the corresponding folder, copy and paste it into the cuda folder

Configure environment variables

Right click on This PC and click Properties, then click Advanced system settings

~O352H`A37OJBTO6AG9Y0L.png

Click the environment variable, find the path environment variable in the system environment variable, and click to enter

QZ6CXNAP7XRQ`02CVNVC.png

AEFM02`8_LFCSUN.png

Add the following to the path environment variable

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\libnvvp
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\include

Generally, the first two environment variables have been automatically configured when CUDA is installed, and we only need to add the latter two.

Different versions of cuDNN can modify the version number

Finally, you can see these four locations in the path to prove that the environment variable configuration is successful

1MPEZYQ337KC7LEU0W_I.png

Verify that cuDNN is successfully installed

Open the terminal command prompt (win+r and enter cmd)
to enter the CUDA file path: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\extras\demo_suite

cd C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\extras\demo_suite

image.png

Execute: deviceQuery.exe , the following rendering will appear:

image.png

Continue to execute: bandwidthTest.exe , and the following effect diagram appears:

image.png

Finally, cuDNN was successfully installed

Verify TensorFlow Available GPUs

Install python3.9.0

python3.9.0 download link

Click the link to download and install. After the installation is complete, enter at the command prompt: python to view the current python version
6K912U2X6N9VT8EZC9E~P.png

Input: exit() to exit the python compilation environment
0_WW`0_EK8R9FXWFV.png

Install tensorflow-gpu 2.6.0

Enter at the command prompt: pip install tensorflow-gpu 2.6.0 Wait for the installation to complete

If you have any questions, please leave a message in the comment area

Finally, enter in the terminal: pip list to view the installed tensorflow-gpu 2.6.0

DX3P2KIKTSTRUL2QE.png

Verify available GPUs

You can enter the following code in the terminal to enter the python compilation environment or other compilers to verify the available GPU

import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

As long as the output result is >0, it proves that there is an available gpu

36446RM52CG_FMJEC5MWU8.png

code testing

Create a test_model.py file in the editor and enter the following code to display the GPU acceleration effect

import tensorflow as tf

# 列出可用的 GPU 设备
print(tf.config.list_physical_devices('GPU'))

# 设置 TensorFlow 使用 GPU 进行计算
gpu_devices = tf.config.list_physical_devices('GPU')
if gpu_devices:
    for device in gpu_devices:
        tf.config.experimental.set_memory_growth(device, True)

# 加载数据集
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

# 定义模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10)
])

# 编译模型
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer='adam', loss=loss_fn, metrics=['accuracy'])

# 训练模型
model.fit(x_train, y_train, epochs=5)

# 评估模型
model.evaluate(x_test, y_test, verbose=2)

operation result:

K8YOG9YUUXU`4YJFX.png

Summarize

The most important thing in the installation process is version compatibility. You can check the official documentation. Finally, if you have any questions, please leave a message in the comment area, I will reply to you and help you solve the problem

Guess you like

Origin blog.csdn.net/qq_52730883/article/details/130650143