Pytorch deep learning introductory notes 1 (Pycharm version)

Pytorch deep learning introductory notes 1 (Pycharm version, environment installation pitfalls, configuration projects)

Environmental requirements and environment configuration

required environment

The version of Pycharm (Pro) should not be too old, just within the past two or three years.

Anaconda3+Python (version = 3.9) There are many nanny-level tutorials on the installation of anaconda (tutorial portal: https://blog.csdn.net/qq_45344586/article/details/124028689), or you can search by yourself

Pytorch

CUDA11+

CUDNN

Environment configuration and some points to note

Enter the Pytorch official website download page (portal: https://pytorch.org/get-started/locally/), copy the command in Run this Command to the Pycharm terminal or system cmd to start the download (not like the traditional website The same method is used to click to download);

If your computer has a GPU , it is recommended to install CUDA+CuDNN. Check the method: Task Manager – Performance, as shown below:

Insert image description here

Regarding the installation of CUDA+CuDNN, there are also well-written and detailed tutorials on the Internet. The general steps can be followed by the tutorial (tutorial portal: https://blog.csdn.net/dreamerzc/article/details/120599278), here Let’s talk about some pitfalls to pay attention to in the new version of pytorch :

  • Many tutorials on the Internet teach you to install versions around CUDA10.2. If you are under a Windows system, the new version of Pytorch no longer supports the GPU CUDA10 version, so we need to download the CUDA11+ version , otherwise we will wait until you have finished downloading CUDA10. 2 As a result, you will find this when you enter the pytorch website:Insert image description here

  • Major versions of CUDA are backward compatible within that version . For example, if you download CUDA11.7, then when you install Pytorch, you can choose to adapt to the 11.6 version; but if you download CUDA11.3, you do not need to install Pytorch. The installation is adapted to version 11.6, and the installation is adapted to pytorch versions less than or equal to 11.3;

  • When installing CUDA, pay attention to following the tutorial to check whether your graphics card is compatible with the CUDA you want to download. This is very important, otherwise big problems may occur! !

  • It is recommended to create a new conda environment before entering the download command (command: conda create -n environment name python=3.x) to avoid some package version conflicts with pytorch;

If your computer does not have an NVIDIA series GPU or you want to use a CPU to run Pytorch , you can directly choose to download the CPU version from the Pytorch official website (or search the pytorch Tsinghua mirror online). CUDA+CUDNN does not need to be downloaded and installed;

Initial understanding of Pytorch

dir() function and help() function

These two functions can help you understand the usage and precautions of each module and the functions of each module in the process of using Pytorch. They are official and formal usage, which is very convenient;

usage:

  • Open the console of pycharm, import the torch package (note that it is not import pytorch but import torch) and then enter dir(torch). You can see many modules inside pytorch:Insert image description here

  • For each torch module, you can also use the dir function to subdivide it, just like a folder containing folders (take torch.sum as an example):Insert image description here

  • Let’s take a look at the help function again and enter help(sum):Insert image description here

You can see here there are very detailed usage methods and examples. In python learning (not just pytorch), we can use these two functions to help us understand the usage of various packages, libraries, and functions without going to Baidu;

data loading

Common data composition forms

Generally speaking, training data has two elements: the data itself and the label;

  • The first type, the label is relatively simple, for example, it only contains classification information, then the data can be named directly with the label;
  • Second, if you encounter some NLP or image processing data, the label may be more complicated and it is inconvenient to name it directly. At this time, the data may be in a folder, and the label is saved in another folder. The file name of each label is the same as the data. The file name corresponds to

Initial understanding of Dataset

Learn now and use it now. Use the help function directly to view the functional information of the dataset (it doesn’t matter if you think it is too complicated, you can just skip this code and read below):

help(torch.utils.data.Dataset)
Help on class Dataset in module torch.utils.data.dataset:
class Dataset(typing.Generic)
 |  An abstract class representing a :class:`Dataset`.
 |  
 |  All datasets that represent a map from keys to data samples should subclass
 |  it. All subclasses should overwrite :meth:`__getitem__`, supporting fetching a
 |  data sample for a given key. Subclasses could also optionally overwrite
 |  :meth:`__len__`, which is expected to return the size of the dataset by many
 |  :class:`~torch.utils.data.Sampler` implementations and the default options
 |  of :class:`~torch.utils.data.DataLoader`.
 |  
 |  .. note::
 |    :class:`~torch.utils.data.DataLoader` by default constructs a index
 |    sampler that yields integral indices.  To make it work with a map-style
 |    dataset with non-integral indices/keys, a custom sampler must be provided.
 |  
 |  Method resolution order:
 |      Dataset
 |      typing.Generic
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __add__(self, other: 'Dataset[T_co]') -> 'ConcatDataset[T_co]'
 |  
 |  __getitem__(self, index) -> +T_co
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __orig_bases__ = (typing.Generic[+T_co],)
 |  
 |  __parameters__ = (+T_co,)
 |  
 |  ----------------------------------------------------------------------
 |  Class methods inherited from typing.Generic:
 |  
 |  __class_getitem__(params) from builtins.type
 |  
 |  __init_subclass__(*args, **kwargs) from builtins.type
 |      This method is called when a class is subclassed.
 |      
 |      The default implementation does nothing. It may be
 |      overridden to extend subclasses.

This involves the use of many built-in functions. I will publish a special article later to explain these function methods in detail. Here we first introduce the three most commonly used functions.

When we create a dataset, we need to first build a class for the dataset, let's say it's called Mydataset;

Three commonly used functions (requires basic knowledge of object-oriented programming):

  • init initialization function: initializes the class. This function will be run when an instance is created. It is generally used to define some "global" variables, etc., similar to the constructor in CPP;
  • getitem function: obtain data information (address, etc.), the default form is def getitem (self, item), generally changed to def getitem (self, idx);
  • len function: obtain information such as data length;
timed

It needs to be mentioned here that if the getitem function needs to obtain the data file address (such as an image address), it needs to use python's os operating system library to turn the file address into a list:

import os  //使用os
dir_path = "文件夹路径,可以相对可以绝对,windows系统下需要注意斜线转义"
img_path_list = os.listdir(dir_path)  //将文件夹中的所有图片变成列表

Demonstrate it:

  • Copy an image (data) folder to the project

  • Right-click the picture folder and select the copy path, either absolute or relative (a tip to simplify the operation, saving you the need to go to the file explorer to copy);Insert image description here

  • Enter the code in the python console (note that the file path must either add double bars, or add an r before the quotation marks to prevent escaping): [External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directlyInsert image description here

With this file name array, we can easily access each data file through the array index. The second parameter of the getitem function was changed to idx to facilitate index access. The most important thing is to develop good habits~

Conclusion

This article mainly records the general process of pytorch environment installation and data loading. The next article will detail the example code and explanation of loading a data;

group, we can easily access each data file through the array index, and the second parameter of the getitem function is changed to idx for the convenience of index access. The most important thing is to develop good habits~

Conclusion

This article mainly records the general process of pytorch environment installation and data loading. The next article will detail the example code and explanation of loading a data;

This article is more for newcomers. At the same time, I am also in the process of learning pytorch. If there are any omissions, you are welcome to add and correct me~

Guess you like

Origin blog.csdn.net/qq_45882682/article/details/125637238