我試圖在每次迭代時(shí)打印圖像名稱。但是,我收到錯(cuò)誤 TypeError: 'ToTensor' object is not iterable。請(qǐng)告訴我我要去哪里?非常感謝from torchvision import datasetsimport torch.utils.datafrom torch.utils.data import DataLoaderfrom torchvision import transformsfrom dataset2 import CellsDatasetfrom torchvision import datasetsimport torchimport torchvisionimport torchvision.transforms as transformsclass ImageFolderWithPaths(datasets.ImageFolder): """Custom dataset that includes image file paths. Extends torchvision.datasets.ImageFolder """# override the __getitem__ method. this is the method that dataloader callsdef __getitem__(self, index): # this is what ImageFolder normally returns original_tuple = super(ImageFolderWithPaths, self).__getitem__(index) # the image file path path = self.imgs[index][0] # make a new tuple that includes original and the path tuple_with_path = (original_tuple + (path,)) return tuple_with_path# EXAMPLE USAGE:# instantiate the dataset and dataloaderdata_dir = "/Users/nubstech/Documents/GitHub/CellCountingDirectCount/Eddata/"dataset = ImageFolderWithPaths(data_dir) # our custom dataset#dataloader = DataLoader(dataset)transform = transforms.Compose([ # you can add other transformations in this list transforms.ToTensor()])dataset = DataLoader(data_dir, transforms.Compose(transforms.ToTensor()))dataloader = torch.utils.DataLoader(dataset)# iterate over datafor inputs, labels, paths in dataloader: # use the above variables freely print(inputs, labels, paths)
1 回答

MMMHUHU
TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊
這是因?yàn)?code>transforms.Compose()需要是一個(gè)列表(可能也接受了其他一些迭代)。問(wèn)題在這里:
dataset = DataLoader(data_dir, transforms.Compose(transforms.ToTensor()))
嘗試:
transforms = transforms.Compose([transforms.ToTensor()])
這將創(chuàng)建一個(gè)可調(diào)用對(duì)象,您可以在其中傳遞數(shù)據(jù)。
添加回答
舉報(bào)
0/150
提交
取消