
    <hk                        S SK Jr  S SKJr  S SKJrJr  S SKJrJ	r	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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!J"r"J#r#  SS
K$J%r%  SSK&J'r'J(r(J)r)J*r*J+r+J,r,J-r-  SSK.J/r/  \," 5       (       a  SSKJ0r0  \)" 5       (       a  S SK1r1\*" 5       (       a   SSKJ2r2  \+" 5       (       a  S SK3J4r5  O	S SK6J4r5  OSr2\-Rn                  " \85      r9\" SS9SSSSSSSSSSSSS\Rt                  4S\	\;   S\	\<   S\	\;   S\	\\<\=\<   4      S\	\\<\=\<   4      S\	\;   S\	\>   S\	\;   S\	\   S\	\;   S\	\   S\	S   S\	\\?\'4      S \	\   4S! jj5       r@S4S"S#S$\	\>   S%S#4S& jjrAS'\\   S%\=\   4S( jrBS)\=S#   S%\C\>   4S* jrDS+\\R                  S#4   S,\>S%\=\\R                  S#4      4S- jrF " S. S/\
S0S19rG\( " S2 S3\5      5       rHg)5    )Iterable)deepcopy)	lru_cachepartial)AnyOptional	TypedDictUnionN   )BaseImageProcessorBatchFeatureget_size_dict)convert_to_rgbget_resize_output_image_sizeget_size_with_aspect_ratiogroup_images_by_shapereorder_images)ChannelDimension
ImageInput	ImageTypeSizeDictget_image_size#get_image_size_for_max_height_widthget_image_typeinfer_channel_dimension_formatmake_flat_list_of_imagesvalidate_kwargsvalidate_preprocess_arguments)Unpack)
TensorTypeauto_docstringis_torch_availableis_torchvision_availableis_torchvision_v2_availableis_vision_availablelogging)is_rocm_platform)PILImageResampling)pil_torch_interpolation_mapping)
functional
   maxsize
do_rescalerescale_factordo_normalize
image_mean	image_stddo_padsize_divisibilitydo_center_crop	crop_size	do_resizesizeresampler(   return_tensorsdata_formatc                     [        U UUUUUUUUU	U
US9  Ub  US:w  a  [        S5      eU[        R                  :w  a  [        S5      eg)z
Checks validity of typically used arguments in an `ImageProcessorFast` `preprocess` method.
Raises `ValueError` if arguments incompatibility is caught.
)r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   Nptz6Only returning PyTorch tensors is currently supported.z6Only channel first data format is currently supported.)r   
ValueErrorr   FIRST)r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   s                 `/var/www/html/shao/venv/lib/python3.13/site-packages/transformers/image_processing_utils_fast.py"validate_fast_preprocess_argumentsrA   K   sk    * "%!+% !n&<QRR&,,,QRR -    tensortorch.Tensoraxisreturnc                 l    Uc  U R                  5       $  U R                  US9$ ! [         a    U s $ f = f)z>
Squeezes a tensor, but only if the axis specified has dim 1.
)rE   )squeezer>   )rC   rE   s     r@   safe_squeezerI   v   s@     |~~~~4~(( s   $ 33valuesc                 N    [        U 6  Vs/ sH  n[        U5      PM     sn$ s  snf )zG
Return the maximum value across all indices of an iterable of values.
)zipmax)rJ   values_is     r@   max_across_indicesrO      s$     +.v,7,hCM,777s   "imagesc                 `    [        U  Vs/ sH  oR                  PM     sn5      u  p#nX44$ s  snf )z@
Get the maximum height and width across all images in a batch.
)rO   shape)rP   img_
max_height	max_widths        r@   get_max_height_widthrW      s3    
  22O992OPA9"" 3Ps   +image
patch_sizec                     / n[        U [        R                  S9u  p4[        SX15       H9  n[        SXA5       H&  nU SS2XUU-   2XfU-   24   nUR	                  U5        M(     M;     U$ )a  
Divides an image into patches of a specified size.

Args:
    image (`Union[np.array, "torch.Tensor"]`):
        The input image.
    patch_size (`int`):
        The size of each patch.
Returns:
    list: A list of Union[np.array, "torch.Tensor"] representing the patches.
)channel_dimr   N)r   r   r?   rangeappend)rX   rY   patchesheightwidthijpatchs           r@   divide_to_patchesrd      sr     G"56F6L6LMMF1f)q%,A!QZ/^1CCDENN5! - *
 NrB   c                   ~   \ rS rSr% \\   \S'   \\\\	4      \S'   \\   \S'   \\
S      \S'   \\   \S'   \\\\	4      \S'   \\   \S	'   \\
\	\4      \S
'   \\   \S'   \\
\\\   4      \S'   \\
\\\   4      \S'   \\   \S'   \\
\\4      \S'   \\   \S'   \\
\\4      \S'   \S   \S'   \\   \S'   Srg)DefaultFastImageProcessorKwargs   r7   r8   default_to_squarer(   F.InterpolationModer9   r5   r6   r.   r/   r0   r1   r2   do_convert_rgbr:   r;   input_data_formattorch.devicedevicedisable_grouping N)__name__
__module____qualname____firstlineno__r   bool__annotations__dictstrintr
   floatlistr    r   __static_attributes__rp   rB   r@   rf   rf      s   ~
4S>
""~%uHIJJTN"S#X''U3:.//4. ud5k1233eT%[0122TN"U3
?344*++c+;&; <==^$$tn$rB   rf   F)totalc                   z  ^  \ rS rSrSrSrSrSrSrSr	Sr
SrSrSrSrSrSr\R$                  rSrSrS/r\rSrS\\   SS4U 4S jjr  S>S	S
S\SSS\SS
4
S jjr\  S>S	S
S\ \!\!4   S\"S   S\SS
4
S jj5       r#S	S
S\$SS
4S jr%S	S
S\&\$\'\$   4   S\&\$\'\$   4   SS
4S jr(\)" SS9      S?S\"\   S\"\&\$\*\$   4      S\"\&\$\*\$   4      S\"\   S\"\$   S\"S   S\ 4S  jj5       r+S!S
S\S\$S\S\&\$\*\$   4   S\&\$\*\$   4   SS
4S" jr,S	S
S\-\.\!4   SS
4S# jr/S	\0S\04S$ jr1S\-4S% jr2 S@S!\0S&\!S\04S' jjr3   SAS	\0S(\"\   S)\"\&\.\4      S\"S   SS
4
S* jjr4    SBS!\0S(\"\   S)\"\&\.\4      S\"S   S&\!S\*S
   4S+ jjr5      S?S\"\   S,\"\   S-\"\   S\"\&\$\*\$   4      S\"\&\$\*\$   4      S.\"\   S\-4S/ jjr6            SCS\"\   S\"\$   S\"\   S\"\&\$\ \$   4      S\"\&\$\ \$   4      S0\"\   S\"\   S1\"\   S,\"\   S2\"\&S3      S4\"\&\.\74      S.\"\   4S5 jjr8S!\0S\\   S\94S6 jr:\;S!\0S\\   S\94S7 j5       r<SS8.S!\0S(\S)\S\"\&\.S4      S\\   S\94S9 jjr=S!\*S
   S0\S\S\"S   S1\S,\S\S\$S\S\"\&\$\*\$   4      S\"\&\$\*\$   4      S:\"\   S4\"\&\.\74      S\94S; jr>U 4S< jr?S=r@U =rA$ )DBaseImageProcessorFast   NTgp?pixel_valueskwargsrF   c                 \  > [         TU ]  " S0 UD6  U R                  U5      nUR                  SU R                  5      nUb#  [        X!R                  SU R                  5      S9OS U l        UR                  SU R                  5      nUb
  [        USS9OS U l        U R                  R                   HE  nUR                  US 5      nUb  [        XU5        M&  [        X[        [        XS 5      5      5        MG     [        U R                  R                  R                  5       5      U l        g )Nr8   rh   r8   rh   r6   
param_namerp   )super__init__filter_out_unused_kwargspopr8   r   rh   r6   valid_kwargsrv   setattrr   getattrr{   keys_valid_kwargs_names)selfr   r8   r6   keykwarg	__class__s         r@   r   BaseImageProcessorFast.__init__   s    	"6"..v6zz&$)),  tzzBUW[WmWm7no 		
 JJ{DNN;	MVMby[Ihl$$44CJJsD)E 5)8GDt,D#EF 5 $((9(9(I(I(N(N(P#Q rB   rX   rD   r8   interpolationrj   	antialiasc                 F   Ub  UO[         R                  R                  nUR                  (       aD  UR                  (       a3  [        UR                  5       SS UR                  UR                  5      nOUR                  (       a%  [        UUR                  S[        R                  S9nOUR                  (       aD  UR                  (       a3  [        UR                  5       SS UR                  UR                  5      nOJUR                  (       a*  UR                  (       a  UR                  UR                  4nO[        SU S35      e[         R"                  R%                  5       (       a!  ['        5       (       a  U R)                  XX45      $ [         R*                  " XX4S9$ )a  
Resize an image to `(size["height"], size["width"])`.

Args:
    image (`torch.Tensor`):
        Image to resize.
    size (`SizeDict`):
        Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image.
    interpolation (`InterpolationMode`, *optional*, defaults to `InterpolationMode.BILINEAR`):
        `InterpolationMode` filter to use when resizing the image e.g. `InterpolationMode.BICUBIC`.

Returns:
    `torch.Tensor`: The resized image.
NF)r8   rh   rl   zjSize must contain 'height' and 'width' keys, or 'max_height' and 'max_width', or 'shortest_edge' key. Got .r   r   )FInterpolationModeBILINEARshortest_edgelongest_edger   r8   r   r   r?   rU   rV   r   r_   r`   r>   torchcompileris_compilingr'   compile_friendly_resizeresize)r   rX   r8   r   r   r   new_sizes          r@   r   BaseImageProcessorFast.resize   s<   , *7)BH[H[HdHd$"3"3 2

RS!""!!H
 3''"'"2"8"8	H __:5::<;Ldoo_c_m_mnH[[TZZTZZ0H6  >>&&((-=-?-?//ZZxx}ZZrB   r   c                    U R                   [        R                  :X  a  U R                  5       S-  n [        R
                  " XX#S9n U S-  n [        R                  " U S:  SU 5      n [        R                  " U S:  SU 5      n U R                  5       R                  [        R                  5      n U $ [        R
                  " XX#S9n U $ )zk
A wrapper around `F.resize` so that it is compatible with torch.compile when the image is a uint8 tensor.
   r      r   )	dtyper   uint8rz   r   r   whereroundto)rX   r   r   r   s       r@   r   .BaseImageProcessorFast.compile_friendly_resize$  s     ;;%++%KKMC'EHHUM_ECKEKKS%8EKK	1e4EKKM$$U[[1E  HHUM_ErB   scalec                 
    X-  $ )z
Rescale an image by a scale factor. image = image * scale.

Args:
    image (`torch.Tensor`):
        Image to rescale.
    scale (`float`):
        The scaling factor to rescale pixel values by.

Returns:
    `torch.Tensor`: The rescaled image.
rp   )r   rX   r   r   s       r@   rescaleBaseImageProcessorFast.rescale9  s    $ }rB   meanstdc                 0    [         R                  " XU5      $ )a  
Normalize an image. image = (image - image_mean) / image_std.

Args:
    image (`torch.Tensor`):
        Image to normalize.
    mean (`torch.Tensor`, `float` or `Iterable[float]`):
        Image mean to use for normalization.
    std (`torch.Tensor`, `float` or `Iterable[float]`):
        Image standard deviation to use for normalization.

Returns:
    `torch.Tensor`: The normalized image.
)r   	normalize)r   rX   r   r   r   s        r@   r    BaseImageProcessorFast.normalizeM  s    * {{5,,rB   r+   r,   r0   r1   r2   r.   r/   rn   rm   c                     U(       a=  U(       a6  [         R                  " X&S9SU-  -  n[         R                  " X6S9SU-  -  nSnX#U4$ )Nrn   g      ?F)r   rC   )r   r0   r1   r2   r.   r/   rn   s          r@   !_fuse_mean_std_and_rescale_factor8BaseImageProcessorFast._fuse_mean_std_and_rescale_factord  sI     ,j@C.DXYJY>#BVWIJj00rB   rP   c           	          U R                  UUUUUUR                  S9u  pVnU(       a/  U R                  UR                  [        R
                  S9XV5      nU$ U(       a  U R                  X5      nU$ )z
Rescale and normalize images.
)r0   r1   r2   r.   r/   rn   )r   )r   rn   r   r   r   float32r   )r   rP   r.   r/   r0   r1   r2   s          r@   rescale_and_normalize,BaseImageProcessorFast.rescale_and_normalizeu  sx     -1,R,R%!!)== -S -
)
z ^^FIIEMMI$BJZF  \\&9FrB   c                     UR                   b  UR                  c  [        SUR                  5        35      e[        R
                  " XS   US   45      $ )aj  
Center crop an image to `(size["height"], size["width"])`. If the input size is smaller than `crop_size` along
any edge, the image is padded with 0's and then center cropped.

Args:
    image (`"torch.Tensor"`):
        Image to center crop.
    size (`dict[str, int]`):
        Size of the output image.

Returns:
    `torch.Tensor`: The center cropped image.
z=The size dictionary must have keys 'height' and 'width'. Got r_   r`   )r_   r`   r>   r   r   center_crop)r   rX   r8   r   s       r@   r   "BaseImageProcessorFast.center_crop  sQ    & ;;$**"4\]a]f]f]h\ijkk}}U(^T']$CDDrB   c                     [        U5      $ )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 (ImageInput):
        The image to convert.

Returns:
    ImageInput: The converted image.
)r   )r   rX   s     r@   r   %BaseImageProcessorFast.convert_to_rgb  s     e$$rB   c                     U R                   c  U$ U R                    H4  nX!;   d  M
  [        R                  SU S35        UR                  U5        M6     U$ )z:
Filter out the unused kwargs from the kwargs dictionary.
z!This processor does not use the `z ` parameter. It will be ignored.)unused_kwargsloggerwarning_oncer   )r   r   
kwarg_names      r@   r   /BaseImageProcessorFast.filter_out_unused_kwargs  sW     %M,,J###&G
|Ss$tu

:& - rB   expected_ndimsc                     [        XS9$ )z
Prepare the images structure for processing.

Args:
    images (`ImageInput`):
        The input images to process.

Returns:
    `ImageInput`: The images with a valid nesting.
r   )r   )r   rP   r   s      r@   _prepare_images_structure0BaseImageProcessorFast._prepare_images_structure  s     (NNrB   rk   rl   c                    [        U5      nU[        R                  [        R                  [        R                  4;  a  [        SU 35      eU(       a  U R                  U5      nU[        R                  :X  a  [        R                  " U5      nO8U[        R                  :X  a$  [        R                  " U5      R                  5       nUR                  S:X  a  UR                  S5      nUc  [        U5      nU[        R                   :X  a!  UR#                  SSS5      R                  5       nUb  UR%                  U5      nU$ )NzUnsupported input image type    r   r   )r   r   PILTORCHNUMPYr>   r   r   pil_to_tensorr   
from_numpy
contiguousndim	unsqueezer   r   LASTpermuter   )r   rX   rk   rl   rn   
image_types         r@   _process_image%BaseImageProcessorFast._process_image  s    $E*
immY__iooNN<ZLIJJ''.E&OOE*E9??*$$U+668E ::?OOA&E $ >u E 0 5 55MM!Q*557E HHV$ErB   c           
      V   U R                  XS9n[        U R                  X#US9n[        U5      S:  =(       a    [	        US   [
        [        45      nU(       a)  U VV	s/ sH  o V	s/ sH
  o" U	5      PM     sn	PM     n
nn	U
$ U V	s/ sH
  o" U	5      PM     n
n	U
$ s  sn	f s  sn	nf s  sn	f )aZ  
Prepare image-like inputs for processing.

Args:
    images (`ImageInput`):
        The image-like inputs to process.
    do_convert_rgb (`bool`, *optional*):
        Whether to convert the images to RGB.
    input_data_format (`str` or `ChannelDimension`, *optional*):
        The input data format of the images.
    device (`torch.device`, *optional*):
        The device to put the processed images on.
    expected_ndims (`int`, *optional*):
        The expected number of dimensions for the images. (can be 2 for segmentation maps etc.)

Returns:
    List[`torch.Tensor`]: The processed images.
r   rk   rl   rn   r   )r   r   r   len
isinstancer{   tuple)r   rP   rk   rl   rn   r   process_image_partialhas_nested_structurenested_listrS   processed_imagess              r@   _prepare_image_like_inputs1BaseImageProcessorFast._prepare_image_like_inputs  s    8 ///V 'lr!

  #6{QW:fQi$PU3WgmngmXc{ S{!6s!;{ Sgmn   GMMfs 5c :fM	 !TnMs   B 'B7B B&B r6   rh   r;   c           	      <   Uc  0 nUb  [        S	0 [        XS9D6nUb  [        S	0 [        USS9D6n[        U[        5      (       a  [	        U5      n[        U[        5      (       a  [	        U5      nUc  [
        R                  nXS'   X'S'   X7S'   XGS'   XWS'   XgS'   U$ )
z
Update kwargs that need further processing before being validated
Can be overridden by subclasses to customize the processing of kwargs.
r   r6   r   r8   rh   r1   r2   r;   rp   )r   r   r   r{   r   r   r?   )r   r8   r6   rh   r1   r2   r;   r   s           r@   _further_process_kwargs.BaseImageProcessorFast._further_process_kwargs(  s     >F\m[\D  T={#STIj$''z*Ji&&i(I*00Kv'{&7"#)|'{ +}rB   r7   r5   r9   ri   r:   c                 ,    [        UUUUUUUUU	U
UUS9  g)z0
validate the kwargs for the preprocess method.
)r.   r/   r0   r1   r2   r7   r8   r5   r6   r9   r:   r;   N)rA   )r   r.   r/   r0   r1   r2   r7   r8   r5   r6   r9   r:   r;   r   s                 r@   _validate_preprocess_kwargs2BaseImageProcessorFast._validate_preprocess_kwargsL  s0    & 	+!)%!))#	
rB   c                 .    U R                   " U/UQ70 UD6$ N)
preprocess)r   rP   argsr   s       r@   __call__BaseImageProcessorFast.__call__n  s    v7777rB   c           	      <   [        UR                  5       U R                  S9  U R                   H  nUR                  U[	        XS 5      5        M!     UR                  S5      nUR                  S5      nUR                  S5      nU R                  " S
0 UD6nU R                  " S
0 UD6  UR                  S5      n[        U[        [        45      (       a	  [        U   OUUS'   UR                  S5        UR                  S5        U R                  " U/UQ7XVUS	.UD6$ )N)captured_kwargsvalid_processor_keysrk   rl   rn   r9   r   rh   r;   r   rp   )r   r   r   
setdefaultr   r   r   r   r   ry   r(   r)   _preprocess_image_like_inputs)	r   rP   r   r   r   rk   rl   rn   r9   s	            r@   r   !BaseImageProcessorFast.preprocessq  s!    	DLdLde 22Jj'$D*IJ 3  $45"JJ':;H% --77 	((262 ::j) :DHsTfNg9h9h+H5nv 	
 	

&'

=!11

*8fl
pv
 	
rB   r   c                N    U R                  XX4S9nU R                  " U/UQ70 UD6$ )z
Preprocess image-like inputs.
To be overriden by subclasses when image-like inputs other than images should be processed.
It can be used for segmentation maps, depth maps, etc.
)rP   rk   rl   rn   )r   _preprocess)r   rP   rk   rl   rn   r   r   s          r@   r   4BaseImageProcessorFast._preprocess_image_like_inputs  s<     00L] 1 
 8888rB   ro   c           	         [        XS9u  nn0 nUR                  5        H"  u  nnU(       a  U R                  UX4S9nUUU'   M$     [        UU5      n[        UUS9u  nn0 nUR                  5        H8  u  nnU(       a  U R	                  UU5      nU R                  UXxXU5      nUUU'   M:     [        UU5      nU(       a  [        R                  " USS9OUn[        SU0US9$ )N)ro   )rX   r8   r   r   )dimr   )datatensor_type)	r   itemsr   r   r   r   r   stackr   )r   rP   r7   r8   r   r5   r6   r.   r/   r0   r1   r2   ro   r:   r   grouped_imagesgrouped_images_indexresized_images_groupedrR   stacked_imagesresized_imagesprocessed_images_groupedr   s                          r@   r   "BaseImageProcessorFast._preprocess  s   $ 0EV/o,,!#%3%9%9%;!E>!%>!j,:"5) &< ((>@TU 0E^fv/w,,#% %3%9%9%;!E>!%!1!1.)!L!77
LV_N /=$U+ &< **BDXYCQ5;;'7Q?Wg.2B!CQ_``rB   c                 l   > [         TU ]  5       nUR                  SS 5        UR                  SS 5        U$ )N_valid_processor_keysr   )r   to_dictr   )r   encoder_dictr   s     r@   r  BaseImageProcessorFast.to_dict  s7    w(0$7.5rB   )r   r6   r8   )NT)NNNNNN)   )NNN)NNNr  )NNNNNNNNNNNN)Brq   rr   rs   rt   r9   r1   r2   r8   rh   r6   r7   r5   r.   r/   r0   rk   r:   r   r?   r;   rl   rn   model_input_namesrf   r   r   r   r   r   ru   r   staticmethodr   ry   r   r   rz   r   r
   r   r   r   r{   r   r   rw   rx   r   r   r   r   r   r   r   r   r    r   r   r   r!   r   r   r   r  r|   __classcell__)r   s   @r@   r   r      s   HJIDIINJNLNN"((KF'(2LMR89R 
R8 044[4[ 4[ -	4[
 4[ 
4[l  :>	S/   56 	
 
 ( 
 
(-- E8E?*+- 5(5/)*	- 
-. r (,:>9=%)*.+/1tn1 U5$u+#5671 E%e"456	1
 TN1 !1 (1 
1 1   	
  %e,- U+, 
8EE 38nE
 
E.%% 
% t    OO O 
	O( *.DH+/$$ !$ $E#/?*?$@A	$
 ($ 
$R *.DH+/* *  !*  $E#/?*?$@A	* 
 (*  *  
n	* \ $((,,0:>9=26"x " H%" $D>	"
 U5$u+#567" E%e"456" ./" 
"L &**.'+;?:>$(#')-(,QU;?26 
TN 
 ! 
 tn	 

 U5%,#678 
 E%u"567 
 D> 
 x  
 ! 
 H% 
 5!LMN 
 !sJ!78 
 ./ 
D8z 8FCb<c 8ht 8 #
 #
fEd>e #
jv #
 #
V 8<99 	9
 ,9 sN2349 899 
9(*a^$*a *a 	*a
   56*a *a *a *a *a *a U5$u+#567*a E%e"456*a #4.*a !sJ!78*a  
!*aX rB   r   r   )Icollections.abcr   copyr   	functoolsr   r   typingr   r   r	   r
   numpynpimage_processing_utilsr   r   r   image_transformsr   r   r   r   r   image_utilsr   r   r   r   r   r   r   r   r   r   r   processing_utilsr   utilsr    r!   r"   r#   r$   r%   r&   utils.import_utilsr'   r(   r   r)   torchvision.transforms.v2r*   r   torchvision.transforms
get_loggerrq   r   r?   ru   rz   r{   ry   rx   rA   rI   rO   r   rW   arrayrd   rf   r   rp   rB   r@   <module>r&     s   %  ( 2 2  S S     %   1 /<"$$=:&*#			H	% 2!%&*#'6:59!'+%)$( $#/37;.>.D.D'S'SUO'S 4.'S ud5k123	'S
 eT%[012'S TN'S  }'S TN'S !'S ~'S 8
'S +,'S U3
?34'S *+'S 'ST
 
x} 
 
8x} 8c 8#n!5 #%* #>)*8;	%.(
)*0%iu %( Z/ Z ZrB   