Deployment#

Functions for Inference and model deployment.

class anomalib.deploy.ExportType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#

Bases: str, Enum

Model export type.

Examples

>>> from anomalib.deploy import ExportType
>>> ExportType.ONNX
'onnx'
>>> ExportType.OPENVINO
'openvino'
>>> ExportType.TORCH
'torch'
class anomalib.deploy.OpenVINOInferencer(path, metadata=None, device='AUTO', task=None, config=None)#

Bases: Inferencer

OpenVINO implementation for the inference.

Parameters:
  • path (str | Path) – Path to the openvino onnx, xml or bin file.

  • metadata (str | Path | dict, optional) – Path to metadata file or a dict object defining the metadata. Defaults to None.

  • device (str | None, optional) – Device to run the inference on (AUTO, CPU, GPU, NPU). Defaults to AUTO.

  • task (TaskType | None, optional) – Task type. Defaults to None.

  • config (dict | None, optional) – Configuration parameters for the inference Defaults to None.

Examples

Assume that we have an OpenVINO IR model and metadata files in the following structure:

$ tree weights
./weights
├── model.bin
├── model.xml
└── metadata.json

We could then create OpenVINOInferencer as follows:

>>> from anomalib.deploy.inferencers import OpenVINOInferencer
>>> inferencer = OpenVINOInferencer(
...     path="weights/model.xml",
...     metadata="weights/metadata.json",
...     device="CPU",
... )

This will ensure that the model is loaded on the CPU device and the metadata is loaded from the metadata.json file. To make a prediction, we can simply call the predict method:

>>> prediction = inferencer.predict(image="path/to/image.jpg")

Alternatively we can also pass the image as a PIL image or numpy array:

>>> from PIL import Image
>>> image = Image.open("path/to/image.jpg")
>>> prediction = inferencer.predict(image=image)
>>> import numpy as np
>>> image = np.random.rand(224, 224, 3)
>>> prediction = inferencer.predict(image=image)

prediction will be an ImageResult object containing the prediction results. For example, to visualize the heatmap, we can do the following:

>>> from matplotlib import pyplot as plt
>>> plt.imshow(result.heatmap)

It is also possible to visualize the true and predicted masks if the task is TaskType.SEGMENTATION:

>>> plt.imshow(result.gt_mask)
>>> plt.imshow(result.pred_mask)
forward(image)#

Forward-Pass input tensor to the model.

Parameters:

image (np.ndarray) – Input tensor.

Returns:

Output predictions.

Return type:

np.ndarray

load_model(path)#

Load the OpenVINO model.

Parameters:

path (str | Path | tuple[bytes, bytes]) – Path to the onnx or xml and bin files or tuple of .xml and .bin data as bytes.

Returns:

Input and Output blob names

together with the Executable network.

Return type:

[tuple[str, str, ExecutableNetwork]]

post_process(predictions, metadata=None)#

Post process the output predictions.

Parameters:
  • predictions (np.ndarray) – Raw output predicted by the model.

  • metadata (Dict, optional) – Metadata. Post-processing step sometimes requires additional metadata such as image shape. This variable comprises such info. Defaults to None.

Returns:

Post processed prediction results.

Return type:

dict[str, Any]

pre_process(image)#

Pre-process the input image by applying transformations.

Parameters:

image (np.ndarray) – Input image.

Returns:

pre-processed image.

Return type:

np.ndarray

predict(image, metadata=None)#

Perform a prediction for a given input image.

The main workflow is (i) pre-processing, (ii) forward-pass, (iii) post-process.

Parameters:
  • image (Union[str, np.ndarray]) – Input image whose output is to be predicted. It could be either a path to image or numpy array itself.

  • metadata (dict[str, Any] | None) – Metadata information such as shape, threshold.

Returns:

Prediction results to be visualized.

Return type:

ImageResult

class anomalib.deploy.TorchInferencer(path, device='auto')#

Bases: Inferencer

PyTorch implementation for the inference.

Parameters:
  • path (str | Path) – Path to Torch model weights.

  • device (str) – Device to use for inference. Options are auto, cpu, cuda. Defaults to auto.

Examples

Assume that we have a Torch pt model and metadata files in the following structure:

>>> from anomalib.deploy.inferencers import TorchInferencer
>>> inferencer = TorchInferencer(path="path/to/torch/model.pt", device="cpu")

This will ensure that the model is loaded on the CPU device. To make a prediction, we can simply call the predict method:

>>> from anomalib.data.utils import read_image
>>> image = read_image("path/to/image.jpg")
>>> result = inferencer.predict(image)

result will be an ImageResult object containing the prediction results. For example, to visualize the heatmap, we can do the following:

>>> from matplotlib import pyplot as plt
>>> plt.imshow(result.heatmap)

It is also possible to visualize the true and predicted masks if the task is TaskType.SEGMENTATION:

>>> plt.imshow(result.gt_mask)
>>> plt.imshow(result.pred_mask)
forward(image)#

Forward-Pass input tensor to the model.

Parameters:

image (torch.Tensor) – Input tensor.

Returns:

Output predictions.

Return type:

Tensor

load_model(path)#

Load the PyTorch model.

Parameters:

path (str | Path) – Path to the Torch model.

Returns:

Torch model.

Return type:

(nn.Module)

post_process(predictions, metadata=None)#

Post process the output predictions.

Parameters:
  • predictions (Tensor | list[torch.Tensor] | dict[str, torch.Tensor]) – Raw output predicted by the model.

  • metadata (dict, optional) – Meta data. Post-processing step sometimes requires additional meta data such as image shape. This variable comprises such info. Defaults to None.

Returns:

Post processed prediction results.

Return type:

dict[str, str | float | np.ndarray]

pre_process(image)#

Pre process the input image.

Parameters:

image (np.ndarray) – Input image

Returns:

pre-processed image.

Return type:

Tensor

predict(image, metadata=None)#

Perform a prediction for a given input image.

The main workflow is (i) pre-processing, (ii) forward-pass, (iii) post-process.

Parameters:
  • image (Union[str, np.ndarray]) – Input image whose output is to be predicted. It could be either a path to image or numpy array itself.

  • metadata (dict[str, Any] | None) – Metadata information such as shape, threshold.

Returns:

Prediction results to be visualized.

Return type:

ImageResult