我正在为 NLP 相关任务创建自定义数据集。
在 PyTorch custom 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()
意味着应该能够一次检索多个项目,这让我想知道:
这种转换如何作用于多个项目?以教程中的自定义转换为例。它们看起来不像可以在一次调用中应用于一批样本。
相关,如果变换只能应用于单个样本,DataLoader 如何并行检索一批多个样本并应用所述变换?