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.
def __init__(self, transform):
self.transform = transform
def __call__(self, vox):
voxi = self.transform(vox)
voxj = self.transform(vox)
return(voxi,voxj)
Here is the main custom dataset
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)
making data loader
train_loader = DataLoader(train_set, batch_size=15, drop_last=False, shuffle=False)
Now when checking the length of batch
for batch in train_loader:
print(len(batch))
break
2