def validate_gt_mask(gt_mask: torch.Tensor | None) -> Mask | None:
        """Validate the ground truth mask for a batch.

        This method validates batches of ground truth masks. It handles:
            - Single masks and batches
            - Shape normalization
            - Type conversion to boolean

        Args:
            gt_mask (``torch.Tensor`` | ``None``): Input ground truth mask.

        Returns:
            ``Mask`` | ``None``: Validated ground truth mask as a torchvision Mask object,
                or None.

        Raises:
            TypeError: If ``gt_mask`` is not a torch.Tensor.
            ValueError: If ``gt_mask`` shape is invalid.

        Example:
            Validate ground truth masks::

                >>> import torch
                >>> from anomalib.data.validators import ImageBatchValidator
                >>> gt_mask = torch.randint(0, 2, (4, 224, 224))
                >>> validated = ImageBatchValidator.validate_gt_mask(gt_mask)
                >>> validated.shape
                torch.Size([4, 224, 224])
        """
        if gt_mask is None:
            return None
        if not isinstance(gt_mask, torch.Tensor):
            msg = f"Ground truth mask must be a torch.Tensor, got {type(gt_mask)}."
            raise TypeError(msg)
        if gt_mask.ndim not in {2, 3, 4}:
            msg = f"Ground truth mask must have shape [H, W] or [N, H, W] or [N, 1, H, W] got shape {gt_mask.shape}."
            raise ValueError(msg)
        if gt_mask.ndim == 2:
            gt_mask = gt_mask.unsqueeze(0)
        if gt_mask.ndim == 4:
            if gt_mask.shape[1] != 1:
                msg = f"Ground truth mask must have 1 channel, got {gt_mask.shape[1]}."
                raise ValueError(msg)
            gt_mask = gt_mask.squeeze(1)
        return Mask(gt_mask, dtype=torch.bool)
Validate the ground truth mask for a batch.

        This method validates batches of ground truth masks. It handles:
            - Single masks and batches
            - Shape normalization
            - Type conversion to boolean

        Args:
            gt_mask (``torch.Tensor`` | ``None``): Input ground truth mask.

        Returns:
            ``Mask`` | ``None``: Validated ground truth mask as a torchvision Mask object,
                or None.

        Raises:
            TypeError: If ``gt_mask`` is not a torch.Tensor.
            ValueError: If ``gt_mask`` shape is invalid.

        Example:
            Validate ground truth masks::

                >>> import torch
                >>> from anomalib.data.validators import ImageBatchValidator
                >>> gt_mask = torch.randint(0, 2, (4, 224, 224))
                >>> validated = ImageBatchValidator.validate_gt_mask(gt_mask)
                >>> validated.shape
                torch.Size([4, 224, 224])
        
src/anomalib/data/validators/torch/image.py