monai.deploy.operators.MonaiSegInferenceOperator#

class monai.deploy.operators.MonaiSegInferenceOperator(fragment, *args, roi_size, pre_transforms, post_transforms, app_context, model_name='', overlap=0.25, sw_batch_size=4, inferer=sliding_window, model_path=PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/monai-deploy-app-sdk/checkouts/stable/docs/source/model/model.ts'), **kwargs)[source]#

Bases: InferenceOperator

This segmentation operator uses MONAI transforms and performs Simple or Sliding Window Inference.

This operator performs pre-transforms on a input image, inference using a given model, and post-transforms. The segmentation image is saved as a named Image object in memory.

If specified in the post transforms, results may also be saved to disk.

This operator uses the MONAI inference utils functions for sliding window and simple inference, and thus input parameters need to be as expected by these functions.

Any additional sliding window arguments not explicitly defined in this operator can be passed via **kwargs for forwarding to ‘sliding_window_inference’.

Named Input:

image: Image object of the input image.

Named Output:

seg_image: Image object of the segmentation image. Not requiring a ready receiver.

Creates a instance of this class.

Parameters:
  • fragment (Fragment) – An instance of the Application class which is derived from Fragment.

  • roi_size (Sequence[int], int, optional) – The window size to execute “SLIDING_WINDOW” evaluation. Applicable for “SLIDING_WINDOW” only.

  • pre_transforms (Compose) – MONAI Compose object used for pre-transforms.

  • post_transforms (Compose) – MONAI Compose object used for post-transforms.

  • app_context (AppContext) – Object holding the I/O and model paths, and potentially loaded models.

  • model_name (str, optional) – Name of the model. Default to “” for single model app.

  • overlap (Sequence[float], float) – The amount of overlap between scans along each spatial dimension. Defaults to 0.25. Applicable for “SLIDING_WINDOW” only.

  • sw_batch_size (int) – The batch size to run window slices. Defaults to 4. Applicable for “SLIDING_WINDOW” only.

  • inferer (InfererType, str) – The type of inferer to use, “SIMPLE” or “SLIDING_WINDOW”. Defaults to “SLIDING_WINDOW”.

  • model_path (Path) – Path to the model file. Defaults to model/models.ts of current working dir.

  • **kwargs – any other sliding window parameters to forward (e.g. mode, cval, etc.).

Methods

__init__(fragment, *args, roi_size, ...[, ...])

Creates a instance of this class.

add_arg(*args, **kwargs)

Overloaded function.

add_dynamic_flow(*args, **kwargs)

Overloaded function.

compute(op_input, op_output, context)

Infers with the input image and save the predicted image to output

compute_impl(input_image, context)

enable_metadata(self, enable)

Configure whether or not the metadata feature is enabled for this operator.

filter_sw_kwargs(**kwargs)

Filters the keyword arguments into a tuple of two dictionaries:

find_all_flow_info(self, predicate)

Find all flow info objects in the operator's next flows that match a given condition.

find_flow_info(self, predicate)

Find a flow info in the operator's next flows based on a given predicate.

initialize()

Default implementation of initialize

post_process(data, *args, **kwargs)

Transforms the prediction results from the model(s).

pre_process(data, *args, **kwargs)

Transforms input before being used for predicting on a model.

predict(data, *args, **kwargs)

Predicts results using the models(s) with input tensors.

queue_policy(self, port_name, port_type, policy)

Set the queue policy to be used by an input (or output) port's receiver (or transmitter).

receiver(self, port_name)

Get the receiver used by an input port.

resource(self, name)

Resources associated with the operator.

service(self, service_type[, id])

Retrieve a registered fragment service through the component's fragment.

setup(spec)

Default implementation of setup method.

start()

Default implementation of start

stop()

Default implementation of stop

stop_execution(self)

Stop the execution of the operator.

transmitter(self, port_name)

Get the transmitter used by an output port.

Attributes

INPUT_EXEC_PORT_NAME

MODEL_LOCAL_PATH

OUTPUT_EXEC_PORT_NAME

args

The list of arguments associated with the component.

async_condition

The internal asynchronous condition for the operator.

conditions

Conditions associated with the operator.

description

YAML formatted string describing the operator.

execution_context

The execution context for the operator.

fragment

The fragment (holoscan.core.Fragment) that the operator belongs to.

id

The identifier of the component.

inferer

The type of inferer to use

input_dataset_key

This is the input image key name used in dictionary based MONAI pre-transforms.

is_metadata_enabled

Boolean indicating whether the fragment this operator belongs to has metadata transmission enabled.

metadata

The metadata dictionary (holoscan.core.MetadataDictionary) associated with the operator.

metadata_policy

The metadata policy (holoscan.core.MetadataPolicy) associated with the operator.

name

The name of the operator.

next_flows

Get the list of flow information for connections to downstream operators.

operator_type

The operator type.

overlap

This is the overlap used during sliding window inference

pred_dataset_key

This is the prediction key name used in dictionary based MONAI post-transforms.

resources

Resources associated with the operator.

roi_size

The ROI size of tensors used in prediction.

spec

The operator spec (holoscan.core.OperatorSpec) associated with the operator.

sw_batch_size

The batch size to run window slices

class OperatorType(self: holoscan.core._core.OperatorBase.OperatorType, value: int)#

Bases: pybind11_object

Enum class for operator types used by the executor.

  • NATIVE: Native operator.

  • GXF: GXF operator.

  • VIRTUAL: Virtual operator. (for internal use, not intended for use by application authors)

Members:

NATIVE

GXF

VIRTUAL

property name#
__init__(fragment, *args, roi_size, pre_transforms, post_transforms, app_context, model_name='', overlap=0.25, sw_batch_size=4, inferer=sliding_window, model_path=PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/monai-deploy-app-sdk/checkouts/stable/docs/source/model/model.ts'), **kwargs)[source]#

Creates a instance of this class.

Parameters:
  • fragment (Fragment) – An instance of the Application class which is derived from Fragment.

  • roi_size (Sequence[int], int, optional) – The window size to execute “SLIDING_WINDOW” evaluation. Applicable for “SLIDING_WINDOW” only.

  • pre_transforms (Compose) – MONAI Compose object used for pre-transforms.

  • post_transforms (Compose) – MONAI Compose object used for post-transforms.

  • app_context (AppContext) – Object holding the I/O and model paths, and potentially loaded models.

  • model_name (str, optional) – Name of the model. Default to “” for single model app.

  • overlap (Sequence[float], float) – The amount of overlap between scans along each spatial dimension. Defaults to 0.25. Applicable for “SLIDING_WINDOW” only.

  • sw_batch_size (int) – The batch size to run window slices. Defaults to 4. Applicable for “SLIDING_WINDOW” only.

  • inferer (InfererType, str) – The type of inferer to use, “SIMPLE” or “SLIDING_WINDOW”. Defaults to “SLIDING_WINDOW”.

  • model_path (Path) – Path to the model file. Defaults to model/models.ts of current working dir.

  • **kwargs – any other sliding window parameters to forward (e.g. mode, cval, etc.).

add_arg(*args, **kwargs)#

Overloaded function.

  1. add_arg(self: holoscan.core._core.OperatorBase, arg: holoscan.core._core.Arg) -> None

Add an argument to the component.

  1. add_arg(self: holoscan.core._core.OperatorBase, arg: holoscan.core._core.ArgList) -> None

Add a list of arguments to the component.

  1. add_arg(self: holoscan.core._core.OperatorBase, **kwargs) -> None

Add arguments to the component via Python kwargs.

  1. add_arg(self: holoscan.core._core.OperatorBase, arg: holoscan.core._core.ConditionBase) -> None

  2. add_arg(self: holoscan.core._core.OperatorBase, arg: holoscan.core._core.ResourceBase) -> None

Add a condition or resource to the Operator.

This can be used to add a condition or resource to an operator after it has already been constructed.

Parameters#

argholoscan.core.Condition or holoscan.core.Resource

The condition or resource to add.

add_dynamic_flow(*args, **kwargs)#

Overloaded function.

  1. add_dynamic_flow(self: holoscan.core._core.OperatorBase, flow: holoscan.core._core.FlowInfo) -> None

  2. add_dynamic_flow(self: holoscan.core._core.OperatorBase, flows: list[holoscan.core._core.FlowInfo]) -> None

  3. add_dynamic_flow(self: holoscan.core._core.OperatorBase, next_op: holoscan.core._core.OperatorBase, next_input_port_name: str = ‘’) -> None

Add a dynamic flow from this operator to another operator.

Parameters#

next_opholoscan.core.Operator

The downstream operator to connect to.

next_input_port_namestr, optional

The name of the input port on the downstream operator to connect to. If not specified, the first available input port will be used.

Notes#

This method has several overloads to support different ways of creating dynamic flows:

  1. add_dynamic_flow(next_op: Operator, next_input_port_name: str = ‘’) - Basic connection using default output port. This is the simplest form for connecting

    two operators when you only need to specify the destination.

  2. add_dynamic_flow(curr_output_port_name: str, next_op: Operator, next_input_port_name: str = ‘’) - Connection with explicit output port specification. Use this when the source operator has

    multiple output ports and you need to specify which one to use.

  3. add_dynamic_flow(flow: FlowInfo) - Connection using a FlowInfo object, which encapsulates all connection details including:

    • Source operator and its output port specification

    • Destination operator and its input port specification

    • Port names and associated IOSpecs

    • This is useful for complex connections or when reusing connection patterns.

  4. add_dynamic_flow(flows: List[FlowInfo]) - Batch connection using multiple FlowInfo objects. Use this to set up multiple

    connections in a single call, which is more efficient than making multiple individual connections.

The FlowInfo class provides a complete description of a flow connection between operators, including all port specifications and naming. It’s particularly useful when you need to:

  • Store and reuse connection patterns

  • Create complex routing configurations

  • Handle dynamic port specifications

  • Manage multiple connections systematically

  1. add_dynamic_flow(self: holoscan.core._core.OperatorBase, curr_output_port_name: str, next_op: holoscan.core._core.OperatorBase, next_input_port_name: str = ‘’) -> None

property args#

The list of arguments associated with the component.

Returns#

arglist : holoscan.core.ArgList

property async_condition#

The internal asynchronous condition for the operator.

This property provides access to the internal asynchronous condition for the operator, which controls the scheduling of the operator’s compute method.

Returns#

conditionholoscan.conditions.AsynchronousCondition

An instance of holoscan.conditions.AsynchronousCondition that is the internal asynchronous condition for the operator.

Notes#

This object is only accessible after the executor has called Operator.initialize() via run() or run_async(). If accessed during Application.compose(), it will return None.

compute(op_input, op_output, context)[source]#

Infers with the input image and save the predicted image 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.

property conditions#

Conditions associated with the operator.

property description#

YAML formatted string describing the operator.

enable_metadata(self: holoscan.core._core.OperatorBase, enable: bool) None#

Configure whether or not the metadata feature is enabled for this operator. If it is not set, the default value will be determined by the enable_metadata setting from the Fragment that this operator belongs to.

property execution_context#

The execution context for the operator.

This property provides access to the execution context of the operator, which contains information about the current execution environment including scheduling details.

Returns#

holoscan.core.ExecutionContext

The execution context object for this operator.

static filter_sw_kwargs(**kwargs)[source]#

Filters the keyword arguments into a tuple of two dictionaries:

  1. A dictionary of named parameters to pass to the sliding_window_inference function that:
    • Are not explicitly defined in the __init__ of this class

    • Are not explicitly used when calling sliding_window_inference

  2. A dictionary of named parameters to pass to the base class __init__ of this class that:
    • Are not used when calling sliding_window_inference

    • Can be successfully converted from Python –> Holoscan’s C++ layer

Parameters:

**kwargs – extra arguments passed into __init__ beyond the explicitly defined args.

Returns:

A filtered dictionary of arguments to be passed to sliding_window_inference. filtered_base_init_params: A filtered dictionary of arguments to be passed to the base class __init__.

Return type:

filtered_swi_params

find_all_flow_info(self: holoscan.core._core.OperatorBase, predicate: Callable[[holoscan.core._core.FlowInfo], bool]) list[holoscan.core._core.FlowInfo]#

Find all flow info objects in the operator’s next flows that match a given condition.

Parameters#

predicatecallable

A function that takes a FlowInfo object and returns a boolean.

Returns#

list[holoscan.core.FlowInfo]

List of matching FlowInfo objects.

find_flow_info(self: holoscan.core._core.OperatorBase, predicate: Callable[[holoscan.core._core.FlowInfo], bool]) holoscan.core._core.FlowInfo#

Find a flow info in the operator’s next flows based on a given predicate.

Parameters#

predicatecallable

A function that takes a FlowInfo object and returns a boolean.

Returns#

holoscan.core.FlowInfo or None

The first matching FlowInfo object, or None if not found.

property fragment#

The fragment (holoscan.core.Fragment) that the operator belongs to.

property id#

The identifier of the component.

The identifier is initially set to -1, and will become a valid value when the component is initialized.

With the default executor (holoscan.gxf.GXFExecutor), the identifier is set to the GXF component ID.

Returns#

id : int

property inferer: InfererType | str#

The type of inferer to use

initialize()#

Default implementation of initialize

property input_dataset_key#

This is the input image key name used in dictionary based MONAI pre-transforms.

property is_metadata_enabled#

Boolean indicating whether the fragment this operator belongs to has metadata transmission enabled.

property metadata#

The metadata dictionary (holoscan.core.MetadataDictionary) associated with the operator.

property metadata_policy#

The metadata policy (holoscan.core.MetadataPolicy) associated with the operator.

The supported policies are:

  • MetadataPolicy.REJECT: Reject the new value if the key already exists

  • MetadataPolicy.UPDATE: Replace existing value with the new one if the key already exists

  • MetadataPolicy.INPLACE_UPDATE: Update the value stored within an existing MetadataObject in-place if the key already exists (in contrast to UPDATE which always replaces the existing MetadataObject with a new one).

  • MetadataPolicy.RAISE: Raise an exception if the key already exists

property name#

The name of the operator.

property next_flows#

Get the list of flow information for connections to downstream operators.

Returns#

list[holoscan.core.FlowInfo]

List of flow information objects describing connections to downstream operators.

property operator_type#

The operator type.

holoscan.core.Operator.OperatorType enum representing the type of the operator. The two types currently implemented are native and GXF.

property overlap#

This is the overlap used during sliding window inference

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

Transforms the prediction results from the model(s).

This method must be overridden by a derived class. Expected return is monai.transforms.Compose.

Parameters:

data (monai.transforms.Compose) – The pre-processing transforms in a Compose object.

Return type:

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

Returns:

monai.transforms.Compose encapsulating post-processing transforms.

Raises:

NotImplementedError – When the subclass does not override this method.

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

Transforms input before being used for predicting on a model.

This method must be overridden by a derived class. Expected return is monai.transforms.Compose.

Parameters:

data (monai.data.ImageReader) – Reader used in LoadImage to load monai.deploy.core.Image as the input.

Return type:

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

Returns:

monai.transforms.Compose encapsulating pre transforms

Raises:

NotImplementedError – When the subclass does not override this method.

property pred_dataset_key#

This is the prediction key name used in dictionary based MONAI post-transforms.

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

Predicts results using the models(s) with input tensors.

This method is currently not used in this class, instead monai.inferers.sliding_window_inference is used.

Raises:

NotImplementedError – When the subclass does not override this method.

Return type:

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

queue_policy(self: holoscan.core._core.OperatorBase, port_name: str, port_type: holoscan.core._core.IOSpec.IOType = <IOType.INPUT: 0>, policy: holoscan.core._core.IOSpec.QueuePolicy = <QueuePolicy.FAULT: 2>) None#

Set the queue policy to be used by an input (or output) port’s receiver (or transmitter).

Parameters#

port_namestr

The name of the port.

port_typeIOSpec.IOType, optional

Enum indicating whether port_name corresponds to an input port or output port.

policyIOSpec.QueuePolicy, optional

The queue policy to set. Valid values are:

  • QueuePolicy.POP : If the queue is full, pop the oldest item, then add the new one.

  • QueuePolicy.REJECT : If the queue is full, reject (discard) the new item.

  • QueuePolicy.FAULT : If the queue is full, log a warning and reject the new item.

Returns#

transmitterholoscan.resources.Transmitter or None

The transmitter used by this output port. Will be None if the port does not exist.

receiver(self: holoscan.core._core.OperatorBase, port_name: str) Optional[holoscan::Receiver]#

Get the receiver used by an input port.

Parameters#

port_namestr

The name of the input port.

Returns#

receiverholoscan.resources.Receiver

The receiver used by this input port. Will be None if the port does not exist.

resource(self: holoscan.core._core.OperatorBase, name: str) object | None#

Resources associated with the operator.

Parameters#

namestr

The name of the resource to retrieve

Returns#

holoscan.core.Resource or None

The resource with the given name. If no resource with the given name is found, None is returned.

property resources#

Resources associated with the operator.

property roi_size#

The ROI size of tensors used in prediction.

service(self: object, service_type: type, id: str = '') object#

Retrieve a registered fragment service through the component’s fragment.

This method delegates to the fragment’s service() method to retrieve a previously registered fragment service by its type and optional identifier. Returns None if no fragment service is found with the specified type and identifier.

Parameters#

service_typetype

The type of the fragment service to retrieve. Must be a type that inherits from Resource or FragmentService.

idstr, optional

The identifier of the fragment service. If empty, retrieves by service type only. For Resources, this would typically be the resource’s name.

Returns#

object or None

The fragment service instance of the requested type, or None if not found. If the service wraps a Resource and a Resource type is requested, the unwrapped Resource instance is returned.

Raises#

RuntimeError

If the component has no associated fragment or if the fragment’s service method cannot be accessed.

Notes#

This is a convenience method that internally calls the fragment’s service() method. For services that wrap Resources, the method will automatically unwrap and return the Resource if a Resource type is requested.

setup(spec)[source]#

Default implementation of setup method.

property spec#

The operator spec (holoscan.core.OperatorSpec) associated with the operator.

start()#

Default implementation of start

stop()#

Default implementation of stop

stop_execution(self: holoscan.core._core.OperatorBase) None#

Stop the execution of the operator.

This method is used to stop the execution of the operator by setting the internal async condition to EVENT_NEVER state, which sets the scheduling condition to NEVER. Once stopped, the operator will not be scheduled for execution (the compute() method will not be called).

Note that executing this method does not trigger the operator’s stop() method. The stop() method is called only when the scheduler deactivates all operators together.

property sw_batch_size#

The batch size to run window slices

transmitter(self: holoscan.core._core.OperatorBase, port_name: str) Optional[holoscan::Transmitter]#

Get the transmitter used by an output port.

Parameters#

port_namestr

The name of the output port.

Returns#

transmitterholoscan.resources.Transmitter or None

The transmitter used by this output port. Will be None if the port does not exist.