Medical imaging for AI.

An open-source Python library for efficient loading, preprocessing, augmentation, and patch-based sampling of 3D medical images, built on top of PyTorch.

$ pip install torchio

quickstart.py
import torchio as tio

dirs = ["sub-01", "sub-02", "sub-03", "sub-04"]

# Lazy: only headers are read until .data is accessed
subjects = [
    tio.Subject(
        t1=tio.ScalarImage(f"{d}/t1.nii.gz"),
        seg=tio.LabelMap(f"{d}/seg.nii.gz"),
    )
    for d in dirs
]

# Composable augmentation pipeline
transform = tio.Compose([
    tio.Flip(flip_probability=0.5),
    tio.Affine(degrees=(-15, 15)),
    tio.Standardize(),
    tio.Noise(std=(0, 0.1)),
])

# Augment a whole batch on the GPU in one call
batch = tio.SubjectsBatch.from_subjects(subjects).to("cuda")
augmented = transform(batch)
Augmentations

Realistic MRI artifacts, one transform away

TorchIO ships physics-based augmentations that reproduce real MRI artifacts. Pick one, then move the slider to compare the original scan with the augmented result.

Brain scan with the augmentation applied Original brain scan Original Augmented
tio.Motion()

Simulates patient motion during acquisition, adding blurring and ghosting from inconsistent k-space lines.

Learn how transforms work
Pipeline

From scan to batch in a few lines

Compose your transforms once, then augment whole batches of subjects on the GPU in a single call.

1 Load subjects Lazily, from files or the cloud
2 Compose transforms Flip Affine Standardize Noise
3 Augment the batch One call, on CPU or GPU