
    Uhґ                        d dl mZmZ d dlmZ d dlmZmZ d dlZ	ddl
mZmZmZmZmZ ddlmZmZmZmZmZ ddlmZmZmZmZmZ  e       r
d dlZdd	l
mZ  e       rd dlZ e       rd dlZ  e       rd dl!mZ" 	 dHd
e	jF                  deee$f   deeee$f      de	jF                  fdZ%de	jL                  dfd
e	jF                  de'dee   de	jP                  deee$ef      de	jF                  fdZ)d Z*	 	 	 dId
ee	jF                  ddddf   dee+   dee$   deee$ef      ddf
dZ,dHde-e.e.f   fdZ/	 	 	 dJde	jF                  dee.e-e.e.f   e0e.   e-e.   f   de+d ee.   deee$ef      de-fd!Z1	 	 	 	 	 dKd
e	jF                  de-e.e.f   d"d#d$ee.   dee   d%e+deee$ef      de	jF                  fd&Z2	 	 dLd
e	jF                  d'ee'ee'   f   d(ee'ee'   f   dee   deee$ef      de	jF                  fd)Z3	 	 dLd
e	jF                  de-e.e.f   deee$ef      deee$ef      de	jF                  f
d*Z4dMd,Z5d+e	jF                  de	jF                  fd-Z6dNd.Z7d+edefd/Z8dOd1Z9d0e	jF                  de	jF                  fd2Z:dPd3Z;d0edefd4Z<d5 Z=d6 Z> G d7 d8e      Z?e?j                  d9ddfd
e	jF                  d:ee.e-e.e.f   ee-e.e.f      f   d;e?d<ee'ee'   f   deee$ef      deee$ef      de	jF                  fd=ZAd
edefd>ZB	 	 dLd
e	jF                  dee   deee$ef      de	jF                  fd?ZCd@ ZDdAe0d   de-eEe-e.e.f   e0d   f   eEe.e-e-e.e.f   e.f   f   f   fdBZFdCeEe-e.e.f   df   dDeEe.e-e.e.f   f   de0d   fdEZG G dF dG      ZHy)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                    t        | t        j                        st        dt	        |              |t        |       }t        |      }||k(  r| S |t        j                  k(  rat        t        | j                  dz
              | j                  dz
  | j                  dz
  | j                  dz
  gz   }| j                  |      } | S |t        j                  k(  rat        t        | j                  dz
              | j                  dz
  | j                  dz
  | j                  dz
  gz   }| j                  |      } | S t        d|       )a  
    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        o/var/www/catia.catastroantioquia-mas.com/valormas/lib/python3.12/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                     t        | t        j                        st        dt	        |              | j                  t        j                        |z  }|t        |||      }|j                  |      }|S )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:   `   sk    4 eRZZ(FtE{mTUU\\"**-5N4^[Rcd#**51Nr1   c                    | j                   t        j                  k(  rd}|S t        j                  | | j	                  t
                    rbt        j                  d| k        rt        j                  | dk        rd}|S t        d| j                          d| j                          d      t        j                  d| k        rt        j                  | dk        rd}|S t        d	| j                          d| j                          d      )
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
   
UELL-	.66!u*"&&#"6J  		}Buyy{m3XZ  
U
	uz 2
 	 IIK=599;-/TV
 	
r1   zPIL.Image.Imagetorch.Tensor	tf.Tensorzjnp.ndarrayrC   
image_modec                    t        t        dg       t        | t        j                  j                        r| S t        |       st        |       r| j                         } nRt        |       rt        j                  |       } n1t        | t        j                        st        dt        |              t        | t        j                   |      } | j"                  d   dk(  rt        j$                  | d      n| } |t'        |       n|}|rt)        | d      } | j+                  t        j,                        } t        j                  j/                  | |      S )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	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                    | \  }}d}|St        t        ||f            }t        t        ||f            }||z  |z  |kD  r||z  |z  }t        t	        |            }||k  r||k(  s
||k  r||k(  r||}	}||	fS ||k  r0|}	||t        ||z  |z        }||	fS t        ||z  |z        }||	fS |}||t        ||z  |z        }	||	fS t        ||z  |z        }	||	fS )aC  
    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      sF    MFEH!#vuo"67!#vuo"670047(B"336GGHuX'D%FdNETMB 8O 
H$8X&./B 8O TF]U*+B 8O H$8X%./B 8O TE\F*+B8Or1   input_imager[   default_to_squarer\   c                    t        |t        t        f      r8t        |      dk(  rt        |      S t        |      dk(  r|d   }nt	        d      |r||fS t        | |      \  }}||k  r||fn||f\  }}|}	|	t        |	|z  |z        }}
|.||	k  rt	        d| d|       ||kD  rt        ||
z  |z        |}}
||k  r||
fS |
|fS )a  
    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d|";0ABMFE%*f_5&/65/KE4-s3F3MPU3U/VxI**hZ (4486;  h"%h&:X&E"FxI$)VOHi N)X9NNr1   resampler   reducing_gapreturn_numpyc                 2   t        t        dg       ||nt        j                  }t	        |      dk(  st        d      |t        |       }||n|}d}t        | t        j                  j                        st        |       }t        | ||      } |\  }}	| j                  |	|f||      }
|rit        j                  |
      }
|
j                  dk(  rt        j                  |
d      n|
}
t!        |
|t"        j$                  	      }
|rt'        |
d
      n|
}
|
S )a  
    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
UzM^_MFELL%8R^L_M/ CPBTBTXYBY}2>_l3;:J:O:O

 <Fw7=r1   meanstdc                 N   t        | t        j                        st        d      |t	        |       }t        | |      }| j                  |   }t        j                  | j                  t        j                        s| j                  t        j                        } t        |t              r(t        |      |k7  r t        d| dt        |             |g|z  }t        j                  || j                        }t        |t              r(t        |      |k7  r t        d| dt        |             |g|z  }t        j                  || j                        }|t        j                   k(  r	| |z
  |z  } n| j"                  |z
  |z  j"                  } |t%        | ||      } | S | } | S )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-eGXYL;;|,L ==bkk2RZZ($
#t9$|n<`adeiaj`klmmv$88D,D#z"s8|#~l^;_`cdg`h_ijkkel"
((3ekk
*C,111$''D.C'**R]Ri'{<MNEL puELr1   c                    t        t        dg       t        | t        j                        st        dt        |              t        |t              rt        |      dk7  rt        d      |t        |       }||n|}t        | t        j                  |      } t        | t        j                        \  }}|\  }}t        |      t        |      }}||z
  dz  }	|	|z   }
||z
  dz  }||z   }|	dk\  r8|
|k  r3|dk\  r.||k  r)| d|	|
||f   } t        | |t        j                        } | S t!        ||      }t!        ||      }| j"                  dd ||fz   }t        j$                  | |	      }t'        ||z
  dz        }||z   }t'        ||z
  dz        }||z   }| |d||||f<   |	|z  }	|
|z  }
||z  }||z  }|dt!        d|	      t)        ||
      t!        d|      t)        ||      f   }t        ||t        j                        }|S )
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     s]   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K $
*C;F#)D:E axFk)daiEZ<Oc3v:tEz12+E3EGWG]G]^ [+.JJ
+ICR J	#::Ie95I J,12G;&JY+q01H:%I=BIc7:%x	'99:7NC
gFHD	XE#s1c{SV-DDc!TlUXYbdiUjFjjkI+I7IK[KaKabIr1   bboxes_centerc                     | j                  d      \  }}}}t        j                  |d|z  z
  |d|z  z
  |d|z  z   |d|z  z   gd      }|S )NrJ         ?dimunbindtorchstack)r   center_xcenter_yr^   r]   bbox_cornerss         r/   _center_to_corners_format_torchr     si    (5(<(<R(@%Hhv;;
S5[
 HsV|$;x#PU+?UYadgjpdpYprL
 r1   c                     | j                   \  }}}}t        j                  |d|z  z
  |d|z  z
  |d|z  z   |d|z  z   gd      }|S )Nr   rJ   rK   r   r"   r   r   r   r   r^   r]   bboxes_cornerss         r/   _center_to_corners_format_numpyr   $  sa    (5%HhvXX	C%K	C&L!8(S5[:PRZ]`ci]iRijN
 r1   c                     t        j                  | d      \  }}}}t        j                  |d|z  z
  |d|z  z
  |d|z  z   |d|z  z   gd      }|S )NrJ   rK   r   tfunstackr   r   s         r/   _center_to_corners_format_tfr   .  sh    (*

=r(J%HhvXX	C%K	C&L!8(S5[:PRZ]`ci]iRijN
 r1   c                     t        |       rt        |       S t        | t        j                        rt        |       S t        |       rt        |       S t        dt        |              )a}  
    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  s\     }%.}==	M2::	..}==	m	$+M::
.tM/B.CD
EEr1   r   c                     | j                  d      \  }}}}||z   dz  ||z   dz  ||z
  ||z
  g}t        j                  |d      S )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  s`    =K=R=RSU=V:J
NN	n	$)	n	$)	*	$	*	$		A ;;qb!!r1   c                     | j                   \  }}}}t        j                  ||z   dz  ||z   dz  ||z
  ||z
  gd      }|S )Nr    rJ   rK   r   r   r   r   r   r   r   s         r/   _corners_to_center_format_numpyr   Y  s`    =K=M=M:J
NNHH.(A-.(A-j(j(		
 M r1   c                     t        j                  | d      \  }}}}t        j                  ||z   dz  ||z   dz  ||z
  ||z
  gd      }|S )NrJ   rK   r    r   r   s         r/   _corners_to_center_format_tfr   g  sf    =?ZZ]_=`:J
NNHH.(A-.(A-j(j(		
 M r1   c                     t        |       rt        |       S t        | t        j                        rt        |       S t        |       rt        |       S t        dt        |              )a  
    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  s\     ~&.~>>	NBJJ	/.~>>	n	%+N;;
.tN/C.DE
FFr1   c                 v   t        | t        j                        rt        | j                        dk(  rh| j
                  t        j                  k(  r| j                  t        j                        } | dddddf   d| dddddf   z  z   d| dddddf   z  z   S t        | d   d| d   z  z   d| d   z  z         S )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                    t        | t        j                        rx| j                         }t	        t        | j                        dgz         }t        j                  |t        j                        }t        d      D ]  }|dz  |d|f<   |dz  } |S g }t        d      D ]  }|j                  | dz         | dz  }  |S )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                        e Zd ZdZdZdZdZdZy)PaddingModezP
    Enum class for the different padding modes to use when padding images.
    constantreflect	replicate	symmetricN)__name__
__module____qualname____doc__CONSTANTREFLECT	REPLICATE	SYMMETRIC r1   r/   r   r     s     HGIIr1   r   g        paddingrN   constant_valuesc                     t                fd} ||      }|t        j                  k(  r" ||      }t        j                   |d|       n|t        j
                  k(  rt        j                   |d       nf|t        j                  k(  rt        j                   |d       n:|t        j                  k(  rt        j                   |d       nt        d|       |t         |        S    S )	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                    t        | t        t        f      r	| | f| | ff} nt        | t              r#t	        |       dk(  r| d   | d   f| d   | d   ff} nxt        | t              r&t	        |       dk(  rt        | d   t              r| | f} nBt        | t              r$t	        |       dk(  rt        | d   t              r| } nt        d|        t        j                  k(  rdg| ng | d} j                  dk(  rdg| } | S | } | S )za
        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_formatz$pad.<locals>._expand_for_data_format  s!    fsEl+v&(89F&3v;!+;ay&),vay&).DEF&3v;!+;
6RS9VY@Zf%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                     t        t        dg       t        | t        j                  j                        s| S | j
                  dk(  r| S | 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.
    rI   RGB)r   convert_to_rgbr!   rP   rQ   rN   convert)r   s    r/   r   r     sK     nxj1eSYY__-zzUMM% ELr1   c                     |t        |       n|}|t        j                  k(  r| ddddf   } n,|t        j                  k(  r| ddddf   } nt	        d|       |t        | ||      } | S )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+E;RcdLr1   c                 F    | j                         r| S | j                         S N)is_floating_pointrX   )xs    r/   _cast_tensor_to_floatr   F  s    779r1   imagesc           	      8   i }i }t        |       D ]G  \  }}|j                  dd }||vrg ||<   ||   j                  |       |t        ||         dz
  f||<   I |j	                         D  ci c]  \  }} |t        j                  | d       }}} ||fS c c} }w )a  
    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% J5AB&$&N5!u$$U+#(#nU.C*Dq*H"IQJ N\MaMaMcdME6eU[[Q77dNd/// es   -"Bprocessed_imagesr   c                 t    t        t        |            D cg c]  }| ||   d      ||   d       c}S c c}w )z>
    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 	-a0345I!5LQ5OP  s   5c                   0    e Zd ZdZdej
                  fdZy)NumpyToTensorz4
    Convert a numpy array to a PyTorch tensor.
    r   c                 j    t        j                  |j                  ddd            j                         S )Nr    r   r   )r   
from_numpyr*   
contiguous)selfr   s     r/   __call__zNumpyToTensor.__call__r  s+     1a 89DDFFr1   N)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   