def pad_nextpow2(batch: torch.Tensor) -> torch.Tensor:
    """Pad images to next power of 2 size.

    Finds largest dimension and pads to square power-of-2 size. Handles odd sizes.

    Args:
        batch (torch.Tensor): Batch of images to pad

    Returns:
        torch.Tensor: Padded image batch

    Examples:
        >>> x = torch.randn(1, 3, 127, 128)
        >>> padded = pad_nextpow2(x)
        >>> padded.shape
        torch.Size([1, 3, 128, 128])
    """
    l_dim = 2 ** math.ceil(math.log(max(*batch.shape[-2:]), 2))
    padding_w = [math.ceil((l_dim - batch.shape[-2]) / 2), math.floor((l_dim - batch.shape[-2]) / 2)]
    padding_h = [math.ceil((l_dim - batch.shape[-1]) / 2), math.floor((l_dim - batch.shape[-1]) / 2)]
    return F.pad(batch, pad=[*padding_h, *padding_w])
Pad images to next power of 2 size.

    Finds largest dimension and pads to square power-of-2 size. Handles odd sizes.

    Args:
        batch (torch.Tensor): Batch of images to pad

    Returns:
        torch.Tensor: Padded image batch

    Examples:
        >>> x = torch.randn(1, 3, 127, 128)
        >>> padded = pad_nextpow2(x)
        >>> padded.shape
        torch.Size([1, 3, 128, 128])
    
src/anomalib/data/utils/image.py