A MONAI transform pipeline for a 3D volume
Normalising the intensity of an MRI volume, cropping it and adding random augmentation.
Goal: Applying realistic distortions and extracting patches for training.
import torch, torchio as tio
torch.manual_seed(0)
subject = tio.Subject(mri=tio.ScalarImage(tensor=torch.rand(1, 64, 64, 48)),
seg=tio.LabelMap(tensor=(torch.rand(1, 64, 64, 48) > 0.9).int()))
tf = tio.Compose([tio.RescaleIntensity(out_min_max=(0, 1)), tio.RandomAffine(degrees=10),
tio.RandomBiasField(), tio.RandomNoise(std=0.02)])
aug = tf(subject)
print("Applied transforms:", [t.name for t in aug.history])
sampler = tio.data.UniformSampler(patch_size=32)
patches = list(sampler(aug, num_patches=3))
print("Number of patches:", len(patches), "| patch shape:", tuple(patches[0]["mri"].data.shape))
A Subject groups the image and the mask, and spatial transforms are applied to both together. RandomBiasField simulates magnetic field inhomogeneity in MRI. Then UniformSampler draws fixed-size patches for training with limited memory.
Applied transforms: ['RescaleIntensity', 'Affine', 'BiasField', 'Noise'] Number of patches: 3 | patch shape: (1, 32, 32, 32)
This example was actually run and tested on 21 November 2026 on a CPU — torchio 1.2.1, torch 2.15.0, python 3.13.18
Normalising the intensity of an MRI volume, cropping it and adding random augmentation.
Harmonising voxel size (spacing) between scans from different machines.
Applying an ITK filter to a 3D image through its functional Python API.
Sections
No section has this name. Press “Search” to search all site content.
Favourites
My results & certificates
My academy progressMy quiz resultsKids’ progressAcademy certificatesQuiz certificatesStart any lesson or quiz and it will show up here so you can get back to it quickly.
Settings
I am the AI Frameworks & Libraries Assistant. Describe your project or ask me about a library, a comparison or a code example, and I will answer from the encyclopedia's database with links and sources.
Answers from the encyclopedia's database only, without a language model and without sending your question to any third party.
The assistant can make mistakes, so check the attached sources.