Artificial intelligence configuration environment tutorial 2: Create a virtual environment in Anaconda Install the GPU version of Pytorch and torchvision and use the virtual environment in VsCode

about the author

Meng Liping, female, School of Electronic Information, Xi'an Polytechnic University, 2021 graduate student, Zhang Hongwei's artificial intelligence research group.
Research direction: machine vision and artificial intelligence.
Email: [email protected]

This tutorial provides the installation packages of CUDA11.3, Pytorch1.10.0, and torchvision0.11.0 that need to be installed, and you can pick them up from the Baidu network disk link below!

Link: https://pan.baidu.com/s/18mgO8GtW1UnG6uijnnQvIQ?pwd=0843 Extraction code: 0843
– Shared from Baidu Netdisk super member V5

1. Check the CUDA version of your computer

According to the following two methods, check whether your computer supports CUDA, if not, then skip directly to [7]!

1.1 Method 1

1.1 .1 Use the shortcut key 'win + R'- - -> input cmd - - -> click OK
insert image description here

1.1.2 Enter the command 'nvidia-smi' in the terminal - - -> Check the CUDA version supported by your computer

The current CUDA version is 11.6, indicating that the highest supported version of CUDA is 11.6, which is backward compatible

insert image description here

1.2 Method 2

1.2.1 Right click on the desktop - - -> Open NVIDIA Control Panel

insert image description here

1.2.2 With help - - -> select system information

insert image description here

1.2.3 In Components - - -> View CUAN version

insert image description here

2. Download and install CUDA

NVIDIA official website: https://developer.nvidia.com/cuda-toolkit-archive

  • Find the CUDA version suitable for your computer on the NVIDIA official website. It is recommended to install CUDA11.3 if the requirements are met; this tutorial provides the installation package of CUDA11.3
    insert image description here
  • Select the corresponding version of CUDA and download and install it;
    insert image description here
  • After downloading, install it in the default path;

CUDA installation reference post: https://blog.csdn.net/m0_45447650/article/details/123704930/ .

3. View environment variables

1. In Settings –> Search Advanced System Settings –> View Environment Variables

  • The following four environment variables should appear in the environment system:
    (This environment variable is for CUDA11.3 version, other versions need to correspond to the CUDA version you downloaded)

CUDA_PATH
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3

CUDA_PATH_V11_3
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3

NVCUDASAMPLES_ROOT
C:\ProgramData\NVIDIA Corporation\CUDA Samples\v11.3

NVCUDASAMPLES11_3_RO…
C:\ProgramData\NVIDIA Corporation\CUDA Samples\v11.3

insert image description here
insert image description here

  • If there is no corresponding environment variable, you need to add it manually

Verify that CUDA is successfully installed

4. Create a virtual environment

4.1 Create a virtual environment using instructions

  • Add mirror source:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/
conda config --set show_channel_urls yes
conda config --set ssl_verify false
  • Create a virtual environment, install Python 3.6 version in this tutorial; run the following commands on the local cmd terminal:
conda create -n py36 python=3.6

insert image description here

insert image description here

4.2 Check whether the created virtual environment appears in Anconda

Open Anconda, click Environments to see if there is a virtual environment named py36

insert image description here
Note: Next, install various scientific computing packages and frameworks (Pytorch) in a virtual environment named py36. If there are multiple environments, for example, python3.7 is required, and a virtual environment of python3.7 is created. Conda management will not There is a version conflict.

4.3 Virtual environment related instructions

a : The terminal runs the following command: View the local environment

conda env list

b: The terminal runs the following command: enter the virtual environment

conda activate py36

c: The terminal runs the following command: Exit the virtual environment

conda deactivate

Precautions: Enter the virtual environment as shown in the figure: there will be parentheses (virtual environment name: py36), after entering the virtual environment, you can configure Pytorch in an environment where Python is 3.6, and install it as much as you like!

5. Install the GPU version of Pytorch and torchvision

This tutorial installs CUDA11.3, Pytorch1.10.0, torchvision0.11.0

There are two ways to install Pytorch and torchvision, online and offline; if there is a problem that the installation fails in the online way, you can choose to install offline, and the corresponding installation package will be provided.

5.1 Online installation

  • Install the mirror source: execute the following five instructions in sequence;
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/
conda config --set show_channel_urls yes
conda config --set ssl_verify false
  • Enter the created virtual environment (py36) in the local terminal, when (py36) appears at the top, it indicates that the virtual environment of py36 has been entered; insert image description here
    - Enter the official website of Pytorch: https://pytorch.org/

  • Click on previous Pytorch versions
    insert image description here

  • Find the Pytorch installation instruction under CUDA11.3 under Windows system and copy it;
    If the installed CUDA version is not 11.3, you need to select the corresponding version of Pytorch and torchvision
    insert image description here

  • Paste in the py36 virtual environment of the cmd local terminal to run the installation:

conda install pytorch==1.10.0 torchvision==0.11.0 torchaudio==0.10.0 cudatoolkit=11.3 -c pytorch -c conda-forge

insert image description here

5.2 Offline installation

5.2.1 Download and install torch

Pytorch download link: https://download.pytorch.org/whl/torch_stable.html .

  • Select the corresponding version of torch and download it
    insert image description here
    insert image description here

  • Enter the following command in the local terminal to install offline. When Successfully installed... appears , it indicates that the installation is successful; use any one of the following three commands;

pip install F:\Demo\torch\torch-1.10.0+cu113-cp36-cp36m-win_amd64.whl
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple F:\Demo\torch\torch-1.10.0+cu113-cp36-cp36m-win_amd64.whl
pip install F:\Demo\torch\torch-1.10.0+cu113-cp36-cp36m-win_amd64.whl http://pypi.douban.com/simple/trusted host pypi.douban.com

Remarks: "F:\Demo\torch\torch-1.10.0+cu113-cp36-cp36m-win_amd64.whl" in the above command indicates the path + file name to download the torch installation package and save it on your computer; download according to your own Changes to saved conditions;

insert image description here

5.2.2 Download and install torchvision

Pytorch, torchvision and python have version correspondence

This tutorial found that the torchvision version corresponding to Pytoch1.10.0 is 0.11.0 .

insert image description here

insert image description here

  • Use the pip command to install torchvision0.11.0; use any of the following three commands;
pip install F:\Demo\torchvision\torchvision-0.11.0+cu113-cp36-cp36m-win_amd64.whl
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple F:\Demo\torchvision\torchvision-0.11.0+cu113-cp36-cp36m-win_amd64.whl
pip install F:\Demo\torchvision\torchvision-0.11.0+cu113-cp36-cp36m-win_amd64.whl http://pypi.douban.com/simple/trusted host pypi.douban.com

Note: "F:\Demo\torchvision\torchvision-0.11.0+cu113-cp36-cp36m-win_amd64.whl" in the above command indicates the path + file name of the torchvision installation package; change it according to your own situation;

- If torch is automatically updated during the installation of torchvision, use the following command to install torchvision;

pip install --no-deps F:\Demo\torchvision\torchvision-0.11.0+cu113-cp36-cp36m-win_amd64.whl

insert image description here

5.3 Verify successful installation

  • After the installation is complete, run the following commands in sequence in the virtual environment to verify whether Pytoch is successfully installed and whether the GPU is available;
python
import torch 
torch.cuda.is_available()

insert image description here

If import torch does not report an error, it means that torch is installed successfully

If the result of torch.cuda.is_available() is True, it means that the GPU version is installed

6. Using a virtual environment in VsCode

6.1 Install the Python plugin

  • Enter the VsCode software, click "Extension", enter Python in the search box, and then select Install in the lower right corner of the Python plug-in;
    insert image description here

6.2 Add a virtual environment

  • Press the shortcut key "Ctrl+Shift+P" to bring up the global setting search window, and then enter "Python:Select Interpreter" and the "Python:Select Interpreter" option will appear, click this option;
    insert image description here

  • Click this option to jump to the Python interpreter configuration window, where the added Python interpreter is displayed, select py36, and you can use Python in VsCode;
    insert image description here

  • Note: If VsCode enters the terminal, it is a PS environment; Then first output cmd in the terminal , exit the PS environment, and then manually enter "conda activate py36" to enter the configured py36 environment; you can view the installed torch and torchvision by entering "pip list", and confirm that you downloaded and installed it yourself version of
    insert image description here

The above is all about creating a virtual environment in Anaconda, installing the GPU version of Pytorch and torchvision, and using the virtual environment in VsCode! ! !

Next is the installation tutorial of the CPU version of Pytorch! ! !

Prerequisite: Complete step 4 first. Create a virtual environment

Seven. Install the CPU version of Pytorch and torchvision

  • Enter the virtual environment, use the Pytoch official website to install and run according to the instructions;
    insert image description here
conda install pytorch==1.10.0 torchvision==0.11.0 torchaudio==0.10.0 cpuonly -c pytorch
  • Verify that the installation is successful, and import torch succeeds without reporting an error:
python 
import torch 

The above is all about creating a virtual environment in Anaconda, installing the CPU version of Pytorch and torchvision! ! !

Guess you like

Origin blog.csdn.net/m0_37758063/article/details/130422948