ó
    ‡Ž¡j¦)  ã                   óì   • S r SSK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  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    " S S\SS9r!\ " S S\5      5       r"S/r#g)zImage processor class for BEiT.é    )ÚUnionN)Ú
functionalé   )ÚTorchvisionBackend)Ú'SemanticSegmentationPostProcessorOutput)ÚBatchFeature)Úgroup_images_by_shapeÚreorder_images)ÚIMAGENET_STANDARD_MEANÚIMAGENET_STANDARD_STDÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚSizeDict)ÚImagesKwargsÚUnpack)Ú
TensorTypeÚauto_docstringÚis_torch_availablec                   ó$   • \ rS rSr% Sr\\S'   Srg)ÚBeitImageProcessorKwargsé&   aW  
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.
Údo_reduce_labels© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚboolÚ__annotations__Ú__static_attributes__r   ó    Ú¼C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\transformers/models/beit/image_processing_beit.pyr   r   &   s   ‡ ñð Ör#   r   F)Útotalc                   óØ  ^ • \ rS rSrSr\r\R                  r	\
r\rSSS.rSrSSS.rSrSrSrSrSrS\\   4U 4S jjr\ S*S
\S\S	-  S\\   S\4U 4S jjj5       r S*S
\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   4S$ jjr& S,S%\"\'   S	-  S&\SS'4S( jjr(S)r)U =r*$ )-ÚBeitImageProcessoré1   z/PIL backend for BEiT with reduce_label support.éà   )ÚheightÚwidthTFÚkwargsc                 ó&   >• [         TU ]  " S0 UD6  g )Nr   )ÚsuperÚ__init__)Úselfr,   Ú	__class__s     €r$   r/   ÚBeitImageProcessor.__init__C   s   ø€ Ü‰ÒÑ"˜6Ó"r#   NÚimagesÚsegmentation_mapsÚreturnc                 ó&   >• [         TU ]  " X40 UD6$ )zX
segmentation_maps (`ImageInput`, *optional*):
    The segmentation maps to preprocess.
)r.   Ú
preprocess)r0   r3   r4   r,   r1   s       €r$   r7   ÚBeitImageProcessor.preprocessF   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.)r3   r9   r:   r<   Fr   Úpixel_valuesé   )r3   Úexpected_ndimsr9   r:   )Údo_normalizeÚ
do_rescaler3   r   Úlabels)ÚdataÚtensor_typer   )Ú_prepare_image_like_inputsÚcopyÚ_preprocessr   ÚFIRSTÚupdateÚsqueezeÚtoÚtorchÚint64r   )r0   r3   r4   r9   r:   r;   r<   r,   Úimages_kwargsrD   Úprocessed_segmentation_mapsÚsegmentation_maps_kwargsÚprocessed_segmentation_maps                r$   Ú_preprocess_image_like_inputsÚ0BeitImageProcessor._preprocess_image_like_inputsS   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$rC   ztorch.Tensorc           
      ó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   éÿ   )Údtyper<   é   éþ   )ÚrangeÚlenrM   ÚwhereÚtensorrW   r<   )r0   rC   ÚidxÚlabels       r$   Úreduce_labelÚBeitImageProcessor.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#   Ú	do_resizeÚsizeÚresamplez7PILImageResampling | tvF.InterpolationMode | int | NoneÚdo_center_cropÚ	crop_sizerB   Úrescale_factorrA   Ú
image_meanÚ	image_stdÚdisable_groupingr   c           	      ó¨  • U(       a  U R                  U5      n[        XS9u  nn0 nUR                  5        H$  u  nnU(       a  U R                  UX45      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XxXšU5      nUUU'   M:     [	        UU5      nU$ )zCustom preprocessing for BEiT.)rj   )r`   r	   ÚitemsÚresizer
   Úcenter_cropÚrescale_and_normalize)r0   r3   rb   rc   rd   re   rf   rB   rg   rA   rh   ri   rj   r   r,   Úgrouped_imagesÚgrouped_images_indexÚresized_images_groupedÚshapeÚstacked_imagesÚresized_imagesÚprocessed_images_groupedÚprocessed_imagess                          r$   rH   ÚBeitImageProcessor._preprocess‰   sý   € ö$ Ø×&Ñ& vÓ.ˆFô 0EÀVÑ/oÑ,ˆÐ,Ø!#ÐØ%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%§¡¨^¸TÓ!L�Ø,:Ð" 5Ó)ñ &<ô (Ð(>Ð@TÓUˆô 0EÀ^ÐfvÑ/wÑ,ˆÐ,Ø#%Ð Ø%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%×!1Ñ!1°.À)Ó!L�à!×7Ñ7Ø 
¸LÐV_óˆNð /=Ð$ UÓ+ñ &<ô *Ð*BÐDXÓYÐàÐr#   Útarget_sizesÚreturn_segmentation_scoreszBlist[torch.Tensor] | list[SemanticSegmentationPostProcessorOutput]c                 óÈ  • [        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       HZ  n[        R                  " XF   R                  SS9X&   SSS9nUS   R                  SS9nUR                  [        X‡S   S.S	95        M\     OEUR                  S
S9n	[        UR                   S   5       V
s/ s H  n
[        Xš   XJ   S.S	9PM     nn
U(       d  U Vs/ s H  o»R"                  PM     nnU$ s  sn
f s  snf )ag  
Converts the output of [`BeitForSemanticSegmentation`] into semantic segmentation maps.

Args:
    outputs ([`BeitForSemanticSegmentation`]):
        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.
    return_segmentation_scores (`bool`, *optional*, defaults to `False`):
        Whether to return segmentation scores alongside the segmentation map. When `True`, each element of
        the returned list is a [`SemanticSegmentationPostProcessorOutput`] with fields `segmentation`
        (class IDs, shape `(height, width)`) and `segmentation_scores` (shape `(num_classes, height, width)`).

Returns:
    `list[torch.Tensor]` or `list[SemanticSegmentationPostProcessorOutput]`: When
    `return_segmentation_scores=False` (default), a list of length `batch_size` where each item is a
    segmentation map of shape `(height, width)` with class IDs. When `return_segmentation_scores=True`,
    a list of [`SemanticSegmentationPostProcessorOutput`] with fields `segmentation` (class IDs, shape
    `(height, width)`) and `segmentation_scores` (shape `(num_classes, height, width)`). In both cases,
    `(height, width)` corresponds to the target size (if `target_sizes` is specified).
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)rc   ÚmodeÚalign_corners)ÚsegmentationÚsegmentation_scores)rD   rX   )r   ÚImportErrorÚlogitsr[   Ú
ValueErrorÚ
isinstancerM   ÚTensorÚnumpyrZ   ÚFÚinterpolateÚ	unsqueezeÚargmaxÚappendr   rs   r€   )r0   Úoutputsry   rz   rƒ   Úsemantic_segmentationr^   Úresized_logitsÚsemantic_mapÚseg_mapsÚiÚitems               r$   Ú"post_process_semantic_segmentationÚ5BeitImageProcessor.post_process_semantic_segmentation·   ss  € ô2 "×#Ñ#ÜÐZÓ[Ð[à—‘ˆð Ñ#Ü�6‹{œc ,Ó/Ó/Ü Øjóð ô ˜,¬¯©×5Ñ5Ø+×1Ñ1Ó3�à$&Ð!äœS ›[Ö)�Ü!"§¢Ø‘K×)Ñ)¨aÐ)Ð0°|Ñ7HÈzÐinñ"�ð  .¨aÑ0×7Ñ7¸AÐ7Ð>�Ø%×,Ñ,Ü;Ø.:ÐbcÑSdÑeñöò *ð —}‘}¨�}Ð+ˆHô
 ˜vŸ|™|¨A™Ô/ó	%ò 0�Aô 8Ø*2©+ÈfÉiÑXôñ 0ð	 "ð %ö *ÙCXÓ$YÒCX¸4×%6Ô%6ÑCXÐ!Ð$Yà$Ð$ùò%ùò %Zs   ÄEÅEr   )N)F)NF)+r   r   r   r   r   r   Úvalid_kwargsr   ÚBICUBICrd   r   rh   r   ri   rc   Údefault_to_squarerf   rb   re   rB   rA   r   r   r/   r   r   r   r7   r    r   Ústrr   r   rS   Úlistr`   r   ÚfloatrH   Útupler”   r"   Ú__classcell__)r1   s   @r$   r'   r'   1   s?  ø† á9à+€Là!×)Ñ)€HØ'€JØ%€IØ CÑ(€DØÐØ¨Ñ-€IØ€IØ€NØ€JØ€LØÐð# Ð(@Ñ!A÷ #ð ð 04ñ
Gàð
Gð &¨Ñ,ð
Gð Ð1Ñ2ð	
Gð
 
÷
Gó ð
Gð& 59ñ*Càð*Cð &¨Ñ,ð*Cð ð	*Cð
 ,ð*Cð ˜jÑ(¨4Ñ/ð*Cð �c˜>Ð)Ñ*¨TÑ1ð*Cð 
õ*CðX 4¨Ñ#7ð ¸DÀÑ<Pô ð0 "'ñ, à�^Ñ$ð, ð ð, ð ð	, ð
 Lð, ð ð, ð ð, ð ð, ð ð, ð ð, ð ˜D ™KÑ'¨$Ñ.ð, ð ˜4 ™;Ñ&¨Ñ-ð, ð  ™+ð, ð ð, ð  
ˆnÑ	õ!, ð^ diñ@%Ø%)¨%¡[°4Ñ%7ð@%Ø\`ð@%à	M÷@%ó @%r#   r'   )$r   Útypingr   rM   Útorch.nn.functionalÚnnr   rˆ   Útorchvision.transforms.v2ÚtvFÚimage_processing_backendsr   Úimage_processing_outputsr   Úimage_processing_utilsr   Úimage_transformsr	   r
   Úimage_utilsr   r   r   r   r   r   Úprocessing_utilsr   r   Úutilsr   r   r   r   r'   Ú__all__r   r#   r$   Ú<module>r«      sr   ðñ &å ã ß Ð Ý 7å ;Ý OÝ 2ß E÷÷ ÷ 5ß CÑ Cô˜|°5ò ð ôE%Ð+ó E%ó ðE%ðP  Ð
 �r#   