Source code for anomalib.models.patchcore.anomaly_map
"""Anomaly Map Generator for the PatchCore model implementation."""# Copyright (C) 2022 Intel Corporation# SPDX-License-Identifier: Apache-2.0fromtypingimportTuple,Unionimporttorchimporttorch.nn.functionalasFfromomegaconfimportListConfigfromtorchimportnnfromanomalib.models.componentsimportGaussianBlur2d
[docs]defforward(self,**kwargs:torch.Tensor)->Tuple[torch.Tensor,torch.Tensor]:"""Returns anomaly_map and anomaly_score. Expects `patch_scores` keyword to be passed explicitly Expects `feature_map_shape` keyword to be passed explicitly Example >>> anomaly_map_generator = AnomalyMapGenerator(input_size=input_size) >>> map, score = anomaly_map_generator(patch_scores=numpy_array, feature_map_shape=feature_map_shape) Raises: ValueError: If `patch_scores` key is not found Returns: Tuple[torch.Tensor, torch.Tensor]: anomaly_map, anomaly_score """if"patch_scores"notinkwargs:raiseValueError(f"Expected key `patch_scores`. Found {kwargs.keys()}")if"feature_map_shape"notinkwargs:raiseValueError(f"Expected key `feature_map_shape`. Found {kwargs.keys()}")patch_scores=kwargs["patch_scores"]feature_map_shape=kwargs["feature_map_shape"]anomaly_map=self.compute_anomaly_map(patch_scores,feature_map_shape)anomaly_score=self.compute_anomaly_score(patch_scores)returnanomaly_map,anomaly_score