monai_bundle_inference
MONAI Bundle Inference Algorithm.
This module provides an algorithm for running inference using pre-trained MONAI bundles from the MONAI Model Zoo on medical imaging data. The algorithm is datasource-agnostic and works with any file-based medical imaging format supported by a FileSystemIterableSourceInferrable datasource (e.g., NIFTISource, DICOMSource).
Supports both segmentation bundles (e.g., wholeBrainSeg_Large_UNEST_segmentation) and detection bundles (e.g., lung_nodule_ct_detection) which output bounding boxes with classification scores.
Classes
MONAIBundleInference
class MONAIBundleInference( datastructure: DataStructure, bundle_name: str, bundle_version: Optional[str] = None, dataframe_output: bool = True, nifti_output: bool = False, json_output: bool = False, batch_size: int = 1, num_workers: int = 1, inference_config_overrides: Optional[dict[str, Any]] = None, restore_spatial: bool = True, invert_orientation: bool = True,):MONAI Bundle Inference Algorithm.
This algorithm runs inference using pre-trained MONAI bundles from the MONAI Model Zoo on medical imaging data. It uses MONAI's DataLoader for efficient batched inference with GPU parallelism and data prefetching.
The algorithm is datasource-agnostic and works with any file-based medical imaging datasource that extends FileSystemIterableSourceInferrable, including:
- NIFTISource: For NIfTI neuroimaging data (.nii, .nii.gz)
- DICOMSource: For DICOM medical images (.dcm)
Arguments
- **
**kwargs**: Additional keyword arguments. batch_size: Batch size for inference. Defaults to 1. Higher values improve GPU utilization but require more memory. Medical images are often large, so start with small batch sizes (1-4).bundle_name: Name of the MONAI bundle from Model Zoo (e.g., "wholeBrainSeg_Large_UNEST_segmentation").bundle_version: Specific version of the bundle (default: latest).dataframe_output: Whether to include prediction arrays in the output DataFrame for CSV generation. Defaults to True.datastructure: The data structure defining the data to use.inference_config_overrides: Dictionary of config values to override in the bundle's inference config.invert_orientation: Whether to invert orientation changes during restoration. Only used when restore_spatial=True. Defaults to True. Disable if the bundle's preprocessing doesn't change orientation.json_output: Whether to save detection results as JSON files. Defaults to False. Only applicable to detection bundles.nifti_output: Whether to save segmentation masks as NIfTI files. Defaults to False. When enabled, files are saved to the task results directory (same location as CSVReportAlgorithm output).num_workers: Number of worker processes for data loading. Defaults to 0 (single-process loading for maximum compatibility). Set to 2-4 on Linux/macOS for improved throughput with large datasets.restore_spatial: Whether to restore predictions to original image space. When True (default), predictions are resized and re-oriented to match the original input image dimensions and affine matrix. This ensures output NIfTI files can be correctly overlaid on original images. Disable only if you need predictions in the preprocessed canonical space (e.g., 1mm isotropic, RAS orientation).
Attributes
batch_size: Batch size for DataLoader.bundle_name: Name of the MONAI bundle.bundle_version: Version of the bundle.class_name: The name of the algorithm class.dataframe_output: Whether to include predictions in DataFrame.fields_dict: A dictionary mapping all attributes that will be serialized in the class to their marshmallow field type. (e.g. fields_dict ={"class_name": fields.Str()}).inference_config_overrides: Config overrides.invert_orientation: Whether orientation inversion is enabled.json_output: Whether to save detection JSON files.nested_fields: A dictionary mapping all nested attributes to a registry that contains class names mapped to the respective classes. (e.g. nested_fields ={"datastructure": datastructure.registry})nifti_output: Whether to save NIfTI files.num_workers: Number of DataLoader workers.restore_spatial: Whether spatial restoration is enabled.
Raises
RuntimeError: If the Bitfount engine is not set to PyTorch.ValueError: If bundle_name is empty, batch_size < 1, or num_workers < 1.
Variables
- static
fields_dict : ClassVar[dict[str, marshmallow.fields.Field]]
Methods
create
def create(self, role: Union[str, Role], **kwargs: Any) ‑> Any:Create an instance representing the role specified.
modeller
def modeller( self, *, context: ProtocolContext, **kwargs: Any,) ‑> bitfount.federated.algorithms.monai_algorithms.base._MONAIModellerSide:Returns the modeller side of the MONAIBundleInference algorithm.
Arguments
context: The protocol context.- **
**kwargs**: Additional keyword arguments.
Returns The modeller side instance.
worker
def worker( self, *, context: ProtocolContext, **kwargs: Any,) ‑> bitfount.federated.algorithms.monai_algorithms.monai_bundle_inference._MONAIWorkerSide:Returns the worker side of the MONAIBundleInference algorithm.
Arguments
context: The protocol context.- **
**kwargs**: Additional keyword arguments.
Returns The worker side instance.