ó
    qyüi˜C  ã            
       ó<  • S SK r S SKJr  S SKJrJr  S SKrS SKJs  J	r
  S SKJ	r  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JrJr  SS
KJrJr  SSKJrJ r J!r!J"r"  \(       a  SSK#J$r$   " S S\SS9r%SSS\&\\&   -  S\'S\&S\4
S jr(\  " S S\5      5       r)S/r*g)é    N)ÚIterable)ÚTYPE_CHECKINGÚUnion)Ú
functionalé   )ÚTorchvisionBackend)ÚBatchFeature)Úgroup_images_by_shapeÚreorder_images)ÚIMAGENET_STANDARD_MEANÚIMAGENET_STANDARD_STDÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚSizeDict)ÚImagesKwargsÚUnpack)Ú
TensorTypeÚauto_docstringÚis_torch_availableÚrequires_backends)ÚDepthEstimatorOutputc                   óB   • \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   Srg)	ÚDPTImageProcessorKwargsé1   a  
ensure_multiple_of (`int`, *optional*, defaults to 1):
    If `do_resize` is `True`, the image is resized to a size that is a multiple of this value. Can be overridden
    by `ensure_multiple_of` in `preprocess`.
keep_aspect_ratio (`bool`, *optional*, defaults to `False`):
    If `True`, the image is resized to the largest possible size such that the aspect ratio is preserved. Can
    be overridden by `keep_aspect_ratio` in `preprocess`.
do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`):
    Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0
    is used for background, and background itself is not included in all classes of a dataset (e.g.
    ADE20k). The background label will be replaced by 255.
Úensure_multiple_ofÚsize_divisorÚkeep_aspect_ratioÚdo_reduce_labels© N)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚintÚ__annotations__ÚboolÚ__static_attributes__r    ó    Úi/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/models/dpt/image_processing_dpt.pyr   r   1   s!   ‡ ñð ÓØÓØÓØÖr*   r   F)ÚtotalÚinput_imageútorch.TensorÚoutput_sizer   ÚmultipleÚreturnc                 óÐ   • SS jnU R                   SS  u  pVUu  pxXu-  n	X†-  n
U(       a#  [        SU
-
  5      [        SU	-
  5      :  a  U
n	OU	n
U" X•-  US9nU" X¦-  US9n[        X¼S9$ )Nc                 ó¬   • [        X-  5      U-  nUb   XC:”  a  [        R                  " X-  5      U-  nXB:  a  [        R                  " X-  5      U-  nU$ ©N)ÚroundÚmathÚfloorÚceil)Úvalr0   Úmin_valÚmax_valÚxs        r+   Úconstrain_to_multiple_ofÚ>get_resize_output_image_size.<locals>.constrain_to_multiple_ofK   sQ   € Ü�#‘.Ó! HÑ,ˆàÑ 1£;Ü—
’
˜3™>Ó*¨XÑ5ˆAà‹;Ü—	’	˜#™.Ó)¨HÑ4ˆAàˆr*   éþÿÿÿé   )r0   ©ÚheightÚwidth)r   N)ÚshapeÚabsr   )r-   r/   r   r0   r=   Úinput_heightÚinput_widthÚoutput_heightÚoutput_widthÚscale_heightÚscale_widthÚ
new_heightÚ	new_widths                r+   Úget_resize_output_image_sizerN   E   sŒ   € ô	ð !,× 1Ñ 1°"°#Ð 6Ñ€LØ"-Ñ€Mð !Ñ/€LØÑ,€Kæäˆq�;‰Ó¤# a¨,Ñ&6Ó"7Ó7à&‰Lð 'ˆKá)¨,Ñ*EÐPXÑY€JÙ(¨Ñ)BÈXÑV€Iä˜:Ñ7Ð7r*   c            $       óŽ  ^ • \ rS rSrSr\r\R                  r	\
r\rSS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
\\   4U 4S jjr\ S5S\S\S-  S
\\   S\4U 4S jjj5       r S5S\S\S-  S\ S\!S\"\#-  S-  S\$\"S4   S-  S\4S jjr%S\&S   S\&S   4S jr'S\&S   S\ S\ S\(SSS\ S\(S \ S!\)S"\ S#\)\&\)   -  S-  S$\)\&\)   -  S-  S%\ S&\*S-  S'\ S(\*S-  S)\ S-  S\4$S* jr+S5S+\&\,   S-  4S, jjr-   S6S-SS\(SSS.\ S&\*S-  S%\ SS4U 4S/ jjjr. S7S-SS(\*SS4S0 jjr/ S5S1S2S+\#\&\,\*\*4      -  S-  S-  S\&\0\"\#4      4S3 jjr1S4r2U =r3$ )8ÚDPTImageProcessorél   z.PIL backend for DPT with reduce_label support.i€  rA   TNFgp?r@   Úkwargsc                 ó&   >• [         TU ]  " S0 UD6  g )Nr    )ÚsuperÚ__init__)ÚselfrR   Ú	__class__s     €r+   rU   ÚDPTImageProcessor.__init__ƒ   s   ø€ Ü‰ÒÑ"˜6Ó"r*   ÚimagesÚsegmentation_mapsr1   c                 ó&   >• [         TU ]  " X40 UD6$ )zX
segmentation_maps (`ImageInput`, *optional*):
    The segmentation maps to preprocess.
)rT   Ú
preprocess)rV   rY   rZ   rR   rW   s       €r+   r\   ÚDPTImageProcessor.preprocess†   s   ø€ ô ‰wÒ! &ÑF¸vÑFÐFr*   Údo_convert_rgbÚinput_data_formatÚreturn_tensorsÚdeviceztorch.devicec                 óÒ  • U R                  XXFS9nUR                  5       nSUS'   0 n	U R                  " U40 UD6U	S'   Ubš  U R                  USS[        R                  S9n
UR                  5       nUR                  SSS.5        U R                  " SSU
0UD6n
U
 Vs/ s H1  nUR                  S	5      R                  [        R                  5      PM3     n
nX©S
'   [        X•S9$ s  snf )z"Handle extra inputs beyond images.)rY   r^   r_   ra   Fr   Úpixel_valuesé   )rY   Úexpected_ndimsr^   r_   )Údo_normalizeÚ
do_rescalerY   r   Úlabels)ÚdataÚtensor_typer    )Ú_prepare_image_like_inputsÚcopyÚ_preprocessr   ÚFIRSTÚupdateÚsqueezeÚtoÚtorchÚint64r	   )rV   rY   rZ   r^   r_   r`   ra   rR   Úimages_kwargsri   Úprocessed_segmentation_mapsÚsegmentation_maps_kwargsÚprocessed_segmentation_maps                r+   Ú_preprocess_image_like_inputsÚ/DPTImageProcessor._preprocess_image_like_inputs“   s!  € ð ×0Ñ0ØÐL]ð 1ð 
ˆð Ÿ™›ˆØ,1ˆÐ(Ñ)ØˆØ#×/Ò/°ÑH¸-ÑHˆˆ^Ñð Ñ(Ø*.×*IÑ*IØ(Ø Ø$Ü"2×"8Ñ"8ð	 +Jð +Ð'ð (.§{¡{£}Ð$Ø$×+Ñ+¸UÐRWÑ,XÔYØ*.×*:Ò*:ñ +Ø2ð+Ø6Nñ+Ð'ñ 3Nó+â2MÐ.ð +×2Ñ2°1Ó5×8Ñ8¼¿¹ÖEÙ2Mð (ð +ð 9�‰Nä ÑBÐBùò+s   Â8C$rh   r.   c           
      ób  • [        [        U5      5       H–  nX   n[        R                  " US:H  [        R                  " SUR
                  UR                  S9U5      nUS-
  n[        R                  " US:H  [        R                  " SUR
                  UR                  S9U5      nX1U'   M˜     U$ )z/Reduce label values by 1, replacing 0 with 255.r   éÿ   )Údtypera   r@   éþ   )ÚrangeÚlenrr   ÚwhereÚtensorr|   ra   )rV   rh   ÚidxÚlabels       r+   Úreduce_labelÚDPTImageProcessor.reduce_label¿   s‘   € äœ˜V›Ö%ˆCØ‘KˆEÜ—K’K ¨¡
¬E¯LªL¸ÀEÇKÁKÐX]×XdÑXdÑ,eÐglÓmˆEØ˜A‘IˆEÜ—K’K ¨¡¬e¯lªl¸3ÀeÇkÁkÐZ_×ZfÑZfÑ.gÐinÓoˆEØ�3‹Kñ &ð ˆr*   r   Ú	do_resizeÚsizeÚresamplez7PILImageResampling | tvF.InterpolationMode | int | NoneÚdo_center_cropÚ	crop_sizerg   Úrescale_factorrf   Ú
image_meanÚ	image_stdr   r   Údo_padr   Údisable_groupingc           	      óÞ  • U(       a  U R                  U5      n[        UUS9u  nn0 nUR                  5        H%  u  nnU(       a  U R                  UUUUUS9nUUU'   M'     [	        UU5      n[        UUS9u  nn0 nUR                  5        HQ  u  nnU(       a  U R                  UU5      nU R                  UX‰X«U5      nU(       a  U R                  UU5      nUUU'   MS     [	        UU5      nU$ )zCustom preprocessing for DPT.)r�   )Úimager‡   rˆ   r   r   )r„   r
   ÚitemsÚresizer   Úcenter_cropÚrescale_and_normalizeÚ	pad_image)rV   rY   r   r†   r‡   rˆ   r‰   rŠ   rg   r‹   rf   rŒ   r�   r   r   rŽ   r   r�   rR   Úgrouped_imagesÚgrouped_images_indexÚresized_images_groupedrD   Ústacked_imagesÚresized_imagesÚprocessed_images_groupedÚprocessed_imagess                              r+   rm   ÚDPTImageProcessor._preprocessÉ   s'  € ö, Ø×&Ñ& vÓ.ˆFô 0EÀVÐ^nÑ/oÑ,ˆÐ,Ø!#ÐØ%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%§¡Ø(ØØ%Ø'9Ø&7ð "-ð "�ð -;Ð" 5Ó)ñ &<ô (Ð(>Ð@TÓUˆô 0EÀ^ÐfvÑ/wÑ,ˆÐ,Ø#%Ð Ø%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%×!1Ñ!1°.À)Ó!L�à!×7Ñ7Ø 
¸LÐV_óˆNö Ø!%§¡°ÀÓ!M�Ø.<Ð$ UÓ+ñ &<ô *Ð*BÐDXÓYÐàÐr*   Útarget_sizesc                 óL  • [        5       (       d  [        S5      eUR                  nUb¼  [        U5      [        U5      :w  a  [	        S5      e[        U[        R                  5      (       a  UR                  5       n/ n[        [        U5      5       HN  n[        R                  " X5   R                  SS9X%   SSS9nUS   R                  SS9nUR                  U5        MP     U$ UR                  SS9n[        UR                  S   5       Vs/ s H  o„U   PM	     nnU$ s  snf )	a½  
Converts the output of [`DPTForSemanticSegmentation`] into semantic segmentation maps.

Args:
    outputs ([`DPTForSemanticSegmentation`]):
        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.
z:PyTorch is required for post_process_semantic_segmentationzTMake sure that you pass in as many target sizes as the batch dimension of the logitsr   )ÚdimÚbilinearF©r‡   ÚmodeÚalign_cornersr@   )r   ÚImportErrorÚlogitsr   Ú
ValueErrorÚ
isinstancerr   ÚTensorÚnumpyr~   ÚFÚinterpolateÚ	unsqueezeÚargmaxÚappendrD   )	rV   ÚoutputsrŸ   r§   Úsemantic_segmentationr‚   Úresized_logitsÚsemantic_mapÚis	            r+   Ú"post_process_semantic_segmentationÚ4DPTImageProcessor.post_process_semantic_segmentation  s+  € ô  "×#Ñ#ÜÐZÓ[Ð[à—‘ˆð Ñ#Ü�6‹{œc ,Ó/Ó/Ü Øjóð ô ˜,¬¯©×5Ñ5Ø+×1Ñ1Ó3�à$&Ð!äœS ›[Ö)�Ü!"§¢Ø‘K×)Ñ)¨aÐ)Ð0°|Ñ7HÈzÐinñ"�ð  .¨aÑ0×7Ñ7¸AÐ7Ð>�Ø%×,Ñ,¨\Ö:ñ *ð %Ð$ð %+§M¡M°a MÐ$8Ð!ÜGLÐMb×MhÑMhÐijÑMkÔGlÓ$mÒGlÀ!¸1Ô%=ÑGlÐ!Ð$mà$Ð$ùò %ns   ÄD!r‘   Ú	antialiasc                 óà   >• UR                   (       a  UR                  (       d  [        SUR                  5        35      e[	        UUR                   UR                  4UUS9n[
        TU ]  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`.
    antialias (`bool`, *optional*, defaults to `True`):
        Whether to use antialiasing when resizing the image
    ensure_multiple_of (`int`, *optional*):
        If `do_resize` is `True`, the image is resized to a size that is a multiple of this value
    keep_aspect_ratio (`bool`, *optional*, defaults to `False`):
        If `True`, and `do_resize` is `True`, the image is resized to the largest possible size such that the aspect ratio is preserved.

Returns:
    `torch.Tensor`: The resized image.
zDThe size dictionary must contain the keys 'height' and 'width'. Got )r/   r   r0   )rˆ   r¸   )rB   rC   r¨   ÚkeysrN   rT   r“   )	rV   r‘   r‡   rˆ   r¸   r   r   r/   rW   s	           €r+   r“   ÚDPTImageProcessor.resize1  sh   ø€ ð: �{�{ $§*§*ÜÐcÐdh×dmÑdmÓdoÐcpÐqÓrÐrä2ØØŸ™ d§j¡jÐ1Ø/Ø'ñ	
ˆô ‰w‰~˜e¸8ˆ~ÐYÐYr*   c                 ó†   • UR                   SS u  p4S nU" X25      u  pgU" XB5      u  p‰X†X—4n
[        R                  " X5      $ )aL  
Center pad a batch of images to be a multiple of `size_divisor`.

Args:
    image (`torch.Tensor`):
        Image to pad.  Can be a batch of images of dimensions (N, C, H, W) or a single image of dimensions (C, H, W).
    size_divisor (`int`):
        The width and height of the image will be padded to a multiple of this number.
r?   Nc                 óX   • [         R                  " X-  5      U-  nX -
  nUS-  nX4-
  nXE4$ )Nrd   )r6   r8   )r‡   r   Únew_sizeÚpad_sizeÚpad_size_leftÚpad_size_rights         r+   Ú_get_padÚ-DPTImageProcessor.pad_image.<locals>._get_padi  s9   € Ü—y’y Ñ!4Ó5¸ÑDˆHØ‘ˆHØ$¨™MˆMØ%Ñ5ˆNØ Ð0Ð0r*   )rD   ÚtvFÚpad)rV   r‘   r   rB   rC   rÂ   Úpad_topÚ
pad_bottomÚpad_leftÚ	pad_rightÚpaddings              r+   r–   ÚDPTImageProcessor.pad_imageY  sP   € ð Ÿ™ B CÐ(‰ˆò	1ñ ' vÓ<ÑˆÙ& uÓ;ÑˆØ iÐ<ˆÜ�wŠw�uÓ&Ð&r*   r±   r   c                 ó®  • [        U S5        UR                  nUb#  [        U5      [        U5      :w  a  [        S5      e/ nUc  S/[        U5      -  OUn[	        X25       Hq  u  pVUbV  [
        R                  R                  R                  UR                  S5      R                  S5      USSS9R                  5       nUR                  S	U05        Ms     U$ )
aj  
Converts the raw output of [`DepthEstimatorOutput`] into final depth predictions and depth PIL images.
Only supports PyTorch.

Args:
    outputs ([`DepthEstimatorOutput`]):
        Raw outputs of the model.
    target_sizes (`TensorType` or `List[Tuple[int, int]]`, *optional*):
        Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size
        (height, width) of each image in the batch. If left to None, predictions will not be resized.

Returns:
    `List[Dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth
    predictions.
rr   Nz]Make sure that you pass in as many target sizes as the batch dimension of the predicted depthr   r@   ÚbicubicFr£   Úpredicted_depth)r   rÎ   r   r¨   Úziprr   Únnr   r­   r®   rp   r°   )rV   r±   rŸ   rÎ   ÚresultsÚdepthÚtarget_sizes          r+   Úpost_process_depth_estimationÚ/DPTImageProcessor.post_process_depth_estimationu  sÛ   € ô( 	˜$ Ô(à!×1Ñ1ˆàÑ$¬3¨Ó+?Ä3À|ÓCTÓ+TÜØoóð ð ˆØ8DÑ8L˜�v¤ OÓ 4Ò4ÐR^ˆÜ"% oÖ"DÑˆEØÑ&ÜŸ™×+Ñ+×7Ñ7Ø—O‘O AÓ&×0Ñ0°Ó3¸+ÈIÐejð 8ð ç‘'“)ð ð �N‰NÐ-¨uÐ5Ö6ñ #Eð ˆr*   r    r4   )Tr@   F)r@   )4r!   r"   r#   r$   r%   r   Úvalid_kwargsr   ÚBICUBICrˆ   r   rŒ   r   r�   r‡   Údefault_to_squarerŠ   r†   r‰   rg   rf   r   rŽ   r‹   r   r   r   rU   r   r   r	   r\   r(   r   Ústrr   r   rx   Úlistr„   r   Úfloatr&   rm   Útupler¶   r“   r–   ÚdictrÔ   r)   Ú__classcell__)rW   s   @r+   rP   rP   l   s=  ø† á8à*€LØ!×)Ñ)€HØ'€JØ%€IØ CÑ(€DØÐð €IØ€IØ€NØ€JØ€LØÐØ€FØ€NØÐØÐð# Ð(?Ñ!@÷ #ð ð 04ñ
Gàð
Gð &¨Ñ,ð
Gð Ð0Ñ1ð	
Gð
 
÷
Gó ð
Gð& 59ñ*Càð*Cð &¨Ñ,ð*Cð ð	*Cð
 ,ð*Cð ˜jÑ(¨4Ñ/ð*Cð �c˜>Ð)Ñ*¨TÑ1ð*Cð 
õ*CðX 4¨Ñ#7ð ¸DÀÑ<Pô ð9 à�^Ñ$ð9 ð ð9 ð ð	9 ð
 ð9 ð Lð9 ð ð9 ð ð9 ð ð9 ð ð9 ð ð9 ð ˜D ™KÑ'¨$Ñ.ð9 ð ˜4 ™;Ñ&¨Ñ-ð9 ð  ð9 ð   $™Jð9 ð  ð!9 ð" ˜D‘jð#9 ð$  ™+ð%9 ð( 
ô)9 ñv+%ÈÈUÉÐVZÑHZõ +%ðd Ø)*Ø"'ñ&Zàð&Zð ð&Zð Lð	&Zð
 ð&Zð   $™Jð&Zð  ð&Zð 
÷&Zð &ZðV ñ'àð'ð ð'ð 
õ	'ð> JNñ'à'ð'ð ! 4¨¨c°3¨h©Ñ#8Ñ8¸4Ñ?À$ÑFð'ð 
ˆd�3˜
�?Ñ#Ñ	$÷	'ó 'r*   rP   )+r6   Úcollections.abcr   Útypingr   r   rr   Útorch.nn.functionalrÐ   r   r¬   Útorchvision.transforms.v2rÄ   Úimage_processing_backendsr   Úimage_processing_baser	   Úimage_transformsr
   r   Úimage_utilsr   r   r   r   r   r   Úprocessing_utilsr   r   Úutilsr   r   r   r   Úmodeling_outputsr   r   r&   r(   rN   rP   Ú__all__r    r*   r+   Ú<module>rë      s·   ðó, Ý $ß 'ã ß Ð Ý 7å ;Ý 1ß E÷÷ ÷ 5ß VÓ Vö Ý8ô˜l°%ò ð($8Øð$8à�x ‘}Ñ$ð$8ð ð$8ð ð	$8ð
 ô$8ðN ôoÐ*ó oó ðoðd	 Ð
�r*   