
    cCi:                        S r SSKJrJr  SSKrSSKJrJrJ	r	  SSK
JrJrJr  SSKJrJrJrJrJrJrJrJrJr  SSKJrJrJrJr  SS	KJr  \" 5       (       a  SSKr\R@                  " \!5      r"S
 r#S r$\" SS9 " S S\5      5       r%S/r&g)z#Image processor class for ImageGPT.    )OptionalUnionN   )BaseImageProcessorBatchFeatureget_size_dict)rescaleresizeto_channel_dimension_format)	ChannelDimension
ImageInputPILImageResamplinginfer_channel_dimension_formatis_scaled_imagemake_list_of_imagesto_numpy_arrayvalid_imagesvalidate_preprocess_arguments)
TensorTypefilter_out_non_signature_kwargsis_vision_availablelogging)requiresc                     UR                   n[        R                  " [        R                  " U 5      SS9n[        R                  " [        R                  " U5      SS9n[        R                  " X5      nUS S 2S 4   SU-  -
  US S S 24   -   nU$ )N   axisr      )Tnpsumsquarematmul)aba2b2abds         p/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/imagegpt/image_processing_imagegpt.pysquared_euclidean_distancer+   -   sp    	A			!1	%B			!1	%B	1B
1d7a"fr$'{*AH    c                 f    U R                  SS5      n [        X5      n[        R                  " USS9$ )Nr   r   r   )reshaper+   r    argmin)xclustersr)   s      r*   color_quantizer3   6   s-    			"aA"1/A99QQr,   )vision)backendsc                     ^  \ rS rSrSrS/rSSS\R                  SS4S\\	\
\
\      \R                  4      S\S\\\\4      S	\S
\S\SS4U 4S jjjr\R                  SS4S\R                  S\\\4   S	\S\\	\\4      S\\	\\4      S\R                  4S jjr  SS\R                  S\\	\\4      S\\	\\4      S\R                  4S jjr\" 5       SSSSSSS\R,                  S4	S\S\\   S\\\\4      S	\\   S
\\   S\\   S\\	\
\
\      \R                  4      S\\	\\4      S\\	\\4      S\\	\\4      S\R4                  R4                  4S jj5       rU 4S jrSrU =r$ )ImageGPTImageProcessor<   a^  
Constructs a ImageGPT image processor. This image processor can be used to resize images to a smaller resolution
(such as 32x32 or 64x64), normalize them and finally color quantize them to obtain sequences of "pixel values"
(color clusters).

Args:
    clusters (`np.ndarray` or `list[list[int]]`, *optional*):
        The color clusters to use, of shape `(n_clusters, 3)` when color quantizing. Can be overridden by `clusters`
        in `preprocess`.
    do_resize (`bool`, *optional*, defaults to `True`):
        Whether to resize the image's dimensions to `(size["height"], size["width"])`. Can be overridden by
        `do_resize` in `preprocess`.
    size (`dict[str, int]` *optional*, defaults to `{"height": 256, "width": 256}`):
        Size of the image after resizing. Can be overridden by `size` in `preprocess`.
    resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`):
        Resampling filter to use if resizing the image. Can be overridden by `resample` in `preprocess`.
    do_normalize (`bool`, *optional*, defaults to `True`):
        Whether to normalize the image pixel value to between [-1, 1]. Can be overridden by `do_normalize` in
        `preprocess`.
    do_color_quantize (`bool`, *optional*, defaults to `True`):
        Whether to color quantize the image. Can be overridden by `do_color_quantize` in `preprocess`.
pixel_valuesNTr2   	do_resizesizeresampledo_normalizedo_color_quantizereturnc                    > [         TU ]  " S0 UD6  Ub  UOSSS.n[        U5      nUb  [        R                  " U5      OS U l        X l        X0l        X@l        XPl	        X`l
        g )N   )heightwidth )super__init__r   r    arrayr2   r:   r;   r<   r=   r>   )	selfr2   r:   r;   r<   r=   r>   kwargs	__class__s	           r*   rF   ImageGPTImageProcessor.__init__W   sb     	"6"'tc-JT".6.B*"	 (!2r,   imagedata_formatinput_data_formatc                     [        U5      nSU;  d  SU;  a  [        SUR                  5        35      eUS   US   4n[        U4UUUUS.UD6$ )a  
Resize an image to `(size["height"], size["width"])`.

Args:
    image (`np.ndarray`):
        Image to resize.
    size (`dict[str, int]`):
        Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image.
    resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`):
        `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`.
    data_format (`ChannelDimension` or `str`, *optional*):
        The channel dimension format for the output image. If unset, the channel dimension format of the input
        image is used. 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.
    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.

Returns:
    `np.ndarray`: The resized image.
rB   rC   zFThe `size` dictionary must contain the keys `height` and `width`. Got )r;   r<   rM   rN   )r   
ValueErrorkeysr
   )rH   rL   r;   r<   rM   rN   rI   output_sizes           r*   r
   ImageGPTImageProcessor.resizem   sy    F T"47$#6efjfofofqersttH~tG}5
#/
 
 	
r,   c                 &    [        USX#S9nUS-
  nU$ )a  
Normalizes an images' pixel values to between [-1, 1].

Args:
    image (`np.ndarray`):
        Image to normalize.
    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.
g?)rL   scalerM   rN   r   )r	   )rH   rL   rM   rN   s       r*   	normalize ImageGPTImageProcessor.normalize   s     " e9+s	r,   imagesreturn_tensorsc           
         Ub  UOU R                   nUb  UOU R                  n[        U5      nUb  UOU R                  nUb  UOU R                  nUb  UOU R
                  nUb  UOU R                  n[        R                  " U5      n[        U5      n[        U5      (       d  [        S5      e[        UUUS9  U(       a  Uc  [        S5      eU Vs/ s H  n[        U5      PM     nnU(       a(  [        US   5      (       a  [        R!                  S5        U
c  [#        US   5      n
U(       a   U Vs/ s H  nU R%                  XXJS9PM     nnU(       a  U Vs/ s H  oR'                  XS9PM     nnU(       a  U Vs/ s H  n[)        U[*        R,                  U
5      PM      nn[        R                  " U5      n[/        X5      R1                  UR2                  SS	 5      nUR2                  S   nUR1                  US	5      n[5        U5      nS
U0nO U Vs/ s H  n[)        XU
5      PM     nnSU0n[7        XS9$ s  snf s  snf s  snf s  snf s  snf )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_normalize=False`.
    do_resize (`bool`, *optional*, defaults to `self.do_resize`):
        Whether to resize the image.
    size (`dict[str, int]`, *optional*, defaults to `self.size`):
        Size of 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_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
        Whether to normalize the image
    do_color_quantize (`bool`, *optional*, defaults to `self.do_color_quantize`):
        Whether to color quantize the image.
    clusters (`np.ndarray` or `list[list[int]]`, *optional*, defaults to `self.clusters`):
        Clusters used to quantize the image of shape `(n_clusters, 3)`. Only has an effect if
        `do_color_quantize` is set to `True`.
    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:
            - `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
            - `ChannelDimension.LAST`: image in (height, width, num_channels) format.
        Only has an effect if `do_color_quantize` is set to `False`.
    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.
NzkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)r:   r;   r<   z8Clusters must be specified if do_color_quantize is True.r   zIt looks like you are trying to rescale already rescaled images. If you wish to do this, make sure to set `do_normalize` to `False` and that pixel values are between [-1, 1].)rL   r;   r<   rN   )rL   rN   r.   	input_idsr9   )datatensor_type)r:   r;   r   r<   r=   r>   r2   r    rG   r   r   rP   r   r   r   loggerwarning_oncer   r
   rV   r   r   LASTr3   r/   shapelistr   )rH   rX   r:   r;   r<   r=   r>   r2   rY   rM   rN   rL   
batch_sizer\   s                 r*   
preprocess!ImageGPTImageProcessor.preprocess   sb   l "+!6IDNN	'tTYYT"'38'3'?|TEVEV1B1N-TXTjTj'3888H%$V,F##:  	&	
 !1WXX 6<<VE.'V<OF1I66h
 $ >vay I $#E %Xk#  
 djkdj[`nn5nVdjFkpvwpvgl1%9I9N9NPabpvFwXXf%F#F5==fll3B>OPF  aJ^^J3F &\F(Dflmfl]b1%FWXflFm"F+DBBK = l x ns   H>4II %IIc                    > [         TU ]  5       nUR                  S5      b8  [        US   [        R
                  5      (       a  US   R                  5       US'   / SQnU H  nX1;   d  M
  S X'   M     U$ )Nr2   )
image_mean	image_stdrescale_factor
do_rescale)rE   to_dictget
isinstancer    ndarraytolist)rH   outputmissing_keyskeyrJ   s       r*   rk   ImageGPTImageProcessor.to_dict,  sp    "::j!-*VJ=OQSQ[Q[2\2\!'
!3!:!:!<F:RC}"   r,   )r2   r>   r=   r:   r<   r;   )NN)__name__
__module____qualname____firstlineno____doc__model_input_namesr   BILINEARr   r   rb   intr    rn   booldictstrrF   r   r
   rV   r   FIRSTr   r   PILImagerd   rk   __static_attributes____classcell__)rJ   s   @r*   r7   r7   <   s   . ((
 BF)-'9'B'B!"&3 5d3i"**!<=>3 	3
 tCH~&3 %3 3  3 
3 34 (:'B'B>BDH.
zz.
 38n.
 %	.

 eC)9$9:;.
 $E#/?*?$@A.
 
.
f ?CDH	zz eC)9$9:; $E#/?*?$@A	
 
* %& %))-15'+,0AE;?>N>T>TDHwCwC D>wC tCH~&	wC
 -.wC tnwC $D>wC 5d3i"**!<=>wC !sJ!78wC eC)9$9:;wC $E#/?*?$@AwC 
wC 'wCr r,   r7   )'rx   typingr   r   numpyr    image_processing_utilsr   r   r   image_transformsr	   r
   r   image_utilsr   r   r   r   r   r   r   r   r   utilsr   r   r   r   utils.import_utilsr   r   
get_loggerrt   r^   r+   r3   r7   __all__rD   r,   r*   <module>r      s    * "  U U L L
 
 
 _ ^ *  
		H	%  
;z/ z  zz $
$r,   