
    sg]                     D   d Z ddlZddlmZmZmZmZmZmZm	Z	 ddl
ZddlmZ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mZmZmZmZmZ ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z% dd	l&m'Z'  e%jP                  e)      Z* e$       rddl+Z+ G d
 de      Z,dedeeejZ                        fdZ.dedefdZ/dee0e0f   dee0e0f   de0fdZ1	 ddede	e0ee0e0f   ee0   ee0   f   de	e0ee0e0f   ee0   ee0   f   dee	e2ef      de3f
dZ4de	e2e f   deeef   fdZ5de	e2e f   defdZ6 G d de      Z7y)z"Image processor class for Pixtral.    N)AnyCallableDictListOptionalTupleUnion   )BaseImageProcessorBatchFeatureget_size_dict)resizeto_channel_dimension_format)ChannelDimension
ImageInputPILImageResamplingget_image_sizeinfer_channel_dimension_formatis_scaled_imageis_valid_imageto_numpy_arrayvalid_imagesvalidate_kwargsvalidate_preprocess_arguments)
TensorTypeis_torch_deviceis_torch_dtypeis_torch_tensoris_vision_availablelogging)requires_backendsc                       e Zd ZddZy)BatchMixFeaturec                    t        | dg       ddl}i }|j                  d      }|et        |      dkD  rW|d   }t	        |      rnFt        |t              st        |      st        |t              r|}nt        dt        |       d      | j                         D ]  \  }}t        |t              r9|D 	
cg c]'  }	|	D ]   }
t        |
      s |
j                  |i |" ) c}
}	||<   Ot        ||j                        r'|j                  |      r |j                  |i |||<   t        ||j                        r||j                  |      ||<   |||<    || _        | S c c}
}	w )a  
        Send all values to device by calling `v.to(*args, **kwargs)` (PyTorch only). This should support casting in
        different `dtypes` and sending the `BatchFeature` to a different `device`.

        Args:
            args (`Tuple`):
                Will be passed to the `to(...)` function of the tensors.
            kwargs (`Dict`, *optional*):
                Will be passed to the `to(...)` function of the tensors.

        Returns:
            [`BatchFeature`]: The same instance after modification.
        torchr   Ndevicez*Attempting to cast a BatchFeature to type z. This is not supported.)r&   )r!   r%   getlenr   
isinstancestrr   int
ValueErroritemslistr   toTensoris_floating_pointdata)selfargskwargsr%   new_datar&   argkvsampleelements              g/var/www/html/venv/lib/python3.12/site-packages/transformers/models/pixtral/image_processing_pixtral.pyr/   zBatchMixFeature.to4   sf    	$	*H%>c$i!mq'Cc"C%)=CQTAU !#McRUhZWo!pqqJJL 	 DAq!T">?4:vDKYhipYqJGJJ/// Au||,1H1H1K"addD3F3Au||,1Cdd&d1	  	s   7E&E&N)returnr#   )__name__
__module____qualname__r/        r<   r#   r#   3   s    .rB   r#   imagesr=   c                 l   t        |       r| gg} | S t        | t        t        f      r!t	        |       dkD  rt        | d         r| g} | S t        | t        t        f      rLt	        |       dkD  r>t        | d   t        t        f      r%t	        | d         dkD  rt        | d   d         r	 | S t        d      )z
    Convert a single image or a list of images to a list of numpy arrays.

    Args:
        images (`ImageInput`):
            A single image or a list of images.

    Returns:
        A list of numpy arrays.
    r   z]Invalid input type. Must be a single image, a list of images, or a list of batches of images.)r   r)   r.   tupler(   r,   )rC   s    r<   make_list_of_imagesrF   f   s     f(" M 
FT5M	*s6{Q>RXYZR[C\ M 	6D%=)K!Ovay4-0q	NQ6!9Q<(
 M k
 	
rB   imagec                 V   t        t        dg       t        | t        j                  j                        s| S | j
                  dk(  r| S | j                  d      } t        j                  j                  d| j                  d      }|j                  | d|        |j                  d      }|S )z
    Converts an image to RGB format. Only converts if the image is of type PIL.Image.Image, otherwise returns the image
    as is.
    Args:
        image (Image):
            The image to convert.
    visionRGBRGBAWHITE)r   r   )
r!   convert_to_rgbr)   PILImagemodeconvertnewsizepaste)rG   	new_images     r<   rM   rM      s     nxj1eSYY__-zzU MM&!E 		fejj':IOOE65)!!%(IrB   
image_size
patch_sizec                 ~    | \  }}t        |t        t        f      r|n||f\  }}|dz
  |z  dz   }|dz
  |z  dz   }||fS )aP  
    Calculate the number of image tokens given the image size and patch size.

    Args:
        image_size (`Tuple[int, int]`):
            The size of the image as `(height, width)`.
        patch_size (`Tuple[int, int]`):
            The patch size as `(height, width)`.

    Returns:
        `int`: The number of image tokens.
       )r)   rE   r.   )rV   rW   heightwidthpatch_heightpatch_widthnum_width_tokensnum_height_tokenss           r<   _num_image_tokensr`      sa     MFE.8eT].S
ZdfpYqL+	k1A5!4q8...rB   input_imagerS   input_data_formatc                    t        |t        t        f      r|n||f\  }}t        |t        t        f      r|n||f\  }}t        | |      \  }}	t	        ||z  |	|z        }
|
dkD  rBt        t        j                  ||
z              }t        t        j                  |	|
z              }	t        ||	f||f      \  }}||z  ||z  fS )aN  
    Find the target (height, width) dimension of the output image after resizing given the input image and the desired
    size.

    Args:
        input_image (`ImageInput`):
            The image to resize.
        size (`int` or `Tuple[int, int]`):
            Max image size an input image can be. Must be a dictionary with the key "longest_edge".
        patch_size (`int` or `Tuple[int, int]`):
            The patch_size as `(height, width)` to use for resizing the image. If patch_size is an integer, `(patch_size, patch_size)`
            will be used
        input_data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the input image. If unset, will use the inferred format from the input.

    Returns:
        `tuple`: The target (height, width) dimension of the output image after resizing.
    rY   )	r)   rE   r.   r   maxr+   mathceilr`   )ra   rS   rW   rb   
max_height	max_widthr\   r]   rZ   r[   ratior_   r^   s                r<   get_resize_output_image_sizerj      s    0 %/teT]$CD$PTJ	.8eT].S
ZdfpYqL+";0ABMFE#UY%67EqyTYYv~./DIIeem,-*;VUOl\gMh*i''|+-=-KKKrB   tensor_typec                 4    t               j                  |       S N)r   _get_is_as_tensor_fns)rk   s    r<   rn   rn      s    >//<<rB   c                 B    t        |      \  }} ||       r| S  ||       S rm   )rn   )arrayrk   	is_tensor	as_tensors       r<   convert_to_tensorrs      s)    0=IyUrB   c                    h    e Zd ZdZdgZdddej                  ddddddf
dedee	e
f   dee	e
f   d	ed
edee
ef   dedeeeee   f      deeeee   f      deddf fdZej                  ddfdej"                  dee	e
f   dee	e
f   d	edeee	ef      deee	ef      dej"                  fdZdddddddddddej(                  dfdededee	e
f   dee	e
f   d	ed
edededeeeee   f      deeeee   f      dedeee	ef      dee   deee	ef      dej0                  j0                  fdZ xZS )PixtralImageProcessoraa
  
    Constructs a Pixtral image processor.

    Args:
        do_resize (`bool`, *optional*, defaults to `True`):
            Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by
            `do_resize` in the `preprocess` method.
        size (`Dict[str, int]` *optional*, defaults to `{"longest_edge": 1024}`):
            Size of the maximum dimension of either the height or width dimension of the image. Used to control how
            images are resized. If either the height or width are greater than `size["longest_edge"]` then both the height and width are rescaled by `height / ratio`, `width /ratio` where `ratio = max(height / longest_edge, width / longest_edge)`
        patch_size (`Dict[str, int]` *optional*, defaults to `{"height": 16, "width": 16}`):
            Size of the patches in the model, used to calculate the output image size. Can be overridden by `patch_size` in the `preprocess` method.
        resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`):
            Resampling filter to use if resizing the image. Can be overridden by `resample` in the `preprocess` method.
        do_rescale (`bool`, *optional*, defaults to `True`):
            Whether to rescale the image by the specified scale `rescale_factor`. Can be overridden by `do_rescale` in
            the `preprocess` method.
        rescale_factor (`int` or `float`, *optional*, defaults to `1/255`):
            Scale factor to use if rescaling the image. Can be overridden by `rescale_factor` in the `preprocess`
            method.
        do_normalize (`bool`, *optional*, defaults to `True`):
            Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method.
        image_mean (`float` or `List[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`):
            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.
        image_std (`float` or `List[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`):
            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.
        do_convert_rgb (`bool`, *optional*, defaults to `True`):
            Whether to convert the image to RGB.
    pixel_valuesTNgp?	do_resizerS   rW   resample
do_rescalerescale_factordo_normalize
image_mean	image_stddo_convert_rgbr=   c                    t        |   d
i | ||nddi}||nddd}t        |d      }|| _        || _        || _        || _        || _        || _        || _	        ||ng d| _
        |	|	ng d| _        |
| _        g d	| _        y )Nlongest_edgei      )rZ   r[   Tdefault_to_square)g3<4'?gwgM?gy{ ?)gB91?gwt.?g	U?)rC   rw   rS   rW   rx   ry   rz   r{   r|   r}   r~   return_tensorsdata_formatrb   rA   )super__init__r   rw   rS   rW   rx   ry   rz   r{   r|   r}   r~   _valid_processor_keys)r3   rw   rS   rW   rx   ry   rz   r{   r|   r}   r~   r5   	__class__s               r<   r   zPixtralImageProcessor.__init__  s     	"6"'tnd-C#-#9Z"WY?Z
":F
"	$ $,((2(>*Dg&/&;Ae,&
"rB   rG   r   rb   c                     d|v r|d   |d   f}nd|v rd|v r|d   |d   f}nt        d      d|v rd|v r|d   |d   f}nt        d      t        ||||      }t        |f||||d|S )a  
        Resize an image. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge
        resized to keep the input aspect ratio.

        Args:
            image (`np.ndarray`):
                Image to resize.
            size (`Dict[str, int]`):
                Dict containing the longest possible edge of the image.
            patch_size (`Dict[str, int]`):
                Patch size used to calculate the size of the output image.
            resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`):
                Resampling filter to use when resiizing the image.
            data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format of the image. If not provided, it will be the same as the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the input image. If not provided, it will be inferred.
        r   rZ   r[   z@size must contain either 'longest_edge' or 'height' and 'width'.zGpatch_size must contain either 'shortest_edge' or 'height' and 'width'.)rS   rW   rb   )rS   rx   r   rb   )r,   rj   r   )	r3   rG   rS   rW   rx   r   rb   r5   output_sizes	            r<   r   zPixtralImageProcessor.resize;  s    8 T!($~*>?D'T/NDM2D_``z!g&;$X.
70CDJfgg2!/	
 
#/
 
 	
rB   rC   r   c                    ||n| j                   }t        |d      }||n| j                  }||n| j                  }||n| j                  }||n| j
                  }||n| j                  }||n| j                  }|	|	n| j                  }	|
|
n| j                  }
||n| j                  }t        |j                         | j                         t        |      }t        |d         st!        d      t#        ||||	|
|||       |r(|D cg c]  }|D cg c]  }t%        |       c} }}}|D cg c]  }|D cg c]  }t'        |       c} }}}t)        |d   d         r|rt*        j-                  d       |t/        |d   d         }g }g }|D ]  }g }g }|D ]s  }|r| j1                  |||||	      }|r| j3                  |||
      }|r| j5                  ||	|
|      }|j7                  |       |j7                  t9        ||             u |j7                  |       |j7                  |        |D cg c]  }|D cg c]  }t;        |||       c}  }}}|D cg c]  }|D cg c]  }t=        ||       c} }}}t?        ||dd      S c c}w c c}}w c c}w c c}}w c c}w c c}}w c c}w c c}}w )a  
        Preprocess an image or batch of images.

        Args:
            images (`ImageInput`):
                Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
                passing in images with pixel values between 0 and 1, set `do_rescale=False`.
            do_resize (`bool`, *optional*, defaults to `self.do_resize`):
                Whether to resize the image.
            size (`Dict[str, int]`, *optional*, defaults to `self.size`):
                Describes the maximum input dimensions to the model.
            patch_size (`Dict[str, int]`, *optional*, defaults to `self.patch_size`):
                Patch size in the model. Used to calculate the image after resizing.
            resample (`int`, *optional*, defaults to `self.resample`):
                Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only
                has an effect if `do_resize` is set to `True`.
            do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
                Whether to rescale the image.
            rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
                Rescale factor to rescale the image by if `do_rescale` is set to `True`.
            do_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
                Whether to normalize the image.
            image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`):
                Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`.
            image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`):
                Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to
                `True`.
            do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`):
                Whether to convert the image to RGB.
            return_tensors (`str` or `TensorType`, *optional*):
                The type of tensors to return. Can be one of:
                - Unset: Return a list of `np.ndarray`.
                - `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
                - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
                - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
                - `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.
            data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`):
                The channel dimension format for the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - Unset: Use the channel dimension format of the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
        NTr   )captured_kwargsvalid_processor_keysr   zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)ry   rz   r{   r|   r}   rw   rS   rx   zIt looks like you are trying to rescale already rescaled images. If the input images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again.)rG   rS   rW   rx   rb   )rG   scalerb   )rG   meanstdrb   )input_channel_dim)rv   image_sizes)r2   rk   ) rW   r   rw   rS   rx   ry   rz   r{   r|   r}   r~   r   keysr   rF   r   r,   r   rM   r   r   loggerwarning_oncer   r   rescale	normalizeappendr   r   rs   r#   )r3   rC   rw   rS   rW   rx   ry   rz   r{   r|   r}   r~   r   r   rb   r5   images_listrG   batch_imagesbatch_image_sizessample_imagesr   s                         r<   
preprocessz PixtralImageProcessor.preprocessr  s   D $.#9Zt
":F
!*!6IDNN	'tTYY'38#-#9Zt
+9+E4K^K^'3'?|TEVEV#-#9Zt
!*!6IDNN	+9+E4K^K^DLfLfg)&1KN+: 
 	&!)%!		
 U`a6vFeN51FaKa R]]v6B%u-B]];q>!,-*s
 $ >{1~a?P Q( 	2MFK& M KK#!#-!)*; ( E  LLuN^oLpE NN#*)Wh + E e$"">%9J#KL'M( '$$[11	28 '
 pvvfk(O`av
 
 eppZ`fUU)%@Upp[Qb$cquvve Ga C]R w
 Vps`   	J)J$,J)9	J4J/J4	J?J:#J?0	K
9KK
$J)/J4:J?K
)r>   r?   r@   __doc__model_input_namesr   BICUBICboolr   r*   r+   r	   floatr   r   r   npndarrayr   r   FIRSTr   r   rN   rO   r   __classcell__)r   s   @r<   ru   ru      s   B (( #%)'9'A'A,3!:>9=#,
,
 38n,
 cN	,

 %,
 ,
 c5j),
 ,
 U5$u+#567,
 E%e"456,
 ,
 
,
f (:'A'A>BDH5
zz5
 38n5
 cN	5

 %5
 eC)9$9:;5
 $E#/?*?$@A5
 
5
t #%)'+ $!:>9=#;?2B2H2HDHWwWw Ww 38n	Ww
 cNWw %Ww Ww Ww Ww U5$u+#567Ww E%e"456Ww Ww !sJ!78Ww ./Ww $E#/?*?$@AWw" 
#WwrB   ru   rm   )8r   re   typingr   r   r   r   r   r   r	   numpyr   image_processing_utilsr   r   r   image_transformsr   r   image_utilsr   r   r   r   r   r   r   r   r   r   r   utilsr   r   r   r   r   r    utils.import_utilsr!   
get_loggerr>   r   rN   r#   r   rF   rM   r+   r`   r*   rE   rj   rn   rs   ru   rA   rB   r<   <module>r      s   )  D D D  U U    p o 3 
		H	% /l /f
 tD4D/E D*  4/%S/ /uS#X /SV /0 AE	$L$L
U38_d3is;
<$L c5c?DIuSzAB$L  c+;&; <=	$L
 $LP=uS*_'= =%RZHZB[ =%Z*@ S `w. `wrB   