Last active
October 20, 2020 21:28
-
-
Save fepegar/77a81c967c92a7c7f0a150cf0152940f to your computer and use it in GitHub Desktop.
Possible bug in SimpleITK or in PyTorch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Untitled8.ipynb", | |
"provenance": [], | |
"authorship_tag": "ABX9TyMpidVlJ6mo6mAgV/r+FOI9", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/fepegar/77a81c967c92a7c7f0a150cf0152940f/untitled8.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "44q_UGrqziY4" | |
}, | |
"source": [ | |
"!pip install --quiet "torchio==0.17.48" | |
], | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "inGkuZt4zlrc", | |
"outputId": "20267eed-b40f-4874-aaed-18154c10aa58", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 105 | |
} | |
}, | |
"source": [ | |
"import torch\n", | |
"import torchio as tio\n", | |
"import SimpleITK as sitk" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"If you use TorchIO for your research, please cite the following paper:\n", | |
"Pérez-García et al., TorchIO: a Python library for efficient loading,\n", | |
"preprocessing, augmentation and patch-based sampling of medical images\n", | |
"in deep learning. Credits instructions: https://torchio.readthedocs.io/#credits\n", | |
"\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "cvfkmpK6zogM", | |
"outputId": "ee56c9d2-8f14-403f-9ee7-a63502208815", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 72 | |
} | |
}, | |
"source": [ | |
"transform = tio.RandomAffine()\n", | |
"ixi = tio.datasets.IXITiny('ixi_tiny', download=True, transform=transform)\n", | |
"\n", | |
"paths = [ixi[i].image.path for i in range(4)]\n", | |
"print(paths)\n", | |
"\n", | |
"class MyDataset(torch.utils.data.Dataset):\n", | |
" def __init__(self, paths):\n", | |
" self.paths = paths\n", | |
" \n", | |
" def __len__(self):\n", | |
" return len(self.paths)\n", | |
" \n", | |
" def __getitem__(self, index):\n", | |
" path = self.paths[index]\n", | |
" image = sitk.ReadImage(str(path))\n", | |
"\n", | |
" resampler = sitk.ResampleImageFilter()\n", | |
" resampler.SetInterpolator(sitk.sitkNearestNeighbor)\n", | |
" resampler.SetReferenceImage(image)\n", | |
" resampler.SetDefaultPixelValue(0.0)\n", | |
" resampler.SetOutputPixelType(sitk.sitkFloat32)\n", | |
" resampled = resampler.Execute(image)\n", | |
"\n", | |
" return sitk.GetArrayFromImage(resampled)\n", | |
"\n", | |
"my_dataset = MyDataset(paths)\n", | |
"\n", | |
"loader_sp = torch.utils.data.DataLoader(my_dataset, batch_size=4, num_workers=0)\n", | |
"loader_mp = torch.utils.data.DataLoader(my_dataset, batch_size=4, num_workers=2)" | |
], | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Root directory for IXITiny found: ixi_tiny\n", | |
"[PosixPath('ixi_tiny/image/IXI002-Guys-0828_image.nii.gz'), PosixPath('ixi_tiny/image/IXI012-HH-1211_image.nii.gz'), PosixPath('ixi_tiny/image/IXI013-HH-1212_image.nii.gz'), PosixPath('ixi_tiny/image/IXI014-HH-1236_image.nii.gz')]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "4LZ1MUJb0CVn", | |
"outputId": "68bb29a5-53bb-4194-a12a-aedd7ecd7c9e", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"batch_sp = next(iter(loader_sp))\n", | |
"print(batch_sp.shape)" | |
], | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"torch.Size([4, 55, 44, 83])\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "WEckhVxE0KHQ" | |
}, | |
"source": [ | |
"batch_mp = next(iter(loader_mp))\n", | |
"print(batch_mp.shape)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code, just in case: