
    eThґ                     j   S SK JrJr  S SKJr  S SKJrJr  S SKr	SSK
JrJrJrJrJr  SSKJrJrJrJrJr  SSKJrJrJrJrJr  \" 5       (       a
  S SKrSS	K
Jr  \" 5       (       a  S SKr\" 5       (       a  S SKr \" 5       (       a  S SK!Jr"   SHS
\	RF                  S\\\$4   S\\\\$4      S\	RF                  4S jjr%S\	RL                  S4S
\	RF                  S\'S\\   S\	RP                  S\\\$\4      S\	RF                  4S jjr)S r*   SIS
\\	RF                  SSSS4   S\\+   S\\$   S\\\$\4      SS4
S jjr,SHS\-\.\.4   4S jjr/   SJS\	RF                  S\\.\-\.\.4   \0\.   \-\.   4   S\+S \\.   S\\\$\4      S\-4S! jjr1     SKS
\	RF                  S\-\.\.4   S"S#S$\\.   S\\   S%\+S\\\$\4      S\	RF                  4S& jjr2  SLS
\	RF                  S'\\'\\'   4   S(\\'\\'   4   S\\   S\\\$\4      S\	RF                  4S) jjr3  SLS
\	RF                  S\-\.\.4   S\\\$\4      S\\\$\4      S\	RF                  4
S* jjr4SMS, jr5S+\	RF                  S\	RF                  4S- jr6SNS. jr7S+\S\4S/ jr8SOS1 jr9S0\	RF                  S\	RF                  4S2 jr:SPS3 jr;S0\S\4S4 jr<S5 r=S6 r> " S7 S8\5      r?\?R                  S9SS4S
\	RF                  S:\\.\-\.\.4   \\-\.\.4      4   S;\?S<\\'\\'   4   S\\\$\4      S\\\$\4      S\	RF                  4S= jjrAS
\S\4S> jrB  SLS
\	RF                  S\\   S\\\$\4      S\	RF                  4S? jjrCS@ rDSA\0S   S\-\E\-\.\.4   \0S   4   \E\.\-\-\.\.4   \.4   4   4   4SB jrFSC\E\-\.\.4   S4   SD\E\.\-\.\.4   4   S\0S   4SE jrG " SF SG5      rHg)Q    )
CollectionIterable)ceil)OptionalUnionN   )ChannelDimension
ImageInputget_channel_dimension_axisget_image_sizeinfer_channel_dimension_format)ExplicitEnum
TensorTypeis_jax_tensoris_tf_tensoris_torch_tensor)is_flax_availableis_tf_availableis_torch_availableis_vision_availablerequires_backends)PILImageResamplingimagechannel_diminput_channel_dimreturnc                    [        U [        R                  5      (       d  [        S[	        U 5       35      eUc  [        U 5      n[        U5      nX#:X  a  U $ U[        R                  :X  aa  [        [        U R                  S-
  5      5      U R                  S-
  U R                  S-
  U R                  S-
  /-   nU R                  U5      n U $ U[        R                  :X  aa  [        [        U R                  S-
  5      5      U R                  S-
  U R                  S-
  U R                  S-
  /-   nU R                  U5      n U $ [        SU 35      e)az  
Converts `image` to the channel dimension format specified by `channel_dim`. The input
can have arbitrary number of leading dimensions. Only last three dimension will be permuted
to format the `image`.

Args:
    image (`numpy.ndarray`):
        The image to have its channel dimension set.
    channel_dim (`ChannelDimension`):
        The channel dimension format to use.
    input_channel_dim (`ChannelDimension`, *optional*):
        The channel dimension format of the input image. If not provided, it will be inferred from the input image.

Returns:
    `np.ndarray`: The image with the channel dimension set to `channel_dim`.
,Input image must be of type np.ndarray, got    r      z&Unsupported channel dimension format: )
isinstancenpndarray	TypeErrortyper   r	   FIRSTlistrangendim	transposeLAST
ValueError)r   r   r   target_channel_dimaxess        U/var/www/auris/envauris/lib/python3.13/site-packages/transformers/image_transforms.pyto_channel_dimension_formatr0   5   s*   * eRZZ((FtE{mTUU :5A)+6.-333E%**q.)*ejj1nejj1nejj[\n-]]% L 
/44	4E%**q.)*ejj1nejj1nejj[\n-]]% L A+OPP    scaledata_formatdtypeinput_data_formatc                     [        U [        R                  5      (       d  [        S[	        U 5       35      eU R                  [        R                  5      U-  nUb  [        XRU5      nUR                  U5      nU$ )a  
Rescales `image` by `scale`.

Args:
    image (`np.ndarray`):
        The image to rescale.
    scale (`float`):
        The scale to use for rescaling the image.
    data_format (`ChannelDimension`, *optional*):
        The channel dimension format of the image. If not provided, it will be the same as the input image.
    dtype (`np.dtype`, *optional*, defaults to `np.float32`):
        The dtype of the output image. Defaults to `np.float32`. Used for backwards compatibility with feature
        extractors.
    input_data_format (`ChannelDimension`, *optional*):
        The channel dimension format of the input image. If not provided, it will be inferred from the input image.

Returns:
    `np.ndarray`: The rescaled image.
r   )r!   r"   r#   r$   r%   astypefloat64r0   )r   r2   r3   r4   r5   rescaled_images         r/   rescaler:   `   sl    4 eRZZ((FtE{mTUU\\"**-5N4^Rcd#**51Nr1   c                 Z   U R                   [        R                  :X  a  SnU$ [        R                  " X R	                  [
        5      5      (       an  [        R                  " SU :*  5      (       a"  [        R                  " U S:*  5      (       a  SnU$ [        SU R                  5        SU R                  5        S35      e[        R                  " SU :*  5      (       a"  [        R                  " U S:*  5      (       a  SnU$ [        S	U R                  5        SU R                  5        S35      e)
z
Detects whether or not the image needs to be rescaled before being converted to a PIL image.

The assumption is that if the image is of type `np.float` and all values are between 0 and 1, it needs to be
rescaled.
Fr      zZThe image to be converted to a PIL image contains values outside the range [0, 255], got [z, z%] which cannot be converted to uint8.r   TzXThe image to be converted to a PIL image contains values outside the range [0, 1], got [)
r4   r"   uint8allcloser7   intallr,   minmax)r   
do_rescales     r/   _rescale_for_pil_conversionrD      s	    {{bhh
   
ULL-	.	.66!u*"&&#"6"6J  		}Buyy{m3XZ  
U
		uz 2 2
 	 IIK=599;-/TV
 	
r1   zPIL.Image.Imagetorch.Tensor	tf.Tensorzjnp.ndarrayrC   
image_modec                    [        [        S/5        [        U [        R                  R                  5      (       a  U $ [        U 5      (       d  [        U 5      (       a  U R                  5       n O][        U 5      (       a  [        R                  " U 5      n O6[        U [        R                  5      (       d  [        S[        U 5       35      e[        U [        R                   U5      n U R"                  S   S:X  a  [        R$                  " U SS9OU n Uc  ['        U 5      OUnU(       a  [)        U S5      n U R+                  [        R,                  5      n [        R                  R/                  XS9$ )a  
Converts `image` to a PIL Image. Optionally rescales it and puts the channel dimension back as the last axis if
needed.

Args:
    image (`PIL.Image.Image` or `numpy.ndarray` or `torch.Tensor` or `tf.Tensor`):
        The image to convert to the `PIL.Image` format.
    do_rescale (`bool`, *optional*):
        Whether or not to apply the scaling factor (to make pixel values integers between 0 and 255). Will default
        to `True` if the image type is a floating type and casting to `int` would result in a loss of precision,
        and `False` otherwise.
    image_mode (`str`, *optional*):
        The mode to use for the PIL image. If unset, will use the default mode for the input image type.
    input_data_format (`ChannelDimension`, *optional*):
        The channel dimension format of the input image. If unset, will use the inferred format from the input.

Returns:
    `PIL.Image.Image`: The converted image.
visionz Input image type not supported: r   axisr<   mode)r   to_pil_imager!   PILImager   r   numpyr   r"   arrayr#   r,   r%   r0   r	   r+   shapesqueezerD   r:   r7   r=   	fromarray)r   rC   rG   r5   s       r/   rO   rO      s   2 lXJ/%)) ue!4!4	u		rzz**;DK=IJJ (/?/D/DFWXE +0++b/Q*>BJJu2&EE 8B7I,U3zJs#LL"E99u66r1   c                    U u  p4SnUbP  [        [        X445      5      n[        [        X445      5      nXv-  U-  U:  a  X&-  U-  n[        [	        U5      5      nX4::  a  X1:X  d
  XC::  a
  XA:X  a  X4pX4$ XC:  a/  Un	Ub  Ub  [        XS-  U-  5      nX4$ [        X-  U-  5      n X4$ UnUb  Ub  [        XT-  U-  5      n	X4$ [        X-  U-  5      n	X4$ )a  
Computes the output image size given the input image size and the desired output size.

Args:
    image_size (`Tuple[int, int]`):
        The input image size.
    size (`int`):
        The desired output size.
    max_size (`int`, *optional*):
        The maximum allowed output size.
N)floatrA   rB   r?   round)

image_sizesizemax_sizeheightwidthraw_sizemin_original_sizemax_original_sizeohows
             r/   get_size_with_aspect_ratiord      s    MFH!#vo"67!#vo"67047(B36GGHuX'DFNEMB 8O 
H$8X&./B 8O T]U*+B 8O H$8X%./B 8O T\F*+B8Or1   input_imager[   default_to_squarer\   c                    [        U[        [        45      (       a:  [        U5      S:X  a  [        U5      $ [        U5      S:X  a  US   nO[	        S5      eU(       a  X4$ [        X5      u  pVXe::  a  Xe4OXV4u  pxUn	U	[        X-  U-  5      pUb,  X9::  a  [	        SU SU 35      eX:  a  [        X:-  U-  5      UpXe::  a  X4$ X4$ )aV  
Find the target (height, width) dimension of the output image after resizing given the input image and the desired
size.

Args:
    input_image (`np.ndarray`):
        The image to resize.
    size (`int` or `Tuple[int, int]` or List[int] or `Tuple[int]`):
        The size to use for resizing the image. If `size` is a sequence like (h, w), output size will be matched to
        this.

        If `size` is an int and `default_to_square` is `True`, then image will be resized to (size, size). If
        `size` is an int and `default_to_square` is `False`, then smaller edge of the image will be matched to this
        number. i.e, if height > width, then image will be rescaled to (size * height / width, size).
    default_to_square (`bool`, *optional*, defaults to `True`):
        How to convert `size` when it is a single int. If set to `True`, the `size` will be converted to a square
        (`size`,`size`). If set to `False`, will replicate
        [`torchvision.transforms.Resize`](https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.Resize)
        with support for resizing only the smallest edge and providing an optional `max_size`.
    max_size (`int`, *optional*):
        The maximum allowed for the longer edge of the resized image: if the longer edge of the image is greater
        than `max_size` after being resized according to `size`, then the image is resized again so that the longer
        edge is equal to `max_size`. As a result, `size` might be overruled, i.e the smaller edge may be shorter
        than `size`. Only used if `default_to_square` is `False`.
    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.
r    r   r   z7size must have 1 or 2 elements if it is a list or tuplezmax_size = zN must be strictly greater than the requested size for the smaller edge size = )r!   tupler'   lenr,   r   r?   )re   r[   rf   r\   r5   r]   r^   shortlongrequested_new_short	new_shortnew_longs               r/   get_resize_output_image_sizero      s    J $&&t9>;Y!^7DVWW|";BMF%*_5/6/KE-s3F3MPU3U/Vx*hZ (4486;  "%h&:X&E"Fx$)OH N)9NNr1   resampler   reducing_gapreturn_numpyc                 <   [        [        S/5        Ub  UO[        R                  n[	        U5      S:X  d  [        S5      eUc  [        U 5      nUc  UOUnSn[        U [        R                  R                  5      (       d  [        U 5      n[        XUS9n Uu  pU R                  X4X#S9n
U(       aj  [        R                  " U
5      n
U
R                  S:X  a  [        R                  " U
SS9OU
n
[!        X["        R$                  S	9n
U(       a  ['        U
S
5      OU
n
U
$ )aw  
Resizes `image` to `(height, width)` specified by `size` using the PIL library.

Args:
    image (`np.ndarray`):
        The image to resize.
    size (`Tuple[int, int]`):
        The size to use for resizing the image.
    resample (`int`, *optional*, defaults to `PILImageResampling.BILINEAR`):
        The filter to user for resampling.
    reducing_gap (`int`, *optional*):
        Apply optimization by resizing the image in two steps. The bigger `reducing_gap`, the closer the result to
        the fair resampling. See corresponding Pillow documentation for more details.
    data_format (`ChannelDimension`, *optional*):
        The channel dimension format of the output image. If unset, will use the inferred format from the input.
    return_numpy (`bool`, *optional*, defaults to `True`):
        Whether or not to return the resized image as a numpy array. If False a `PIL.Image.Image` object is
        returned.
    input_data_format (`ChannelDimension`, *optional*):
        The channel dimension format of the input image. If unset, will use the inferred format from the input.

Returns:
    `np.ndarray`: The resized image.
rI   r    zsize must have 2 elementsF)rC   r5   )rp   rq   rJ   rK   r   gp?)r   resizer   BILINEARri   r,   r   r!   rP   rQ   rD   rO   r"   rS   r)   expand_dimsr0   r	   r+   r:   )r   r[   rp   rq   r3   rr   r5   rC   r]   r^   resized_images              r/   ru   ru   B  s   B fxj)#/x5G5P5PHt9>455  :5A'2':#K JeSYY__--07
UM^_MFLL%8L_M/ CPBTBTXYBY}2>_l3:J:O:O

 <Fw7=r1   meanstdc                 n   [        U [        R                  5      (       d  [        S5      eUc  [	        U 5      n[        XS9nU R                  U   n[        R                  " U R                  [        R                  5      (       d  U R                  [        R                  5      n [        U[        5      (       a*  [        U5      U:w  a  [        SU S[        U5       35      eOU/U-  n[        R                  " XR                  S9n[        U[        5      (       a*  [        U5      U:w  a  [        SU S[        U5       35      eOU/U-  n[        R                  " X R                  S9nU[        R                   :X  a  X-
  U-  n OU R"                  U-
  U-  R"                  n Ub  [%        XU5      n U $ U n U $ )a  
Normalizes `image` using the mean and standard deviation specified by `mean` and `std`.

image = (image - mean) / std

Args:
    image (`np.ndarray`):
        The image to normalize.
    mean (`float` or `Collection[float]`):
        The mean to use for normalization.
    std (`float` or `Collection[float]`):
        The standard deviation to use for normalization.
    data_format (`ChannelDimension`, *optional*):
        The channel dimension format of the output image. If unset, will use the inferred format from the input.
    input_data_format (`ChannelDimension`, *optional*):
        The channel dimension format of the input image. If unset, will use the inferred format from the input.
zimage must be a numpy array)r5   zmean must have z$ elements if it is an iterable, got r4   zstd must have )r!   r"   r#   r,   r   r   rT   
issubdtyper4   floatingr7   float32r   ri   rS   r	   r+   Tr0   )r   ry   rz   r3   r5   channel_axisnum_channelss          r/   	normalizer     s   0 eRZZ((677 :5A-eYL;;|,L ==bkk22RZZ($
##t9$|n<`adeiaj`klmm % v$88D,D#z""s8|#~l^;_`cdg`h_ijkk $ el"
((3kk
*C,111$''D.C'**R]Ri'<MNEL puELr1   c                    [        [        S/5        [        U [        R                  5      (       d  [        S[        U 5       35      e[        U[        5      (       a  [        U5      S:w  a  [        S5      eUc  [        U 5      nUb  UOUn[        U [        R                  U5      n [        U [        R                  5      u  pVUu  px[        U5      [        U5      pXW-
  S-  n	X-   n
Xh-
  S-  nX-   nU	S:  a6  X::  a1  US:  a+  X::  a&  U SX2X24   n [        X[        R                  5      n U $ [!        Xu5      n[!        X5      nU R"                  SS X4-   n[        R$                  " XS	9n['        X-
  S-  5      nUU-   n['        X-
  S-  5      nUU-   nU USUU2UU24'   U	U-  n	U
U-  n
UU-  nUU-  nUS[!        SU	5      [)        X5      2[!        SU5      [)        X5      24   n[        UU[        R                  5      nU$ )
a  
Crops the `image` to the specified `size` using a center crop. Note that if the image is too small to be cropped to
the size given, it will be padded (so the returned result will always be of size `size`).

Args:
    image (`np.ndarray`):
        The image to crop.
    size (`Tuple[int, int]`):
        The target size for the cropped image.
    data_format (`str` or `ChannelDimension`, *optional*):
        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.
        If unset, will use the inferred format of the input image.
    input_data_format (`str` or `ChannelDimension`, *optional*):
        The channel dimension format for 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.
        If unset, will use the inferred format of the input image.
Returns:
    `np.ndarray`: The cropped image.
rI   r   r    zOsize must have 2 elements representing the height and width of the output imageNr   .)rT   )r   center_cropr!   r"   r#   r$   r%   r   ri   r,   r   r0   r	   r&   r   r?   rB   rT   
zeros_liker   rA   )r   r[   r3   r5   output_data_formatorig_height
orig_widthcrop_height
crop_widthtopbottomleftright
new_height	new_width	new_shape	new_imagetop_pad
bottom_padleft_pad	right_pads                        r/   r   r     s9   8 kH:.eRZZ((FtE{mTUUdH%%Tajkk :5A(3(?EV (/?/E/EGXYE,U4D4J4JKK"K!+.J $
*CF#)DE axF)daiE<Oc3:tz12+EGWG]G]^ [.JJ+ICR J#::Ie5I J,12G;&JY+q01H:%I=BIc7:%x	'99:7NC
gFHD	XE#s1c{S-DDc!TlUXYbUjFjjkI+I7IK[KaKabIr1   bboxes_centerc                     U R                  S5      u  pp4[        R                  " USU-  -
  USU-  -
  USU-  -   USU-  -   /SS9nU$ )NrJ         ?dimunbindtorchstack)r   center_xcenter_yr^   r]   bbox_cornerss         r/   _center_to_corners_format_torchr     se    (5(<(<R(@%H;;
S5[
 HsV|$;x#PU+?UYadgjpdpYprL
 r1   c                     U R                   u  pp4[        R                  " USU-  -
  USU-  -
  USU-  -   USU-  -   /SS9nU$ )Nr   rJ   rK   r   r"   r   r   r   r   r^   r]   bboxes_cornerss         r/   _center_to_corners_format_numpyr   $  s]    (5%HXX	C%K	C&L!8(S5[:PRZ]`ci]iRijN
 r1   c                     [         R                  " U SS9u  pp4[         R                  " USU-  -
  USU-  -
  USU-  -   USU-  -   /SS9nU$ )NrJ   rK   r   tfunstackr   r   s         r/   _center_to_corners_format_tfr   .  sd    (*

=r(J%HXX	C%K	C&L!8(S5[:PRZ]`ci]iRijN
 r1   c                     [        U 5      (       a  [        U 5      $ [        U [        R                  5      (       a  [        U 5      $ [        U 5      (       a  [        U 5      $ [        S[        U 5       35      e)ae  
Converts bounding boxes from center format to corners format.

center format: contains the coordinate for the center of the box and its width, height dimensions
    (center_x, center_y, width, height)
corners format: contains the coordinates for the top-left and bottom-right corners of the box
    (top_left_x, top_left_y, bottom_right_x, bottom_right_y)
Unsupported input type )
r   r   r!   r"   r#   r   r   r   r,   r%   )r   s    r/   center_to_corners_formatr   9  se     }%%.}==	M2::	.	..}==	m	$	$+M::
.tM/B.CD
EEr1   r   c                 |    U R                  S5      u  pp4X-   S-  X$-   S-  X1-
  XB-
  /n[        R                  " USS9$ )NrJ   r    r   r   )r   
top_left_x
top_left_ybottom_right_xbottom_right_ybs         r/   _corners_to_center_format_torchr   N  sT    =K=R=RSU=V:JN		$)		$)		$		$		A ;;qb!!r1   c                 r    U R                   u  pp4[        R                  " X-   S-  X$-   S-  X1-
  XB-
  /SS9nU$ )Nr    rJ   rK   r   r   r   r   r   r   r   s         r/   _corners_to_center_format_numpyr   Y  sT    =K=M=M:JNHH(A-(A-((		
 M r1   c                     [         R                  " U SS9u  pp4[         R                  " X-   S-  X$-   S-  X1-
  XB-
  /SS9nU$ )NrJ   rK   r    r   r   s         r/   _corners_to_center_format_tfr   g  sZ    =?ZZ]_=`:JNHH(A-(A-((		
 M r1   c                     [        U 5      (       a  [        U 5      $ [        U [        R                  5      (       a  [        U 5      $ [        U 5      (       a  [        U 5      $ [        S[        U 5       35      e)ai  
Converts bounding boxes from corners format to center format.

corners format: contains the coordinates for the top-left and bottom-right corners of the box
    (top_left_x, top_left_y, bottom_right_x, bottom_right_y)
center format: contains the coordinate for the center of the box and its the width, height dimensions
    (center_x, center_y, width, height)
r   )
r   r   r!   r"   r#   r   r   r   r,   r%   )r   s    r/   corners_to_center_formatr   u  se     ~&&.~>>	NBJJ	/	/.~>>	n	%	%+N;;
.tN/C.DE
FFr1   c                    [        U [        R                  5      (       a  [        U R                  5      S:X  ai  U R
                  [        R                  :X  a  U R                  [        R                  5      n U SS2SS2S4   SU SS2SS2S4   -  -   SU SS2SS2S4   -  -   $ [        U S   SU S   -  -   SU S   -  -   5      $ )z"
Converts RGB color to unique ID.
r   Nr      r   i   r    )
r!   r"   r#   ri   rT   r4   r=   r7   int32r?   )colors    r/   	rgb_to_idr     s     %$$U[[)9Q)>;;"(("LL*EQ1W~eAq!Gn 44y5Aq>7QQQuQx#a.(9uQx+??@@r1   c                    [        U [        R                  5      (       ax  U R                  5       n[	        [        U R                  5      S/-   5      n[        R                  " U[        R                  S9n[        S5       H  nUS-  USU4'   US-  nM     U$ / n[        S5       H  nUR                  U S-  5        U S-  n M     U$ )z"
Converts unique ID to RGB color.
r   r|   r   .)r!   r"   r#   copyrh   r'   rT   zerosr=   r(   append)id_mapid_map_copy	rgb_shapergb_mapir   _s          r/   	id_to_rgbr     s     &"**%%kkm$v||,s23	((9BHH5qA)C/GCFOCK  E1XVc\"3  Lr1   c                   (    \ rS rSrSrSrSrSrSrSr	g)	PaddingModei  zH
Enum class for the different padding modes to use when padding images.
constantreflect	replicate	symmetric N)
__name__
__module____qualname____firstlineno____doc__CONSTANTREFLECT	REPLICATE	SYMMETRIC__static_attributes__r   r1   r/   r   r     s     HGIIr1   r   g        paddingrN   constant_valuesc                   ^ ^ Tc  [        T 5      mU U4S jnU" U5      nU[        R                  :X  a   U" U5      n[        R                  " T USUS9m OU[        R
                  :X  a  [        R                  " T USS9m OdU[        R                  :X  a  [        R                  " T USS9m O9U[        R                  :X  a  [        R                  " T USS9m O[        SU 35      eUb  [        T UT5      m T $ T m T $ )	a  
Pads the `image` with the specified (height, width) `padding` and `mode`.

Args:
    image (`np.ndarray`):
        The image to pad.
    padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`):
        Padding to apply to the edges of the height, width axes. Can be one of three formats:
        - `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis.
        - `((before, after),)` yields same before and after pad for height and width.
        - `(pad,)` or int is a shortcut for before = after = pad width for all axes.
    mode (`PaddingMode`):
        The padding mode to use. Can be one of:
            - `"constant"`: pads with a constant value.
            - `"reflect"`: pads with the reflection of the vector mirrored on the first and last values of the
              vector along each axis.
            - `"replicate"`: pads with the replication of the last value on the edge of the array along each axis.
            - `"symmetric"`: pads with the reflection of the vector mirrored along the edge of the array.
    constant_values (`float` or `Iterable[float]`, *optional*):
        The value to use for the padding if `mode` is `"constant"`.
    data_format (`str` or `ChannelDimension`, *optional*):
        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.
        If unset, will use same as the input image.
    input_data_format (`str` or `ChannelDimension`, *optional*):
        The channel dimension format for 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.
        If unset, will use the inferred format of the input image.

Returns:
    `np.ndarray`: The padded image.

c                 N  > [        U [        [        45      (       a  X 4X 44n O[        U [        5      (       a$  [	        U 5      S:X  a  U S   U S   4U S   U S   44n O[        U [        5      (       a+  [	        U 5      S:X  a  [        U S   [        5      (       a  X 4n OM[        U [        5      (       a*  [	        U 5      S:X  a  [        U S   [        5      (       a  U n O[        SU  35      eT[        R                  :X  a  S/U Q7O/ U QSP7n TR                  S:X  a  S/U Q7n U $ U n U $ )zQ
Convert values to be in the format expected by np.pad based on the data format.
r   r   r    zUnsupported format: )r   r      )	r!   r?   rX   rh   ri   r,   r	   r&   r)   )valuesr   r5   s    r/   _expand_for_data_format$pad.<locals>._expand_for_data_format  s-    fsEl++&(89F&&3v;!+;ay&),vay&).DEF&&3v;!+;
6RS9VY@Z@Z%F&&3v;!+;
6RS9V[@\@\F3F8<== '8;K;Q;Q&Q&"6"WhY_WhagWh ',jjAo&"6" <Br1   r   )rN   r   r   rM   edger   zInvalid padding mode: )
r   r   r   r"   padr   r   r   r,   r0   )r   r   rN   r   r3   r5   r   s   `    ` r/   r   r     s    V  :5A, &g.G{###1/BugJX	$$	$ugI6	&&	&ugF3	&&	&ugK81$899R]Ri'{<MNEL puELr1   c                     [        [        S/5        [        U [        R                  R                  5      (       d  U $ U R
                  S:X  a  U $ U R                  S5      n U $ )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.
rI   RGB)r   convert_to_rgbr!   rP   rQ   rN   convert)r   s    r/   r   r     sN     nxj1eSYY__--zzUMM% ELr1   c                     Uc  [        U 5      OUnU[        R                  :X  a  U SSSS24   n O-U[        R                  :X  a  U SSS2S4   n O[	        SU 35      eUb
  [        XUS9n U $ )a}  
Flips the channel order of the image.

If the image is in RGB format, it will be converted to BGR and vice versa.

Args:
    image (`np.ndarray`):
        The image to flip.
    data_format (`ChannelDimension`, *optional*):
        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.
        If unset, will use same as the input image.
    input_data_format (`ChannelDimension`, *optional*):
        The channel dimension format for the input image. Can be one of:
            - `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
            - `ChannelDimension.LAST`: image in (height, width, num_channels) format.
        If unset, will use the inferred format of the input image.
N.rJ   zUnsupported channel dimension: rt   )r   r	   r+   r&   r,   r0   )r   r3   r5   s      r/   flip_channel_orderr      s    0 BSAZ6u=`q,111c4R4i 	.44	4ddCi :;L:MNOO+ERcdLr1   c                 P    U R                  5       (       a  U $ U R                  5       $ N)is_floating_pointrX   )xs    r/   _cast_tensor_to_floatr   F  s"    779r1   imagesc           
      2   0 n0 n[        U 5       HD  u  p4UR                  SS nXQ;  a  / X'   X   R                  U5        U[        X   5      S-
  4X#'   MF     UR	                  5        VV s0 s H  u  pPU[
        R                  " U SS9_M     nnn X4$ s  sn nf )z
Groups images by shape.
Returns a dictionary with the shape as key and a list of images with that shape as value,
and a dictionary with the index of the image in the original list as key and the shape and index in the grouped list as value.
r   Nr   r   )	enumeraterT   r   ri   itemsr   r   )r   grouped_imagesgrouped_images_indexr   r   rT   s         r/   group_images_by_shaper   L  s     Nf%AB&$&N!$$U+#(#n.C*Dq*H"I & N\MaMaMcdMcMEeU[[Q77McNd// es   +"Bprocessed_imagesr   c                 v    [        [        U5      5       Vs/ s H  nXU   S      X   S      PM     sn$ s  snf )z6
Reconstructs a list of images in the original order.
r   r   )r(   ri   )r   r   r   s      r/   reorder_imagesr   a  sO     s/011A 	a0345I5LQ5OP1  s   6c                   :    \ rS rSrSrS\R                  4S jrSrg)NumpyToTensorim  z,
Convert a numpy array to a PyTorch tensor.
r   c                 l    [         R                  " UR                  SSS5      5      R                  5       $ )Nr    r   r   )r   
from_numpyr*   
contiguous)selfr   s     r/   __call__NumpyToTensor.__call__r  s+     1a 89DDFFr1   r   N)	r   r   r   r   r   r"   r#   r  r   r   r1   r/   r  r  m  s    Gbjj Gr1   r  r   )NNN)TNN)NNNTN)NN)r   rE   r   rE   )r   rF   r   rF   )r   rE   r   rE   )r   rF   r   rF   )Icollections.abcr   r   mathr   typingr   r   rR   r"   image_utilsr	   r
   r   r   r   utilsr   r   r   r   r   utils.import_utilsr   r   r   r   r   rP   r   r   
tensorflowr   	jax.numpyjnpr#   strr0   r   rX   r4   r:   rD   boolrO   rh   r?   rd   r'   ro   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   dictr   r   r  r   r1   r/   <module>r     s&   1  "   Z Y  / AE(::(',-(  &6&; <=( ZZ	(\ /3jj@D#::## *+# 88	#
  c+;&; <=# ZZ#L: "& $@D	37.]Z[3737 37  c+;&; <=	37
 37l$5c? $V #"@D@O@O
U38_d3is;
<@O @O sm	@O
  c+;&; <=@O @OL &*"&.2@DD::D
S/D #D 3-	D
 *+D D  c+;&; <=D ZZDV /3@D::::
z%((
): 
uj''	(: *+	:
  c+;&; <=: ZZ:@ ;?@D	Q::Q
S/Q %%5 567Q  c+;&; <=	Q
 ZZQh2:: "** FJ F: F*"BJJ 2:: GZ GJ G.A&,  $,,58:>@DS::S3c3h%S/)BBCS S 5(5/12	S
 %%5 567S  c+;&; <=S ZZSn*  , /3@D#::#*+#  c+;&; <=# ZZ	#L0 0
4c3hn!556S%cSVhY\H\B]=]8^^_0*	5c?N:;	SWX[]bcfhkck]lXlSm		.	G Gr1   