MONAI: Medical Open Network for AI

1.5.2 · active · verified Thu Apr 16

MONAI (Medical Open Network for AI) is a PyTorch-based, open-source framework providing domain-optimized foundational capabilities for deep learning in healthcare imaging. It offers standardized, efficient, and reproducible components like data loaders, transforms, networks, and metrics, specifically designed for medical applications. The current version is 1.5.2, with regular minor and patch releases, typically multiple times per year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic MONAI transform pipeline using dictionary-based transforms (suffixed with 'd'). It loads a dummy NIfTI image, applies several common preprocessing steps like channel reordering, intensity scaling, orientation standardization, and spatial resampling, then prints the shape and data type of the transformed image. This setup mirrors typical medical imaging data workflows.

import torch
import numpy as np
from monai.transforms import Compose, LoadImaged, EnsureChannelFirstd, ScaleIntensityRanged, Orientationd, Spacingd
import os
import nibabel as nib

# Create dummy image file for demonstration
dummy_image_path = "dummy_image.nii.gz"
if not os.path.exists(dummy_image_path):
    print(f"Creating dummy NIfTI image at {dummy_image_path}...")
    dummy_data = np.random.rand(10, 10, 10).astype(np.float32)
    affine = np.diag([1, 1, 1, 1])
    nifti_img = nib.Nifti1Image(dummy_data, affine)
    nib.save(nifti_img, dummy_image_path)

# 1. Define a transform pipeline for dictionary-based data
keys = ["image"] # working with dictionary data, key for the image
transform = Compose(
    [
        LoadImaged(keys=keys), # Load medical image data
        EnsureChannelFirstd(keys=keys), # Ensure channel dimension is first
        ScaleIntensityRanged(keys=keys, a_min=0, a_max=1, b_min=0.0, b_max=1.0, clip=True), # Normalize intensity
        Orientationd(keys=keys, axcodes="RAS"), # Reorient image to standard anatomical space
        Spacingd(keys=keys, pixdim=(1.5, 1.5, 2.0), mode="bilinear"), # Resample to desired spacing
    ]
)

# 2. Create dummy data list (mimicking a dataset of file paths)
data = [{
    "image": dummy_image_path
}] * 2 # Two dummy items for batching effect

# 3. Apply transforms (typically done within a Dataset/DataLoader)
transformed_data = [transform(item) for item in data]

print(f"Original image path: {data[0]['image']}")
print(f"Transformed image shape: {transformed_data[0]['image'].shape}")
print(f"Transformed image dtype: {transformed_data[0]['image'].dtype}")

# Clean up dummy file
if os.path.exists(dummy_image_path):
    os.remove(dummy_image_path)
    print(f"Cleaned up dummy NIfTI image: {dummy_image_path}")

view raw JSON →