Post Processing

Methods to help post-process raw model outputs.

class anomalib.post_processing.ImageResult(image: np.ndarray, pred_score: float, pred_label: str, anomaly_map: np.ndarray | None = None, gt_mask: np.ndarray | None = None, pred_mask: np.ndarray | None = None, gt_boxes: np.ndarray | None = None, pred_boxes: np.ndarray | None = None, box_labels: np.ndarray | None = None)[source]

Bases: object

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

anomalous_boxes: np.ndarray
anomaly_map: np.ndarray | None = None
box_labels: np.ndarray | None = None
gt_boxes: np.ndarray | None = None
gt_mask: np.ndarray | None = None
heat_map: np.ndarray
image: np.ndarray
normal_boxes: np.ndarray
pred_boxes: np.ndarray | None = None
pred_label: str
pred_mask: np.ndarray | None = None
pred_score: float
segmentations: np.ndarray
class anomalib.post_processing.NormalizationMethod(value)[source]

Bases: str, Enum

Normalization method for normalization.

CDF = 'cdf'
MIN_MAX = 'min_max'
NONE = 'none'
class anomalib.post_processing.ThresholdMethod(value)[source]

Bases: str, Enum

Threshold method to apply post-processing to the output predictions.

ADAPTIVE = 'adaptive'
MANUAL = 'manual'
class anomalib.post_processing.Visualizer(mode: str, task: TaskType)[source]

Bases: object

Class that handles the logic of composing the visualizations.

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

  • task (TaskType) – task type “segmentation”, “detection” or “classification”

static save(file_path: Path, image: ndarray) None[source]

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.

static show(title: str, image: ndarray, delay: int = 0) None[source]

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.

visualize_batch(batch: dict) Iterator[ndarray][source]

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) ndarray[source]

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.

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

Adds the anomalous label to the image.

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

Adds the normal label to the image.

anomalib.post_processing.anomaly_map_to_color_map(anomaly_map: ndarray, normalize: bool = True) 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: ndarray, threshold: float, kernel_size: int = 4) 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: ndarray, image: ndarray, alpha: float = 0.4, gamma: int = 0, normalize: bool = False) 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