
    eThe                        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+  \*" 5       (       a  SSKJ,r,  \'" 5       (       a  S SK-r-\(" 5       (       a   SSKJ.r.  \)" 5       (       a  S SK/J0r1  O	S SK2J0r1  OSr.\+Rf                  " \45      r5\" SS9SSSSSSSSSSSSS\Rl                  4S\\7   S\\8   S\\7   S\\	\8\9\8   4      S\\	\8\9\8   4      S\\7   S\\:   S\\7   S\\   S\\7   S\\   S\S   S\\	\;\%4      S\\   4S jj5       r<S4S S!S"\\:   S#S!4S$ jjr=S%\\   S#\9\   4S& jr>S'\9S!   S#\?\:   4S( jr@S)\	\R                  S!4   S*\:S#\9\	\R                  S!4      4S+ jrB " S, S-\S.S/9rC\& " S0 S1\5      5       rD " S2 S35      rEg)5    )Iterable)	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)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-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   Nptz6Only returning PyTorch tensors is currently supported.z6Only channel first data format is currently supported.)r   
ValueErrorr   FIRST)r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   s                 `/var/www/auris/envauris/lib/python3.13/site-packages/transformers/image_processing_utils_fast.py"validate_fast_preprocess_argumentsr?   I   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.
)rC   )squeezer<   )rA   rC   s     r>   safe_squeezerG   t   s@     |~~~~4~(( s   $ 33valuesc                 P    [        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)rH   values_is     r>   max_across_indicesrM      s$     +.v,7,hCM,777s   #imagesc                 b    [        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.
)rM   shape)rN   img_
max_height	max_widths        r>   get_max_height_widthrU      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)rV   rW   patchesheightwidthijpatchs           r>   divide_to_patchesrb      sr     G"56F6L6LMMF1f)q%,A!QZ/^1CCDENN5! - *
 Nr@   c                   n   \ 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rg)DefaultFastImageProcessorKwargs   r5   r6   default_to_squarer&   F.InterpolationModer7   r3   r4   r,   r-   r.   r/   r0   do_convert_rgbr8   r9   input_data_formattorch.devicedevice N)__name__
__module____qualname____firstlineno__r   bool__annotations__dictstrintr	   floatlistr   r   __static_attributes__rm   r@   r>   rd   rd      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+;&; <==^$$r@   rd   F)totalc                     ^  \ 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  S8S	S
S\SSS\SS
4
S jj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      S9S\%\   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	\.S\.4S" jr/S\*4S# jr0S\.S\.4S$ jr1   S:S	\.S%\%\   S&\%\!\+\4      S\%S   SS
4
S' jjr2   S:S\.S%\%\   S&\%\!\+\4      S\%S   S\&S
   4
S( jjr3      S9S\%\   S)\%\   S*\%\   S\%\!\\&\   4      S\%\!\\&\   4      S+\%\   S\*4S, jjr4            S;S\%\   S\%\   S\%\   S\%\!\\'\   4      S\%\!\\'\   4      S-\%\   S\%\   S.\%\   S)\%\   S/\%\!S0      S1\%\!\+\54      S+\%\   4S2 jjr6S\.S\\   S\74S3 jr8\9S\.S\\   S\74S4 j5       r:S\&S
   S-\S\S\%S   S.\S)\S\S\S\S\%\!\\&\   4      S\%\!\\&\   4      S1\%\!\+\54      S\74S5 jr;U 4S6 jr<S7r=U =r>$ )<BaseImageProcessorFast   NTgp?pixel_valueskwargsrD   c           
      f  > [         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                  R                  5        H<  nUR                  US 5      nUb  [        XU5        M&  [        X[        XS 5      5        M>     [        U R                  R                  R                  5       5      U l        g )Nr6   rf   r6   rf   r4   
param_namerm   )super__init__filter_out_unused_kwargspopr6   r   rf   r4   valid_kwargsrs   keyssetattrgetattrrx   _valid_kwargs_names)selfr   r6   r4   keykwarg	__class__s         r>   r   BaseImageProcessorFast.__init__   s    	"6"..v6zz&$)),  tzzBUW[WmWm7no 		
 JJ{DNN;	MVMby[Ihl$$4499;CJJsD)E 5)74d#;< < $((9(9(I(I(N(N(P#Q r@   rV   rB   r6   interpolationrh   	antialiasc                    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                   " 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)r6   rf   rj   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   r6   r   r   r=   rS   rT   r   r]   r^   r<   resize)r   rV   r6   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  xx}ZZr@   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.
rm   )r   rV   r   r   s       r>   rescaleBaseImageProcessorFast.rescale  s    $ }r@   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   rV   r   r   r   s        r>   r    BaseImageProcessorFast.normalize0  s    * {{5,,r@   r)   r*   r.   r/   r0   r,   r-   rl   rk   c                     U(       a=  U(       a6  [         R                  " X&S9SU-  -  n[         R                  " X6S9SU-  -  nSnX#U4$ )N)rl   g      ?F)torchrA   )r   r.   r/   r0   r,   r-   rl   s          r>   !_fuse_mean_std_and_rescale_factor8BaseImageProcessorFast._fuse_mean_std_and_rescale_factorG  sI     ,j@C.DXYJY>#BVWIJj00r@   rN   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.
)r.   r/   r0   r,   r-   rl   )dtype)r   rl   r   tor   float32r   )r   rN   r,   r-   r.   r/   r0   s          r>   rescale_and_normalize,BaseImageProcessorFast.rescale_and_normalizeX  sx     -1,R,R%!!)== -S -
)
z ^^FIIEMMI$BJZF  \\&9Fr@   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   rV   r6   r   s       r>   r   "BaseImageProcessorFast.center_cropt  sQ    & ;;$**"4\]a]f]f]h\ijkk}}U(^T']$CDDr@   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   rV   s     r>   r   %BaseImageProcessorFast.convert_to_rgb  s     e$$r@   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

:& - r@   c                     [        U5      $ )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   rN   s     r>   _prepare_images_structure0BaseImageProcessorFast._prepare_images_structure  s     (//r@   ri   rj   c                 B   [        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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
contiguousr   r   LASTpermuter   )r   rV   ri   rj   rl   
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 $ >u E 0 5 55MM!Q*557E HHV$Er@   c                     U R                  U5      n[        U R                  UUUS9n/ nU H  nUR                  U" U5      5        M     U$ )z*
Prepare the input images for processing.
)ri   rj   rl   )r   r   r   r[   )r   rN   ri   rj   rl   process_image_fnprocessed_imagesrV   s           r>   _prepare_input_images,BaseImageProcessorFast._prepare_input_images  s_     //7")/	
 E##$4U$;<   r@   r4   rf   r9   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   r4   r   r6   rf   r/   r0   r9   rm   )r   r   
isinstancerx   tupler   r=   )r   r6   r4   rf   r/   r0   r9   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"#)|'{ +}r@   r5   r3   r7   rg   r8   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-   r.   r/   r0   r5   r6   r3   r4   r7   r8   r9   N)r?   )r   r,   r-   r.   r/   r0   r5   r6   r3   r4   r7   r8   r9   r   s                 r>   _validate_preprocess_kwargs2BaseImageProcessorFast._validate_preprocess_kwargs  s0    & 	+!)%!))#	
r@   c                 .    U R                   " U/UQ70 UD6$ N)
preprocess)r   rN   argsr   s       r>   __call__BaseImageProcessorFast.__call__8  s    v7777r@   c           	      V   [        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                  XXgS9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0 UD6$ )N)captured_kwargsvalid_processor_keysri   rj   rl   )rN   ri   rj   rl   r7   r   rf   r9   rm   )r   r   r   
setdefaultr   r   r   r   r   r   rv   r&   r'   _preprocess)	r   rN   r   r   r   ri   rj   rl   r7   s	            r>   r   !BaseImageProcessorFast.preprocess;  s&    	DLdLde 22Jj'$D*IJ 3  $45"JJ':;H%++L] , 

 --77 	((262 ::j) :DHsTfNg9h9h+H5nv 	
 	

&'

=!8888r@   c           	         [        U5      u  p0 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5      u  p0 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)rV   r6   r   r   dimr~   )datatensor_type)	r   itemsr   r   r   r   r   stackr   )r   rN   r5   r6   r   r3   r4   r,   r-   r.   r/   r0   r8   r   grouped_imagesgrouped_images_indexresized_images_groupedrP   stacked_imagesresized_imagesprocessed_images_groupedr   s                         r>   r   "BaseImageProcessorFast._preprocessc  s   " 0EV/L,!#%3%9%9%;!E>!%>!j,:"5) &< ((>@TU 0E^/T,#% %3%9%9%;!E>!%!1!1.)!L!77
LV_N /=$U+ &< **BDXYCQ5;;'7Q?Wg.2B!CQ_``r@   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r@   )r   r4   r6   )NT)NNNNNN)NNN)NNNNNNNNNNNN)?rn   ro   rp   rq   r7   r/   r0   r6   rf   r4   r5   r3   r,   r-   r.   ri   r8   r   r=   r9   rj   rl   model_input_namesrd   r   r   r   r   r   rr   r   rw   r   r	   r   r   r   r   rx   r   r   r   rt   ru   rv   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r   ry   __classcell__)r   s   @r>   r|   r|      s2   HJIDIINJNLNN"((KF'(2LMR89R 
R8 04/[/[ /[ -	/[
 /[ 
/[b 
 
(-- 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 00 
0& *.DH+/   !  $E#/?*?$@A	 
 (  
 J *.DH+/   !  $E#/?*?$@A	 
 (  
n	 4 $((,,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 %9 %9fEd>e %9jv %9 %9N)a^$)a )a 	)a
   56)a )a )a )a )a )a U5$u+#567)a E%e"456)a !sJ!78)a 
)aV r@   r|   c                   2    \ rS rSrSS\\\      4S jjrSrg)SemanticSegmentationMixini  Ntarget_sizesc                    UR                   nUb  [        U5      [        U5      :w  a  [        S5      e/ n[        [        U5      5       Ha  n[        R
                  R                  R                  X5   R                  SS9X%   SSS9nUS   R                  SS9nUR                  U5        Mc     U$ UR                  SS9n[        UR                  S   5       Vs/ s H  oU   PM	     nnU$ s  snf )a  
Converts the output of [`MobileNetV2ForSemanticSegmentation`] into semantic segmentation maps. Only supports PyTorch.

Args:
    outputs ([`MobileNetV2ForSemanticSegmentation`]):
        Raw outputs of the model.
    target_sizes (`List[Tuple]` of length `batch_size`, *optional*):
        List of tuples corresponding to the requested final size (height, width) of each prediction. If unset,
        predictions will not be resized.

Returns:
    semantic_segmentation: `List[torch.Tensor]` of length `batch_size`, where each item is a semantic
    segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is
    specified). Each entry of each `torch.Tensor` correspond to a semantic class id.
zTMake sure that you pass in as many target sizes as the batch dimension of the logitsr   r   bilinearF)r6   modealign_cornersr
   )logitslenr<   rZ   r   nnr(   interpolate	unsqueezeargmaxr[   rP   )	r   outputsr   r  semantic_segmentationidxresized_logitssemantic_mapr_   s	            r>   "post_process_semantic_segmentation<SemanticSegmentationMixin.post_process_semantic_segmentation  s	      #6{c,// j  %'!S[)!&!4!4!@!@K))a)0|7Hzin "A "  .a077A7>%,,\: * %$ %+MMaM$8!GLMbMhMhijMkGl$mGl!1%=Gl!$m$$ %ns   C+rm   r   )	rn   ro   rp   rq   r   rx   r   r  ry   rm   r@   r>   r   r     s    (%QUV[Q\H] (% (%r@   r   r   )Fcollections.abcr   	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%   r&   r   r'   torchvision.transforms.v2r(   r   torchvision.transforms
get_loggerrn   r   r=   rr   rw   rx   rv   ru   r?   rG   rM   r   rU   arrayrb   rd   r|   r   rm   r@   r>   <module>r     s   % ( 2 2  S S     %   /<"$$=:&*#			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 %& T/ T Tn)% )%r@   