Why my dataloader swaps the batch size to the 2nd dimension automatically

STRE_RING: <p>I am preparing data for an RNN, the data point in the dataset is a list containing 11 RGB images, thus the dimension of “out” in the following is 11 * 3 * 224 * 224:</p>
<p>def <strong>getitem</strong>(self,index):</p>
<pre><code>    topk_s = self.tops[index]
    if self.shuffle:
        topk_s = random.sample(topk_s,len(topk_s))

    out = []
    
    for i in topk_s:
        img = Image.open(os.path.join(self.slides, self.grid[i])).convert('RGB')
        if self.mult != 1:
            img = img.resize((224,224), Image.BILINEAR)
        if self.transform is not None:
            img = self.transform(img)
        out.append(img)
    
    return out
</code></pre>
<p>the dimension of dataset is:<br>
(614, 11, 3, 224, 224)</p>
<p>But after making a dataloader, the batch size (128) is swapped to 2nd dimension, why would this happen?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/d/0d65f96d9aad26b0ee36e1c9c2454281926b0bc2.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/0d65f96d9aad26b0ee36e1c9c2454281926b0bc2" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/d/0d65f96d9aad26b0ee36e1c9c2454281926b0bc2.jpeg" alt="image" data-base62-sha1="1UwGxoBGYHQoLNjleEsgk6HX7zk" width="476" height="500" data-dominant-color="202121"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">491×515 48.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Thank you!</p>

ptrblck: <p>I cannot reproduce the issue and get the expected shapes:</p>
<pre><code class="lang-python">class MyDataset(Dataset):
    def __init__(self):
        pass
    
    def __len__(self):
        return 614
    
    def __getitem__(self, index):
        out = torch.randn(11, 3, 224, 224)
        return out
    
dataset = MyDataset()
x = dataset[0]
print(x.shape)
# torch.Size([11, 3, 224, 224])

loader = DataLoader(dataset, batch_size=128)
for data in loader:
    print(data.shape)
# torch.Size([128, 11, 3, 224, 224])
# torch.Size([128, 11, 3, 224, 224])
# torch.Size([128, 11, 3, 224, 224])
# torch.Size([128, 11, 3, 224, 224])
# torch.Size([102, 11, 3, 224, 224])
</code></pre>

STRE_RING: <p>Thank you! I learned a lot from your answer to simplify a tricky question!</p>
<p>class MyDataset(data.Dataset):</p>
<pre><code>def __init__(self):
    pass

def __len__(self):
    return 614

def __getitem__(self, index):
    out = []
    for i in range(11):
        out.append(torch.randn(3, 224, 224))
    return out
</code></pre>
<p>dataset = MyDataset()<br>
x = dataset[0]<br>
print(len(x),len(x[0]),len(x[0][0]),len(x[0][0][0]))</p>
<p>loader = torch.utils.data.DataLoader(dataset, batch_size=128)<br>
for i, data1 in enumerate(loader):<br>
print(len(data1),len(data1[0]),len(data1[0][0]),len(data1[0][0][0]),len(data1[0][0][0][0]))</p>
<p>The answer is:<br>
11 3 224 224<br>
11 128 3 224 224<br>
11 128 3 224 224<br>
11 128 3 224 224<br>
11 128 3 224 224<br>
11 102 3 224 224</p>
<p>But I am still not sure if DataLoader swapped the 2 dimensions automatically for convenience of users to train RNN.</p>

ptrblck: <p>If you are returning a <code>list</code> from the <code>__getitem__</code> PyTorch will keep it and <code>x</code> as well as <code>data1</code> will both be <code>list</code>s containing the tensors. If you <code>out = torch.stack(out)</code> the <code>list</code> in <code>__getitem__</code> the same output shapes seem in my code snippet will be returned.</p>

STRE_RING: <p>You are right. Thank you!</p>
<p>I have confirmed that data1 gives lists with swapped dimensions.</p>