anomalib.deploy.inferencers.base

Base Inferencer for Torch and OpenVINO.

Module Contents

Classes

Inferencer

Abstract class for the inference.

class anomalib.deploy.inferencers.base.Inferencer[source]

Bases: abc.ABC

Abstract class for the inference.

This is used by both Torch and OpenVINO inference.

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

Load Model.

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

Pre-process.

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

Forward-Pass input to model.

abstract post_process(self, predictions: Union[numpy.ndarray, torch.Tensor], meta_data: Optional[Dict]) Tuple[numpy.ndarray, float][source]

Post-Process.

predict(self, image: Union[str, numpy.ndarray, pathlib.Path], superimpose: bool = True, meta_data: Optional[dict] = None, overlay_mask: bool = False) Tuple[numpy.ndarray, float][source]

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.

  • superimpose (bool) – If this is set to True, output predictions will be superimposed onto the original image. If false, predict method will return the raw heatmap.

  • overlay_mask (bool) – If this is set to True, output segmentation mask on top of image.

Returns

Output predictions to be visualized.

Return type

np.ndarray

_superimpose_segmentation_mask(self, meta_data: dict, anomaly_map: numpy.ndarray, image: numpy.ndarray)[source]

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__(self, image: numpy.ndarray) Tuple[numpy.ndarray, float][source]

Call predict on the Image.

Parameters

image (np.ndarray) – Input Image

Returns

Output predictions to be visualized

Return type

np.ndarray

_normalize(self, 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][source]

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(self, path: Optional[Union[str, pathlib.Path]] = None) Union[omegaconf.DictConfig, Dict[str, Union[float, numpy.ndarray, torch.Tensor]]][source]

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]