Trouble in making dataloaders

887574002: <p>Hi, I am trying to make data loaders from a medical image dataset. I wrote a custom dataset class and I passed the transformations. for each image in the dataset, I want to have two augmented version, that’s why  I use an auxiliary class where it returns two augmented version of each image. The data set is working correctly. But when I try to make data loaders using DataLoaders in PyTorch it returns two images. I mean each batch includes two images. It seems it can not batch the images that it has already made. Here is my code. I would be thankful if you help me to correct this code.</p>
<pre><code class="lang-auto">    
    def __init__(self, transform):
        
        self.transform = transform

    def __call__(self, vox):
        
        voxi = self.transform(vox)
        voxj = self.transform(vox)
        
        return(voxi,voxj)
</code></pre>
<p>Here is the main custom dataset</p>
<pre><code class="lang-auto">class NPDataSet(Dataset):
    
    
    def __init__(self,root_dir, transform=None):
        
        self.root_dir = root_dir
        
        VolList = sorted([vocname for vocname in os.listdir(root_dir)])
        
        # path to each volume
        self.pathvol = sorted([os.path.join(self.root_dir,vol) for vol in VolList])
        
        self.transform = transform
        
        self.len = len(VolList)
        
        
    def __len__(self):
        
        return(self.len)
    
    
    def __getitem__(self,index):
        
         the shape is in the form (depth,highet,width,channel)
        
        self.vol = np.load(self.pathvol[index])
        print(self.pathvol[index])
        
         checking to see if it is in the form (channel,deoth,hight,width)
        
        assert len(self.vol.shape)==4
        
         Now we transpose the axis if the order is in the shape (depth,hight,width,channel)
        
        if self.vol.shape[-1] ==1:
            
            self.vol = np.transpose(self.vol,(3,0,1,2))
        
         applying transformations

        if self.transform:
            
            aug  = AguPairVox(self.transform)
            
            self.vol = aug(self.vol)
  
        return(self.vol)      
</code></pre>
<p>making data loader</p>
<pre><code class="lang-auto">train_loader = DataLoader(train_set, batch_size=15, drop_last=False, shuffle=False)
</code></pre>
<p>Now when checking the length of batch</p>
<pre><code class="lang-auto">for batch in train_loader:
    
    print(len(batch))
    
    break

2
</code></pre>

ptrblck: <p>I assume <code>self.vol</code> is containing both images?<br>
If so, try to return both tensors separately as:</p>
<pre><code class="lang-python">return self.vol[0], self.vol[1]
</code></pre>
<p>and rerun the script again.</p>

887574002: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>, thank you. I did it and it still returns a batch of size 2!</p>

harsha_g: <p><a class="mention" href="/u/887574002">@887574002</a> I am trusting you verified the elements of the batch to be images. But, just in case, is it possible that the batch returns a tuple (of size 2) where each element is a tensor of shape <code>batch_size x shape_of_transformed_image</code>?</p>

887574002: <p>Hi, sorry. That’s the mistake that I made. You are right. Each batch is a tuple that each element of this tuple is a tensor of shape [batch_size, h,w,d]. Wrongly, I thought the length of batch should be the same as the batch_size that we pass to the data loader. Thanks for the point.</p>