
    3ji                        S SK Jr  S SKJr  S SKJrJrJr  S SKr	SSK
Jr  SSKJr  SSKJr  SS	KJrJrJrJrJrJr  SS
KJr  SSKJr  SSK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*J+r+  SSK,J-r-J.r.J/r/J0r0J1r1  SSK2J3r3J4r4J5r5J6r6  \0" 5       (       a  SSKJ7r7  \." 5       (       a  S SK8r8\/" 5       (       a  S SK9J:r;  SSKJ<r<J=r=  OSr<Sr=\1R|                  " \?5      r@\6" SS9 " S S\5      5       rA\6" SS9 " S S\5      5       rB\ArCg)    )Iterable)	lru_cache)AnyOptionalUnionN   )BatchFeature)BaseImageProcessor)center_crop)convert_to_rgbdivide_to_patchesget_resize_output_image_sizeget_size_with_aspect_ratiogroup_images_by_shapereorder_images)	normalize)rescale)resize)ChannelDimension
ImageInput	ImageTypeSizeDictget_image_size#get_image_size_for_max_height_widthget_image_typeget_max_height_widthinfer_channel_dimension_formatis_valid_imageload_image_as_tensor)ImagesKwargsUnpack)
TensorTypeis_torch_availableis_torchvision_availableis_vision_availablelogging)is_rocm_platformis_torchdynamo_compilingis_torchvision_greater_or_equalrequires)PILImageResampling)
functional)pil_torch_interpolation_mappingtorch_pil_interpolation_mapping)torchtorchvision)backendsc                    r  ^  \ rS rSrSrS\\   4U 4S jjr\S\	4S j5       r
\S\4S j5       rS\\\   -  \\\      -  4S	 jr   S;S\S\	S
-  S\\-  S
-  S\S   S\\   SS4S jjrS\S\4S jr      S<S\S   S\S\S
-  S\S
-  S\	S\	S
-  S\	S
-  S\\S   S4   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'\\\   -  S(\\\   -  SS4S) jr\ " S*S+9      S>S,\	S
-  S-\\\   -  S
-  S.\\\   -  S
-  S/\	S
-  S0\S
-  S\S   S\4S1 jj5       r!SSS/\	S0\S,\	S-\\\   -  S.\\\   -  SS4S2 jr"SSS\SS4S3 jr#S\S   S4\	S\SSS5\	S6\S/\	S0\S,\	S-\\\   -  S
-  S.\\\   -  S
-  S7\	S
-  S\S
-  S\	S
-  S8\\$-  S
-  S\%4 S9 jr&S:r'U =r($ )?TorchvisionBackendU   zATorchvision backend for GPU-accelerated batched image processing.kwargsc                 J   > [         TU ]  " S0 UD6  U R                  " S0 UD6  g N super__init___set_attributesselfr5   	__class__s     `/home/wildlama/miniconda3/lib/python3.13/site-packages/transformers/image_processing_backends.pyr;   TorchvisionBackend.__init__Y   $    "6"&v&    returnc                 .    [         R                  S5        g)
`bool`: Whether or not this image processor is using the fast (Torchvision) backend.
The `is_fast` property is deprecated and will be removed in v5.3 of Transformers.
Use the `backend` attribute instead (e.g., `processor.backend == "torchvision"`).
The `is_fast` property is deprecated and will be removed in v5.3 of Transformers. Use the `backend` attribute instead (e.g., `processor.backend == 'torchvision'`).Tloggerwarning_oncer>   s    r@   is_fastTorchvisionBackend.is_fast]   s     	`	
 rC   c                     g)2
`str`: The backend used by this image processor.
r0   r8   rK   s    r@   backendTorchvisionBackend.backendj   s    
 rC   image_url_or_urlsc                    [        U[        [        45      (       a!  U Vs/ s H  o R                  U5      PM     sn$ [        U[        5      (       a  [        U5      $ [        U5      (       a  U$ [        S[        U5       35      es  snf )z
Convert a single or a list of URLs / paths into `torch.Tensor` objects.

Already-valid image objects (tensors, numpy arrays, PIL Images) are passed through
unchanged so that callers who pre-load images are unaffected.
z=only a single or a list of entries is supported but got type=)	
isinstancelisttuplefetch_imagesstrr   r   	TypeErrortype)r>   rR   xs      r@   rW   TorchvisionBackend.fetch_imagesq   s     '$772CD2CQ%%a(2CDD)3//'(9::-..$$[\`ar\s[tuvv Es   BNimagedo_convert_rgbinput_data_formatdeviceztorch.devicetorch.Tensorc                    [        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$ )z/Process a single image for torchvision backend.Unsupported input image type    r   r   )r   r   PILTORCHNUMPY
ValueErrorr   tvFpil_to_tensorr/   
from_numpy
contiguousndim	unsqueezer   r   LASTpermuteto)r>   r]   r^   r_   r`   r5   
image_types          r@   process_image TorchvisionBackend.process_image   s     $E*
immY__iooNN<ZLIJJ''.E&%%e,E9??*$$U+668E::?OOA&E$ >u E 0 5 55MM!Q*557EHHV$ErC   c                     [        U5      $ zConvert an image to RGB format.r   r>   r]   s     r@   r   !TorchvisionBackend.convert_to_rgb       e$$rC   imagespad_size
fill_valuepadding_modereturn_maskdisable_grouping	is_nested)ra   ra   c                    UbJ  UR                   (       a  UR                  (       d  [        SU S35      eUR                   UR                  4nO[        U5      n[	        XUS9u  p0 n0 nU	R                  5        H  u  pUR                  SS nUS   US   -
  nUS   US   -
  nUS:  d  US:  a  [        SU S	U S35      eX:w  a  SSUU4n[        R                  " UUX4S
9nXU'   U(       d  Mv  [        R                  " U[        R                  S9SSSS2SS24   nSUSSUS   2SUS   24'   UX'   M     [        XUS9nU(       a  [        XUS9nUU4$ U$ )z5Pad images using Torchvision with batched operations.NCPad size must contain 'height' and 'width' keys only. Got pad_size=.)r   r   r   r   zrPadding dimensions are negative. Please make sure that the `pad_size` is larger than the image size. Got pad_size=z, image_size=)fillr~   dtype.)r   )heightwidthrh   r   r   itemsshaperi   padr/   
zeros_likeint64r   )r>   r{   r|   r}   r~   r   r   r   r5   grouped_imagesgrouped_images_indexprocessed_images_groupedprocessed_masks_groupedr   stacked_images
image_sizepadding_heightpadding_widthpaddingstacked_masksprocessed_imagesprocessed_maskss                         r@   r   TorchvisionBackend.pad   s    OO #fgofppq!rss 8H+F3H/D0
, $& "$%3%9%9%;!E'--bc2J%a[:a=8N$QK*Q-7M!]Q%6 008zzlRSU  %a?!$z!m.<U+{ % 0 0u{{ STWYZ\]_`T` aGHc?Z]?OjmOCD1>'.# &<& **Bdmn,-DfopO#_44rC   sizeresamplez7PILImageResampling | tvF.InterpolationMode | int | None	antialiasc                 $   Ub(  [        U[        [        45      (       a
  [        U   nOUnO[        R
                  R                  nU[        R
                  R                  :X  a?  [        S5      (       d/  [        R                  S5        [        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[1        SU S35      e[3        5       (       a!  [5        5       (       a  U R7                  XXd5      $ [        R8                  " XXdS	9$ )
z"Resize an image using Torchvision.Nz0.27aC  You have used a torchvision backend image processor with LANCZOS resample which is not supported for torch.Tensor with torchvision < 0.27. BICUBIC resample will be used as an alternative. Please upgrade torchvision to 0.27+ or fall back to a pil backend image processor if you want full consistency with the original model.r   Fr   default_to_squarer_   jSize must contain 'height' and 'width' keys, or 'max_height' and 'max_width', or 'shortest_edge' key. Got r   interpolationr   )rT   r+   intr-   ri   InterpolationModeBILINEARLANCZOSr)   rI   rJ   BICUBICshortest_edgelongest_edger   r   r   r   FIRST
max_height	max_widthr   r   r   rh   r(   r'   _compile_friendly_resizer   )r>   r]   r   r   r   r5   r   new_sizes           r@   r   TorchvisionBackend.resize   s    (%7$=>> ? I (11::MC11999BabhBiBiA  1199M$"3"31

RS!""!!H
 3''"'"2"8"8	H __:5::<;Ldoo_c_m_mnH[[TZZTZZ0H6  $%%*:*<*<00-[[zz%\\rC   r   r   ztvF.InterpolationModec                    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 $ )zOA wrapper around tvF.resize for torch.compile compatibility with uint8 tensors.   r      r   )	r   r/   uint8floatri   r   whereroundrq   )r]   r   r   r   s       r@   r   +TorchvisionBackend._compile_friendly_resize  s     ;;%++%KKMC'EJJumaECKEKKS%8EKK	1e4EKKM$$U[[1E  JJumaErC   scalec                 
    X-  $ )z5Rescale an image by a scale factor using Torchvision.r8   r>   r]   r   r5   s       r@   r   TorchvisionBackend.rescale#  s     }rC   meanstdc                 0    [         R                  " XU5      $ )z%Normalize an image using Torchvision.)ri   r   r>   r]   r   r   r5   s        r@   r   TorchvisionBackend.normalize,  s     }}U#..rC   
   )maxsizedo_normalize
image_mean	image_std
do_rescalerescale_factorc                     U(       a=  U(       a6  [         R                  " X&S9SU-  -  n[         R                  " X6S9SU-  -  nSnX#U4$ )N)r`   g      ?F)r/   tensor)r>   r   r   r   r   r   r`   s          r@   !_fuse_mean_std_and_rescale_factor4TorchvisionBackend._fuse_mean_std_and_rescale_factor6  sI     ,j@C.DXYJY>#BVWIJj00rC   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$ )zFRescale and normalize images using Torchvision (fused for efficiency).)r   r   r   r   r   r`   r   )r   r`   r   rq   r/   float32r   )r>   r{   r   r   r   r   r   s          r@   rescale_and_normalize(TorchvisionBackend.rescale_and_normalizeG  sx     -1,R,R%!!)== -S -
)
z ^^FIIEMMI$BJZF  \\&9FrC   c                    UR                   b  UR                  c  [        SUR                  5        35      eUR                  SS u  pEUR                   UR                  pvXu:  d  Xd:  an  Xu:  a  Xu-
  S-  OSXd:  a  Xd-
  S-  OSXu:  a
  Xu-
  S-   S-  OSXd:  a
  Xd-
  S-   S-  OS/n[
        R                  " XSS9nUR                  SS u  pEXu:X  a  Xd:X  a  U$ [        XF-
  S-  5      n	[        XW-
  S-  5      n
[
        R                  " XXU5      $ )	z'Center crop an image using Torchvision.N=The size dictionary must have keys 'height' and 'width'. Got r   rd   r   r   )r   g       @)	r   r   rh   keysr   ri   r   r   crop)r>   r]   r   r5   image_heightimage_widthcrop_height
crop_widthpadding_ltrbcrop_top	crop_lefts              r@   r   TorchvisionBackend.center_crop`  s+    ;;$**"4\]a]f]f]h\ijkk$)KK$4!"&++tzzZ#{'A3=3K)a/QR5@5O+1UV7A7O)A-!3UV9D9S+a/A5YZ	L GGEa8E(-BC(8%L([-H2c9:1S89	xxLLrC   	do_resizedo_center_crop	crop_sizedo_padreturn_tensorsc           	         [        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  U R                  UXS9n[        SU0US9$ )z=Preprocess using Torchvision backend (fast, GPU-accelerated).)r   r]   r   r   )r|   r   pixel_valuesdatatensor_type)r   r   r   r   r   r   r   r	   )r>   r{   r   r   r   r   r   r   r   r   r   r   r   r|   r   r   r5   r   r   resized_images_groupedr   r   resized_imagesr   r   s                            r@   _preprocessTorchvisionBackend._preprocess|  s   * 0EV/o,,!#%3%9%9%;!E>!%>!`,:"5) &< ((>@TU 0E^fv/w,,#% %3%9%9%;!E>!%!1!1.)!L!77
LV_N /=$U+ &< **BDXY#xx(88xo.2B!CQ_``rC   r8   )NNN)Nr   constantFFF)NT)NNNNNN))__name__
__module____qualname____firstlineno____doc__r!   r    r;   propertyboolrL   rX   rP   rU   rW   r   r   r   rs   r   r   r   r   rV   r   r   staticmethodr   r   r   r   r   r   r   r   r   r"   r	   r   __static_attributes____classcell__r?   s   @r@   r3   r3   U   s   K'!5 ' 
 
 
   wcDIoT#Y.O w& '+;?+/!! t! !11D8	!
 (! &! 
!F%J %: % "!"#-!(-!&0 ^$0  0  $J	0 
 Dj0  0  +0  $;0  
u34nD	E0 l OS4]4] 4] L	4]
 4] 
4]l  <@	S/   78 	
 
 $ 
 
// huo%/ Xe_$	/ 
/ r %)1504"&'++/1Tk1 DK'$.1 4;&-	1
 4K1 1 (1 
1 1   	
  DK' 4;& 
2MM M
 
M8-a^$-a -a 	-a
 L-a -a -a -a -a -a DK'$.-a 4;&--a t-a T/-a +-a  j(4/!-a$ 
%-a -arC   r3   )visionc                   d  ^  \ rS rSrSrS\\   4U 4S jjr\S\	4S j5       r
\S\4S j5       r  S,S	\S
\	S-  S\\-  S-  S\\   S\R                   4
S jjrS	\S\4S jr    S-S\\R                      S\S\S-  S\S-  S\	S\\\R                      \\R                      4   \\R                      -  4S jjr  S,S	\R                   S\SSS\S-  S\R                   4
S jjrS	\R                   S\S\R                   4S jrS	\R                   S\\\   -  S\\\   -  S\R                   4S jrS	\R                   S\S\R                   4S jrS\\R                      S\	S\SSS \	S!\S"\	S#\S$\	S%\\\   -  S-  S&\\\   -  S-  S'\	S-  S\S-  S(\\-  S-  S\4S) jr S\!\\"4   4U 4S* jjr#S+r$U =r%$ ).
PilBackendi  z9PIL/NumPy backend for portable CPU-only image processing.r5   c                 J   > [         TU ]  " S0 UD6  U R                  " S0 UD6  g r7   r9   r=   s     r@   r;   PilBackend.__init__  rB   rC   rD   c                 .    [         R                  S5        g)rF   rG   FrH   rK   s    r@   rL   PilBackend.is_fast  s     	`	
 rC   c                     g)rO   pilr8   rK   s    r@   rP   PilBackend.backend  s    
 rC   Nr]   r^   r_   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UR                  S:  a  Uc  [        R                  OUnO$U[        R                  :X  a  UR                  5       nUR                  S:X  a  [        R                  " USS9nUc  [        U5      nU[        R                  :X  a6  [        U[        R                   5      (       a  [        R"                  " US5      nU$ )z'Process a single image for PIL backend.rc      rd   r   )axis)rd   r   r   )r   r   re   rf   rg   rh   r   nparrayrm   r   ro   numpyexpand_dimsr   rT   ndarray	transpose)r>   r]   r^   r_   r5   rr   s         r@   rs   PilBackend.process_image  s    $E*
immY__iooNN<ZLIJJ''.E&HHUOEzzQ=N=V$4$9$9\m!9??*KKME::?NN5q1E$ >u E 0 5 55%,,UI6rC   c                     [        U5      $ rv   rw   rx   s     r@   r   PilBackend.convert_to_rgb  rz   rC   r{   r|   r}   r~   r   c                    UbI  UR                   (       a  UR                  (       d  [        SU S35      eUR                   UR                  pO[        U5      u  px/ n	/ n
U H  n[	        U[
        R                  S9u  pX|-
  nX-
  nUS:  d  US:  a  [        SU SU SU SU S	3	5      eX:w  d  X:w  a=  S
SU4SU44nUS:X  a  [        R                  " UUSUS9nO[        R                  " UUUS9nU	R                  U5        U(       d  M  [        R                  " Xx4[        R                  S9nSUSU2SU24'   U
R                  U5        M     U(       a  X4$ U	$ )z)Pad images to specified size using NumPy.Nr   r   channel_dimr   zsPadding dimensions are negative. Please make sure that the `pad_size` is larger than the image size. Got pad_size=(z, z), image_size=(z).)r   r   r   )modeconstant_values)r  r   r   )r   r   rh   r   r   r   r   r  r   appendzerosr   )r>   r{   r|   r}   r~   r   r5   target_heighttarget_widthr   r   r]   r   r   r   r   	pad_widthmasks                     r@   r   PilBackend.pad  s    OO #fgofppq!rss*2//8>><*>v*F'ME*5>N>T>TUMF*3N(0M!]Q%6 11>r,_e^ffhinhooqs 
 &%*? $a%81m:LM	:-FF5)*V`aEFF5),GE##E*{xx =RXXN()WfWfuf_%&&t,3 6 #44rC   r   r   zPILImageResampling | Nonereducing_gapc           	         UbF  [        U[        [        45      (       d+  [        b  U[        ;   a
  [        U   nO[        R                  nUb  UO[        R                  nUR
                  (       aN  UR                  (       a=  [        U[        R                  S9u  pg[        Xg4UR
                  UR                  5      nOUR
                  (       a%  [        UUR
                  S[        R                  S9nOUR                  (       aN  UR                  (       a=  [        U[        R                  S9u  pg[        Xg4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[%        UUUU[        R                  [        R                  S9$ )z Resize an image using PIL/NumPy.r  Fr   r   r   )r   r   r  data_formatr_   )rT   r+   r   r.   r   r   r   r   r   r   r   r   r   r   r   r   r   rh   	np_resize)	r>   r]   r   r   r  r5   r   r   r   s	            r@   r   PilBackend.resize#  sp    
8>PRU=V(W(W.:xKj?j:8D-66'389K9T9T$"3"3*5>N>T>TUMF1""!!H
 3''"'"2"8"8	H __*5>N>T>TUMF:F?DOO]a]k]klH[[TZZTZZ0H6 
 %(...44
 	
rC   r   c                 R    [        UU[        R                  [        R                  S9$ )z/Rescale an image by a scale factor using NumPy.)r   r  r_   )
np_rescaler   r   r   s       r@   r   PilBackend.rescaleV  s)     (...44	
 	
rC   r   r   c                 T    [        UUU[        R                  [        R                  S9$ )zNormalize an image using NumPy.)r   r   r  r_   )np_normalizer   r   r   s        r@   r   PilBackend.normalized  s,     (...44
 	
rC   c                     UR                   b  UR                  c  [        SUR                  5        35      e[	        UUR                   UR                  4[
        R                  [
        R                  S9$ )z!Center crop an image using NumPy.r   )r   r  r_   )r   r   rh   r   np_center_cropr   r   )r>   r]   r   r5   s       r@   r   PilBackend.center_cropt  sg     ;;$**"4\]a]f]f]h\ijkk++tzz*(...44	
 	
rC   r   r   r   r   r   r   r   r   r   r   c                 D   / nU Hv  nU(       a  U R                  UX4S9nU(       a  U R                  UU5      nU(       a  U R                  UU5      nU	(       a  U R                  UX5      nUR	                  U5        Mx     U(       a  U R                  UUS9n[        SU0US9$ )z2Preprocess using PIL backend (portable, CPU-only).r   )r|   r   r   )r   r   r   r   r  r   r	   )r>   r{   r   r   r   r   r   r   r   r   r   r   r   r|   r   r5   r   r]   s                     r@   r   PilBackend._preprocess  s    & E%dN((	:UN;ujD##E*  #xx(88xL.2B!CQ_``rC   c                    > [         TU ]  5       nUR                  SS5      R                  S5      (       a  US   S S US'   U$ )Nimage_processor_type Pil)r:   to_dictgetendswith)r>   processor_dictr?   s     r@   r1  PilBackend.to_dict  sN    *4b9BB5II5CDZ5[\_]_5`N12rC   r8   )NN)Nr   r   F)&r   r   r   r   r   r!   r    r;   r   r   rL   rX   rP   r   r   r  r  rs   r   rU   r   r   rV   r   r   r   r   r   r   r   r"   r	   r   dictr   r1  r   r   r   s   @r@   r   r     s   C'!5 ' 
 
 
    '+;?	"" t" !11D8	"
 &" 
"H%J %: % "!"#-!1 RZZ 1  1  $J	1 
 Dj1  1  
tBJJbjj!11	2T"**5E	E1 n 15#'1
zz1
 1
 .	1

 Dj1
 
1
f
zz
 

 


zz
 huo%
 Xe_$	
 

 
zz
 

 

""aRZZ "a "a 	"a
 ."a "a "a "a "a "a DK'$."a 4;&-"a t"a T/"a j(4/"a" 
#"aHc3h  rC   r   )Dcollections.abcr   	functoolsr   typingr   r   r   r	  r  image_processing_baser	   image_processing_utilsr
   image_transformsr   r(  r   r   r   r   r   r   r   r%  r   r"  r   r  image_utilsr   r   r   r   r   r   r   r   r   r   r   processing_utilsr    r!   utilsr"   r#   r$   r%   r&   utils.import_utilsr'   r(   r)   r*   r+   r/   torchvision.transforms.v2r,   ri   r-   r.   
get_loggerr   rI   r3   r   BaseImageProcessorFastr8   rC   r@   <module>rD     s   %  ' '  / 6     3  v u /;]]&*#&*# 
		H	% 
+,Sa+ Sa -Sal
 
;A# A  AJ , rC   