monai.deploy.operators.MonaiBundleInferenceOperator

class monai.deploy.operators.MonaiBundleInferenceOperator(input_mapping, output_mapping, model_name='', bundle_path='', bundle_config_names=<monai.deploy.operators.monai_bundle_inference_operator.BundleConfigNames object>, *args, **kwargs)[source]

Bases: monai.deploy.operators.inference_operator.InferenceOperator

This inference operator automates the inference operation for a given MONAI Bundle.

This inference operator configures itself based on the parsed data from a MONAI bundle file. This file is included with a MAP as a Torchscript file with added bundle metadata or a zipped bundle with weights. The class will configure how to do pre- and post-processing, inference, which device to use, state its inputs, outputs, and dependencies. Its compute method is meant to be general purpose to most any bundle such that it will handle any input specified in the bundle and produce output as specified, using the inference object the bundle defines. A number of methods are provided which define parts of functionality relating to this behavior, users may wish to overwrite these to change behavior is needed for specific bundles.

The input(s) and output(s) for this operator need to be provided when an instance is created, and their labels need to correspond to the bundle network input and output names, which are also used as the keys in the pre and post processing.

For image input and output, the type is the Image class. For output of probabilities, the type is Dict.

This operator is expected to be linked with both source and destination operators, e.g. receiving an Image object from the DICOMSeriesToVolumeOperator, and passing a segmentation Image to the DICOMSegmentationWriterOperator. In such cases, the I/O storage type can only be IN_MEMORY due to the restrictions imposed by the application executor. However, when used as the first operator in an application, its input storage type needs to be DISK, and the file needs to be a Python pickle file, e.g. containing an Image instance. When used as the last operator, its output storage type also needs to DISK with the path being the application’s output folder, and the operator’s output will be saved as a pickle file whose name is the same as the output name.

_summary_

Parameters
  • input_mapping (List[IOMapping]) – Define the inputs’ name, type, and storage type.

  • output_mapping (List[IOMapping]) – Defines the outputs’ name, type, and storage type.

  • model_name (Optional[str], optional) – Name of the model/bundle, needed in multi-model case. Defaults to “”.

  • bundle_path (Optional[str], optional) – For completing . Defaults to None.

  • bundle_config_names (BundleConfigNames, optional) – Relevant config item names in a the bundle. Defaults to DEFAULT_BundleConfigNames.

Methods

__init__(input_mapping, output_mapping[, …])

_summary_

add_input(label, data_type, storage_type)

add_output(label, data_type, storage_type)

compute(op_input, op_output, context)

Infers with the input(s) and saves the prediction result(s) to output

ensure_valid()

Ensures that the operator is valid.

post_compute()

This method gets executed after “compute()” of an operator is called.

post_process(data, *args, **kwargs)

Processes the output list/dictionary with the stored transform sequence self._postproc.

pre_compute()

This method gets executed before compute() of an operator is called.

pre_process(data, *args, **kwargs)

Processes the input dictionary with the stored transform sequence self._preproc.

predict(data, *args, **kwargs)

Predicts output using the inferer.

Attributes

bundle_path

The path of the MONAI Bundle model.

env

Gives access to the environment.

known_io_data_types

kw_preprocessed_inputs

model_name

rtype

str

name

Returns the name of this operator.

op_info

Retrieves the operator info.

parser

The ConfigParser object.

uid

Gives access to the UID of the operator.

__init__(input_mapping, output_mapping, model_name='', bundle_path='', bundle_config_names=<monai.deploy.operators.monai_bundle_inference_operator.BundleConfigNames object>, *args, **kwargs)[source]

_summary_

Parameters
  • input_mapping (List[IOMapping]) – Define the inputs’ name, type, and storage type.

  • output_mapping (List[IOMapping]) – Defines the outputs’ name, type, and storage type.

  • model_name (Optional[str], optional) – Name of the model/bundle, needed in multi-model case. Defaults to “”.

  • bundle_path (Optional[str], optional) – For completing . Defaults to None.

  • bundle_config_names (BundleConfigNames, optional) – Relevant config item names in a the bundle. Defaults to DEFAULT_BundleConfigNames.

property bundle_path: Optional[pathlib.Path]

The path of the MONAI Bundle model.

Return type

Optional[Path]

compute(op_input, op_output, context)[source]

Infers with the input(s) and saves the prediction result(s) to output

Parameters
  • op_input (InputContext) – An input context for the operator.

  • op_output (OutputContext) – An output context for the operator.

  • context (ExecutionContext) – An execution context for the operator.

ensure_valid()

Ensures that the operator is valid.

This method needs to be executed by add_operator() and add_flow() methods in the compose() method of the application. This sets default values for the operator in the graph if necessary. (e.g., set default value for the operator’s input port, set default value for the operator’s output port, etc.)

property env: monai.deploy.core.operator.OperatorEnv

Gives access to the environment.

This sets a default value for the operator’s environment if not set.

Return type

OperatorEnv

Returns

An instance of OperatorEnv.

property name: str

Returns the name of this operator.

Return type

str

property op_info: monai.deploy.core.operator_info.OperatorInfo

Retrieves the operator info.

Args:

Return type

OperatorInfo

Returns

An instance of OperatorInfo.

property parser: Optional[monai.bundle.config_parser.ConfigParser]

The ConfigParser object.

Return type

Optional[ConfigParser]

post_compute()

This method gets executed after “compute()” of an operator is called.

This is a post-execution step before the operator is done doing its main action. This needs to be overridden by a base class for any meaningful action.

post_process(data, *args, **kwargs)[source]

Processes the output list/dictionary with the stored transform sequence self._postproc.

The “processed_inputs”, in fact the metadata in it, need to be passed in so that the invertible transforms in the post processing can work properly.

Return type

Union[Image, Any, Tuple[Any, …], Dict[Any, Any]]

pre_compute()

This method gets executed before compute() of an operator is called.

This is a preperatory step before the operator executes its main job. This needs to be overridden by a base class for any meaningful action.

pre_process(data, *args, **kwargs)[source]

Processes the input dictionary with the stored transform sequence self._preproc.

Return type

Union[Image, Any, Tuple[Any, …], Dict[Any, Any]]

predict(data, *args, **kwargs)[source]

Predicts output using the inferer.

Return type

Union[Image, Any, Tuple[Any, …], Dict[Any, Any]]

property uid: uuid.UUID

Gives access to the UID of the operator.

Return type

UUID

Returns

UID of the operator.