"""Anomaly Map Generator for the STFPM 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.fromtypingimportDict,Tuple,Unionimporttorchimporttorch.nn.functionalasFfromomegaconfimportListConfigfromtorchimportTensor
[docs]defcompute_layer_map(self,teacher_features:Tensor,student_features:Tensor)->Tensor:"""Compute the layer map based on cosine similarity. Args: teacher_features (Tensor): Teacher features student_features (Tensor): Student features Returns: Anomaly score based on cosine similarity. """norm_teacher_features=F.normalize(teacher_features)norm_student_features=F.normalize(student_features)layer_map=0.5*torch.norm(norm_teacher_features-norm_student_features,p=2,dim=-3,keepdim=True)**2layer_map=F.interpolate(layer_map,size=self.image_size,align_corners=False,mode="bilinear")returnlayer_map
[docs]defcompute_anomaly_map(self,teacher_features:Dict[str,Tensor],student_features:Dict[str,Tensor])->torch.Tensor:"""Compute the overall anomaly map via element-wise production the interpolated anomaly maps. Args: teacher_features (Dict[str, Tensor]): Teacher features student_features (Dict[str, Tensor]): Student features Returns: Final anomaly map """batch_size=list(teacher_features.values())[0].shape[0]anomaly_map=torch.ones(batch_size,1,self.image_size[0],self.image_size[1])forlayerinteacher_features.keys():layer_map=self.compute_layer_map(teacher_features[layer],student_features[layer])anomaly_map=anomaly_map.to(layer_map.device)anomaly_map*=layer_mapreturnanomaly_map
[docs]def__call__(self,**kwds:Dict[str,Tensor])->torch.Tensor:"""Returns anomaly map. Expects `teach_features` and `student_features` keywords to be passed explicitly. Example: >>> anomaly_map_generator = AnomalyMapGenerator(image_size=tuple(hparams.model.input_size)) >>> output = self.anomaly_map_generator( teacher_features=teacher_features, student_features=student_features ) Raises: ValueError: `teach_features` and `student_features` keys are not found Returns: torch.Tensor: anomaly map """ifnot("teacher_features"inkwdsand"student_features"inkwds):raiseValueError(f"Expected keys `teacher_features` and `student_features. Found {kwds.keys()}")teacher_features:Dict[str,Tensor]=kwds["teacher_features"]student_features:Dict[str,Tensor]=kwds["student_features"]returnself.compute_anomaly_map(teacher_features,student_features)