class_docstr: Patchcore Module.
class_docstr_tok: ["Patchcore","Module","."]
docstr: Return Embedding during training, or a tuple of anomaly map and anomaly score during testing.
  
  Steps performed:
  1. Get features from a CNN.
  2. Generate embedding based on the features.
  3. Compute anomaly map in test mode.
  
  Args:
      input_tensor (Tensor): Input tensor
  
  Returns:
      Tensor | dict[str, Tensor]: Embedding for training,
          anomaly map and anomaly score for testing.
docstr_tok: ["Return","Embedding","during","training",",","or","a","tuple","of","anomaly","map","and","anomaly","score","during","testing",".","Steps","performed",":","1",".","Get","features","from","a","CNN",".","2",".","Generate","embedding","based","on","the","features",".","3",".","Compute","anomaly","map","in","test","mode",".","Args",":","input_tensor","(","Tensor",")",":","Input","tensor","Returns",":","Tensor","|","dict","[","str",",","Tensor","]",":","Embedding","for","training",",","anomaly","map","and","anomaly","score","for","testing","."]
code: def forward(self, input_tensor: Tensor) -> Tensor | dict[str, Tensor]:
      """Return Embedding during training, or a tuple of anomaly map and anomaly score during testing.
  
      Steps performed:
      1. Get features from a CNN.
      2. Generate embedding based on the features.
      3. Compute anomaly map in test mode.
  
      Args:
          input_tensor (Tensor): Input tensor
  
      Returns:
          Tensor | dict[str, Tensor]: Embedding for training,
              anomaly map and anomaly score for testing.
      """
      if self.tiler:
          input_tensor = self.tiler.tile(input_tensor)
  
      with torch.no_grad():
          features = self.feature_extractor(input_tensor)
  
      features = {
          layer: self.feature_pooler(feature)
          for layer, feature in features.items()
      }
      embedding = self.generate_embedding(features)
  
      if self.tiler:
          embedding = self.tiler.untile(embedding)
  
      batch_size, _, width, height = embedding.shape
      embedding = self.reshape_embedding(embedding)
  
      if self.training:
          output = embedding
      else:
          # apply nearest neighbor search
          patch_scores, locations = self.nearest_neighbors(
              embedding=embedding, n_neighbors=1
          )
          # reshape to batch dimension
          patch_scores = patch_scores.reshape((batch_size, -1))
          locations = locations.reshape((batch_size, -1))
          # compute anomaly score
          pred_score = self.compute_anomaly_score(
              patch_scores, locations, embedding
          )
          # reshape to w, h
          patch_scores = patch_scores.reshape(
              (batch_size, 1, width, height)
          )
          # get anomaly map
          anomaly_map = self.anomaly_map_generator(patch_scores)
  
          output = {
              "anomaly_map": anomaly_map,
              "pred_score": pred_score,
          }
  
      return output
  
file_path: anomalib/src/anomalib/models/patchcore/torch_model.py
imports: from __future__ import annotations
  import torch
  import torch.nn.functional
  from torch import Tensor, nn
  from anomalib.models.components import DynamicBufferModule, FeatureExtractor, KCenterGreedy
  from anomalib.models.patchcore.anomaly_map import AnomalyMapGenerator
  from anomalib.pre_processing import Tiler
label_desc: Use image node_id 2 for calling the PatchcoreModel obj's underlying member method code with example usage: obj.forward(input_tensor) and returns: output