Skip to content

Instantly share code, notes, and snippets.

@fepegar
Last active October 20, 2020 21:28
Show Gist options
  • Save fepegar/77a81c967c92a7c7f0a150cf0152940f to your computer and use it in GitHub Desktop.
Save fepegar/77a81c967c92a7c7f0a150cf0152940f to your computer and use it in GitHub Desktop.
Possible bug in SimpleITK or in PyTorch
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@fepegar
Copy link
Author

fepegar commented Oct 20, 2020

The code, just in case:

# -*- coding: utf-8 -*-
"""Untitled8.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1GzO2RQYn7jI26frWS8S-YlbSmjFAehWa
"""

# pip install --quiet torchio==0.17.48

import torch
import torchio as tio
import SimpleITK as sitk

transform = tio.RandomAffine()
ixi = tio.datasets.IXITiny('ixi_tiny', download=True, transform=transform)

paths = [ixi[i].image.path for i in range(4)]
print(paths)


class MyDataset(torch.utils.data.Dataset):
    def __init__(self, paths):
        self.paths = paths

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

    def __getitem__(self, index):
        path = self.paths[index]
        image = sitk.ReadImage(str(path))

        resampler = sitk.ResampleImageFilter()
        resampler.SetInterpolator(sitk.sitkNearestNeighbor)
        resampler.SetReferenceImage(image)
        resampler.SetDefaultPixelValue(0.0)
        resampler.SetOutputPixelType(sitk.sitkFloat32)
        resampled = resampler.Execute(image)

        return sitk.GetArrayFromImage(resampled)

my_dataset = MyDataset(paths)

loader_sp = torch.utils.data.DataLoader(my_dataset, batch_size=4, num_workers=0)
loader_mp = torch.utils.data.DataLoader(my_dataset, batch_size=4, num_workers=2)

batch_sp = next(iter(loader_sp))
print(batch_sp.shape)

batch_mp = next(iter(loader_mp))
print(batch_mp.shape)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment