def rgb2gray(img):
    """
    Converts a batch of RGB images to gray.

    :param img:
        an RGB image batch of any type.
    :return:
        a batch of gray images.
    """
    if len(img.shape) != 4:
        raise ValueError('Input images must have four dimensions, not %d' %
            len(img.shape))
    return (0.299 * img[:, (0)] + 0.587 * img[:, (1)] + 0.114 * img[:, (2)]
        ).unsqueeze(1)
