def validate_anomaly_map(anomaly_map: torch.Tensor | np.ndarray | None) -> Mask | None:
        """Validate the anomaly map for a batch.

        This method validates batches of anomaly maps. It handles:
            - Conversion from numpy arrays
            - Shape normalization
            - Type conversion to float32

        Args:
            anomaly_map (``torch.Tensor`` | ``np.ndarray`` | ``None``): Input anomaly map.

        Returns:
            ``Mask`` | ``None``: Validated anomaly map as a torchvision Mask object, or None.

        Raises:
            ValueError: If ``anomaly_map`` cannot be converted to tensor or has invalid shape.

        Example:
            Validate anomaly maps::

                >>> import torch
                >>> from anomalib.data.validators import ImageBatchValidator
                >>> anomaly_map = torch.rand(4, 224, 224)
                >>> validated = ImageBatchValidator.validate_anomaly_map(anomaly_map)
                >>> validated.shape
                torch.Size([4, 224, 224])
        """
        if anomaly_map is None:
            return None
        if not isinstance(anomaly_map, torch.Tensor):
            try:
                anomaly_map = torch.tensor(anomaly_map)
            except Exception as e:
                msg = "Failed to convert anomaly_map to a torch.Tensor."
                raise ValueError(msg) from e
        if anomaly_map.ndim not in {2, 3, 4}:
            msg = f"Anomaly map must have shape [H, W] or [N, H, W] or [N, 1, H, W], got shape {anomaly_map.shape}."
            raise ValueError(msg)
        if anomaly_map.ndim == 2:
            anomaly_map = anomaly_map.unsqueeze(0)
        if anomaly_map.ndim == 4:
            if anomaly_map.shape[1] != 1:
                msg = f"Anomaly map must have 1 channel, got {anomaly_map.shape[1]}."
                raise ValueError(msg)
            anomaly_map = anomaly_map.squeeze(1)
        return Mask(anomaly_map, dtype=torch.float32)
Validate the anomaly map for a batch.

        This method validates batches of anomaly maps. It handles:
            - Conversion from numpy arrays
            - Shape normalization
            - Type conversion to float32

        Args:
            anomaly_map (``torch.Tensor`` | ``np.ndarray`` | ``None``): Input anomaly map.

        Returns:
            ``Mask`` | ``None``: Validated anomaly map as a torchvision Mask object, or None.

        Raises:
            ValueError: If ``anomaly_map`` cannot be converted to tensor or has invalid shape.

        Example:
            Validate anomaly maps::

                >>> import torch
                >>> from anomalib.data.validators import ImageBatchValidator
                >>> anomaly_map = torch.rand(4, 224, 224)
                >>> validated = ImageBatchValidator.validate_anomaly_map(anomaly_map)
                >>> validated.shape
                torch.Size([4, 224, 224])
        
src/anomalib/data/validators/torch/image.py