def validate_gt_mask(gt_mask: np.ndarray | None) -> np.ndarray | None:
        """Validate the ground truth mask batch.

        This method validates batches of ground truth masks. It handles:
            - Channel-first and channel-last formats
            - Type conversion to boolean
            - Shape validation

        Args:
            gt_mask (``np.ndarray`` | ``None``): Input ground truth mask batch.

        Returns:
            ``np.ndarray`` | ``None``: Validated ground truth mask batch as boolean array,
                or ``None``.

        Raises:
            TypeError: If ``gt_mask`` is not a numpy array.
            ValueError: If ``gt_mask`` shape is invalid.

        Examples:
            Validate channel-last masks::

                >>> masks = np.random.randint(0, 2, (4, 224, 224))
                >>> validated = NumpyImageBatchValidator.validate_gt_mask(masks)
                >>> validated.shape
                (4, 224, 224)
                >>> validated.dtype
                dtype('bool')

            Validate channel-first masks::

                >>> masks = np.random.randint(0, 2, (4, 1, 224, 224))
                >>> validated = NumpyImageBatchValidator.validate_gt_mask(masks)
                >>> validated.shape
                (4, 224, 224, 1)
        """
        if gt_mask is None:
            return None
        if not isinstance(gt_mask, np.ndarray):
            msg = f"Ground truth mask batch must be a numpy.ndarray, got {type(gt_mask)}."
            raise TypeError(msg)
        if gt_mask.ndim not in {3, 4}:
            msg = f"Ground truth mask batch must have shape [N, H, W] or [N, H, W, 1], got shape {gt_mask.shape}."
            raise ValueError(msg)

        # Check if the mask is in [N, H, W, 1] format and rearrange if necessary
        if gt_mask.ndim == 4 and gt_mask.shape[3] != 1:
            gt_mask = np.transpose(gt_mask, (0, 2, 3, 1))

        if gt_mask.ndim == 4 and gt_mask.shape[3] != 1:
            msg = f"Ground truth mask batch must have 1 channel, got {gt_mask.shape[3]}."
            raise ValueError(msg)

        return gt_mask.astype(bool)
Validate the ground truth mask batch.

        This method validates batches of ground truth masks. It handles:
            - Channel-first and channel-last formats
            - Type conversion to boolean
            - Shape validation

        Args:
            gt_mask (``np.ndarray`` | ``None``): Input ground truth mask batch.

        Returns:
            ``np.ndarray`` | ``None``: Validated ground truth mask batch as boolean array,
                or ``None``.

        Raises:
            TypeError: If ``gt_mask`` is not a numpy array.
            ValueError: If ``gt_mask`` shape is invalid.

        Examples:
            Validate channel-last masks::

                >>> masks = np.random.randint(0, 2, (4, 224, 224))
                >>> validated = NumpyImageBatchValidator.validate_gt_mask(masks)
                >>> validated.shape
                (4, 224, 224)
                >>> validated.dtype
                dtype('bool')

            Validate channel-first masks::

                >>> masks = np.random.randint(0, 2, (4, 1, 224, 224))
                >>> validated = NumpyImageBatchValidator.validate_gt_mask(masks)
                >>> validated.shape
                (4, 224, 224, 1)
        
src/anomalib/data/validators/numpy/image.py