nabsabs:

` class data(Dataset):
def init(self, img_dir, crop_size=192, voxel_slices=16, shuffle=False):
self.patients = glob(img_dir)
self.crop_size = crop_size
self.slices = 128           # how many slices to take from 155 slice volume
self.voxel_slices = voxel_slices
def __len__(self):
    return len(self.patients)

def __getitem__(self, index):
    vol = np.zeros((1, 240, 240,155))

    path = glob(self.patients[index] + '/*_flair.nii.gz')
    vol = nib.load(path[0]).get_data()
    vol = np.swapaxes(vol,-1,0)  # (240,240,155) -> (155,240,240)        
    if self.crop_size is not None:
        start = vol.shape[-1]//2 - self.crop_size//2
        stop = vol.shape[-1]//2 + self.crop_size//2
        num_chunks = self.slices//self.voxel_slices
        
        #remove blank channels (0-8 and 136-155) and crop to crop_size
        vol = vol[8: 8 + self.slices, start:stop, start:stop]
          
    voxels = np.zeros((num_chunks, self.voxel_slices, self.crop_size, self.crop_size))     # 1, 128//16, 16, 192, 192
        
    for i in range(num_chunks):
        voxels[i,:,:,:] =  vol[i*self.voxel_slices : (i+1)*self.voxel_slices, :, :]
            
    _voxels = torch.from_numpy(voxels).float()    
    _gt = torch.from_numpy(voxels[-1]).long()
    print(_voxels.shape)
    return _voxels, _gt`



should I use this code for my ADNI MRI data for loading the files from directory?
I have nii file format so could it create error to load tham? please someone provide me dataloading code which load the data into my memory in an iterative way.