Source code for anomalib.models.patchcore.anomaly_map
"""Anomaly Map Generator for the PatchCore model implementation."""# Copyright (C) 2020 Intel Corporation## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions# and limitations under the License.fromtypingimportTuple,Unionimporttorchimporttorch.nn.functionalasFfromkornia.filtersimportgaussian_blur2dfromomegaconfimportListConfig
[docs]def__call__(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