Hello all, I have 10 MRI image type of .nii with size of 128x256x256 (CxHxW)
. I have converted it to hdf5 files such as
1.h5
2.h5
...
9.h5
10.h5
I am using pytorch 0.4 to create a dataloader (read .h5 files). The dataloader has to randomly read the h5 files with crop size of 64x64x64
. Could you tell me the steps to do it? This is my current solution
import h5py
import torch
import torch.utils.data as data
import glob
import os
#BCHW order
class H5Dataset(data.Dataset):
def __init__(self, root_path, crop_size=(64,64,64)):
self.hdf5_list = [x for x in glob.glob(os.path.join(root_path, '*.h5'))]
self.crop_size = crop_size
def __getitem__(self, index):
h5_file = h5py.File(self.hdf5_list[index])
self.data = h5_file.get('data')
self.target = h5_file.get('label')
return (torch.from_numpy(self.data[index,:,:,:,:]).float(),
torch.from_numpy(self.target[index,:,:,:,:]).float())
def __len__(self):
return len(self.hdf5_list)
if __name__ == '__main__':
mri_Data = H5Dataset("./data")
trainloader = data.DataLoader(mri_Data, batch_size=4)
for i, data in enumerate(trainloader):
imgs, labels = data