PyTorch DataLoader如何与PyTorch数据集交互以转换批?

2024-04-19 17:06:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为NLP相关任务创建自定义数据集

在PyTorchcustom datast tutorial中,我们看到__getitem__()方法在返回样本之前为转换留出了空间:

def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()

        img_name = os.path.join(self.root_dir,
                                self.landmarks_frame.iloc[idx, 0])
        image = io.imread(img_name)
       
        ### SOME DATA MANIPULATION HERE ###

        sample = {'image': image, 'landmarks': landmarks}
        if self.transform:
            sample = self.transform(sample)

        return sample

但是,此处的代码:

        if torch.is_tensor(idx):
            idx = idx.tolist()

意味着一次应该能够检索多个项目,这让我感到疑惑:

  1. 该转换如何处理多个项目?以本教程中的自定义转换为例。它们看起来不可能在一次调用中应用于一批样本

  2. 相关,如果转换只能应用于单个样本,数据加载器如何并行检索一批多个样本并应用所述转换?


Tags: samplenameimageselfimgifistransform
2条回答
  1. 该转换如何处理多个项目?它们通过使用数据加载器处理多个项目。通过使用转换,您可以指定单个数据发射(例如,batch_size=1)应该发生什么情况。数据加载器接受您指定的batch_size并对torch数据集中的__getitem__方法进行n调用,将转换应用于发送到训练/验证中的每个样本。然后,它将n样本整理成从数据加载器发出的批大小

  2. 相关,如果转换只能应用于单个样本,那么数据加载器如何并行检索一批多个样本并应用所述转换?希望上述内容对您有意义。并行化由torch数据集类和数据加载器完成,其中指定num_workers。Torch将对数据集进行pickle处理,并将其传播给工人

transforms from torchvision的文件中:

All transformations accept PIL Image, Tensor Image or batch of Tensor Images as input. Tensor Image is a tensor with (C, H, W) shape, where C is a number of channels, H and W are image height and width. Batch of Tensor Images is a tensor of (B, C, H, W) shape, where B is a number of images in the batch. Deterministic or random transformations applied on the batch of Tensor Images identically transform all the images of the batch.

这意味着您可以传递一批图像,变换将应用于整个批次,只要它尊重形状。列表索引作用于数据帧中的iloc,数据帧选择单个索引或它们的列表,返回请求的子集

相关问题 更多 >