The data set that comes with pytorch is provided by two upper-level APIs, which are torchvision
andtorchtext
among them:
torchvision
Provides APIs and data related to image data processing- Data location:,
torchvision.datasets
for example:torchvision.datasets.MNIST
(handwritten digital picture data)
- Data location:,
torchtext
Provides API and data related to text data processing- Data location:,
torchtext.datasets
for example:torchtext.datasets.IMDB(电影
comment text data)
- Data location:,
Let's take Mnist handwritten numbers as an example to see how pytorch loads its own data set
The method of use is the same as before:
- Prepare the Dataset instance
- Give the dataset to the dataloder to disrupt the order and form a batch
4.1 torchversion.datasets
torchversoin.datasets
The dataset classes in (for example torchvision.datasets.MNIST
) are inherited fromDataset
Means: torchvision.datasets.MNIST
an instance that can be obtained Dataset
by instantiating directly
But the parameters in the MNIST API need to be noted:
torchvision.datasets.MNIST(root='/files/', train=True, download=True, transform=)
root
The parameter indicates where the data is storedtrain:
The bool type indicates whether to use the data of the training set or the data of the test setdownload:
bool type, indicating whether you need to download data to the root directorytransform:
Realized image processing function
4.2 Introduction to the MNIST data set
The original address of the data set:http://yann.lecun.com/exdb/mnist/
MNIST is Yann LeCun
a free image recognition data set provided by et al., which includes 60,000 training samples and 10,000 test samples. The size of the image has been standardized. They are all black and white images with a size of28X28
Execute the code, download the data, observe the data type:
import torchvision
dataset = torchvision.datasets.MNIST(root="./data",train=True,download=True,transform=None)
print(dataset[0])
The downloaded data is as follows:
The output of the code is as follows:
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
Processing...
Done!
(<PIL.Image.Image image mode=L size=28x28 at 0x18D303B9C18>, tensor(5))
Two pieces of data can be returned from the data set, which can be guessed as the data and target value of the picture
The 0th return value is of type Image, which can be opened by calling show() method and found to be handwritten number 5.
import torchvision
dataset = torchvision.datasets.MNIST(root="./data",train=True,download=True,transform=None)
print(dataset[0])
img = dataset[0][0]
img.show() #打开图片
The pictures are as follows:
It can be seen from the above: the return value is (图片,目标值)
, this result can also be obtained by observing the source code