codingutf8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license image processor class for idefics from typing import callable dict list optional union from pil import image from imageprocessingutils import baseimageprocessor batchfeature from imagetransforms import resize tochanneldimensionformat from imageutils import channeldimension imageinput pilimageresampling makelistofimages tonumpyarray validimages from utils import tensortype istorchavailable ideficsstandardmean 0 48145466 0 4578275 0 40821073 ideficsstandardstd 0 26862954 0 26130258 0 27577711 def converttorgbimage image convertrgb would only work for jpg images as it creates a wrong background for transparent images the call to alphacomposite handles this case if image mode rgb return image imagergba image convertrgba background image newrgba imagergba size 255 255 255 alphacomposite image alphacompositebackground imagergba alphacomposite alphacomposite convertrgb return alphacomposite class ideficsimageprocessorbaseimageprocessor r constructs a idefics image processor args imagesize int optional defaults to 224 resize to image size imagemean float or listfloat optional defaults to ideficsstandardmean mean to use if normalizing the image this is a float or list of floats the length of the number of channels in the image can be overridden by the imagemean parameter in the preprocess method can be overridden by the imagemean parameter in the preprocess method imagestd float or listfloat optional defaults to ideficsstandardstd standard deviation to use if normalizing the image this is a float or list of floats the length of the number of channels in the image can be overridden by the imagestd parameter in the preprocess method can be overridden by the imagestd parameter in the preprocess method imagenumchannels int optional defaults to 3 number of image channels preprocess a batch of images args images imageinput a list of images to preprocess imagesize int optional defaults to self imagesize resize to image size imagenumchannels int optional defaults to self imagenumchannels number of image channels imagemean float or listfloat optional defaults to ideficsstandardmean mean to use if normalizing the image this is a float or list of floats the length of the number of channels in the image can be overridden by the imagemean parameter in the preprocess method can be overridden by the imagemean parameter in the preprocess method imagestd float or listfloat optional defaults to ideficsstandardstd standard deviation to use if normalizing the image this is a float or list of floats the length of the number of channels in the image can be overridden by the imagestd parameter in the preprocess method can be overridden by the imagestd parameter in the preprocess method transform callable optional defaults to none a custom transform function that accepts a single image can be passed for training for example torchvision compose can be used to compose multiple transforms if none an inference mode is assumed and then a preset of inferencespecific transforms will be applied to the images returns a pytorch tensor of the processed images for training a user needs to pass their own set of transforms as a callable for reference this is what was used in the original idefics training transform transforms compose converttorgb transforms randomresizedcropsize size scale0 9 1 0 interpolationtransforms interpolationmode bicubic transforms totensor transforms normalizemeanimagemean stdimagestd for inference we do the exact transforms that were used to train idefics further transforms expect numpy arrays todo this converts to torch tensors switch to converttotensors once it becomes available coding utf 8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license image processor class for idefics image convert rgb would only work for jpg images as it creates a wrong background for transparent images the call to alpha_composite handles this case constructs a idefics image processor args image_size int optional defaults to 224 resize to image size image_mean float or list float optional defaults to idefics_standard_mean mean to use if normalizing the image this is a float or list of floats the length of the number of channels in the image can be overridden by the image_mean parameter in the preprocess method can be overridden by the image_mean parameter in the preprocess method image_std float or list float optional defaults to idefics_standard_std standard deviation to use if normalizing the image this is a float or list of floats the length of the number of channels in the image can be overridden by the image_std parameter in the preprocess method can be overridden by the image_std parameter in the preprocess method image_num_channels int optional defaults to 3 number of image channels preprocess a batch of images args images imageinput a list of images to preprocess image_size int optional defaults to self image_size resize to image size image_num_channels int optional defaults to self image_num_channels number of image channels image_mean float or list float optional defaults to idefics_standard_mean mean to use if normalizing the image this is a float or list of floats the length of the number of channels in the image can be overridden by the image_mean parameter in the preprocess method can be overridden by the image_mean parameter in the preprocess method image_std float or list float optional defaults to idefics_standard_std standard deviation to use if normalizing the image this is a float or list of floats the length of the number of channels in the image can be overridden by the image_std parameter in the preprocess method can be overridden by the image_std parameter in the preprocess method transform callable optional defaults to none a custom transform function that accepts a single image can be passed for training for example torchvision compose can be used to compose multiple transforms if none an inference mode is assumed and then a preset of inference specific transforms will be applied to the images returns a pytorch tensor of the processed images for training a user needs to pass their own set of transforms as a callable for reference this is what was used in the original idefics training transform transforms compose convert_to_rgb transforms randomresizedcrop size size scale 0 9 1 0 interpolation transforms interpolationmode bicubic transforms totensor transforms normalize mean image_mean std image_std for inference we do the exact transforms that were used to train idefics further transforms expect numpy arrays todo this converts to torch tensors switch to convert_to_tensors once it becomes available
from typing import Callable, Dict, List, Optional, Union

from PIL import Image

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available


IDEFICS_STANDARD_MEAN = [0.48145466, 0.4578275, 0.40821073]
IDEFICS_STANDARD_STD = [0.26862954, 0.26130258, 0.27577711]


def convert_to_rgb(image):
    
    
    if image.mode == "RGB":
        return image

    image_rgba = image.convert("RGBA")
    background = Image.new("RGBA", image_rgba.size, (255, 255, 255))
    alpha_composite = Image.alpha_composite(background, image_rgba)
    alpha_composite = alpha_composite.convert("RGB")
    return alpha_composite


class IdeficsImageProcessor(BaseImageProcessor):
    r

    model_input_names = ["pixel_values"]

    def __init__(
        self,
        image_size: int = 224,
        image_mean: Optional[Union[float, List[float]]] = None,
        image_std: Optional[Union[float, List[float]]] = None,
        image_num_channels: Optional[int] = 3,
        **kwargs,
    ) -> None:
        super().__init__(**kwargs)

        self.image_size = image_size
        self.image_num_channels = image_num_channels
        self.image_mean = image_mean
        self.image_std = image_std

    def preprocess(
        self,
        images: ImageInput,
        image_num_channels: Optional[int] = 3,
        image_size: Optional[Dict[str, int]] = None,
        image_mean: Optional[Union[float, List[float]]] = None,
        image_std: Optional[Union[float, List[float]]] = None,
        transform: Callable = None,
        **kwargs,
    ) -> TensorType.PYTORCH:
        
        image_size = image_size if image_size is not None else self.image_size
        image_num_channels = image_num_channels if image_num_channels is not None else self.image_num_channels
        image_mean = image_mean if image_mean is not None else self.image_mean
        image_std = image_std if image_std is not None else self.image_std
        size = (image_size, image_size)

        if isinstance(images, list) and len(images) == 0:
            return []

        images = make_list_of_images(images)

        if not valid_images(images):
            raise ValueError(
                "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
                "torch.Tensor, tf.Tensor or jax.ndarray."
            )

        
        
        
        
        
        
        
        
        if transform is not None:
            if not is_torch_available():
                raise ImportError("To pass in `transform` torch must be installed")
            import torch

            images = [transform(x) for x in images]
            return torch.stack(images)

        
        images = [convert_to_rgb(x) for x in images]
        
        images = [to_numpy_array(x) for x in images]
        images = [resize(x, size, resample=PILImageResampling.BICUBIC) for x in images]
        images = [self.rescale(image=image, scale=1 / 255) for image in images]
        images = [self.normalize(x, mean=image_mean, std=image_std) for x in images]
        images = [to_channel_dimension_format(x, ChannelDimension.FIRST) for x in images]
        
        images = BatchFeature(data={"pixel_values": images}, tensor_type=TensorType.PYTORCH)["pixel_values"]

        return images