I am creating a custom dataset and data loader. Unfortunately, when I apply next(iter(dataloader), it gives the following error.
TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found <U4
I am very new to PyTorch (I used to code in Keras). I found that this error is very common if there is no transformation for the samples. However, in my source code, I applied the following transformation for the data before returning it from getitem function.
train_transform = torchvision.transforms.Compose([
torchvision.transforms.ToTensor()
])
I also checked the shape of the sample after the transformation. It seems the shape is as it supposed to be ( torch.Size([1, 16, 132]) ), and the type is also tensor as expected ( <class ‘torch.Tensor’> ).
To confirm that, the data loader has enough items to iterate, I checked its length. It seems the count is quite accurate.
To ensure that it can handle exception automatically, I also tried below try-catch. Unfortunately, no luck yet!
dataloader_iterator = iter(train_loader)
for i in range(iterations):
try:
data, target = next(dataloader_iterator)
except StopIteration:
dataloader_iterator = iter(train_loader)
data, target = next(dataloader_iterator)
So far, my understanding is, whenever I try to use next() function on any iter object, it throws the error.
Below are the full error logs.
File “/root/dataloader.py”, line 71, in get_loader
data, target = next(dataloader_iterator)
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 345, in next
data = self._next_data()
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 385, in _next_data
data = self._dataset_fetcher.fetch(index) # may raise StopIteration
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/_utils/fetch.py”, line 47, in fetch
return self.collate_fn(data)
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/_utils/collate.py”, line 79, in default_collate
return [default_collate(samples) for samples in transposed]
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/_utils/collate.py”, line 79, in
return [default_collate(samples) for samples in transposed]
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/_utils/collate.py”, line 62, in default_collate
raise TypeError(default_collate_err_msg_format.format(elem.dtype))
TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found <U4
Update: (adding custom dataset implementation)
class CustomDataset(Dataset):
def __init__(self, x_data, y_data, transform=None):
self.x_data = x_data
self.y_data = y_data
self.transform = transform
def __getitem__(self, index):
image = self.x_data[index]
image = image[..., np.newaxis]
label = self.y_data[index]
if self.transform is not None:
image = self.transform(image)
return image, label