def compute_anomaly_map(
        self,
        distribution: list[torch.Tensor],
        height: list[int],
        width: list[int],
        image_size: tuple[int, int] | torch.Size | None,
    ) -> torch.Tensor:
        """Compute anomaly map from layer-wise likelihood distributions.

        The method normalizes likelihood scores from each layer, upsamples them to
        a common size, and combines them into a final anomaly map.

        Args:
            distribution (list[torch.Tensor]): List of likelihood distributions for
                each layer.
            height (list[int]): List of feature map heights for each layer.
            width (list[int]): List of feature map widths for each layer.
            image_size (tuple[int, int] | torch.Size | None): Target size for the
                output anomaly map. If None, keeps the original size.

        Returns:
            torch.Tensor: Anomaly map tensor where higher values indicate higher
                likelihood of anomaly.
        """
        layer_maps: list[torch.Tensor] = []
        for layer_idx in range(len(self.pool_layers)):
            layer_distribution = distribution[layer_idx].clone().detach()
            # Normalize the likelihoods to (-Inf:0] and convert to probs in range [0:1]
            layer_probabilities = torch.exp(layer_distribution - layer_distribution.max())
            layer_map = layer_probabilities.reshape(-1, height[layer_idx], width[layer_idx])
            # upsample
            if image_size is not None:
                layer_map = F.interpolate(
                    layer_map.unsqueeze(1),
                    size=image_size,
                    mode="bilinear",
                    align_corners=True,
                ).squeeze(1)
            layer_maps.append(layer_map)
        # score aggregation
        score_map = torch.zeros_like(layer_maps[0])
        for layer_idx in range(len(self.pool_layers)):
            score_map += layer_maps[layer_idx]

        # Invert probs to anomaly scores
        return score_map.max() - score_map
Compute anomaly map from layer-wise likelihood distributions.

        The method normalizes likelihood scores from each layer, upsamples them to
        a common size, and combines them into a final anomaly map.

        Args:
            distribution (list[torch.Tensor]): List of likelihood distributions for
                each layer.
            height (list[int]): List of feature map heights for each layer.
            width (list[int]): List of feature map widths for each layer.
            image_size (tuple[int, int] | torch.Size | None): Target size for the
                output anomaly map. If None, keeps the original size.

        Returns:
            torch.Tensor: Anomaly map tensor where higher values indicate higher
                likelihood of anomaly.
        
src/anomalib/models/image/cflow/anomaly_map.py