Anaconda and PyCharm build Pytorch deep learning environment GPU version

foreword

The most common use of deep learning environments is the cooperation between Anaconda and PyCharm

Emphasis: No need to download additional Python language packs

Recently, to do image segmentation algorithm training through neural network, it is necessary to build the GPU version of PyTorch in the Windows system. The existing GPU is NVIDIA GeForce RTX 2060 SUPER.

The era of artificial intelligence has arrived, and artificial intelligence will bring acceleration in various fields!


1. What is Anaconda?

The model training process of deep learning is like cooking. As we all know, the knives, utensils and clothing used in Chinese food, Western food, and Japanese food must be different. If there are different rooms with different cooking environments, it must be convenient for the chef. Anaconda is used to arrange different deep learning environments. You can think of Anaconda as consisting of small rooms.


Second, the installation of Anaconda

Click to download: anaconda official website download

 Python3.9 in the picture above refers to the Anaconda that will be downloaded, and the highest version of Python3.9 can be installed in its small room.

Then clickDownload

 Download completed

 Double-click to open and click Next all the way until you select the saved address, preferably a non-C drive

 After that click Next

 The first arrow in the figure above means to add Anaconda to the environment variables of the computer, and it is recommended to check it here. Because it may be used later, such as using pyqt for interface development.

The second arrow is optional and has little effect.

Then click install, and the installation will be successful in the next step.

 The content above can be found in the start menu. It means that Anacond is installed successfully.

After Anacond is successfully installed, you already have a small room and a tool conda. The house number of the small room is called base, which already has the Python language and some commonly used Python libraries. Conda and Anaconda are different. Anaconda is a large warehouse, and many small rooms can be created in the large warehouse. Conda is like a small cart, which can transport the required tools to each small room, and can also carry the tools inside.

We can also use the small tool conda to create a small room in the large warehouse, give it a different house name, and then transport things to the new room.


3. Determine the GPU model of the computer

 Open the task manager and find the model number of the GPU. Mine is NVIDIA GeForce RTX 2060 SUPER

If you are sure that the GPU has been installed on your computer but the task manager does not display it, it is likely that the GPU driver is not installed. It is recommended to use the 360 ​​driver master here. After downloading and opening, click Diagnosis and it will automatically detect that the driver is not installed, just click Install.


4. Determine the appropriate CUDA version to install

CUDA is a computing platform launched by the graphics card manufacturer NVIDIA . It is a kind of architecture computing. Only through CUDA can the GPU be called to solve complex parallel computing problems. Because CUDA is launched by NVIDIA, CUDA is only suitable for NVIDIA graphics cards.

 When the GPU driver is installed, the Cuda driver will also be installed. Cuda driver and Cuda runtime must be distinguished. The Cuda driver in the driver can be regarded as hardware cuda, and the Cuda runtime in the above figure can be regarded as software cuda. Cuda runtime is the CUDA computing platform we want to install. The deep learning model calls the hardware cuda on the software cuda, and then controls the GPU to perform graphics processing operations.

Find Anaconda Prompt (anaconda) in the start menu and open it.

Type nvidia-smi and hit enter.

The CUDA Version here: 12.0 is the Cuda dirver version. And the version of the CUDA computing platform we want to reinstall needs to satisfy Cuda runtime version ≤ Cuda Driver version.

After a certain type of GPU hardware is produced, it has a certain computing power, which is an attribute of itself and a fixed value.

From the CUDA computing power table below, we found that the GPU computing power is 7.5 according to GeForce RTX 2060 SUPER, and the architecture is Turing.

CUDA calculation table

In the figure above, CUDA SDK10.0-10.2 version corresponds to Cuda runtime version 10.0-10.2, computer capability3.0-7.5 means computing power 3.0-7.5, and Turing is included in the brackets, indicating that Cuda runtime version 10.0-10.2 supports computing power 3.0-7.5 , and it is a Turing-structured GPU.

To install a suitable Cuda runtime version, it is necessary to satisfy Cuda runtime version ≤ Cuda Driver version

The CUDA SDK version range that supports computing power 7.5 is 10.0-12.0, and my Cuda Driver version=12.0, because 10.0-12.0 ≤ 12.0, the Cuda runtime version that matches my GPU is 10.0-12.0. (If my Cuda Drive version=11.5, then my CUDA version is 10.0-11.5)


5. Create a virtual environment and install the required installation package

Open Anaconda Prompt (anaconda) again, enter the following command and press Enter

conda create -n pytorch02 python=3.8

Create a new room with the conda car, the room is named pytorch02, and the python language used in the room is version 3.8. (It is recommended that the selected python version ≥ 3.7)

 Then go to the above steps, enter y and press Enter.

 done: Indicates that the environment is created.

It can also be understood that this is a small room with the house number pytorch02 and some basic tools inside. At present, the house number is only called pytorch02, and the toolkit used by pytorch has not been transported by the conda car. That is to say, you can also send some paddle kits inside now, and this room can be used as a paddle environment. Because the python3.8 language is configured in it, the pip gadget that comes with python can also be used, and pip and conda are the same.

Then enter the following command to activate the environment and enter this environment.

conda activate pytorch02

 It can be seen that the base environment has been switched to the pytorch02 environment.

Next, we use the conda car to install the pytorch, torchvision, torchaudio and other toolkits that pytorch needs to use in the pytorch02 environment.

Click on the pytorch official website to enter the download toolkit. pytorch official website

 After entering, click install

 The CUDA version here corresponds to the Cuda runtime version. I already know that the CUDA range that matches me is 10.0-12.0, so I choose the CUDA11.7 version here, and try to choose a higher version within the range.

After that, enter the command on the right of Run this Command: above in the pytorch02 environment to download pytorch and other toolkits. code show as below:

conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia

 Enter the above code and hit enter.

 In the prompted information, you can find the corresponding pytorch, torchvision, torchaudio and cudatoolkit. Note that their version should be 11.7, and they all have cuda11.7 or cu117 attached.

Then enter y after proceed([y]/n)? and hit enter.

 done indicates that the installation was successful.

Enter python and press Enter to enter the environment. The >>> in front indicates that the entry is successful.

Then enter import torch and hit enter

Then enter torch.cuda.is_available() and press Enter. If True is displayed, it means that the GPU version environment of pytorch is created successfully!

My install took about 20 minutes. If you use this method to install too slowly or fail to download, it may be a network problem. You can try to download it through pip, find the command as shown in the figure below, and enter it into the pytorch02 environment.

 If the above two methods are unsuccessful, you can try to download other CUDA versions, as long as it is in your own suitable version range, mine range is 10.0-12.0. Previous versions are in Previous PyTorch Versions.

Because the above method needs to connect to the server abroad, the download speed will be very slow or unstable. We can also use the mirror address of Tsinghua University to download. From the above command, we know that we need to download four toolkits: pytorch, torchvision, torchaudio, and cudatoolkit (replace pytorch-cuda with cudatoolkit).

First add Tsinghua mirror source channel

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

 After that install the toolkit. Because the cudatoolkit=11.7 version is too new, the image source will not be updated in time and the installation will fail, so the version number is not added here.

conda install pytorch torchvision torchaudio cudatoolkit

 However, several toolkits downloaded through the Tsinghua image source are all cpu versions. For how to change to GPU versions, please refer to the following article.

Change the CPU version toolkit to the GPU version

The method for verifying a successful installation is the same as above.


6. Download PyCharm and import PyTorch environment

Click to enter the official website of PyChram. PyCharm official website

 click DOWNLOAD

 Download the Community version for Windows.

During the installation process, you can put the installation path on the non-C drive, and this place must be ticked.

 Whether you create a project yourself or open someone else's project, the method of using the created environment through PyCharm is the same. I am here by downloading someone else's project pytorch-mask-rcnn-master as an example.

Click File—>Settings in turn

 Find your own project name and click Project: pytorch-mask-rcnn-master—>Python Interpreter

 Then click steps 1 and 2 in turn

 Continue to point 1, 2

 My environment path is D:\soft\anaconda\envs\pytorch02. Find python.exe in pytorch02, select and click OK

 click OK

 Points 1 and 2, the pytorch02 environment is successfully applied.

 There will be a hint of Python 3.8 (pytorch02) in the lower right corner of the editing page.

One more thing, the cpu environment configuration idea of ​​​​deep learning PyTorch is similar to the above, and the commands on the following pages can be used.

Summary
The above is the content of this article. This article introduces the installation of PyTorch's GPU and CPU version environment in detail. PyTorch is easy to get started, and it is a fairly simple, efficient and fast framework. The design is in line with human thinking, allowing users to focus on realizing their own ideas as much as possible.

Guess you like

Origin blog.csdn.net/m0_63769180/article/details/128375465