Data loader unexpected behaviour

elidor22: <p>Currently working on a custom dataloader and it is showing an unexpected behavior. If the dataloader is called as an enumerator and the values are received using next(enumerate(dataloader)) it will work without any issues. However if called in a loop than the problems start.<br>
The dataset code is as follows:</p>
<pre><code class="lang-auto">class CustomDataset(Dataset):
    def __init__(self, csv_path):
        self.paths_df = pd.read_csv(csv_path)

    def __len__(self):
        return len(self.paths_df)

    def __getitem__(self, idx):
        
        id = self.paths_df.id[idx]
        target =  y = np.float32(self.paths_df.target[idx])
        filename = f'../input/g2net-detecting-continuous-gravitational-waves/train/{id}.hdf5'
        img = np.empty((2, 360, 128), dtype=np.float32)
        
        with h5py.File(filename, 'r') as f:
            g = f[id]

            for ch, s in enumerate(['H1', 'L1']):
                a = g[s]['SFTs'][:, :4096] * 1e22  # Fourier coefficient complex64

                p = a.real**2 + a.imag**2  # power
                p /= np.mean(p)  # normalize
#                 print(p.shape)
                p = np.mean(p.reshape(360, 128, 32), axis=2)  # compress 4096 -&gt; 128
                img[ch] = p

            
#         print(img.shape)
        return img, y
</code></pre>
<p>The following code section works without any issue:</p>
<pre><code class="lang-auto">dataset = CustomDataset('path_to_csv/file.csv')
train_dataloader = torch.utils.data.DataLoader(dataset, batch_size=1 ,shuffle=True, num_workers=4)
loop = enumerate(train_dataloader)
idx, data = next(loop)
</code></pre>
<p>However if the train_dataloader is called in a loop as follows:</p>
<pre><code class="lang-auto">train_dataloader1 = torch.utils.data.DataLoader(dataset, batch_size=32 ,shuffle=False, num_workers=2)
dataiter = iter(train_dataloader1) # creating a iterator

for data, label in train_dataloader1:
    print(label)

</code></pre>
<p>It will give an error as showed below:<br>
ValueError: Caught ValueError in DataLoader worker process 0.<br>
Original Traceback (most recent call last):<br>
File “/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py”, line 287, in _worker_loop<br>
data = fetcher.fetch(index)<br>
File “/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py”, line 49, in fetch<br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py”, line 49, in <br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/tmp/ipykernel_27/4159678793.py”, line 24, in <strong>getitem</strong><br>
p = np.mean(p.reshape(360, 128, 32), axis=2)  # compress 4096 → 128<br>
ValueError: cannot reshape array of size 258480 into shape (360,128,32)</p>
<p>The issue comes from the array reshaping part and occurs only when calling the dataloader in a loop as then it returns a batch which most likely makes the reshape operation fail. How can this issue be avoided and why does makes it happen(as it seems specific to the way the Pytorch handles the dataloader).</p>

srishti-git1110: <p>Hi Elidor,</p>
<aside class="quote no-group" data-username="elidor22" data-post="1" data-topic="164531">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/898d66/48.png" class="avatar"> elidor22:</div>
<blockquote>
<p><code>print(p.shape)</code></p>
</blockquote>
</aside>
<p>Could you please uncomment this and post the shape of <code>p</code> as I’m not sure why batching would cause this error.<br>
I would think the items are first fetched from the <code>__getitem__</code> method (subsequent calls are made) and then batched together before returning as a batch. I might be wrong here so I’ll cross check the source code of <code>DataLoader</code> meanwhile.</p>

elidor22: <p>It seems that the code is fine. The dataset contains some data that has a different shape. That issue gives the error above as the code doesn’t take into account the possibility of different size tensors(or np arrays as used above).  So at the end this is not a Pytorch issue, and the code runs well using next() as it doesn’t get to process the wrong shaped array.</p>