U
    4AfT                     @   s   d Z ddlmZmZmZmZmZ ddlmZ ddl	m
Z
mZ ddlmZmZ ddlmZmZmZmZmZmZ ddlmZmZ d	d
dgZdddgZdd ZG dd de
ZdS )z"Image processor class for Idefics.    )CallableDictListOptionalUnion)Image   )BaseImageProcessorBatchFeature)resizeto_channel_dimension_format)ChannelDimension
ImageInputPILImageResamplingmake_list_of_imagesto_numpy_arrayvalid_images)
TensorTypeis_torch_availableg3<4'?gwgM?gy{ ?gB91?gwt.?g	U?c                 C   sB   | j dkr| S | d}td|jd}t||}|d}|S )NZRGBZRGBA)   r   r   )modeconvertr   newsizealpha_composite)imageZ
image_rgbaZ
backgroundr    r   X/tmp/pip-unpacked-wheel-zw5xktn0/transformers/models/idefics/image_processing_idefics.pyconvert_to_rgb&   s    


r   c                       s   e Zd ZdZdgZdeeeee	e f  eeee	e f  ee dd fddZ
dddddejfeee eeeef  eeee	e f  eeee	e f  eeeeef  ed	d
dZ  ZS )IdeficsImageProcessora1  
    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.
    pixel_values   Nr   )
image_size
image_mean	image_stdimage_num_channelsreturnc                    s*   t  jf | || _|| _|| _|| _d S )N)super__init__r"   r%   r#   r$   )selfr"   r#   r$   r%   kwargs	__class__r   r   r(   H   s
    zIdeficsImageProcessor.__init__)imagesr%   r"   r#   r$   	transformreturn_tensorsr&   c           
         s8  |dk	r|nj }|dk	r|nj} dk	r0 nj dk	rBnj||ft|trjt|dkrjg S t|}t|st	ddk	rt
 stdddl}	fdd|D }|	|S dd |D }dd |D }fd	d|D }fd
d|D } fdd|D }dd |D }td|i|dd }|S )a  
        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

        Nr   zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.z.To pass in `transform` torch must be installedc                    s   g | ]} |qS r   r   .0x)r.   r   r   
<listcomp>   s     z4IdeficsImageProcessor.preprocess.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )r   r0   r   r   r   r3      s     c                 S   s   g | ]}t |qS r   )r   r0   r   r   r   r3      s     c                    s   g | ]}t | tjd qS ))Zresample)r   r   ZBICUBICr0   )r   r   r   r3      s     c                    s   g | ]} j |d dqS )gp?)r   Zscale)Zrescale)r1   r   )r)   r   r   r3      s     c                    s   g | ]}j | d qS ))ZmeanZstd)	normalizer0   )r#   r$   r)   r   r   r3      s     c                 S   s   g | ]}t |tjqS r   )r   r   ZFIRSTr0   r   r   r   r3      s     r    )dataZtensor_type)r"   r%   r#   r$   
isinstancelistlenr   r   
ValueErrorr   ImportErrortorchstackr
   )
r)   r-   r%   r"   r#   r$   r.   r/   r*   r;   r   )r#   r$   r)   r   r.   r   
preprocessW   s4    &
z IdeficsImageProcessor.preprocess)r!   NNr   )__name__
__module____qualname____doc__Zmodel_input_namesintr   r   floatr   r(   r   ZPYTORCHr   r   strr   r=   __classcell__r   r   r+   r   r   3   s:       r   N)rA   typingr   r   r   r   r   ZPILr   Zimage_processing_utilsr	   r
   Zimage_transformsr   r   Zimage_utilsr   r   r   r   r   r   utilsr   r   ZIDEFICS_STANDARD_MEANZIDEFICS_STANDARD_STDr   r   r   r   r   r   <module>   s    

