anomalib.deploy

Functions for Inference and model deployment.

Subpackages

Submodules

Package Contents

Classes

Inferencer

Abstract class for the inference.

OpenVINOInferencer

OpenVINO implementation for the inference.

TorchInferencer

PyTorch implementation for the inference.

Functions

export_convert(model, input_size, onnx_path, export_path)

Export the model to onnx format and convert to OpenVINO IR.

get_model_metadata(→ Dict[str, torch.Tensor])

Get meta data related to normalization from model.

class anomalib.deploy.Inferencer

Bases: abc.ABC

Abstract class for the inference.

This is used by both Torch and OpenVINO inference.

abstract load_model(path: Union[str, pathlib.Path])

Load Model.

abstract pre_process(image: numpy.ndarray) Union[numpy.ndarray, torch.Tensor]

Pre-process.

abstract forward(image: Union[numpy.ndarray, torch.Tensor]) Union[numpy.ndarray, torch.Tensor]

Forward-Pass input to model.

abstract post_process(predictions: Union[numpy.ndarray, torch.Tensor], meta_data: Optional[Dict[str, Any]]) Dict[str, Any]

Post-Process.

predict(image: Union[str, numpy.ndarray, pathlib.Path], meta_data: Optional[Dict[str, Any]] = None) anomalib.post_processing.ImageResult

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.

  • meta_data – Meta-data information such as shape, threshold.

Returns

Prediction results to be visualized.

Return type

ImageResult

_superimpose_segmentation_mask(meta_data: dict, anomaly_map: numpy.ndarray, image: numpy.ndarray)

Superimpose segmentation mask on top of image.

Parameters
  • meta_data (dict) – Metadata of the image which contains the image size.

  • anomaly_map (np.ndarray) – Anomaly map which is used to extract segmentation mask.

  • image (np.ndarray) – Image on which segmentation mask is to be superimposed.

Returns

Image with segmentation mask superimposed.

Return type

np.ndarray

__call__(image: numpy.ndarray) anomalib.post_processing.ImageResult

Call predict on the Image.

Parameters

image (np.ndarray) – Input Image

Returns

Prediction results to be visualized.

Return type

ImageResult

_normalize(anomaly_maps: Union[torch.Tensor, numpy.ndarray], pred_scores: Union[torch.Tensor, numpy.float32], meta_data: Union[Dict, omegaconf.DictConfig]) Tuple[Union[numpy.ndarray, torch.Tensor], float]

Applies normalization and resizes the image.

Parameters
  • anomaly_maps (Union[Tensor, np.ndarray]) – Predicted raw anomaly map.

  • pred_scores (Union[Tensor, np.float32]) – Predicted anomaly score

  • meta_data (Dict) – Meta data. Post-processing step sometimes requires additional meta data such as image shape. This variable comprises such info.

Returns

Post processed predictions that are ready to be visualized and

predicted scores.

Return type

Tuple[Union[np.ndarray, Tensor], float]

_load_meta_data(path: Optional[Union[str, pathlib.Path]] = None) Union[omegaconf.DictConfig, Dict]

Loads the meta data from the given path.

Parameters

path (Optional[Union[str, Path]], optional) – Path to JSON file containing the metadata. If no path is provided, it returns an empty dict. Defaults to None.

Returns

Dictionary containing the metadata.

Return type

Union[DictConfig, Dict]

class anomalib.deploy.OpenVINOInferencer(config: Union[str, pathlib.Path, omegaconf.DictConfig, omegaconf.ListConfig], path: Union[str, pathlib.Path, Tuple[bytes, bytes]], meta_data_path: Union[str, pathlib.Path] = None)

Bases: anomalib.deploy.inferencers.base_inferencer.Inferencer

OpenVINO implementation for the inference.

Parameters
  • config (Union[str, Path, DictConfig, ListConfig]) – Configurable parameters that are used during the training stage.

  • path (Union[str, Path]) – Path to the openvino onnx, xml or bin file.

  • meta_data_path (Union[str, Path], optional) – Path to metadata file. Defaults to None.

load_model(path: Union[str, pathlib.Path, Tuple[bytes, bytes]])

Load the OpenVINO model.

Parameters

path (Union[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]]

pre_process(image: numpy.ndarray) numpy.ndarray

Pre process the input image by applying transformations.

Parameters

image (np.ndarray) – Input image.

Returns

pre-processed image.

Return type

np.ndarray

forward(image: numpy.ndarray) numpy.ndarray

Forward-Pass input tensor to the model.

Parameters

image (np.ndarray) – Input tensor.

Returns

Output predictions.

Return type

np.ndarray

post_process(predictions: numpy.ndarray, meta_data: Optional[Union[Dict, omegaconf.DictConfig]] = None) Dict[str, Any]

Post process the output predictions.

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

  • meta_data (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, Any]

class anomalib.deploy.TorchInferencer(config: Union[str, pathlib.Path, omegaconf.DictConfig, omegaconf.ListConfig], model_source: Union[str, pathlib.Path, anomalib.models.components.AnomalyModule], meta_data_path: Union[str, pathlib.Path] = None)

Bases: anomalib.deploy.inferencers.base_inferencer.Inferencer

PyTorch implementation for the inference.

Parameters
  • config (Union[str, Path, DictConfig, ListConfig]) – Configurable parameters that are used during the training stage.

  • model_source (Union[str, Path, AnomalyModule]) – Path to the model ckpt file or the Anomaly model.

  • meta_data_path (Union[str, Path], optional) – Path to metadata file. If none, it tries to load the params from the model state_dict. Defaults to None.

_load_meta_data(path: Optional[Union[str, pathlib.Path]] = None) Union[Dict, omegaconf.DictConfig]

Load metadata from file or from model state dict.

Parameters

path (Optional[Union[str, Path]], optional) – Path to metadata file. If none, it tries to load the params from the model state_dict. Defaults to None.

Returns

Dictionary containing the meta_data.

Return type

Dict

load_model(path: Union[str, pathlib.Path]) anomalib.models.components.AnomalyModule

Load the PyTorch model.

Parameters

path (Union[str, Path]) – Path to model ckpt file.

Returns

PyTorch Lightning model.

Return type

(AnomalyModule)

pre_process(image: numpy.ndarray) torch.Tensor

Pre process the input image by applying transformations.

Parameters

image (np.ndarray) – Input image

Returns

pre-processed image.

Return type

Tensor

forward(image: torch.Tensor) torch.Tensor

Forward-Pass input tensor to the model.

Parameters

image (Tensor) – Input tensor.

Returns

Output predictions.

Return type

Tensor

post_process(predictions: torch.Tensor, meta_data: Optional[Union[Dict, omegaconf.DictConfig]] = None) Dict[str, Any]

Post process the output predictions.

Parameters
  • predictions (Tensor) – Raw output predicted by the model.

  • meta_data (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, Union[str, float, np.ndarray]]

anomalib.deploy.export_convert(model: anomalib.models.components.AnomalyModule, input_size: Union[List[int], Tuple[int, int]], onnx_path: Union[str, pathlib.Path], export_path: Union[str, pathlib.Path])[source]

Export the model to onnx format and convert to OpenVINO IR.

Parameters
  • model (AnomalyModule) – Model to convert.

  • input_size (Union[List[int], Tuple[int, int]]) – Image size used as the input for onnx converter.

  • onnx_path (Union[str, Path]) – Path to output onnx model.

  • export_path (Union[str, Path]) – Path to exported OpenVINO IR.

anomalib.deploy.get_model_metadata(model: anomalib.models.components.AnomalyModule) Dict[str, torch.Tensor][source]

Get meta data related to normalization from model.

Parameters

model (AnomalyModule) – Anomaly model which contains metadata related to normalization.

Returns

metadata

Return type

Dict[str, Tensor]