Registry / ai-ml / monai
library1.5.2pypypi✓ verified 85d ago

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.

pip install monai
INSTALL
IMPORT
SIG · MONAI
M
monai
ai-mlpythonv1.5.2
Install
70.2s avg
Import
7895ms
Disk
4890MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.5.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 79.48s
py 3.11
✕ build_error
✓ 73.43s
py 3.12
✕ build_error
✓ 65.1s
py 3.13
✕ build_error
✓ 62.75s
py 3.9
✕ build_error
✕ timeout
4890MB installed
● package 4890MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Compose
from monai.transforms import Compose
LoadImaged
from monai.transforms import LoadImaged
from monai.transforms import LoadNifti
LoadNifti and similar format-specific loaders were replaced by LoadImaged (dictionary) or LoadImaget (tuple) for generality, typically from v0.9.0 onwards.
CacheDataset
from monai.data import CacheDataset
from monai.data.dataset import CacheDataset
Import path for Dataset objects moved directly under `monai.data` from v0.6.0.
UNet
from monai.networks.nets import UNet
sliding_window_inference
from monai.inferers import sliding_window_inference
from monai.data import sliding_window_inference
The sliding_window_inference utility was moved from `monai.data` to `monai.inferers` starting from v0.9.0, deprecated in `monai.data` in v1.0.0.

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}")
Debug
Known issues
breakingThe `apply` method for custom transforms was removed in v1.0.0. Custom transforms should override the `__call__` method.
fix
For custom transforms, change method name from `apply` to `__call__` and ensure it accepts and returns data consistent with MONAI's transform interface.
affects: >=1.0.0
breakingThe `sliding_window_inference` utility was moved from `monai.data` to `monai.inferers`.
fix
Update import statements from `from monai.data import sliding_window_inference` to `from monai.inferers import sliding_window_inference`.
affects: >=0.9.0
breakingFormat-specific image loaders like `LoadNifti` or `LoadDICOM` were deprecated/removed in favor of `LoadImaged` (for dictionary-based inputs) or `LoadImaget` (for tuple-based inputs).
fix
Replace `LoadNifti(...)` with `LoadImaged(keys=..., reader='NibabelReader')` or `LoadImaget(...)`, specifying `reader` if needed.
affects: >=0.9.0
gotchaDictionary-based transforms (e.g., `LoadImaged`, `ScaleIntensityRanged`) operate on specific keys. Mismatching keys between your input dictionary and the transform's `keys` argument will result in a `KeyError`.
fix
Ensure the keys in your input data dictionary (e.g., `{'image': 'path/to/img.nii.gz', 'label': 'path/to/label.nii.gz'}`) exactly match the `keys` argument provided to the transform (e.g., `LoadImaged(keys=['image', 'label'])`).
affects: all
gotchaMONAI heavily relies on PyTorch. Users must ensure their PyTorch version is compatible with the installed MONAI version and their CUDA setup to avoid `RuntimeError` or performance issues.
fix
Consult the MONAI documentation for recommended PyTorch versions. Install PyTorch separately, ensuring the correct CUDA-enabled version for your hardware, before installing MONAI.
affects: all
Errors
Common errors & fixes
RuntimeError: CUDA out of memory. Tried to allocate X GiB (GPU Y)
The model, batch size, or image dimensions exceed the available VRAM on the GPU.
fix
Reduce the `batch_size`, crop input images to smaller dimensions, or enable mixed precision training (`torch.cuda.amp.autocast()`) if your model supports it.
TypeError: can't convert CUDA tensor to numpy. Use .cpu().numpy() instead if you want to transfer the tensor to CPU
Attempting to call `.numpy()` directly on a PyTorch tensor residing on the GPU.
fix
Before converting a GPU tensor to a NumPy array, first move it to the CPU using `.cpu()`: `my_tensor.cpu().numpy()`.
KeyError: 'image' (or 'label', etc.)
A dictionary-based transform (e.g., `LoadImaged`) cannot find one of the specified `keys` in the input dictionary.
fix
Verify that your input dictionary contains all the keys specified in the `keys` argument of the transform. For example, if `LoadImaged(keys=['data_key'])`, ensure your input dictionary has `{'data_key': ...}`.
AttributeError: 'Dataset' object has no attribute 'apply'
Attempting to use the `apply` method on a transform. This method was removed in MONAI v1.0.0.
fix
For built-in transforms, simply call them directly. For custom transforms, ensure you have overridden the `__call__` method instead of `apply`.
ImportError: cannot import name 'UNet' from 'monai.networks.nets'
Incorrect import path for a specific symbol, or an older version of MONAI where the symbol's location was different.
fix
Consult the official MONAI documentation for the exact import path corresponding to your installed MONAI version. For `UNet`, the correct path is `from monai.networks.nets import UNet`.
Upgrade
Version history
1.5.2latest on PyPI · released Jan 27, 2026
Audit
Dependencies
torchrequiredCore deep learning framework (users install separately for CUDA configuration)
numpyrequiredFundamental library for numerical operations on arrays
pillowrequiredImage loading and processing utilities
scikit-imagerequiredImage processing utilities, used by some transforms
scipyrequiredScientific computing utilities, especially for image interpolation and processing
tqdmrequiredProgress bars for iterations and loops
pytorch-igniterequiredProvides a high-level engine and event-handler system for training loops
nibabeloptionalRequired for reading/writing NIfTI and other neuroimaging file formats
matplotliboptionalFor data visualization and plotting
tensorboardoptionalFor logging metrics, visualizations, and debugging models
pyyamloptionalFor loading configuration files
Agent activity
6 hits · last 30 days
node
6
Resources
monai — pip install monai · libregistry