Deep learning environment configuration ultra-detailed tutorial [Anaconda+PyTorch (GPU version)+CUDA+cuDNN]

Deep learning environment configuration

To get started with deep learning, the first thing to do is to build a deep learning environment. No matter you are a Windows user, a Mac user or an Ubuntu user, as long as the computer configuration allows, you can do deep learning. After all, Windows, Mac and Ubuntu systems can all build deep learning environments. Next, I will record the process of building a deep learning environment on the Windows system, so that I can archive it myself and provide a reference for everyone.
The main mode of this environment configuration is based on Anaconda+PyTorch (GPU version)+CUDA+cuDNN .

Required tools:

  1. Python integrated development environment: Anaconda
  2. CUDA, cuDNN : Computing platforms provided by Nvidia for Nvidia graphics cards. It is used to improve the operating efficiency of the neural network. If the computer graphics card does not meet the requirements, you can use the CPU for calculation without installing it.
  3. Development tools: PyCharm
  4. Deep learning library: PyTorch (Google's open source TensorFlow platform can also be used, but the PyTorch platform is generally used in academia.)

1. Install Anaconda

1.1 Download Anaconda

Anaconda official website: https://www.anaconda.com
Tsinghua University open source mirror download: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
Both websites can be downloaded, choose the version suitable for your computer to download That's it.
insert image description here
Click Download to download Anaconda.

1.2 Install Anaconda

Click the downloaded .exe file to install it. There is generally no major problem with the installation, just keep clicking next.

Here, if you are the only user on the computer, you can also choose Just Me; choosing All Users means that all users on this computer can use it, otherwise administrator privileges are required. Generally select All Users.
insert image description here
insert image description here
Note here: the folder must be empty, otherwise an error will be reported; secondly, Chinese characters should not appear in the folder name.
insert image description here
Then install it.
For case 1, open cmd and enter python -V to view the version that comes with anaconda;
for case 2, the version to view is the origin-python version before installing anaconda:
insert image description here

1.3 Add environment variables

Right-click "My Computer"->Properties->Advanced System Settings->Environment Variables,
insert image description here
select the Path of the system variable to edit;
insert image description here
if you directly create an Anaconda folder on the D drive for installation, you can directly set the following four Add the path:
D:\Anaconda
D:\Anaconda\Scripts
D:\Anaconda\Library\mingw-w64\bin
D:\Anaconda\Library\bin
insert image description here
The format of the above path is: the drive letter you installed + your folder Name + the following constant content;
for example: if you installed Anaconda to a folder named Python in the E drive, then your format is

E:\Python\Library\name-w64\bin

The other three are similar, just modify the previous content.

1.4 Test whether the installation is successful

1.4.1 Click Anaconda Navigator
to see if you enter the following page (the response time is long), and you can enter it smoothly: there
insert image description here
may be problems here: (If there is no flashback problem, you can directly jump to 1.4.2 to continue testing)
I am installing In anaconda, when I opened the above page for the first time, it was relatively smooth but an update prompt appeared. After clicking the update, I found that the Anaconda Navigator page could no longer be opened, or it would crash when opening this page, but there was no other place Any problem, only Anaconda Navigator can't open successfully.
The Anaconda flashback solution is as follows:

  1. Open Anaconda Prompt with administrator privileges
  2. To upgrade the navigator, the upgrade version information will be listed in the middle of the execution conda update anaconda-navigator
    insert image description here
    , as shown in the red box in the middle of the figure above, check the upgraded version, confirm that the upgraded version is acceptable, and then enter yconfirmation to upgrade. There will also be an option to choose whether to continue in the next few steps, just enter after checking y.
  3. Reset navigator, executeanaconda-navigator --reset
    insert image description here
  4. To upgrade the client, executeconda update anaconda-client
    insert image description here
  5. Upgrade and install the dependency package, and execute it. conda update -f anaconda-client
    insert image description here
    Now Anaconda Navigator can be opened normally.
    1.4.2 Click Anaconda Prompt
    here to continue testing whether anaconda is installed successfully.
    Input conda info, and observe whether the following bunch of data is output:
    insert image description here
    Re-enter conda --version, and observe whether the output version number:
    insert image description here
    If it prompts that conda is not an internal or external command, it means that anaconda has not configured the environment variables, and check back to see what went wrong.
    1.4.3 Change the conda source (subsequent installation of third-party libraries can speed up the speed).
    The official download server is abroad, and the download speed is slow. Tsinghua University in China provides the Anaconda mirror warehouse. We change the source to the Tsinghua University mirror source:

Operate in the Anaconda prompt:

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 --set show_channel_urls yes

Check if the channel is modified:

conda config --show channels

2. Install the NVIDIA graphics card driver

Go directly to NVIDIA official website: https://www.nvidia.cn/geforce/drivers/
insert image description here
There is a GeForce driver here, download it immediately, this is to download an NVIDIA driver that automatically recognizes your computer graphics card, remember to register an account after installation and Log in, and then update the graphics card driver inside.
insert image description here
First click the driver, then check for updates, and the graphics card driver can be automatically updated.
After updating the graphics card driver, enter the command interface of Anaconda prompt, enter nvidia-smi, and you can see the relevant information of your graphics card.
insert image description here

3. Install CUDA and cnDNN

3.1CUDA

After installing NVIDIA, we also need to install CUDA and cuDNN. These two patches are officially given by NVIDIA to facilitate deep learning calculations. Before
installing CUDA, we need to look at the compatible version. Panel, click Help-System Information in turn, and select components in the pop-up interface: In the
insert image description here
third line, we can see that my computer needs to install cuda11.6 version, we can download cuda not higher than this version, it is best Also don't go too low.

CUDA official website: https://developer.nvidia.com/cuda-downloads

Just download the corresponding version.
After downloading, the folder storing CUDA will disappear automatically, and you can find the corresponding path from the C drive later.

3.2cuDNN

Download address: https://developer.nvidia.com/rdp/cudnn-download

Pay attention to the version number when downloading. Make sure that the version numbers of cudnn and cuda are exactly the same.
After downloading, open the cudnn compressed package, and then open the cuda directory (the directory is in the picture):
insert image description here
You can see that cudnn has three folders, and put the contents of these folders into the corresponding folders of cuda.
So far cuda+cudnn installation is complete, we open the anaconda prompt, enter nvcc -Vto see cuda information:
insert image description here

4. PyTorch installation

After going through the above steps, we finally configured the driver related to the graphics card, and then we started to install pytorch.
First, you need to create a virtual environment, then enter the pytorch environment we created, and enter the following command:

Create a virtual environment called pytorch:

conda create -n pytorch python=3.9

Enter the pytorch virtual environment:

conda activate pytorch

Then install pytorch:

conda install pytorch

Then wait for the solving environment, and then press y to enter according to the prompt, and it will be installed automatically
to verify whether our installation is valid.

That is, first enter conda activate pytorchthe pytorch virtual environment, and then enter python in the terminal to enter the python interface

Enter separately

import torch
torch.cuda.is_available()

insert image description here
After importing torch, press Enter without error, the second line of command returns true and you're done

I have a problem with the above method, and the final result is false. So I changed the method, as follows:
PyTorch official website: https://pytorch.org

Pull down the official website interface
insert image description here
to select the relevant configuration of your computer, and then run the code in Run this Command in the anaconda prompt:

conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

The verification method is the same as above.
code show as below:

import torch
print(torch.__version__) #查看pytorch版本
print(torch.cuda.is_available()) #查看cuda是否可用 输出为True 或者False

Above, we have completed the simple deep learning environment configuration under windows.

----------------------------------2022.06.09 update: ----------- --------------------------------

In the fourth part of pytorch installation, if none of the above operations are successful, you can try the following operations:

1. Prerequisite: The first three parts are successfully operated, as shown in the figure below:

insert image description here

2. The main idea : There are always problems when installing pytorch after establishing a virtual environment. Here, install pytorch directly with pip, and then move it to the virtual environment.

First download the pytorch package corresponding to your own python version
. Click the link to enter the pytorch package download path in the following way: https://download.pytorch.org/whl/cu113
insert image description here
Select torch, torchaudio and torchvision that match your python version, cu115 in the figure below It means matching cuda11.5 version, cp39 means matching python3.9 version, win means windows:
insert image description here
After downloading these three packages, place them in the site-packages directory, which is where you usually install the package, mine is here C:\Users\25837\AppData\Roaming\Python\Python39\site-packages,
insert image description here
Then run cmd directly in the directory bar, and then install the three whl files in order,

pip install 名称

insert image description here
After the installation is successful, the following six folders will appear in this directory:
insert image description here
At this time, re-enter the python environment, and enter the following code to check whether the environment is configured successfully:

import torch
print(torch.__version__) #查看pytorch版本
print(torch.cuda.is_available()) #查看cuda是否可用 输出为True 或者False

insert image description here
At this point, the configuration of the pytorch environment is complete - but here it is only configured in python. If a virtual environment is created in conda, this virtual environment does not include torch and its deep learning environment.

So below we configure the deep learning environment of the virtual environment created by conda!!!

3. Configure the deep learning environment in the virtual environment created by conda

First, open the anaconda prompt as an administrator , so that the created virtual environment will be in the envs under the anaconda installation directory, otherwise the created virtual environment will be created on the C drive because there is no permission!! After the creation is complete, directly install the front using
insert image description here
pip The six folders related to torch are copied to the site-packages directory in the virtual environment, and then verified.

import torch
print(torch.__version__) #查看pytorch版本
print(torch.cuda.is_available()) #查看cuda是否可用 输出为True 或者False

The result is True, that is, the environment configuration is successful!!

(The experience post said that it can be copied directly like this, but I didn’t succeed after I tried it. I don’t know why woo woo woo!!)

---------------------------------- 2022.06.10 update ------------ ----------------------------

After several days of tossing, finally successfully configured the pytorch deep learning environment in the virtual environment!!!

The method of directly copying and pasting mentioned above does not apply to me, so I changed to a new method again.
That is, to change the mirror source , the above operation is actually no problem, but the download speed of different mirror sources is different, there may be interruptions, so pytorch will not be installed successfully, so the operation steps here are still:

1. First create a pytorch virtual environment in anaconda

conda create -n pytorch python=3.9

2. Use the conda command to install pytorch

After establishing the pytorch virtual environment, first enter the pytorch virtual environment and then install pytorch

Enter the pytorch environment with the following command:

conda activate pytorch

Then install pytorch with the following code:

Note: Here you can change 11.3 to the version corresponding to the cuda you installed. For example, the cuda version I installed is 11.5. After copying the code, just change 11.3 to 11.5 and run it (but it will not affect it if you don’t change it )

If the installation process here is interrupted or the installation is unsuccessful, you can change the mirror source and reinstall.

You can search for specific mirror sources by yourself.
(Because the mirror source I used is too slow, I don’t know which one is the fastest and best to use, so I don’t recommend it, anyway, just try a few more mirror sources and the download will always be successful! !)
insert image description here

Note: If the conda command is always unsuccessful, you can also try to use the pip command to install it. At this time, you need to change the mirror source to the pip mirror source; at the same time, if the conda command and the pip command do not work, you can also download whl first. file, and then pip installs it directly.

(This is just to provide you with different ideas. For specific operations, you can refer to the above process or search by yourself. In short, try several methods and it will always succeed. And if it goes well, maybe the first installation will be successful. My first installation The time went smoothly, and this time the reinstallation will be abolished, and the boss stepped on a lot of pits.)

3. Verify that the installation is successful

Also after the installation is complete, use the following code to check whether the installation is successful.

import torch
print(torch.__version__) #查看pytorch版本
print(torch.cuda.is_available()) #查看cuda是否可用 输出为True 或者False

insert image description here

------------------------------ At this point, the pytorch environment configuration is complete. -----------------------------------

Guess you like

Origin blog.csdn.net/qq_43874102/article/details/123164105