Return a SimpleITK.SimpleITK.Image from dataloader

Hello PyTorch Community!
I want to return a SimpleITK.SimpleITK.Image from my data loader as my model needs it at some stage, I am getting following error.
TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found <class ‘SimpleITK.SimpleITK.Image’>

with this code

def __getitem__(self, index):
    img_path = os.path.join(self.images_folder, str(self.images_name[index]).zfill(3),str(self.images_name[index]).zfill(3))
    img_SA_path = img_path+'_SA_ES.nii.gz'
    img_SA_ES_itk = sitk.ReadImage(img_SA_path)  
    return img_SA_ES_itk 


As the error message describes you would need to transform your SimpleITK.Image to a tensor or another data type PyTorch understands. You could alternatively also create a custom collate_fn, which could deal with your data type, but would eventually still need to transform it to a tensor before feeding it into a model.

Thanks for the reply. I cannot convert because the formation related to spacing, angles, and directions, and some related parameters are lost, and my model uses this information at a later stage.
As an alternative, I am returning the path of SimpleITK.SimpleITK.Image and later will read them during the training/inference stage.


Wouldn’t it be possible to return the transformed image as a tensor with all needed metadata together?
At one point you would still need to transform the image, so I’m unsure why not in the __getitem__ method to avoid the error.