"""Anomaly Map Generator for CFlow 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.fromtypingimportList,Tuple,Union,castimporttorchimporttorch.nn.functionalasFfromomegaconfimportListConfigfromtorchimportTensor
[docs]defcompute_anomaly_map(self,distribution:Union[List[Tensor],List[List]],height:List[int],width:List[int])->Tensor:"""Compute the layer map based on likelihood estimation. Args: distribution: Probability distribution for each decoder block height: blocks height width: blocks width Returns: Final Anomaly Map """test_map:List[Tensor]=[]forlayer_idxinrange(len(self.pool_layers)):test_norm=torch.tensor(distribution[layer_idx],dtype=torch.double)# pylint: disable=not-callabletest_norm-=torch.max(test_norm)# normalize likelihoods to (-Inf:0] by subtracting a constanttest_prob=torch.exp(test_norm)# convert to probs in range [0:1]test_mask=test_prob.reshape(-1,height[layer_idx],width[layer_idx])# upsampletest_map.append(F.interpolate(test_mask.unsqueeze(1),size=self.image_size,mode="bilinear",align_corners=True).squeeze())# score aggregationscore_map=torch.zeros_like(test_map[0])forlayer_idxinrange(len(self.pool_layers)):score_map+=test_map[layer_idx]score_mask=score_map# invert probs to anomaly scoresanomaly_map=score_mask.max()-score_maskreturnanomaly_map
[docs]def__call__(self,**kwargs:Union[List[Tensor],List[int],List[List]])->Tensor:"""Returns anomaly_map. Expects `distribution`, `height` and 'width' keywords to be passed explicitly Example >>> anomaly_map_generator = AnomalyMapGenerator(image_size=tuple(hparams.model.input_size), >>> pool_layers=pool_layers) >>> output = self.anomaly_map_generator(distribution=dist, height=height, width=width) Raises: ValueError: `distribution`, `height` and 'width' keys are not found Returns: torch.Tensor: anomaly map """ifnot("distribution"inkwargsand"height"inkwargsand"width"inkwargs):raiseKeyError(f"Expected keys `distribution`, `height` and `width`. Found {kwargs.keys()}")# placate mypydistribution:List[Tensor]=cast(List[Tensor],kwargs["distribution"])height:List[int]=cast(List[int],kwargs["height"])width:List[int]=cast(List[int],kwargs["width"])returnself.compute_anomaly_map(distribution,height,width)