anomalib.post_processing

Methods to help post-process raw model outputs.

Subpackages

Submodules

Package Contents

Classes

ImageResult

Collection of data needed to visualize the predictions for an image.

Visualizer

Class that handles the logic of composing the visualizations.

Functions

add_anomalous_label(image[, confidence])

Adds the anomalous label to the image.

add_normal_label(image[, confidence])

Adds the normal label to the image.

anomaly_map_to_color_map(→ numpy.ndarray)

Compute anomaly color heatmap.

compute_mask(→ numpy.ndarray)

Compute anomaly mask via thresholding the predicted anomaly map.

superimpose_anomaly_map(→ numpy.ndarray)

Superimpose anomaly map on top of in the input image.

anomalib.post_processing.add_anomalous_label(image: numpy.ndarray, confidence: Optional[float] = None)[source]

Adds the anomalous label to the image.

anomalib.post_processing.add_normal_label(image: numpy.ndarray, confidence: Optional[float] = None)[source]

Adds the normal label to the image.

anomalib.post_processing.anomaly_map_to_color_map(anomaly_map: numpy.ndarray, normalize: bool = True) numpy.ndarray[source]

Compute anomaly color heatmap.

Parameters
  • anomaly_map (np.ndarray) – Final anomaly map computed by the distance metric.

  • normalize (bool, optional) – Bool to normalize the anomaly map prior to applying the color map. Defaults to True.

Returns

[description]

Return type

np.ndarray

anomalib.post_processing.compute_mask(anomaly_map: numpy.ndarray, threshold: float, kernel_size: int = 4) numpy.ndarray[source]

Compute anomaly mask via thresholding the predicted anomaly map.

Parameters
  • anomaly_map (np.ndarray) – Anomaly map predicted via the model

  • threshold (float) – Value to threshold anomaly scores into 0-1 range.

  • kernel_size (int) – Value to apply morphological operations to the predicted mask. Defaults to 4.

Returns

Predicted anomaly mask

anomalib.post_processing.superimpose_anomaly_map(anomaly_map: numpy.ndarray, image: numpy.ndarray, alpha: float = 0.4, gamma: int = 0, normalize: bool = False) numpy.ndarray[source]

Superimpose anomaly map on top of in the input image.

Parameters
  • anomaly_map (np.ndarray) – Anomaly map

  • image (np.ndarray) – Input image

  • alpha (float, optional) – Weight to overlay anomaly map on the input image. Defaults to 0.4.

  • gamma (int, optional) – Value to add to the blended image to smooth the processing. Defaults to 0. Overall, the formula to compute the blended image is I’ = (alpha*I1 + (1-alpha)*I2) + gamma

  • normalize – whether or not the anomaly maps should be normalized to image min-max

Returns

Image with anomaly map superimposed on top of it.

Return type

np.ndarray

class anomalib.post_processing.ImageResult[source]

Collection of data needed to visualize the predictions for an image.

image :numpy.ndarray
pred_score :float
pred_label :str
anomaly_map :Optional[numpy.ndarray]
gt_mask :Optional[numpy.ndarray]
pred_mask :Optional[numpy.ndarray]
heat_map :numpy.ndarray
segmentations :numpy.ndarray
__post_init__() None

Generate heatmap overlay and segmentations, convert masks to images.

class anomalib.post_processing.Visualizer(mode: str, task: str)[source]

Class that handles the logic of composing the visualizations.

Parameters
  • mode (str) – visualization mode, either “full” or “simple”

  • task (str) – task type, either “segmentation” or “classification”

visualize_batch(batch: Dict) Iterator[numpy.ndarray]

Generator that yields a visualization result for each item in the batch.

Parameters

batch (Dict) – Dictionary containing the ground truth and predictions of a batch of images.

Returns

Generator that yields a display-ready visualization for each image.

visualize_image(image_result: ImageResult) numpy.ndarray

Generate the visualization for an image.

Parameters

image_result (ImageResult) – GT and Prediction data for a single image.

Returns

The full or simple visualization for the image, depending on the specified mode.

_visualize_full(image_result: ImageResult) numpy.ndarray

Generate the full set of visualization for an image.

The full visualization mode shows a grid with subplots that contain the original image, the GT mask (if available), the predicted heat map, the predicted segmentation mask (if available), and the predicted segmentations (if available).

Parameters

image_result (ImageResult) – GT and Prediction data for a single image.

Returns

An image showing the full set of visualizations for the input image.

_visualize_simple(image_result: ImageResult) numpy.ndarray

Generate a simple visualization for an image.

The simple visualization mode only shows the model’s predictions in a single image.

Parameters

image_result (ImageResult) – GT and Prediction data for a single image.

Returns

An image showing the simple visualization for the input image.

static show(title: str, image: numpy.ndarray, delay: int = 0) None

Show an image on the screen.

Parameters
  • title (str) – Title that will be given to the window showing the image.

  • image (np.ndarray) – Image that will be shown in the window.

  • delay (int) – Delay in milliseconds to wait for keystroke. 0 for infinite.

static save(file_path: pathlib.Path, image: numpy.ndarray) None

Save an image to the file system.

Parameters
  • file_path (Path) – Path to which the image will be saved.

  • image (np.ndarray) – Image that will be saved to the file system.