class_docstr: Generate Anomaly Heatmap and segmentation.
class_docstr_tok: ["Generate","Anomaly","Heatmap","and","segmentation","."]
docstr: Generate a likelihood-based anomaly map, from latent variables.
  Args:
      latent_variables: List of latent variables from the UFlow model. Each element is a tensor of shape
      (N, Cl, Hl, Wl), where N is the batch size, Cl is the number of channels, and Hl and Wl are the height and
      width of the latent variables, respectively, for each scale l.
  
  Returns:
      Final Anomaly Map. Tensor of shape (N, 1, H, W), where N is the batch size, and H and W are the height and
      width of the input image, respectively.
docstr_tok: ["Generate","a","likelihood-based","anomaly","map",",","from","latent","variables",".","Args",":","latent_variables",":","List","of","latent","variables","from","the","UFlow","model",".","Each","element","is","a","tensor","of","shape","(","N",",","Cl",",","Hl",",","Wl",")",",","where","N","is","the","batch","size",",","Cl","is","the","number","of","channels",",","and","Hl","and","Wl","are","the","height","and","width","of","the","latent","variables",",","respectively",",","for","each","scale","l.","Returns",":","Final","Anomaly","Map",".","Tensor","of","shape","(","N",",","1",",","H",",","W",")",",","where","N","is","the","batch","size",",","and","H","and","W","are","the","height","and","width","of","the","input","image",",","respectively","."]
code: def compute_anomaly_map(
      self, latent_variables: list[Tensor]
  ) -> Tensor:
      """
      Generate a likelihood-based anomaly map, from latent variables.
      Args:
          latent_variables: List of latent variables from the UFlow model. Each element is a tensor of shape
          (N, Cl, Hl, Wl), where N is the batch size, Cl is the number of channels, and Hl and Wl are the height and
          width of the latent variables, respectively, for each scale l.
  
      Returns:
          Final Anomaly Map. Tensor of shape (N, 1, H, W), where N is the batch size, and H and W are the height and
          width of the input image, respectively.
      """
  
      likelihoods = []
      for z in latent_variables:
          # Mean prob by scale. Likelihood is actually with sum instead of mean. Using mean to avoid numerical issues.
          # Also, this way all scales have the same weight, and it does not depend on the number of channels
          log_prob_i = -torch.mean(z**2, dim=1, keepdim=True) * 0.5
          prob_i = torch.exp(log_prob_i)
          likelihoods.append(
              F.interpolate(
                  prob_i,
                  size=self.input_size,
                  mode="bilinear",
                  align_corners=False,
              )
          )
      anomaly_map = 1 - torch.mean(
          torch.stack(likelihoods, dim=-1), dim=-1
      )
      return anomaly_map
  
file_path: anomalib/src/anomalib/models/uflow/anomaly_map.py
imports: from __future__ import annotations
  from typing import List
  import numpy
  import scipy.stats
  import torch
  import torch.nn.functional
  from mpmath import binomial, mp
  from omegaconf import ListConfig
  from scipy import integrate
  from torch import Tensor, nn
label_desc: Use image node_id 3 for calling the AnomalyMapGenerator obj's underlying member method code with example usage: obj.compute_anomaly_map(latent_variables) and returns: anomaly_map