DataLoader doesn't add batch size

username: realtm
cooked: <p>Hello,</p>
<p>i wrote my own Dataset and tried to put it in DataLoader. All seems to work, but the loaded data doesn’t get the batch size.</p>
<p>I have a 3x64x64 RGB image and a 1x64x64 grayscale image and concatenate them in my Dataset to get a 4x64x64. After using the Dataloader the output should have the shape 64x4x64x64 (batchsize=64) but it still has 4x64x64. Any suggestions or ideas?</p>
<pre><code class="lang-auto">class MyDataset(Dataset):
    def __init__(self, path_grain, path_mask, transform=None):
        self.data_grain = dset.ImageFolder(
            root=path_grain, transform=transforms.Compose([transforms.ToTensor(), 
                                                         transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),]))
        self.data_mask = dset.ImageFolder(root=path_mask, 
                                 transform=transforms.Compose([transforms.Grayscale(num_output_channels=1), 
                                 transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]))
        
    def __getitem__(self, index):
        x_grain, y = self.data_grain[index]
        x_mask, _ = self.data_mask[index]
        x = torch.cat((x_grain, x_mask), dim=0)
        return x
    
    def __len__(self):
        return len(self.data_grain)




dataset = MyDataset("..\data512to64_grain", "..\data512to64_mask")

dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,
                                            shuffle=True, num_workers=int(opt.workers))
</code></pre>
<p>Greetings:)</p>

username: ptrblck
cooked: <p>Your code works fine using dummy data:</p>
<pre><code class="lang-python">class MyDataset(Dataset):
    def __init__(self):
        self.data_grain = torch.randn(100, 3, 64, 64)
        self.data_mask = torch.randn(100, 1, 64, 64)
        
    def __getitem__(self, index):
        x_grain = self.data_grain[index]
        x_mask = self.data_mask[index]
        x = torch.cat((x_grain, x_mask), dim=0)
        return x
    
    def __len__(self):
        return len(self.data_grain)

dataset = MyDataset()

dataloader = torch.utils.data.DataLoader(
    dataset, batch_size=64, shuffle=True, num_workers=0)

data = next(iter(dataloader))
print(data.shape)
</code></pre>
<p>Could you check the <code>opt.batchSize</code> again?<br>
Also, could you print the shape of the output of <code>dataset.data_grain[0]</code> as well as <code>dataset.data_mask[0]</code>?</p>

username: realtm
cooked: <p>Heres the output.</p>
<pre><code class="lang-auto">len(dataset.data_grain[0]): 2
len(dataset.data_mask[0]): 2
(dataset.data_grain[0][0]).shape
torch.Size([3, 64, 64])
(dataset.data_mask[0][0]).shape
torch.Size([1, 64, 64])
batchsize: 064
</code></pre>
<p>dataset.data_grain[0] is a tuple</p>
<p>I took a look at another example of you, and there was a returning y in the get_item function.<br>
I put it in as well,  and the code worked correct after…</p>
<pre><code class="lang-auto">def __getitem__(self, index):
        x_grain, y = self.data_grain[index]
        x_mask, _ = self.data_mask[index]
        x = torch.cat((x_grain, x_mask), dim=0)
        return x, y
</code></pre>