Deep learning environment configuration 10 - torch==1.7.1 environment configuration under Ubuntu


1. 2022/9/18 update

Many fans reported that TypeError: array () takes 1 positional argument but 2 were given errors, which can be solved by modifying the pillow version.

pip install pillow == 8.2.0
  • 1

If the following error occurs:

SubProcess ended with  return code :  4294967295 .
  • 1

Copy ptxas to /usr/local/cuda-11.0/ptxas. The download link is:
Extraction code: vyec

study foreword

I have not made an environment configuration related to Ubuntu , and decided to fill this hole.
insert image description here

Configuration tutorial for each version of pytorch

The configuration tutorials for each version of pytorch are as follows:
deep learning environment configuration 10 - torch==1.7.1 environment configuration under Ubuntu Deep learning environment configuration 8 - (30 series graphics card) torch==1.7.1 environment configuration depth
under windows
Learning environment configuration 5 - torch-cpu=1.2.0 environment configuration
under windows Deep learning environment configuration 2 - torch=1.2.0 environment configuration under windows

environmental content

pytorch: 1.7.1
torchvision: 0.8.2

Environment configuration

1. Anaconda installation

The installation of Anaconda is mainly for the convenience of environment management. You can install multiple environments on one computer at the same time. Different environments place different frameworks: pytorch, tensorflow, and keras can be installed in different environments. You only need to use conda create –n to create a new environment. That's it.

1. Download Anaconda

If you have a system with a visual interface, you can directly log in to Anaconda's official website: . You can directly download the corresponding installation package.
insert image description here
insert image description here
Usually download 64-bit, open after the download is complete.

In addition, you can also download directly in the terminal through the wget command. Specifically as shown.
insert image description here
After the download is complete, the corresponding sh file will appear in the directory. That is, the installation file of anaconda.
insert image description here

2. Installation of Anaconda

First set the sh file to be executable through the instruction.

sudo  chmod -R 777
  • 1

insert image description here
Then use the following command to execute the sh file.

  • 1

After the implementation, there are a bunch of agreements that need to be followed. Generally, at this time, yes, yes, yes.

insert image description here
There is a long list of protocols in the middle, which will be skipped faster by pressing the space. Then enter yes, otherwise it will not be installed normally.

insert image description here
Then select the installation path. The students can install it according to their own needs. By default, it will be installed in the ~ folder. Enter the address and hit Enter, Anaconda will start installing automatically.

insert image description here
It is recommended to select yes in this step, the conda environment will be automatically initialized, and some tedious steps can be removed.

insert image description here
Open it again and you will have the base environment.

insert image description here
The installation of Anaconda is over.

Second, the download and installation of Cudnn and CUDA

I am using torch=1.7.1 here, the officially recommended Cuda version is 11.0, so cuda11.0 will be used, and the cudnn corresponding to cuda11.0 is

1. Download of Cudnn and CUDA

Network disk download:
Extraction code: ylrh

Official website download:
The address of the cuda11.0 official website is:
cuda11.0 official website address
Then you can download it through the command.

  • 1

The address of cudnn's official website is:
You need to go in and look for
cudnn official website address

After downloading, get these two files.
insert image description here

2. Installation of Cudnn and CUDA

a, CUDA installation

Then we open a terminal in this folder and use the following command to start installing CUDA.

sudo  sh
  • 1

Because the current Ubuntu system basically automatically installs the graphics card driver, after running, it will generally prompt that the graphics card driver has been installed. At this time, ignore it and directly select Continue to go to the next step. Then accept the next step of the agreement.

insert image description here
Because the graphics card driver has been installed, no longer select Driver in this step, and then select install to install Cuda.

insert image description here
Once installed, CUDA information also needs to be added to ~/.bashrc, so we do that. Open the .bashrc file with gedit or vim.

gedit ~/.bashrc
  • 1

Then add the following code at the end of the file, and the environment variable has been added at this time.

export  PATH = $PATH :/usr/local/cuda/bin  
 export  LD_LIBRARY_PATH = $LD_LIBRARY_PATH :/usr/local/cuda/lib64  
 export  LIBRARY_PATH = $LIBRARY_PATH :/usr/local/cuda/lib64
  • 1
  • 2
  • 3

At this point, CUDA has been installed, and Cudnn needs to be further installed.

b. Installation of Cudnn

With the terminal still open, use the following command to extract the Cudnn file. Use unzip for zip files and tar for tgz files.

  • 1
tar -xvf cudnn-11.0-linux-x64-v8.0.5.39.tgz
  • 1

After the decompression is complete, you need to copy the files in the cudnn folder to /usr/local/cuda-11.0/lib64/ and /usr/local/cuda-11.0/include/.

Go to the cudnn folder and use the cp command to copy.

cp cuda/lib64/* /usr/local/cuda-11.0/lib64/
 cp cuda/include/* /usr/local/cuda-11.0/include/
  • 1
  • 2

insert image description here
At this point cudnn is also installed.

3. Configure the pytorch-gpu environment

1. Creation and activation of pytorch-gpu environment

ctrl+alt+T, enter the following command in the command prompt:

conda create –n pytorch-gpu python=3.8
  • 1
conda activate pytorch-gpu
  • 1

There are two instructions in total:
the previous one is used to create an environment called pytorch-gpu, and the python version of this environment is 3.8.
The latter instruction is used to activate an environment called pytorch-gpu.

2. Installation of pytorch-gpu library

Since all our operations must be performed in the corresponding environment, we need to activate the environment before installing the library.

conda activate pytorch-gpu
  • 1

At this point the terminal looks like:
insert image description here

Then we enter the following command:

# CUDA 11.0
conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=11.0 -c pytorch
  • 1
  • 2

Install pytorch-gpu.

3. Installation of other dependent libraries

But if you want to run deep learning models, there are some other dependent libraries that need to be installed. details as follows:

scipy == 1.7 .1 
numpy == 1.21 .2 
matplotlib == 3.4 .3 
opencv_python == 4.5 .3 .56 
torch == 1.7 .1 
torchvision == 0.8 .2 
tqdm == 4.62 .2 
Pillow == 8.3 .2 
h5py == 2.10.0 _
scikit - learn
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

If you want a more convenient installation, you can create a requirements.txt file on the desktop or elsewhere, and copy the above content into the txt file.
insert image description here
Use the following command to install it. In the following instructions, the path in front of requirements.txt is the path where I put the file on the desktop, and you can modify it according to your computer.

pip install
 -r ~ / requirements.txt _ _
  • 1

4. The installation is slow, please pay attention to changing the source

It should be noted that if the download and installation in pip is slow, you can change the source. You can go to the user folder, create a pip folder, and then create a txt file in the pip folder.
The creation command is as follows. If you are prompted that you do not have permission, add a sudo in front of it:

mkdir ~/pip
gedit ~/pip/pip.conf
  • 1
  • 2

Modify the content of the file.

[ global ] 
index - url = https : // mirrors . aliyun . com / pypi / simple
 [ install ] 
trusted - host = mirrors . aliyun . com
  • 1
  • 2
  • 3
  • 4

insert image description here
After all installation is complete, restart the computer.

Fourth, install VSCODE

I personally like VSCODE, so I installed it. Other editing software can also be, personal preference.

1. Download VSCODE

Directly load the official website of VSCODE , click deb to download.
insert image description here

2. Installation of VSCODE

After the download is complete, open the terminal and use the following command to install. Installation is complete and it is ready to run.

sudo dpkg -i code_1.69.2-1658162013_amd64.deb
  • 1

insert image description here

3. Designation of the operating environment

First click on the plugin module and install python.insert image description here

After the installation is complete, restart vscode. At this time, the lower left or lower right corner of vscode can be used to select the environment.

insert image description here
After clicking, select the environment. If there is no corresponding interpreter, you can click the refresh in the upper right corner.
insert image description here

Tags: Deep learning environment configuration 10 - torch==1.7.1 environment configuration under Ubuntu

Deep Learning Environment Configuration deep learning ubuntu pytorch Environment configuration 1.7

Related: Deep learning environment configuration 10 - torch==1.7.1 environment configuration under Ubuntu