PyTorch TypeError: 'ToTensor' object is not iterable
I'm trying to print an image name at each iteration. However, I'm getting an error TypeError: 'ToTensor' object is not iterable. Please could some advise where I am going wring please? Many Thanks
from torchvision import datasets
import torch.utils.data
from torch.utils.data import DataLoader
from torchvision import transforms
from dataset2 import CellsDataset
from torchvision import datasets
import torch
import torchvision
import torchvision.transforms as transforms


class ImageFolderWithPaths(datasets.ImageFolder):
    &quot;&quot;&quot;Custom dataset that includes image file paths. Extends
    torchvision.datasets.ImageFolder
    &quot;&quot;&quot;

# override the __getitem__ method. this is the method that dataloader calls
def __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 dataloader
data_dir = &quot;/Users/nubstech/Documents/GitHub/CellCountingDirectCount/Eddata/&quot;
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 data
for inputs, labels, paths in dataloader:
    # use the above variables freely
   print(inputs, labels, paths)


Traceback Message:
Traceback (most recent call last):
  File &quot;file_location2.py&quot;, line 37, in &lt;module&gt;
    dataset = DataLoader(data_dir, transforms.Compose(transforms.ToTensor()))
  File &quot;/Users/nubstech/opt/anaconda3/envs/Cells_Counting/lib/python3.7/site-packages/torch/utils/data/dataloader.py&quot;, line 219, in __init__
    batch_sampler = BatchSampler(sampler, batch_size, drop_last)
  File &quot;/Users/nubstech/opt/anaconda3/envs/Cells_Counting/lib/python3.7/site-packages/torch/utils/data/sampler.py&quot;, line 190, in __init__
    &quot;but got batch_size={}&quot;.format(batch_size))
  File &quot;/Users/nubstech/opt/anaconda3/envs/Cells_Counting/lib/python3.7/site-packages/torchvision/transforms/transforms.py&quot;, line 66, in __repr__
    for t in self.transforms:
TypeError: 'ToTensor' object is not iterable


It's because transforms.Compose() needs to be a list (probably some other iterables are accepted too). The problem is here:
dataset = DataLoader(data_dir, transforms.Compose(transforms.ToTensor()))

Try:
transforms = transforms.Compose([transforms.ToTensor()])

This will create a callable in which you can pass your data.
