I am trying to do brain tumor segmentation. I need to load my data. How to make customized dataset of a brain images ( .nii)?

I am trying to build the customized dataset for brain image. I have two folders

  1. HGG
  2. LGG
    In each folder we have 5 MRI images including Flair, t1, t1c, t2 and a labeled image.
    How to create custom dataset. Your help in this situation is highly appreciable. Thank you

This tutorial might be hellful.
In case you get stuck, could you add more information about your data and how each sample would be loaded?

You can create your own dataset class by subclassing a dataset like so (minimal example):

class Lazy_Dataset(torch.utils.data.Dataset):

    def __init__(self, path):
        self.steps = [function that figures out the size of your dataset located in the path]
        self.path = path

    def __len__(self):
        return self.steps
    
    def __getitem__(self, idx):
        array = [function that loads your nifti file as a numpy array]
        data = torch.Tensor(array)
        label = [function that figures out your label]
        return data, label, idx

You can later create an instance of that dataset (and a dataloader) by calling:

mydataset = Lazy_Dataset(mypath)
mydataloader = torch.utils.data.DataLoader(mydataset, batch_size=24, shuffle=True)

Thank you for your kind response. Here What i did so far. Can you please highlight the mistake.
Here is my code and folder organization.
(upload://pr9pSRiQKjIGy9X03dr2BAlfY4F.png) ![Presentation4|690x388]

patient_dir = “D:\Brats2020\”
l_patnt =os.listdir(patient_dir)
#print(l_patnt)

class Brats2022(Dataset):
def init(self,patient_dir):
self.patient_dir = patient_dir

def __len__(self):
    return len(self.patient_dir)

def __getitem__(self, index):
    #return self.patient_dir[index]
    patient_dir = self.patient_dir[index]
    volumes = []
    patient_id = os.path.split(patient_dir)[-1]
    volume_path = os.path.join(patient_dir,patient_id + "_" + "seg"+".nii.gz")
    volume_seg = nib.load(volume_path)
    volu_im = volume_seg.get_fdata()
    np_arr = np.array(volu_im)
    data_transform = transforms.ToTensor()
    return data_transform(np_arr)