We’ll be covering the PyTorch DataLoader in this tutorial. Large datasets are indispensable in the world of machine learning and deep learning these days. However, working with large datasets requires loading them into memory all at once.

在本教程中,我们将介绍PyTorch DataLoader。 如今,大型数据集在机器学习和深度学习领域中是必不可少的。 但是,使用大型数据集需要一次将它们全部加载到内存中。

This leads to memory outage and slowing down of programs. PyTorch offers a solution for parallelizing the data loading process with the support of automatic batching as well. This is the DataLoader class present within the torch.utils.data package.

这会导致内存中断并降低程序速度。 PyTorch还提供了一种在自动批处理支持下并行化数据加载过程的解决方案。 这是torch.utils.data包中提供的DataLoader类。

PyTorch DataLoader语法 (PyTorch DataLoader Syntax)

DataLoader class has the following constructor:

DataLoader类具有以下构造函数:


DataLoader(dataset, batch_size=1, shuffle=False, sampler=None,batch_sampler=None, num_workers=0, collate_fn=None,pin_memory=False, drop_last=False, timeout=0,worker_init_fn=None)

Let us go over the arguments one by one.

让我们一一讨论。

  1. Dataset – It is mandatory for a DataLoader class to be constructed with a dataset first. PyTorch Dataloaders support two kinds of datasets:

    • Map-style datasets – These datasets map keys to data samples. Each item is retrieved by a __get_item__() method implementation.
    • Iterable-style datasets – These datasets implement the __iter__() protocol. Such datasets retrieve data in a stream sequence rather than doing random reads as in the case of map datasets.

    数据集 –必须首先使用数据集构造DataLoader类。 PyTorch数据加载器支持两种数据集:

    • 映射样式的数据集 –这些数据集将键映射到数据样本。 每一项都是通过__get_item__()方法实现检索的。
    • __iter__() 样式的数据集 –这些数据集实现__iter__()协议。 这样的数据集按流序列检索数据,而不是像地图数据集那样进行随机读取。
  2. Batch size – Refers to the number of samples in each batch.批次大小 –指每个批次中的样本数量。
  3. Shuffle – Whether you want the data to be reshuffled or not.随机播放 –是否要重新随机播放数据。
  4. Sampler – refers to an optional torch.utils.data.Sampler class instance. A sampler defines the strategy to retrieve the sample – sequential or random or any other manner. Shuffle should be set to false when a sampler is used.Sampler –指可选的torch.utils.data.Sampler类实例。 采样器定义了检索样本的策略-顺序,随机或任何其他方式。 使用采样器时,应将随机播放设置为false。
  5. Batch_Sampler – Same as the data sampler defined above, but works at a batch level.Batch_Sampler –与上面定义的数据采样器相同,但以批处理级别工作。
  6. num_workers – Number of sub-processes needed for loading the data.num_workers –加载数据所需的子流程数。
  7. collate_fn – Collates samples into batches. Customized collation is possible in Torch.collat​​e_fn –将样本整理为批次。 在Torch中可以自定义排序规则。
  8. pin_memory – Pinned (page-locked) memory locations are used by GPUs for faster data access. When set to True, this option enables the data loader to copy tensors into the CUDA pinned memory.pin_memory –固定(页面锁定)的内存位置供GPU使用,以加快数据访问速度。 设置为True时,此选项使数据加载器可以将张量复制到CUDA固定的内存中。
  9. drop_last – If the total data size is not a multiple of the batch_size, the last batch has less number of elements than the batch_size. This incomplete batch can be dropped by setting this option to True.drop_last –如果总数据大小不是batch_size的倍数,则最后一批的元素数少于batch_size。 通过将此选项设置为True,可以删除不完整的批处理。
  10. timeout – Sets the time to wait while collecting a batch from the workers (sub-processes).超时 –设置从工作人员(子流程)收集批次时要等待的时间。
  11. worker_init_fn – Defines a routine to be called by each worker process. Allows customized routines.worker_init_fn –定义每个工作进程要调用的例程。 允许自定义的例程。

Let us now look at a few examples of how to use DataLoaders.

现在,让我们看一些如何使用DataLoader的示例。

内置数据集上的PyTorch DataLoader (PyTorch DataLoaders on Built-in Datasets)

MNIST is a dataset comprising of images of hand-written digits. This is one of the most frequently used datasets in deep learning. You can load the MNIST dataset first as follows.

MNIST是一个包含手写数字图像的数据集。 这是深度学习中最常用的数据集之一。 您可以按如下方式首先加载MNIST数据集。


import torch
import matplotlib.pyplot as plt
from torchvision import datasets, transforms

Before we proceed, it will help to learn a little about the torchvision transforms we have just imported. Transforms are commonly used with image datasets in order to perform operations such as normalization, resizing, cropping etc.

在继续之前,这将有助于您了解一些有关我们刚刚导入的火炬视觉转换的信息 。 变换通常与图像数据集一起使用,以执行诸如标准化,调整大小,裁剪等操作。

Transforms are in general stacked together using a compose function and applied to the images in the dataset after converting them to a tensor.

通常,使用compose函数将变换堆叠在一起,并在将其转换为张量后将其应用于数据集中的图像。

The only operation we need to perform upon MNIST images is the normalization. We pass the values 0.5 and 0.5 to the normalization transform to convert the pixels into values between 0 and 1, into distribution with a mean 0.5 and standard deviation of 0.5.

我们需要对MNIST图像执行的唯一操作是归一化。 我们将值0.5和0.5传递给归一化变换,以将像素转换为介于0和1之间的值,以平均为0.5,标准差为0.5的分布。


# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,), (0.5,)),])

Now we load the built-in dataset at ‘~/.pytorch/MNIST_data/’ into our working space as a torch dataset and then build a data loader using this dataset.

现在我们将内置数据集“〜/ .pytorch / MNIST_data /”作为火炬数据集加载到我们的工作空间中,然后使用该数据集构建数据加载器。


# Download and load the training data
trainset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

To access the images from the dataset, all we need to do is to call an iter() function upon the data loader we defined here with the name trainloader. We can now access the images in the dataset using the .next() function.

要访问数据集中的图像,我们要做的就是在我们在此处定义的名称为trainloader的数据加载器上调用iter()函数。 现在,我们可以使用.next()函数访问数据集中的图像。


dataiter = iter(trainloader)
images, labels = dataiter.next()
print(images.shape)
print(labels.shape)
plt.imshow(images[1].numpy().squeeze(), cmap='Greys_r')

The following details regarding the batch size are printed along with the label of the image being printed.

以下有关批量大小的详细信息以及要打印的图像的标签一起打印。


torch.Size([64, 1, 28, 28])
torch.Size([64])
tensor(2)
MNIST Data Set
MNIST数据集

自定义数据集上的DataLoader (DataLoaders on Custom Datasets)

PyTorch allows you to create custom datasets and implement data loaders upon then. This makes programming in PyTorch very flexible.

PyTorch允许您创建自定义数据集并在那时实现数据加载器。 这使得在PyTorch中编程非常灵活。

To define a custom dataset, you need to override two major functions of the torch.util.data.Dataset class – __len__ and __getitem__ – which are used to retrieve the size of the dataset and get a sample item from a particular index respectively.

要定义自定义数据集,您需要覆盖torch.util.data.Dataset类的两个主要函数__len____getitem__ ,这两个函数分别用于检索数据集的大小并从特定索引中获取样本项。

Let us create a sample dataset for illustrating this. We create a dataset that holds 1000 randomly generated numbers.

让我们创建一个样本数据集来说明这一点。 我们创建一个数据集,其中包含1000个随机生成的数字。


from torch.utils.data import Dataset
import randomclass SampleDataset(Dataset):def __init__(self,r1,r2):randomlist=[]for i in range(1,1000):n = random.randint(r1,r2)randomlist.append(n)self.samples=randomlistdef __len__(self):return len(self.samples)def __getitem__(self,idx):return(self.samples[idx])dataset=SampleDataset(4,445)
dataset[100:120]

Output:

输出:


[439, 131, 338, 15, 212, 34, 44, 288, 387, 273, 324, 214, 115, 205, 213, 66, 226, 123, 65, 14]

Now we can define a data loader upon this custom dataset.

现在,我们可以在此自定义数据集上定义一个数据加载器。


from torch.utils.data import DataLoader
loader = DataLoader(dataset,batch_size=12, shuffle=True, num_workers=2 )
for i, batch in enumerate(loader):print(i, batch)

The output of the above code will be data divided into batches of 12. Some of the batches retrieved are shown below.

上面代码的输出将数据分为12个批次。下面显示了一些已检索的批次。


0 tensor([417, 410,   9, 261, 357, 288, 368,  97, 411,   8, 181,  80])
1 tensor([ 27,  59, 159, 392, 402, 294,  69,  67, 201, 427, 243, 402])
2 tensor([142, 267,  21, 399, 192, 377, 425, 270,  83, 370, 237, 199])
3 tensor([266, 305,  41, 315, 231, 260, 254, 383, 266, 285, 165, 118])
4 tensor([265, 320,  92, 162, 192, 153,  49, 344,  97, 240, 312, 192])
5 tensor([417,  35, 109,  75, 288, 258, 218, 275, 158, 251,  71, 276])
6 tensor([203,  86, 291, 429,  93, 334, 288, 392, 167, 242, 430, 194])
7 tensor([ 79,  52, 421, 147, 119,  76, 131,  28,  13, 277, 270, 164])
8 tensor([ 56, 410, 253, 159, 318,  68, 342, 260,  23, 289, 326, 134])
9 tensor([ 55,   9, 132, 353,  43, 225, 188, 217, 387,  32, 214, 242])
10 tensor([131,   6, 106, 191,  89,  91,  81, 271, 247, 347, 259, 366])

结论 (Conclusion)

As you can see, the PyTorch Dataloader can be used with both custom and built-in datasets. PyTorch DataLoaders give much faster data access than the regular I/O performed upon the disk. We hope this tutorial has helped you understand the PyTorch Dataloader in a much better manner.

如您所见,PyTorch Dataloader可以与自定义和内置数据集一起使用。 与在磁盘上执行常规I / O相比,PyTorch DataLoader提供了更快的数据访问速度。 我们希望本教程可以帮助您更好地理解PyTorch Dataloader。

翻译自: https://www.journaldev.com/36576/pytorch-dataloader

PyTorch数据加载器相关推荐

  1. pytorch自定义数据集和数据加载器

    假设有一个保存为npy格式的numpy数据集,现在需要将其变为pytorch的数据集,并能够被数据加载器DataLoader所加载 首先自定义一个数据集类,继承torch.utils.data.Dat ...

  2. 深度学习-Pytorch:项目标准流程【构建、保存、加载神经网络模型;数据集构建器Dataset、数据加载器DataLoader(线性回归案例、手写数字识别案例)】

    1.拿到文本,分词,清晰数据(去掉停用词语): 2.建立word2index.index2word表 3.准备好预训练好的word embedding 4.做好DataSet / Dataloader ...

  3. PyTorch基础-自定义数据集和数据加载器(2)

    处理数据样本的代码可能会变得混乱且难以维护: 理想情况下,我们想要数据集代码与模型训练代码解耦,以获得更好的可读性和模块化.PyTorch 域库提供了许多预加载的数据(例如 FashionMNIST) ...

  4. PyTorch 编写自定义数据集,数据加载器和转换

    本文为 pytorch 官方教程https://pytorch.org/tutorials/beginner/data_loading_tutorial.html代码的注释 w3cschool 的翻译 ...

  5. PyTorch数据加载处理

    PyTorch数据加载处理 PyTorch提供了许多工具来简化和希望数据加载,使代码更具可读性. 1.下载安装包 • scikit-image:用于图像的IO和变换 • pandas:用于更容易地进行 ...

  6. 从numpy里加载_PyTorch强化:01.PyTorch 数据加载和处理

    PyTorch提供了许多工具来简化和希望数据加载,使代码更具可读性. 1.下载安装包 scikit-image:用于图像的IO和变换 pandas:用于更容易地进行csv解析 from __futur ...

  7. PyTorch强化:01.PyTorch 数据加载和处理

    PyTorch提供了许多工具来简化和希望数据加载,使代码更具可读性. 1.下载安装包 scikit-image:用于图像的IO和变换 pandas:用于更容易地进行csv解析 from __futur ...

  8. pytorch 数据加载和处理

    # PyTorch提供了许多工具来简化和希望数据加载,使代码更具可读性. from __future__ import print_function, division import os impor ...

  9. pytorch数据加载时报错OSError: [Errno 22] Invalid argument

    将数据加载器的num_workers删去,即由 train_loader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, ...

最新文章

  1. native react 常用指令_React-Native 常用命令
  2. 基于JAVA实现的排序算法总结
  3. 百信银行基于 Apache Hudi 实时数据湖演进方案
  4. sendEmail实现邮件报警
  5. 猜物品游戏java编程_小猿圈Java初学者练习小案例:猜数字游戏
  6. Android Canvas简单使用
  7. 笔记︱范数正则化L0、L1、L2-岭回归Lasso回归(稀疏与特征工程)
  8. Skara 是研究JDK源代码的工具
  9. ai中如何建立阴影_在投资管理中采用AI:公司如何成功建立
  10. 看了一个大牛的博客,发现了一个很好的文章-初学PHP进
  11. pdf论文中visio画的图出现Times New Roman 字体未嵌入
  12. 【java毕业设计】 基于java+SSH+JSP的保险业务管理系统设计与实现(毕业论文+程序源码)——保险业务管理系统
  13. XF660R型号良田高拍仪接口开发,通过图片文件的二进制数据进行图片上传
  14. PCB封装-正片与负片
  15. Python 获取Windows关机消息
  16. pubwin会员合并
  17. 树莓派 查看当前cpu温度
  18. 用python计算绩点的代码_重庆大学GPA计算python程序
  19. iOS:UITableView实现飘带动画
  20. 光电二极管(光敏二极管)应用电路

热门文章

  1. Symfony路由配置教程【已在腾讯课堂开课】
  2. install intel c/c++ compiler
  3. 编写一个程序,将 d:\java 目录下的所有.java 文件复制到 d:\jad 目录下,并将原来文件的扩展名从.java 改为.jad。...
  4. 优秀代码所具备的5大品质 你的代码呢?
  5. VC++网络安全编程范例(2)-创建自签名证书
  6. bzoj5138 [Usaco2017 Dec]Push a Box
  7. 1.PHP与Web页面的交互
  8. 【leetcode-101】 对称二叉树
  9. phpmyadmin安全预防
  10. TSS ESS RSS