ó
    ˆŽ¡j,  ã                   ó  • S r SSK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  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  \R>                  " \ 5      r! " S S\SS9r"\ " S S\5      5       r#S/r$g)z$Image processor class for MobileViT.é    )Ú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ÚloggingÚrequires_backendsc                   ó.   • \ rS rSr% Sr\\S'   \\S'   Srg)ÚMobileVitImageProcessorKwargsé-   aó  
do_flip_channel_order (`bool`, *optional*, defaults to `self.do_flip_channel_order`):
    Whether to flip the color channels from RGB to BGR or vice versa.
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_flip_channel_orderÚ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/mobilevit/image_processing_mobilevit.pyr   r   -   s   ‡ ñð  ÓØÖr%   r   F)Útotalc                   óÊ  ^ • \ rS rSrSr\r\R                  r	\
r\rSS0rSrSSS.rSr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 jr&  S-S\$S   S\S\'SSS\S\'S \S!\(S"\S	-  S#\S$\S\$S   4S% jjr)  S.S&\$\*\+\+4      S	-  S'\SS(4S) jjr,S*r-U =r.$ )/ÚMobileViTImageProcessoré;   zSTorchvision backend for MobileViT with flip_channel_order and reduce_label support.Úshortest_edgeéà   Fé   )ÚheightÚwidthTNÚkwargsc                 ó&   >• [         TU ]  " S0 UD6  g )Nr   )ÚsuperÚ__init__)Úselfr0   Ú	__class__s     €r&   r3   Ú MobileViTImageProcessor.__init__O   s   ø€ Ü‰ÒÑ"˜6Ó"r%   ÚimagesÚsegmentation_mapsÚreturnc                 ó&   >• [         TU ]  " X40 UD6$ )zX
segmentation_maps (`ImageInput`, *optional*):
    The segmentation maps to preprocess.
)r2   Ú
preprocess)r4   r7   r8   r0   r5   s       €r&   r;   Ú"MobileViTImageProcessor.preprocessR   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[        R                  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.)r7   r=   r>   r@   Fr   Úpixel_valuesé   )r7   Úexpected_ndimsr=   r>   )Ú
do_rescaler   Úresampler7   r   Úlabels)ÚdataÚtensor_typer   )Ú_prepare_image_like_inputsÚcopyÚ_preprocessr   ÚFIRSTÚupdater   ÚNEARESTÚsqueezeÚtoÚtorchÚint64r   )r4   r7   r8   r=   r>   r?   r@   r0   Úimages_kwargsrH   Úprocessed_segmentation_mapsÚsegmentation_maps_kwargsÚprocessed_segmentation_maps                r&   Ú_preprocess_image_like_inputsÚ5MobileViTImageProcessor._preprocess_image_like_inputs_   s/  € ð ×0Ñ0ØÐL]ð 1ð 
ˆð Ÿ™›ˆØ,1ˆÐ(Ñ)ØˆØ#×/Ò/°ÑH¸-ÑHˆˆ^ÑàÑ(Ø*.×*IÑ*IØ(Ø Ø$Ü"2×"8Ñ"8ð	 +Jð +Ð'ð (.§{¡{£}Ð$Ø$×+Ñ+à"'Ø-2ä 2× :Ñ :ñ	ôð +/×*:Ò*:ñ +Ø2ð+Ø6Nñ+Ð'ñ 3Nó+â2MÐ.ð +×2Ñ2°1Ó5×8Ñ8¼¿¹ÖEÙ2Mð (ð +ð 9�‰Nä ÑBÐBùò+s   Â,8C3rG   ú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ÚlenrR   ÚwhereÚtensorr]   r@   )r4   rG   ÚidxÚlabels       r&   Úreduce_labelÚ$MobileViTImageProcessor.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%   c                 óÊ   • UR                   S:X  a  UR                  5       nU/ SQ   USS& U$ UR                   S:X  a&  UR                  5       nUSS2/ SQ4   USS2SS24'   U$ U$ )zFlip RGB to BGR or vice versa.r   )rC   r^   r   r   é   N)ÚndimÚclone)r4   r7   Úflippeds      r&   Úflip_channel_orderÚ*MobileViTImageProcessor.flip_channel_orderš   si   € à�;‰;˜!Óà—l‘l“nˆGØ!¢)Ñ,ˆG�A�aˆLØˆNØ�[‰[˜AÓà—l‘l“nˆGØ$¢Qª	 \Ñ2ˆG’A�q˜�s�F‰OØˆNØˆr%   Ú	do_resizeÚsizerF   z7PILImageResampling | tvF.InterpolationMode | int | NoneÚdo_center_cropÚ	crop_sizerE   Úrescale_factorÚdisable_groupingr   r   c                 óÜ  • U
(       a  U R                  U5      n[        XS9u  pÞ0 nUR                  5        H$  u  nnU(       a  U R                  UX45      nUUU'   M&     [	        Xþ5      n[        UU	S9u  pÞ0 nUR                  5        HU  u  nnU(       a  U R                  UU5      nU(       a  U R                  UU5      nU(       a  U R                  U5      nUUU'   MW     [	        UU5      nU$ )z#Custom preprocessing for MobileViT.)rt   )rf   r	   ÚitemsÚresizer
   Úcenter_cropÚrescalerm   )r4   r7   ro   rp   rF   rq   rr   rE   rs   rt   r   r   r0   Úgrouped_imagesÚgrouped_images_indexÚresized_images_groupedÚshapeÚstacked_imagesÚresized_imagesÚprocessed_images_groupedÚprocessed_imagess                        r&   rL   Ú#MobileViTImageProcessor._preprocess¨   sø   € ö  Ø×&Ñ& vÓ.ˆFä/DÀVÑ/oÑ,ˆØ!#ÐØ%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%§¡¨^¸TÓ!L�Ø,:Ð" 5Ó)ñ &<ô (Ð(>ÓUˆä/DÀ^ÐfvÑ/wÑ,ˆØ#%Ð Ø%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%×!1Ñ!1°.À)Ó!L�ÞØ!%§¡¨n¸nÓ!M�Þ$Ø!%×!8Ñ!8¸Ó!H�Ø.<Ð$ UÓ+ñ &<ô *Ð*BÐDXÓYÐØÐr%   Útarget_sizesÚreturn_segmentation_scoreszBlist[torch.Tensor] | list[SemanticSegmentationPostProcessorOutput]c           	      ó¶  • [        U S5        UR                  n[        U5      nUbÐ  [        U5      [        U5      :w  a  [        S5      e[	        U[
        R                  5      (       a  UR                  5       n/ n[        U5       Hl  n[
        R                  R                  R                  XG   R                  SS9X'   SSS9nUR                  [        US   R                  SS9US   S.S	95        Mn     O6[        U5       V	s/ s H   n	[        XI   R                  SS9XI   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 )
a1  
Converts the output of [`MobileViTForSemanticSegmentation`] into semantic segmentation maps.

Args:
    outputs ([`MobileViTForSemanticSegmentation`]):
        Raw outputs of the model.
    target_sizes (`list[tuple[int, int]]`, *optional*):
        List of tuples corresponding to the requested final size (height, width) of each prediction.
    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).
rR   zTMake sure that you pass in as many target sizes as the batch dimension of the logitsr   )ÚdimÚbilinearF)rp   ÚmodeÚalign_corners)ÚsegmentationÚsegmentation_scores)rH   )r   Úlogitsra   Ú
ValueErrorÚ
isinstancerR   ÚTensorÚtolistr`   Únnr   ÚinterpolateÚ	unsqueezeÚappendr   ÚargmaxrŠ   )r4   Úoutputsrƒ   r„   rŒ   Ú
batch_sizeÚsemantic_segmentationrd   Úresized_logitsÚiÚitems              r&   Ú"post_process_semantic_segmentationÚ:MobileViTImageProcessor.post_process_semantic_segmentationÐ   sr  € ô6 	˜$ Ô(Ø—‘ˆÜ˜“[ˆ
ØÑ#Ü�6‹{œc ,Ó/Ó/Ü Øjóð ô ˜,¬¯©×5Ñ5Ø+×2Ñ2Ó4�Ø$&Ð!Ü˜ZÖ(�Ü!&§¡×!4Ñ!4×!@Ñ!@Ø‘K×)Ñ)¨aÐ)Ð0°|Ñ7HÈzÐinð "Að "�ð &×,Ñ,Ü;à,:¸1Ñ,=×,DÑ,DÈÐ,DÐ,KØ3AÀ!Ñ3Dññöò	 )ô" ˜zÔ*ó	%ò +�Aô 8Ø*0©)×*:Ñ*:¸qÐ*:Ð*AÐZ`ÑZcÑdôñ +ð	 "ð %ö *ÙCXÓ$YÒCX¸4×%6Ô%6ÑCXÐ!Ð$Yà$Ð$ùò%ùò %Zs   Ä'EÄ8Er   )N)r7   rZ   r9   rZ   )FT)NF)/r   r   r   r    r!   r   Úvalid_kwargsr   ÚBICUBICrF   r   Ú
image_meanr   Ú	image_stdrp   Údefault_to_squarerr   ro   rq   rE   Údo_normalizer=   r   r   r   r3   r   r   r   r;   r"   r   Ústrr   r   rX   Úlistrf   rm   r   ÚfloatrL   ÚtupleÚintrœ   r$   Ú__classcell__)r5   s   @r&   r)   r)   ;   s.  ø† á]à0€Là!×)Ñ)€HØ'€JØ%€IØ˜SÐ!€DØÐØ¨Ñ-€IØ€IØ€NØ€JØ€LØ€NØ ÐØÐð# Ð(EÑ!F÷ #ð ð 04ñ
Gàð
Gð &¨Ñ,ð
Gð Ð6Ñ7ð	
Gð
 
÷
Gó ð
Gð& 59ñ/Càð/Cð &¨Ñ,ð/Cð ð	/Cð
 ,ð/Cð ˜jÑ(¨4Ñ/ð/Cð �c˜>Ð)Ñ*¨TÑ1ð/Cð 
õ/Cðb 4¨Ñ#7ð ¸DÀÑ<Pô ôð2 "'Ø&*ñ& à�^Ñ$ð& ð ð& ð ð	& ð
 Lð& ð ð& ð ð& ð ð& ð ð& ð  ™+ð& ð ð& ð  $ð& ð 
ˆnÑ	õ& ðV 6:Ø+0ñ	=%ð ˜5  c ™?Ñ+¨dÑ2ð=%ð %)ð	=%ð
 
N÷=%ó =%r%   r)   )%r!   Útypingr   rR   Útorchvision.transforms.v2r   Ú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   Ú
get_loggerr   Úloggerr   r)   Ú__all__r   r%   r&   Ú<module>r·      s‚   ðñ +å ã Ý 7å ;Ý OÝ 2ß E÷÷ ÷ 5÷ó ð 
×	Ò	˜HÓ	%€ô L¸ò ð ôQ%Ð0ó Q%ó ðQ%ðh %Ð
%�r%   