ó
    qyüi6  ã                   óú   • S r SSKJr  SSK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  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\\ \\\4      4S jr! " S S\SS9r"\ " S S\
5      5       r#S/r$g)z!Image processor class for SegGPT.é    )ÚUnionN)Ú
functionalé   )ÚTorchvisionBackend)ÚBatchFeature)Úgroup_images_by_shapeÚreorder_images)ÚIMAGENET_DEFAULT_MEANÚIMAGENET_DEFAULT_STDÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚSizeDict)ÚImagesKwargsÚUnpack)Ú
TensorTypeÚauto_docstringÚrequires_backendsÚ
num_labelsÚreturnc                 óÖ   • [        U S-  5      S-   nSU-  nS/n[        U 5       H@  nXAS-  -  nXAS-  -  U-  nXA-  nSXR-  -
  nSXb-  -
  n	SXr-  -
  n
UR                  X‰U
45        MB     U$ )NgUUUUUUÕ?é   é   )r   r   r   é   éÿ   )ÚintÚrangeÚappend)r   ÚbaseÚmarginÚ
color_listÚlocationÚ	num_seq_rÚ	num_seq_gÚ	num_seq_bÚRÚGÚBs              Úo/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/models/seggpt/image_processing_seggpt.pyÚbuild_paletter*   '   sœ   € Üˆz˜eÑ$Ó%¨Ñ)€DØ�D‰[€Fð �€JÜ˜*Ö%ˆØ a¡Ñ'ˆ	Ø a¡Ñ'¨DÑ0ˆ	Ø‘Oˆ	à�)Ñ$Ñ$ˆØ�)Ñ$Ñ$ˆØ�)Ñ$Ñ$ˆà×Ñ˜1 ˜)Ö$ñ &ð Ðó    c                   ó$   • \ rS rSr% Sr\\S'   Srg)ÚSegGptImageProcessorKwargsé;   a�  
num_labels (`int`, *optional*):
    Number of classes in the segmentation task (excluding the background). If specified, a palette will be
    built, assuming that class_idx 0 is the background, to map the prompt mask from a plain segmentation map
    to a 3-channel RGB image. Not specifying this will result in the prompt mask being duplicated across the
    channel dimension when `do_convert_rgb` is `True`.
r   © N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Ú__annotations__Ú__static_attributes__r/   r+   r)   r-   r-   ;   s   ‡ ñð †Or+   r-   F)Útotalc                   óJ  ^ • \ 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\\   4U 4S jjrS\S	\\\\\4      4S
 jrS'S\R4                  S\\\\\4      S-  S	\R4                  4S jjr\   S(S\S-  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-  S\$\"S4   S-  S\S-  S	\4S jjr%S\S   S\ S\&SSS\ S\'S\ S \'\\'   -  S-  S!\'\\'   -  S-  S"\ S-  S	\S   4S# jr( S)S$\\\\4      S-  S\S-  4S% jjr)S&r*U =r+$ )*ÚSegGptImageProcessoréG   iÀ  )ÚheightÚwidthTNÚkwargsc                 ó&   >• [         TU ]  " S0 UD6  g )Nr/   )ÚsuperÚ__init__)Úselfr=   Ú	__class__s     €r)   r@   ÚSegGptImageProcessor.__init__U   s   ø€ Ü‰ÒÑ"˜6Ó"r+   r   r   c                 ó   • [        U5      $ )a  Build a palette to map the prompt mask from a single channel to a 3-channel RGB.

Args:
    num_labels (`int`):
        Number of classes in the segmentation task (excluding the background).

Returns:
    `list[tuple[int, int, int]]`: Palette to map the prompt mask to RGB.
)r*   )rA   r   s     r)   Úget_paletteÚ SegGptImageProcessor.get_paletteX   s   € ô ˜ZÓ(Ð(r+   ÚmaskÚpalettec                 óÚ  • UR                   S:X  a  UR                  S5      nUR                  u  p4UGb  [        R                  " SX44[        R
                  S9n[        R                  " U5      nU H–  nX'   nX:H  R                  [        R
                  5      n	[        R                  " U[        R
                  S9R                  SSS5      n
U[        R                  " U	S5      U
-  R                  [        R
                  5      -  nM˜     [        R                  " USS5      R                  [        R
                  5      nU$ [        R                  " U[        R                  S4   SSS9nU$ )aÜ  Converts a segmentation map to RGB format.

Args:
    mask (`np.ndarray`):
        Segmentation map with shape `(height, width)` or `(1, height, width)` where pixel values
        represent the class index.
    palette (`list[tuple[int, int, int]]`, *optional*):
        Palette to use to convert the mask to RGB format. If unset, the mask is duplicated across
        the channel dimension.

Returns:
    `np.ndarray`: The mask in RGB format with shape `(3, height, width)`.
r   r   )Údtyper   r   .)Úaxis)ÚndimÚsqueezeÚshapeÚnpÚzerosÚuint8ÚuniqueÚastypeÚarrayÚreshapeÚexpand_dimsÚclipÚrepeatÚnewaxis)rA   rG   rH   r;   r<   Úrgb_maskÚclasses_in_maskÚ	class_idxÚ	rgb_valueÚ
class_maskÚ	class_rgbs              r)   Úmask_to_rgbÚ SegGptImageProcessor.mask_to_rgbd   s  € ð �9‰9˜‹>Ø—<‘< “?ˆDàŸ
™
‰ˆàÒÜ—x’x  FÐ 2¼"¿(¹(ÑCˆHÜ Ÿiši¨›oˆOÛ,�	Ø#Ñ.�	Ø"Ñ/×7Ñ7¼¿¹ÓA�
ÜŸHšH Y´b·h±hÑ?×GÑGÈÈ1ÈaÓP�	ØœRŸ^š^¨J¸Ó:¸YÑF×NÑNÌrÏxÉxÓXÑX’ñ	 -ô
 —w’w˜x¨¨CÓ0×7Ñ7¼¿¹ÓAˆHð ˆô —y’y ¤b§j¡j°# oÑ!6¸ÀÑBˆHàˆr+   ÚimagesÚprompt_imagesÚprompt_masksc                 ó~   >• [        S XU4 5       5      (       a  [        S5      eUb  UO/ n[        TU ]  " XRU40 UD6$ )a‰  
prompt_images (`ImageInput`, *optional*):
    Prompt images to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255.
prompt_masks (`ImageInput`, *optional*):
    Prompt masks to preprocess. Can be in the format of segmentation maps (no channels) or RGB images.
    If in the format of RGB images, `do_convert_rgb` should be set to `False`. If in the format of
    segmentation maps, specifying `num_labels` is recommended to build a palette to map the prompt mask
    from a single channel to a 3-channel RGB. If `num_labels` is not specified, the prompt mask will be
    duplicated across the channel dimension.
c              3   ó(   #   • U  H  oS L v •  M
     g 7f©Nr/   )Ú.0Úvs     r)   Ú	<genexpr>Ú2SegGptImageProcessor.preprocess.<locals>.<genexpr>—   s   é € ÐHÒ"G˜Q�D�yÒ"Gùs   ‚zFAt least one of images, prompt_images, prompt_masks must be specified.)ÚallÚ
ValueErrorr?   Ú
preprocess)rA   rb   rc   rd   r=   Ú_images_inputrB   s         €r)   rn   ÚSegGptImageProcessor.preprocess…   sM   ø€ ô$ ÑH 6¸,Ñ"GÓH×HÑHÜÐeÓfÐfð #)Ñ"4™¸"ˆÜ‰wÒ! -ÀÑWÐPVÑWÐWr+   Údo_convert_rgbÚinput_data_formatÚreturn_tensorsÚdeviceztorch.devicec	                 ó,  • 0 n
[        U[        5      =(       a    [        U5      S:H  (       + nU(       a'  U R                  USXWS9nU R                  " U40 U	D6U
S'   Ub'  U R                  USXWS9nU R                  " U40 U	D6U
S'   UGb  U(       a¶  U R                  USS[
        R                  US9nUb  U R                  U5      OS n/ nU Ht  nUR                  S5      R                  5       nU R                  UUS9nUR                  [        R                  " UR                  [        R                   5      5      5        Mv     UnOU R                  US	SUUS9n[#        U	5      n[$        R&                  US
'   U R                  " U40 UD6U
S'   [)        X¦S9$ )Nr   F)rb   rq   rr   rt   Úpixel_valuesÚprompt_pixel_valuesr   )rb   Úexpected_ndimsrq   rr   rt   )rH   r   Úresamplerd   )ÚdataÚtensor_type)Ú
isinstanceÚlistÚlenÚ_prepare_image_like_inputsÚ_preprocessr   ÚFIRSTrE   rM   Únumpyr`   r   ÚtorchÚ
from_numpyrS   rO   Úfloat32Údictr   ÚNEARESTr   )rA   rb   rc   rd   rq   rr   rs   rt   r   r=   rz   Ú_images_providedÚprepared_imagesÚprepared_prompt_imagesÚprepared_masksrH   Ú	convertedÚmask_tensorÚmask_npÚrgb_npÚmasks_kwargss                        r)   Ú_preprocess_image_like_inputsÚ2SegGptImageProcessor._preprocess_image_like_inputsž   sÍ  € ð ˆô !+¨6´4Ó 8× M¼SÀ»[ÈAÑ=MÔNÐÞØ"×=Ñ=Ø¨eÐGXð >ð ˆOð $(×#3Ò#3°OÑ#NÀvÑ#NˆD�Ñ ð Ñ$Ø%)×%DÑ%DØ$°UÐN_ð &Eð &Ð"ð +/×*:Ò*:Ð;QÑ*\ÐU[Ñ*\ˆDÐ&Ñ'ð Ò#Þà!%×!@Ñ!@Ø'Ø#$Ø#(Ü&6×&<Ñ&<Ø!ð "Að "�ð ;EÑ:P˜$×*Ñ*¨:Ô6ÐVZ�Ø�	Û#1�KØ)×1Ñ1°!Ó4×:Ñ:Ó<�GØ!×-Ñ-¨g¸wÐ-ÐG�FØ×$Ñ$¤U×%5Ò%5°f·m±mÄBÇJÁJÓ6OÓ%PÖQñ $2ð "+‘ð "&×!@Ñ!@Ø'Ø#$Ø#(Ø&7Ø!ð "Að "�ô   ›<ˆLÜ'9×'AÑ'AˆL˜Ñ$Ø#'×#3Ò#3°NÑ#SÀlÑ#SˆD�Ñ ä ÑBÐBr+   ztorch.TensorÚ	do_resizeÚsizery   z7PILImageResampling | tvF.InterpolationMode | int | NoneÚ
do_rescaleÚrescale_factorÚdo_normalizeÚ
image_meanÚ	image_stdÚdisable_groupingc           	      ó:  • [        XS9u  pÍ0 nUR                  5        H#  u  nnU(       a  U R                  UX45      nUXï'   M%     [        Xí5      n[        UU
S9u  pÍ0 nUR                  5        H  u  nnU R	                  UXVXxU	5      nUUU'   M!     [        UU5      $ )N)rš   )r   ÚitemsÚresizer	   Úrescale_and_normalize)rA   rb   r“   r”   ry   r•   r–   r—   r˜   r™   rš   r=   Úgrouped_imagesÚgrouped_images_indexÚresized_images_groupedrN   Ústacked_imagesÚresized_imagesÚprocessed_images_groupeds                      r)   r€   Ú SegGptImageProcessor._preprocessÞ   s¿   € ô 0EÀVÑ/oÑ,ˆØ!#ÐØ%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%§¡¨^¸TÓ!L�Ø,:Ð"Ó)ñ &<ô (Ð(>ÓUˆä/DÀ^ÐfvÑ/wÑ,ˆØ#%Ð Ø%3×%9Ñ%9Ö%;Ñ!ˆE�>Ø!×7Ñ7Ø 
¸LÐV_óˆNð /=Ð$ UÓ+ñ	 &<ô Ð6Ð8LÓMÐMr+   Útarget_sizesc                 óä  • [        U S/5        UR                  nUSS2SS2UR                  S   S-  S2SS24   n[        R                  " U R
                  5      R                  UR                  5      n[        R                  " U R                  5      R                  UR                  5      nUR                  SSSS5      U-  U-   nUR                  SSSS5      n[        R                  " US-  SS5      n/ nSnUb  U R                  U5      OSn	U	bc  [        R                  " U	5      R                  UR                  [        R                  S9nUR                  u  p«  n
UR                  SSUS-   U5      n[        U5       Hë  u  pÍUb=  [        R                  R                   R#                  UR%                  S5      X,   S	S
9S   nUbu  UR                  u  pïnUR                  SSS5      R                  UUSU5      nUU-
  n[        R&                  " US5      n[        R(                  " USS9nUR+                  SS9nOUR-                  SS9R/                  5       nUR1                  U5        Mí     U$ )a�  
Converts the output of [`SegGptImageSegmentationOutput`] into segmentation maps. Only supports PyTorch.

Args:
    outputs ([`SegGptImageSegmentationOutput`]):
        Raw outputs of the model.
    target_sizes (`list[tuple[int, int]]`, *optional*):
        List of length `batch_size`, where each item corresponds to the requested final size `(height, width)`
        of each prediction. If left to `None`, predictions will not be resized.
    num_labels (`int`, *optional*):
        Number of classes in the segmentation task (excluding the background). If specified, a palette will be
        built to map prediction masks from RGB values back to class indices. Should match the value used during
        preprocessing.

Returns:
    `list[torch.Tensor]` of length `batch_size`, where each item is a semantic segmentation map of shape
    `(height, width)`. Each entry corresponds to a semantic class id.
rƒ   Nr   r   r   r   r   )rt   rJ   Únearest)r”   Úmodeéÿÿÿÿ)Údim)r   Ú
pred_masksrN   rƒ   Útensorr™   Útort   r˜   ÚpermuterW   rE   ÚfloatÚviewÚ	enumerateÚnnr   ÚinterpolateÚ	unsqueezeÚpowÚsumÚargminÚmeanr   r   )rA   Úoutputsr¦   r   ÚmasksÚstdr¹   Úsemantic_segmentationÚpalette_tensorrH   Ú_Únum_channelsÚidxrG   Úchannelsr;   r<   ÚdistÚpreds                      r)   Ú"post_process_semantic_segmentationÚ7SegGptImageProcessor.post_process_semantic_segmentationþ   s@  € ô, 	˜$  	Ô*ð ×"Ñ"ˆð ’aš˜EŸK™K¨™N¨aÑ/Ñ1²1Ð4Ñ5ˆô �lŠl˜4Ÿ>™>Ó*×-Ñ-¨e¯l©lÓ;ˆÜ�|Š|˜DŸO™OÓ,×/Ñ/°·±Ó=ˆØ—‘˜a  A qÓ)¨CÑ/°$Ñ6ˆØ—‘˜a  A qÓ)ˆä—
’
˜5 3™;¨¨3Ó/ˆà "ÐØˆØ2<Ñ2H�$×"Ñ" :Ô.ÈdˆØÑÜ"Ÿ\š\¨'Ó2×5Ñ5¸U¿\¹\ÔQV×Q\ÑQ\Ð5Ð]ˆNØ$)§K¡KÑ!ˆA˜Q Ø+×0Ñ0°°A°zÀA±~À|ÓTˆNä" 5Ö)‰IˆCØÑ'Ü—x‘x×*Ñ*×6Ñ6Ø—N‘N 1Ó%Ø%Ñ*Ø"ð 7ð ð ñ	�ð Ñ%Ø*.¯*©*Ñ'� %Ø—|‘| A q¨!Ó,×1Ñ1°&¸%ÀÀHÓM�Ø˜nÑ,�Ü—y’y  qÓ)�Ü—y’y ¨2Ñ.�Ø—{‘{ r�{Ð*‘à—y‘y Q�yÐ'×+Ñ+Ó-�à!×(Ñ(¨Ö.ñ% *ð( %Ð$r+   r/   rg   )NNN)NN),r0   r1   r2   r3   r-   Úvalid_kwargsr   ÚBICUBICry   r
   r˜   r   r™   r”   r“   r•   r—   rq   r   r   r@   r   r}   ÚtuplerE   rO   Úndarrayr`   r   r   r   rn   Úboolr   Ústrr   r   r‘   r   r°   r€   rÅ   r6   Ú__classcell__)rB   s   @r)   r9   r9   G   sš  ø† à-€Là!×)Ñ)€HØ&€JØ$€IØ CÑ(€DØ€IØ€JØ€LØ€NØ€Jð# Ð(BÑ!C÷ #ð
) cð 
)¨d°5¸¸cÀ3¸Ñ3GÑ.Hô 
)ñ §
¡
ð °T¸%ÀÀSÈ#ÀÑ:NÑ5OÐRVÑ5Vð Ðbd×blÑblõ ðB ð %)Ø+/Ø*.ñ	Xà˜TÑ!ðXð " DÑ(ðXð ! 4Ñ'ð	Xð
 Ð3Ñ4ðXð 
÷Xó ðXð@ 59Ø!%ñ>Càð>Cð " DÑ(ð>Cð ! 4Ñ'ð	>Cð
 ð>Cð ,ð>Cð ˜jÑ(¨4Ñ/ð>Cð �c˜>Ð)Ñ*¨TÑ1ð>Cð ˜$‘Jð>Cð 
õ>Cð@Nà�^Ñ$ðNð ðNð ð	Nð
 LðNð ðNð ðNð ðNð ˜D ™KÑ'¨$Ñ.ðNð ˜4 ™;Ñ&¨Ñ-ðNð  ™+ðNð 
ˆnÑ	ôNðB dhñC%Ø%)¨%°°S°©/Ñ%:¸TÑ%AðC%ØVYÐ\`ÑV`÷C%ó C%r+   r9   )%r4   Útypingr   r‚   rO   rƒ   Útorchvision.transforms.v2r   ÚtvFÚimage_processing_backendsr   Úimage_processing_utilsr   Úimage_transformsr   r	   Úimage_utilsr
   r   r   r   r   r   Úprocessing_utilsr   r   Úutilsr   r   r   r   r}   rÉ   r*   r-   r9   Ú__all__r/   r+   r)   Ú<module>rØ      s�   ðñ (å ã Û Ý 7å ;Ý 2ß E÷÷ ÷ 5ß BÑ Bð
˜cð  d¨5°°c¸3°Ñ+?Ñ&@ô ô(	 °Uò 	ð ôy%Ð-ó y%ó ðy%ðx "Ð
"�r+   