minerva.engines.slidingwindow_inferencer_engine

Classes

SlidingWindowInferencer

This class acts as a normal L.LightningModule that wraps a

SlidingWindowInferencerEngine

Main interface for Engine classes. Engines are used to alter the behavior of a model's prediction.

Module Contents

class minerva.engines.slidingwindow_inferencer_engine.SlidingWindowInferencer(model, strides, input_shape, output_shape=None, weight_function=None, padding=None, return_tuple=None)[source]

Bases: minerva.engines.engine._Inferencer

This class acts as a normal L.LightningModule that wraps a SimpleSupervisedModel model allowing it to perform inference with a custom engine (e.g., PatchInferencerEngine, SlidingWindowInferencerEngine). This is useful when the model’s default input size is smaller than the desired input size (sample size). In this case, the engine split the input tensor into patches, perform inference in each patch, and combine them into a single output of the desired size. The combination of patches can be parametrized by a weight_function allowing a customizable combination of patches (e.g, combining using weighted average). It is important to note that only model’s forward are wrapped, and, thus, any method that requires the forward method (e.g., training_step, predict_step) will be performed in patches, transparently to the user.

Wrap a SimpleSupervisedModel model’s forward method to perform inference with sliding windows, transparently traversing the input tensor, performing inference in windows, and combining them into a single output of the desired size.

Parameters

modelSimpleSupervisedModel

Model to be wrapped.

stridesTuple[int, …]

Sliding window stride for each dimension of the input data, if data

input_shapeTuple[int, …]

Expected input shape of the wrapped model.

output_shapeTuple[int, …], optional

Expected output shape of the wrapped model. For models that return logits (e.g., classification models), the output_shape must include an additional dimension at the beginning to accommodate the number of output classes. For example, if the model processes an input tensor of shape (1, 128, 128) and outputs logits for 10 classes, the expected output_shape should be (10, 1, 128, 128). If the model does not return logits (e.g., return a tensor after applying an argmax operation, or a regression models that usually returns a tensor with the same shape as the input tensor), the output_shape should have the same number of dimensions as the input shape. Defaults to None, which assumes the output shape is the same as the input_shape parameter.

weight_function: Callable[[Tuple[int, …]], torch.Tensor], optional

Function that receives a tensor shape and returns the weights for each position of a tensor with the given shape. Useful when regions of the inference present diminishing performance when getting closer to borders, for instance.

paddingDict[str, Any], optional
Dictionary describing padding strategy. Keys:
  • pad (mandatory): tuple with pad width (int) for each

    dimension, e.g.(0, 3, 3) when working with a tensor with 3 dimensions.

  • mode (optional): ‘constant’, ‘reflect’, ‘replicate’ or

    ‘circular’. Defaults to ‘constant’.

  • value (optional): fill value for ‘constant’. Defaults to 0.

If None, no padding is applied.

return_tuple: int, optional

Some models may return multiple outputs for a single sample (e.g., outputs from multiple auxiliary heads). This parameter is a integer that defines the number of outputs the model generates. By default, it is None, which indicates that the model produces a single output for a single input. When set, it indicates the number of outputs the model produces.

inferencer
model
Parameters:
  • model (minerva.models.nets.base.SimpleSupervisedModel)

  • strides (Tuple[int, Ellipsis])

  • input_shape (Tuple[int, Ellipsis])

  • output_shape (Optional[Tuple[int, Ellipsis]])

  • weight_function (Optional[Callable[[Tuple[int, Ellipsis]], torch.Tensor]])

  • padding (Optional[Dict[str, Any]])

  • return_tuple (Optional[int])

class minerva.engines.slidingwindow_inferencer_engine.SlidingWindowInferencerEngine(strides, input_shape, output_shape=None, padding=None, weight_function=None, return_tuple=None)[source]

Bases: minerva.engines.engine._Engine

Main interface for Engine classes. Engines are used to alter the behavior of a model’s prediction. An engine should be able to take a model and input data x and return a prediction. An use case for Engines is patched inference, where the model’s default input size is smaller them the desired input size. The engine can be used to make predictions in patches and combine this predictions in to a single output.

Parameters

strides: Tuple[int, …]

input_shapeTuple[int, …]

Shape of each patch to process.

output_shapeTuple[int, …], optional

Expected output shape of the model. For models that return logits, the output_shape must include an additional dimension at the beginning to accommodate the number of output classes. Else, the output_shape should have the same number of dimensions as the input_shape (i.e., no logits are returned). Defaults to input_shape.

paddingDict[str, Any], optional
Padding configuration with keys:
  • ‘pad’: Tuple of padding for each expected final dimension,

    e.g., (0, 512, 512) - (c, h, w).

  • ‘mode’: Padding mode, e.g., ‘constant’, ‘reflect’.

  • ‘value’: Padding value if mode is ‘constant’.

Defaults to None, which means no padding is applyied.

weight_functionCallable, optional

Function to calculate the weight of each patch. Defaults to None.

return_tupleint, optional

Number of outputs to return. This is useful when the model returns multiple outputs for a single input (e.g., from multiple auxiliary heads). Defaults to None.

__call__(model, x)[source]

Perform inference in sliding windows, from the input tensor x using the model model.

Parameters

model: Union[L.LightningModule, torch.nn.Module]

Model to perform inference.

xtorch.Tensor

Input tensor of the sample. It can be a single sample or a batch of samples.

Parameters:
  • model (Union[lightning.LightningModule, torch.nn.Module])

  • x (torch.Tensor)

_combine_patches(patches, weight_distribution, index, ref_shape)[source]

Combines each patch extracted to obtain final result. The combination is done by placing each inference result in its respective output place, the weight distribution is also placed on a weight accumulator. After all patches have been accumulated, the weighted average of the resulting volume is computed.

Parameters:
  • patches (torch.Tensor)

  • weight_distribution (torch.Tensor)

  • index (Tuple[int, Ellipsis])

  • ref_shape (Tuple[int, Ellipsis])

Return type:

torch.Tensor

_compute_base_padding(tensor)[source]

Computes the padding for the base patch set based on the input tensor shape and the model’s input shape.

Parameters:

tensor (torch.Tensor)

_compute_output_shape(tensor)[source]

Computes SlidingWindowInferencer output shape based on input tensor shape, and model’s input and output shapes.

Parameters:

tensor (torch.Tensor)

Return type:

Tuple[int]

_extract_patches(data)[source]

Sliding window patch extraction method.

Parameters:

data (torch.Tensor)

Return type:

Tuple[torch.Tensor, Tuple[int]]

input_shape
logits_dim
output_shape
output_simplified_shape = ()
return_tuple = None
strides
weight_function = None
Parameters:
  • strides (Tuple[int, Ellipsis])

  • input_shape (Tuple[int, Ellipsis])

  • output_shape (Optional[Tuple[int, Ellipsis]])

  • padding (Optional[Dict[str, Any]])

  • weight_function (Optional[Callable])

  • return_tuple (Optional[int])