Batch size is always 1

username: mhong94
cooked: <p>No matter what I put for batch_size, the batch_size defaults to 1.  Here is my code</p>
<pre><code>train_dataset = DataLoader(dataset=dataset,
                       batch_size=4,
                       shuffle=True,
                       num_workers=0)
</code></pre>
<p>and dataset is a custom dataset as follows</p>
<pre><code>class ImageDataset(data.Dataset):

def __init__(self, root_dir, num_augments=2, transform=None):
    
    self.root_dir = root_dir
    self.img_names = os.listdir(root_dir)[::600]
    self.num_augments = num_augments
    self.transform = transform
    
def __getitem__(self, index):
    
    output = []
    img = Image.open(self.root_dir + '/' + self.img_names[index]).convert('RGB')
        
    for i in range(self.num_augments):
        if self.transform is not None:
            img_transform = self.transform(img)
            
        output.append(img_transform)
        
    output = torch.stack(output, axis=0)
        
    return output
        
def __len__(self):
    
    return len(self.img_names)
</code></pre>
<p>I am calling my dataset in the form</p>
<pre><code>for i,images in enumerate(train_dataset):
</code></pre>
<p>I am expecting images to be of size [batch_size, num_augments, 3, height, width], but I am getting [1, num_augments, 3, height, width] regardless of my batch size.</p>

username: albanD
cooked: <p>Hi,</p>
<p>Are you sure you’re iterating over the dataloader and not the dataset?</p>

username: mhong94
cooked: <p>Yep.  The dataset and dataloader code looks as follows:</p>
<pre><code>dataset = ImageDataset(root_dir = image_dir,
                   num_augments = num_augments,
                   transform = augment_image())

train_dataset = DataLoader(dataset=dataset,
                       batch_size=batch_size,
                       shuffle=True,
                       num_workers=0)
</code></pre>
<p>and then I am iterating through train_dataset</p>

username: albanD
cooked: <p>I can’t reproduce that…<br>
Can you share a full example (maybe with a TensorDataset with random data) that reproduces this?</p>

username: mhong94
cooked: <p>Here is a running piece of code</p>
<pre><code>class ImageDataset(data.Dataset):

def __init__(self, root_dir, num_augments=2, transform=None):
    
    self.root_dir = root_dir
    self.img_names = os.listdir(root_dir)[::600]
    self.num_augments = num_augments
    self.transform = transform
    
def __getitem__(self, index):
    
    output = []
    
    for i in range(self.num_augments):
        img_transform = torch.randn(3,224,224)
        
        output.append(img_transform)
        
    output = torch.stack(output, axis=0)
        
    return output
        
def __len__(self):
    
    return len(self.img_names)

dataset = ImageDataset(root_dir = '',
                   num_augments = 10,
                   transform = None)

train_dataset = DataLoader(dataset=dataset,
                       batch_size=5,
                       shuffle=True,
                       num_workers=0)

for i,images in enumerate(train_dataset):
    test_shape = images.shape
</code></pre>
<p>I believe I should be getting [5,10,3,224,224], but getting [1,10,3,224,224]</p>

username: albanD
cooked: <p>And what is the size of the dataset? Doesn’t it contain a single element?</p>

username: mhong94
cooked: <p>Ahh I realized I made a dummy folder with a small dataset and forgot to swap the folder names again.  Thanks!</p>