NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

TorchIO: A Python library for efficient loading, preprocessing, augmentation and patch-based sampling of medical images in deep learning

Fernando Pérez-García, Rachel Sparks, Sébastien OurselinarXiv 2020 · DOI

TL;DR

TorchIO is an open-source Python library designed to bridge the gap between deep learning frameworks like PyTorch and 3D medical imaging. It provides efficient, production-ready tools for data loading, preprocessing, spatial/intensity augmentation, and patch-based sampling while preserving critical physical metadata.

What Problem Does This Paper Address?

Standard computer vision libraries (such as TorchVision or Albumentations) excel at processing 2D RGB images but fall short when applied to 3D medical images like MRI, CT, or ultrasound scans. Volumetric medical data introduces unique challenges:

  • Massive Computational Footprint: A single high-resolution 3D scan can contain hundreds of millions of voxels, easily triggering out-of-memory (OOM) errors if loaded directly into a GPU.
  • Crucial Spatial Metadata: Medical images are not just pixel arrays; they exist in a physical 3D coordinate system defined by metadata specifying voxel spacing, origin, and orientation. Ignoring this metadata leads to severe spatial misalignment during multi-modal training or inference.
  • Data Scarcity: Labeled medical datasets are notoriously small due to patient privacy concerns and the high cost of expert annotation, making robust data augmentation essential.

TorchIO addresses these gaps by providing a standardized, metadata-aware pipeline tailored specifically for deep learning workflows in medical imaging.

Figure 1: Demonstration of the importance of spatial metadata in medical image processing. The size of both the MRI and the segmentation is 181 × 181. When spatial metadata is taken into account (a), images are correctly superimposed (only the borders of each region are shown for clarity purposes). Images are incorrectly superimposed if (b) origin, (c) orientation or (d) spacing are ignored.

Methods

TorchIO introduces a suite of PyTorch-style classes and transforms engineered specifically for volumetric data:

  • Data Structures (torchio.data):
  • Image (and subclasses ScalarImage and LabelMap) wraps 4D tensors alongside their physical affine matrices, employing lazy loading to conserve RAM.
  • Subject groups multiple images belonging to the same individual (e.g., T1-weighted MRI, T2-weighted MRI, and a segmentation mask), ensuring any spatial transform is applied identically across all modalities.
  • SubjectsDataset wraps these subjects into a standard PyTorch-compatible dataset.
  • Patch-Based Training: To handle large volumes, TorchIO provides PatchSampler classes (UniformSampler, WeightedSampler, GridSampler) to extract smaller subvolumes. A specialized Queue class acts as a buffer, utilizing CPU multiprocessing to load, preprocess, and augment volumes in parallel while the GPU handles network forward and backward passes.
  • Transforms (torchio.transforms): Operations are split into Spatial Transforms (geometric modifications like elastic deformations or affine changes using SimpleITK) and Intensity Transforms (modifying voxel values). Notably, TorchIO simulates domain-specific MRI artifacts directly in Fourier (k-space) domain, including motion, spike, and ghosting artifacts.
General diagram of TorchIO, its dependencies and its interfaces.

Datasets

The paper does not introduce a new benchmark dataset. Instead, it integrates built-in dataset downloaders and wrappers for popular publicly available datasets to facilitate reproducibility, such as:

  • IXI Dataset: Information eXtraction from Images dataset (torchio.datasets.IXI).
  • Colin27: The Montreal Neurological Institute (MNI) Colin 27 average brain dataset (torchio.datasets.Colin27).

Key Insights and Ideas

  • Voxel Indices $\neq$ Physical Coordinates: Treating voxels purely as array indices causes errors. TorchIO enforces metadata awareness, ensuring that images with different orientations or spacings are correctly resampled and aligned before entering a neural network.
  • Invertibility for Free Uncertainty Estimation: Most TorchIO transforms track their operations and can be mathematically inverted. This allows for seamless Test-Time Augmentation (TTA). By augmenting an image, running inference, and inverting the resulting segmentation back to the original space, researchers can directly estimate aleatoric uncertainty.
  • Strict Modularity: Unlike monolithic frameworks, TorchIO intentionally avoids implementing neural network architectures or loss functions. It focuses purely on data manipulation, making it highly modular and compatible with higher-level medical AI frameworks like MONAI or Eisen.

Key Results

  • Widespread Adoption: At the time of publication (August 2021), TorchIO achieved a strong community footprint with over 900 GitHub stars and more than 5,000 PyPI downloads per month.
  • Accessible Interfaces: Beyond Python scripting, TorchIO includes a Command-Line Interface (torchio-transform) for shell scripting and a Graphical User Interface (GUI) extension integrated within 3D Slicer, allowing non-programmers to visually experiment with transform parameters.

Limitations and Future Work

  • Non-Differentiable Transforms: Because TorchIO relies on external libraries like SimpleITK and NumPy under the hood, most transforms are not differentiable. This prevents them from being used in architectures that require backpropagation through the transformation layer (e.g., spatial transformer networks for image registration).
  • Modality Bias: The library currently features significantly more domain-specific augmentations for MRI (k-space artifacts) than for other modalities like CT or Ultrasound.
  • Future Directions: The authors aim to implement differentiable spatial transforms and expand physics-based data augmentations tailored specifically to CT and ultrasound imaging.

Summary updated

BibTeX
@misc{perezgarcia2020torchio,
  author = {Fernando Pérez-García and Rachel Sparks and Sébastien Ourselin},
  title = {{TorchIO: A Python library for efficient loading, preprocessing, augmentation and patch-based sampling of medical images in deep learning}},
  year = {2020},
  eprint = {2003.04696},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2003.04696},
}
Figure