keras/src/backend/torch/image.py/rgb_to_grayscale
def rgb_to_grayscale(images, data_format=None):
    images = convert_to_tensor(images)
    data_format = backend.standardize_data_format(data_format)
    if data_format == "channels_last":
        if images.ndim == 4:
            images = images.permute((0, 3, 1, 2))
        elif images.ndim == 3:
            images = images.permute((2, 0, 1))
        else:
            raise ValueError(
                "Invalid images rank: expected rank 3 (single image) "
                "or rank 4 (batch of images). Received input with shape: "
                f"images.shape={images.shape}"
            )
    images = torchvision.transforms.functional.rgb_to_grayscale(img=images)
    if data_format == "channels_last":
        if len(images.shape) == 4:
            images = images.permute((0, 2, 3, 1))
        elif len(images.shape) == 3:
            images = images.permute((1, 2, 0))
    return images