DataLoader iterator is not working for Custom Dataset

username: mirzapinku
cooked: <p>I am creating a custom dataset and data loader. Unfortunately, when I apply next(iter(dataloader), it gives the following error.</p>
<blockquote>
<p>TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found &lt;U4</p>
</blockquote>
<p>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 <strong>getitem</strong> function.</p>
<blockquote>
<p>train_transform = torchvision.transforms.Compose([<br>
torchvision.transforms.ToTensor()<br>
])</p>
</blockquote>
<p>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  ( &lt;class ‘torch.Tensor’&gt; ).</p>
<p>To confirm that, the data loader has enough items to iterate, I checked its length. It seems the count is quite accurate.</p>
<p>To ensure that it can handle exception automatically, I also tried below try-catch. Unfortunately, no luck yet!</p>
<blockquote>
<p>dataloader_iterator = iter(train_loader)<br>
for i in range(iterations):<br>
try:<br>
data, target = next(dataloader_iterator)<br>
except StopIteration:<br>
dataloader_iterator = iter(train_loader)<br>
data, target = next(dataloader_iterator)</p>
</blockquote>
<p>So far, my understanding is, whenever I try to use next() function on any iter object, it throws the error.<br>
Below are the full error logs.</p>
<blockquote>
<p>File “/root/dataloader.py”, line 71, in get_loader<br>
data, target = next(dataloader_iterator)<br>
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 345, in <strong>next</strong><br>
data = self._next_data()<br>
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 385, in _next_data<br>
data = self._dataset_fetcher.fetch(index)  # may raise StopIteration<br>
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/_utils/fetch.py”, line 47, in fetch<br>
return self.collate_fn(data)<br>
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/_utils/collate.py”, line 79, in default_collate<br>
return [default_collate(samples) for samples in transposed]<br>
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/_utils/collate.py”, line 79, in <br>
return [default_collate(samples) for samples in transposed]<br>
File “/root/.virtualenvs/pytorchve/lib/python3.5/site-packages/torch/utils/data/_utils/collate.py”, line 62, in default_collate<br>
raise TypeError(default_collate_err_msg_format.format(elem.dtype))<br>
TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found &lt;U4</p>
</blockquote>
<p>Update: (adding custom dataset implementation)</p>
<p>class CustomDataset(Dataset):</p>
<pre><code> 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
</code></pre>

username: AladdinPerzon
cooked: <p>Could you show your custom dataset implementation and specifically the <code>__getitem__</code>?</p>

username: mirzapinku
cooked: <p>Thank you for the reply. I updated the topic description, and added custom dataset implementation code.</p>

username: AladdinPerzon
cooked: <p>I don’t see what could be causing the issue. Could you provide some code that produces the error?</p>

username: mirzapinku
cooked: <p>Thank you again. Basically, whenever I try to iterate the DataLoader object, I get this error. Following code reproduce the issue -</p>
<p>train_transform = torchvision.transforms.Compose([<br>
torchvision.transforms.ToTensor()<br>
])<br>
train_dataset = CustomDataset(x_train, y_train, transform=train_transform)<br>
test_dataset = CustomDataset(x_test, y_test, transform=train_transform)<br>
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=False)<br>
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)<br>
dataloader_iterator = iter(train_loader)</p>
<pre><code> images, labels = dataloader_iterator.next() // the error throws as soon as this line execute</code></pre>

username: AladdinPerzon
cooked: <p>Sorry if I wasn’t clear but it would be helpful to debug if you could provide a small example that I can run which produces the error. It seems that <code>__getitem__</code> returns something odd and I’m guessing it would be the label but it’s hard to debug without any sample code.</p>

username: mirzapinku
cooked: <p>[Update]<br>
Converting label to int8 solved this issue. Thank you. I am accepting your earlier comments as solution.</p>
<p>Below is the sample getitem for a single index.</p>
<p>getitem tensor([[[0., 0., 0.,  …, 0., 0., 0.],<br>
[0., 0., 0.,  …, 0., 0., 0.],<br>
[0., 0., 0.,  …, 0., 0., 0.],<br>
…,<br>
[0., 0., 0.,  …, 0., 0., 0.],<br>
[0., 0., 0.,  …, 0., 0., 0.],<br>
[0., 0., 0.,  …, 0., 0., 0.]]], dtype=torch.float64) label [‘1’]</p>
<p>One more thing I would like to add. Here, batch_size is 16. I kept the printing for each index. It seems that, get_item is called 16 times (prints all 16 tensors), then the issue occurs. Probably, when it wants to access the second batch, it can not do that and gives the error.</p>