<jupyter_start><jupyter_text>Dataloaders using MedicaltorchIn this notebook we are going to explore a medical imaging open-source library known as [MedicalTorch](https://github.com/perone/medicaltorch), which was built on top of PyTorch. The purpose of this tutorial is how to create a DataLoader using medicaltorch for medical dataset containing data in NIFTI format. For the purpose of this we will be using the Cardiac MRI dataset.The Dataset is available as a part of pastly held [MedicalDecathlon](http://medicaldecathlon.com) and can be downloaded from [here](https://goo.gl/QzVZcm)This tutorial assumes you have installed the medicaltorch library. If you haven't please take a look at [this](https://colab.research.google.com/drive/1V516_8lzebSPw_ygZhJQLfbGedMDikPcscrollTo=bh8sG3huKAK_) notebook  Importing Libraries and Let's Get Started!Let's import the necessary libraries includinf the utility functions from the `medicaltorch` library.<jupyter_code>import numpy as np
import os

from medicaltorch import datasets as mt_datasets
from medicaltorch import transforms as mt_transforms

import torch
from torchvision import transforms
from torch.utils.data import DataLoader
from torch import autograd, optim
import torch.backends.cudnn as cudnn
import torch.nn as nn

import torchvision.utils as vutils

cudnn.benchmark = True
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output>C:\Users\mohit\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters<jupyter_text>Data ExplorationBefore we do any modeling stuff, let's investigate our data first. Let's look at one sample (MRI image) from the dataset. We will see the preprocessing module `mt_datasets.SegmentationPair2D` which is used to read and format the data in a way that we can better explore it in our environment. See the example below.<jupyter_code>ROOT_DIR = "../Datasets/Task02_Heart/"
mri_input_filename = os.path.join(ROOT_DIR,'imagesTr',
                                          'la_004.nii.gz')
mri_gt_filename = "../Datasets/Task02_Heart/labelsTr/la_004.nii.gz"

pair = mt_datasets.SegmentationPair2D(mri_input_filename, mri_gt_filename)
slice_pair = pair.get_pair_slice(55)
input_slice = slice_pair["input"]
gt_slice = slice_pair["gt"]
print(input_slice.shape)<jupyter_output>(320, 320)<jupyter_text>As you can see above, images are 320 X 320 dimensions. We can also view the image in our environment using matplotlib.<jupyter_code>img = input_slice
plt.imshow(img,cmap = 'gray')
plt.show()
img = gt_slice
plt.imshow(img,cmap = 'gray')
plt.show()<jupyter_output><empty_output><jupyter_text>The SegmentationPair2D class gives us a easy-to use wrapper for Analyzing the NIFTI images and thier corresponding segmentation mask.The 2 most frequently used methods are`get_pair_slice` - This method is used to fetch a particular slice from the NIFT image. We can also pass the desired axis (Axial,coronal,Sagittal) to view the image`get_pair_data`- This method is used to fetch the entire image/pixel data and gives us a tuple containing image and mask data<jupyter_code>img_data,seg_data = pair.get_pair_data()
img_data.shape
seg_data.shape<jupyter_output><empty_output><jupyter_text>As you can see we got the entire image and label tensor by using the `get_pair_data` method  Creating a Dataset for 2D segmentation (slice-wise) Next we will see how we can create a Dataset using the NIFTI images. `medicaltorch`  library has a built in generic Dataset called `MRI2DSegmentationDataset` for this purpose.This dataset takes a tuple of input images and thier labels.<jupyter_code>img_list = os.listdir(os.path.join(ROOT_DIR,'imagesTr'))
label_list = os.listdir(os.path.join(ROOT_DIR,'labelsTr'))
filename_pairs = [(os.path.join(ROOT_DIR,'imagesTr',x),os.path.join(ROOT_DIR,'labelsTr',y)) for x,y in zip(img_list,label_list)]
filename_pairs
# load data
train_dataset = mt_datasets.MRI2DSegmentationDataset(filename_pairs,transform=mt_transforms.ToTensor())
type(train_dataset)
print(len(train_dataset))<jupyter_output>2271<jupyter_text>We can check the invidiaul elements of the train_dataset by following<jupyter_code># sample of the training dataset
train_dataset[0]<jupyter_output><empty_output><jupyter_text>The next step is to create a PyTorch Dataloader over this Dataset and let PyTorch do its magic<jupyter_code># PyTorch data loader
dataloader = DataLoader(train_dataset, batch_size=2,collate_fn=mt_datasets.mt_collate)
batch = next(iter(dataloader))
batch<jupyter_output><empty_output><jupyter_text>The minibatch is basically a dictionary with the relevant image and segment data as well as metadata.<jupyter_code># check minibatch size
batch['input'].size()
batch['gt'].size()<jupyter_output><empty_output><jupyter_text>Lets visualize the batches<jupyter_code>for item in batch['input']:
    plt.imshow(item.squeeze(0),cmap = 'gray')
    plt.show()<jupyter_output><empty_output><jupyter_text>Using built in Transformations We will now see how we can use the prebuilt transformations available in `medicaltorch` in creation of data loaders. In addition to the normal torchvision dataloaders there are some specific transforms that we can see `mt_transforms.ElasticTransform` applies both local distortion and random affine transformation. More about this transformation can be found at Simard, <NAME>., <NAME>, and <NAME>. ["Best practices for convolutional neural networks applied to visual document analysis." ICDAR. Vol. 3. 2003.](https://www.microsoft.com/en-us/research/publication/best-practices-for-convolutional-neural-networks-applied-to-visual-document-analysis/)A visualization of the transformation can be also seen [here](https://evizero.github.io/Augmentor.jl/generated/mnist_elastic/footnote-SIMARD2003)<jupyter_code>train_transform = transforms.Compose([
        mt_transforms.Resample(0.25, 0.25),
        mt_transforms.ElasticTransform(alpha_range=(40.0, 60.0),
                                       sigma_range=(2.5, 4.0),
                                       p=0.3),
        mt_transforms.ToTensor()]
)
train_dataset = mt_datasets.MRI2DSegmentationDataset(filename_pairs,transform=train_transform)<jupyter_output><empty_output><jupyter_text>Now lets analyze whether we are able to see the transformation getting applied or not ?<jupyter_code>dataloader = DataLoader(train_dataset, batch_size=2,collate_fn=mt_datasets.mt_collate)
batch = next(iter(dataloader))
for item in batch['input']:
    plt.imshow(item.squeeze(0),cmap = 'gray')
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08320950965824665, 'fraction_numerical': 0.012035661218424963, 'mean_word_length': 5.731, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 5, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '615936', 'license': 'Apache-2.0', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2058, 'n_words': 633, 'path': 'examples/Dataloaders_NIFTI.ipynb', 'repo_name': 'rkalahasty/medicaltorch', 'size': '6750'}