:py:mod:`anomalib.post_processing` ================================== .. py:module:: anomalib.post_processing .. autoapi-nested-parse:: Methods to help post-process raw model outputs. Subpackages ----------- .. toctree:: :titlesonly: :maxdepth: 3 normalization/index.rst Submodules ---------- .. toctree:: :titlesonly: :maxdepth: 1 post_process/index.rst visualizer/index.rst Package Contents ---------------- Classes ~~~~~~~ .. autoapisummary:: anomalib.post_processing.ImageResult anomalib.post_processing.Visualizer Functions ~~~~~~~~~ .. autoapisummary:: anomalib.post_processing.add_anomalous_label anomalib.post_processing.add_normal_label anomalib.post_processing.anomaly_map_to_color_map anomalib.post_processing.compute_mask anomalib.post_processing.superimpose_anomaly_map .. py:function:: add_anomalous_label(image: numpy.ndarray, confidence: Optional[float] = None) Adds the anomalous label to the image. .. py:function:: add_normal_label(image: numpy.ndarray, confidence: Optional[float] = None) Adds the normal label to the image. .. py:function:: anomaly_map_to_color_map(anomaly_map: numpy.ndarray, normalize: bool = True) -> numpy.ndarray Compute anomaly color heatmap. :param anomaly_map: Final anomaly map computed by the distance metric. :type anomaly_map: np.ndarray :param normalize: Bool to normalize the anomaly map prior to applying the color map. Defaults to True. :type normalize: bool, optional :returns: [description] :rtype: np.ndarray .. py:function:: compute_mask(anomaly_map: numpy.ndarray, threshold: float, kernel_size: int = 4) -> numpy.ndarray Compute anomaly mask via thresholding the predicted anomaly map. :param anomaly_map: Anomaly map predicted via the model :type anomaly_map: np.ndarray :param threshold: Value to threshold anomaly scores into 0-1 range. :type threshold: float :param kernel_size: Value to apply morphological operations to the predicted mask. Defaults to 4. :type kernel_size: int :returns: Predicted anomaly mask .. py:function:: superimpose_anomaly_map(anomaly_map: numpy.ndarray, image: numpy.ndarray, alpha: float = 0.4, gamma: int = 0, normalize: bool = False) -> numpy.ndarray Superimpose anomaly map on top of in the input image. :param anomaly_map: Anomaly map :type anomaly_map: np.ndarray :param image: Input image :type image: np.ndarray :param alpha: Weight to overlay anomaly map on the input image. Defaults to 0.4. :type alpha: float, optional :param gamma: 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 :type gamma: int, optional :param normalize: whether or not the anomaly maps should be normalized to image min-max :returns: Image with anomaly map superimposed on top of it. :rtype: np.ndarray .. py:class:: ImageResult Collection of data needed to visualize the predictions for an image. .. py:attribute:: image :annotation: :numpy.ndarray .. py:attribute:: pred_score :annotation: :float .. py:attribute:: pred_label :annotation: :str .. py:attribute:: anomaly_map :annotation: :Optional[numpy.ndarray] .. py:attribute:: gt_mask :annotation: :Optional[numpy.ndarray] .. py:attribute:: pred_mask :annotation: :Optional[numpy.ndarray] .. py:attribute:: heat_map :annotation: :numpy.ndarray .. py:attribute:: segmentations :annotation: :numpy.ndarray .. py:method:: __post_init__() -> None Generate heatmap overlay and segmentations, convert masks to images. .. py:class:: Visualizer(mode: str, task: str) Class that handles the logic of composing the visualizations. :param mode: visualization mode, either "full" or "simple" :type mode: str :param task: task type, either "segmentation" or "classification" :type task: str .. py:method:: visualize_batch(batch: Dict) -> Iterator[numpy.ndarray] Generator that yields a visualization result for each item in the batch. :param batch: Dictionary containing the ground truth and predictions of a batch of images. :type batch: Dict :returns: Generator that yields a display-ready visualization for each image. .. py:method:: visualize_image(image_result: ImageResult) -> numpy.ndarray Generate the visualization for an image. :param image_result: GT and Prediction data for a single image. :type image_result: ImageResult :returns: The full or simple visualization for the image, depending on the specified mode. .. py:method:: _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). :param image_result: GT and Prediction data for a single image. :type image_result: ImageResult :returns: An image showing the full set of visualizations for the input image. .. py:method:: _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. :param image_result: GT and Prediction data for a single image. :type image_result: ImageResult :returns: An image showing the simple visualization for the input image. .. py:method:: show(title: str, image: numpy.ndarray, delay: int = 0) -> None :staticmethod: Show an image on the screen. :param title: Title that will be given to the window showing the image. :type title: str :param image: Image that will be shown in the window. :type image: np.ndarray :param delay: Delay in milliseconds to wait for keystroke. 0 for infinite. :type delay: int .. py:method:: save(file_path: pathlib.Path, image: numpy.ndarray) -> None :staticmethod: Save an image to the file system. :param file_path: Path to which the image will be saved. :type file_path: Path :param image: Image that will be saved to the file system. :type image: np.ndarray