TypeError: 'tuple' object cannot be interpreted as an integer while creating data generators PyTorch
TypeError: 'tuple' object cannot be interpreted as an integer while creating data generators PyTorch
我是 PyTorch 的新手,我正在学习创建用于分割的批量数据。代码如下所示:
class NumbersDataset(Dataset):
def __init__(self):
self.X = list(df['input_img'])
self.y = list(df['mask_img'])
def __len__(self):
return len(self.X), len(self.y)
def __getitem__(self, idx):
return self.X[idx], self.y[idx]
if __name__ == '__main__':
dataset = NumbersDataset()
dataloader = DataLoader(dataset, batch_size=50, shuffle=True, num_workers=2)
# print(len(dataset))
# plt.imshow(dataset[100])
# plt.show()
print(next(iter(dataloader)))
其中 df['input_img']
列包含图像 ('/path/to/pic/480p/boxing-fisheye/00010.jpg'
) 的位置,df['mask_img']
包含所有蒙版图像的位置。我正在尝试加载图像,但出现错误:
TypeError: 'tuple' object cannot be interpreted as an integer
但是,如果我不使用 DataLoader
而只是执行以下操作:
dataset = NumbersDataset()
print(len(dataset))
print(dataset[10:20])
然后我得到了我所期望的。有人可以告诉我我做错了什么吗?
您不能 return tuple
用于 __len__
方法。预期的类型是 int
# perhaps you can add the list length's for the total length
# but no matter how you choose to implement the method you can
# only return on value of type integer `int`
def __len__(self):
return len(self.X) + len(self.y)
我是 PyTorch 的新手,我正在学习创建用于分割的批量数据。代码如下所示:
class NumbersDataset(Dataset):
def __init__(self):
self.X = list(df['input_img'])
self.y = list(df['mask_img'])
def __len__(self):
return len(self.X), len(self.y)
def __getitem__(self, idx):
return self.X[idx], self.y[idx]
if __name__ == '__main__':
dataset = NumbersDataset()
dataloader = DataLoader(dataset, batch_size=50, shuffle=True, num_workers=2)
# print(len(dataset))
# plt.imshow(dataset[100])
# plt.show()
print(next(iter(dataloader)))
其中 df['input_img']
列包含图像 ('/path/to/pic/480p/boxing-fisheye/00010.jpg'
) 的位置,df['mask_img']
包含所有蒙版图像的位置。我正在尝试加载图像,但出现错误:
TypeError: 'tuple' object cannot be interpreted as an integer
但是,如果我不使用 DataLoader
而只是执行以下操作:
dataset = NumbersDataset()
print(len(dataset))
print(dataset[10:20])
然后我得到了我所期望的。有人可以告诉我我做错了什么吗?
您不能 return tuple
用于 __len__
方法。预期的类型是 int
# perhaps you can add the list length's for the total length
# but no matter how you choose to implement the method you can
# only return on value of type integer `int`
def __len__(self):
return len(self.X) + len(self.y)