Ë
    ÜÓ^j=  ã                   ó
  — d Z ddlmZ ddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZmZ dd
lmZmZmZmZmZmZ ddlmZmZ ddlmZmZmZ de de!e"e e e f      fd„Z# G d„ ded¬«      Z$e G d„ de
«      «       Z%dgZ&y)z!Image processor class for SegGPT.é    )ÚUnionN)Ú
functionalé   )ÚTorchvisionBackend)Ú'SemanticSegmentationPostProcessorOutput)Ú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                 óà   — t        | dz  «      dz   }d|z  }dg}t        | «      D ]F  }||dz  z  }||dz  z  |z  }||z  }d||z  z
  }d||z  z
  }	d||z  z
  }
|j                  ||	|
f«       ŒH |S )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              ú}/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/seggpt/image_processing_seggpt.pyÚbuild_paletter+   (   s¯   € Üˆz˜eÑ$Ó%¨Ñ)€DØ�D‰[€Fð �€JÜ˜*Ó%ò 	%ˆØ  a¡Ñ'ˆ	Ø  a¡Ñ'¨DÑ0ˆ	Ø˜t‘Oˆ	à�)˜fÑ$Ñ$ˆØ�)˜fÑ$Ñ$ˆØ�)˜fÑ$Ñ$ˆà×Ñ˜1˜a ˜)Õ$ð	%ð Ðó    c                   ó   — e Zd ZU dZeed<   y)ÚSegGptImageProcessorKwargsaµ  
    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__Ú__doc__r   Ú__annotations__© r,   r*   r.   r.   <   s   … ñð „Or,   r.   F)Útotalc                   ó4  ‡ — e Zd ZeZej                  ZeZ	e
ZdddœZdZdZdZdZdZdee   fˆ fd„Zdedeeeeef      fd	„Zd'd
ej2                  deeeeef      dz  dej2                  fd„Ze	 	 	 d(dedz  dedz  dedz  dee   def
ˆ fd„«       Z	 	 d)dededz  dedz  dede de!e"z  dz  de#e!df   dz  dedz  defd„Z$ded   dede%dddede&dede&ee&   z  dz  d e&ee&   z  dz  d!edz  ded   fd"„Z'	 	 	 d*d#eeeef      dz  dedz  d$edd%fd&„Z(ˆ xZ)S )+ÚSegGptImageProcessoriÀ  )ÚheightÚwidthTNÚkwargsc                 ó$   •— t        ‰| �  di |¤Ž y )Nr4   )ÚsuperÚ__init__)Úselfr:   Ú	__class__s     €r*   r=   zSegGptImageProcessor.__init__V   s   ø€ Ü‰ÑÑ"˜6Ó"r,   r   r   c                 ó   — t        |«      S )aB  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+   )r>   r   s     r*   Úget_palettez SegGptImageProcessor.get_paletteY   s   € ô ˜ZÓ(Ð(r,   ÚmaskÚpalettec                 óà  — |j                   dk(  r|j                  d«      }|j                  \  }}|��t        j                  d||ft        j
                  ¬«      }t        j                  |«      }|D ]˜  }||   }||k(  j                  t        j
                  «      }	t        j                  |t        j
                  ¬«      j                  ddd«      }
|t        j                  |	d«      |
z  j                  t        j
                  «      z  }Œš t        j                  |dd«      j                  t        j
                  «      }|S t        j                  |t        j                  df   dd¬«      }|S )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)r>   rB   rC   r8   r9   Úrgb_maskÚclasses_in_maskÚ	class_idxÚ	rgb_valueÚ
class_maskÚ	class_rgbs              r*   Úmask_to_rgbz SegGptImageProcessor.mask_to_rgbe   s'  € ð �9‰9˜Š>Ø—<‘< “?ˆDàŸ
™
‰ˆ�àÑÜ—x‘x  F¨EÐ 2¼"¿(¹(ÔCˆHÜ Ÿi™i¨›oˆOØ,ò Y�	Ø# IÑ.�	Ø" iÑ/×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‘ð	Yô
 —w‘w˜x¨¨CÓ0×7Ñ7¼¿¹ÓAˆHð ˆô —y‘y ¤b§j¡j°# oÑ!6¸ÀÔBˆHàˆr,   ÚimagesÚprompt_imagesÚprompt_masksc                 ót   •— t        d„ |||fD «       «      rt        d«      ‚|�|ng }t        ‰| �  |||fi |¤ŽS )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   ó$   K  — | ]  }|d u –— Œ
 y ­w©Nr4   )Ú.0Úvs     r*   ú	<genexpr>z2SegGptImageProcessor.preprocess.<locals>.<genexpr>˜   s   è ø€ ÒH˜Qˆq�DŒyÑHùs   ‚zFAt least one of images, prompt_images, prompt_masks must be specified.)ÚallÚ
ValueErrorr<   Ú
preprocess)r>   r\   r]   r^   r:   Ú_images_inputr?   s         €r*   rg   zSegGptImageProcessor.preprocess†   sN   ø€ ô$ ÑH 6¨=¸,Ð"GÔHÔHÜÐeÓfÐfð #)Ð"4™¸"ˆÜ‰wÑ! -°ÀÑWÐPVÑWÐWr,   Údo_convert_rgbÚinput_data_formatÚreturn_tensorsÚdeviceztorch.devicec	                 ó$  — i }
t        |t        «      xr t        |«      dk(   }|r+| j                  |d||¬«      } | j                  |fi |	¤Ž|
d<   |�+| j                  |d||¬«      } | j                  |fi |	¤Ž|
d<   |��|r¸| j                  |ddt
        j                  |¬«      }|�| j                  |«      nd }g }|D ]u  }|j                  d«      j                  «       }| j                  ||¬«      }|j                  t        j                  |j                  t        j                   «      «      «       Œw |}n| j                  |d	d||¬«      }t#        |	«      }t$        j&                  |d
<    | j                  |fi |¤Ž|
d<   t)        |
|¬«      S )Nr   F)r\   ri   rj   rl   Úpixel_valuesÚprompt_pixel_valuesr   )r\   Úexpected_ndimsri   rj   rl   )rC   r   Úresampler^   )ÚdataÚtensor_type)Ú
isinstanceÚlistÚlenÚ_prepare_image_like_inputsÚ_preprocessr   ÚFIRSTrA   rH   Únumpyr[   r   ÚtorchÚ
from_numpyrN   rJ   Úfloat32Údictr   ÚNEARESTr   )r>   r\   r]   r^   ri   rj   rk   rl   r   r:   rr   Ú_images_providedÚprepared_imagesÚprepared_prompt_imagesÚprepared_masksrC   Ú	convertedÚmask_tensorÚmask_npÚrgb_npÚmasks_kwargss                        r*   Ú_preprocess_image_like_inputsz2SegGptImageProcessor._preprocess_image_like_inputsŸ   sâ  € ð ˆô !+¨6´4Ó 8Ò M¼SÀ»[ÈAÑ=MÐNÐÙØ"×=Ñ=Ø¨eÐGXÐagð >ó ˆOð $4 4×#3Ñ#3°OÑ#NÀvÑ#NˆD�Ñ ð Ð$Ø%)×%DÑ%DØ$°UÐN_Ðhnð &Eó &Ð"ð +;¨$×*:Ñ*:Ð;QÑ*\ÐU[Ñ*\ˆDÐ&Ñ'ð Ñ#Ùà!%×!@Ñ!@Ø'Ø#$Ø#(Ü&6×&<Ñ&<Ø!ð "Aó "�ð ;EÐ:P˜$×*Ñ*¨:Ô6ÐVZ�Ø�	Ø#1ò R�KØ)×1Ñ1°!Ó4×:Ñ:Ó<�GØ!×-Ñ-¨g¸wÐ-ÓG�FØ×$Ñ$¤U×%5Ñ%5°f·m±mÄBÇJÁJÓ6OÓ%PÕQðRð "+‘ð "&×!@Ñ!@Ø'Ø#$Ø#(Ø&7Ø!ð "Aó "�ô   ›<ˆLÜ'9×'AÑ'AˆL˜Ñ$Ø#3 4×#3Ñ#3°NÑ#SÀlÑ#SˆD�Ñ ä °>ÔBÐBr,   ztorch.TensorÚ	do_resizeÚsizerq   z7PILImageResampling | tvF.InterpolationMode | int | NoneÚ
do_rescaleÚrescale_factorÚdo_normalizeÚ
image_meanÚ	image_stdÚdisable_groupingc           	      óD  — t        ||
¬«      \  }}i }|j                  «       D ]  \  }}|r| j                  |||«      }|||<   Œ! t        ||«      }t        ||
¬«      \  }}i }|j                  «       D ]   \  }}| j	                  ||||||	«      }|||<   Œ" t        ||«      S )N)r‘   )r	   ÚitemsÚresizer
   Úrescale_and_normalize)r>   r\   rŠ   r‹   rq   rŒ   r�   rŽ   r�   r�   r‘   r:   Úgrouped_imagesÚgrouped_images_indexÚresized_images_groupedrI   Ústacked_imagesÚresized_imagesÚprocessed_images_groupeds                      r*   rx   z SegGptImageProcessor._preprocessß   sÝ   € ô 0EÀVÐ^nÔ/oÑ,ˆÐ,Ø!#ÐØ%3×%9Ñ%9Ó%;ò 	;Ñ!ˆE�>ÙØ!%§¡¨^¸TÀ8Ó!L�Ø,:Ð" 5Ò)ð	;ô (Ð(>Ð@TÓUˆä/DÀ^ÐfvÔ/wÑ,ˆÐ,Ø#%Ð Ø%3×%9Ñ%9Ó%;ò 	=Ñ!ˆE�>Ø!×7Ñ7Ø 
¨N¸LÈ*ÐV_óˆNð /=Ð$ UÒ+ð		=ô Ð6Ð8LÓMÐMr,   Útarget_sizesÚreturn_segmentation_scoreszBlist[torch.Tensor] | list[SemanticSegmentationPostProcessorOutput]c                 óÄ  — t        | dg«       |j                  }|�"t        |«      t        |«      k7  rt        d«      ‚|dd…dd…|j                  d   dz  d…dd…f   }t        j                  | j                  «      j                  |j                  «      }t        j                  | j                  «      j                  |j                  «      }|j                  dddd«      |z  |z   }|j                  dddd«      }t        j                  |dz  dd«      }g }d}	|�| j                  |«      nd}
|
�ft        j                  |
«      j                  |j                  t
        j                  ¬	«      }	|j                  \  }}}}|	j                  dd|dz   |«      }	t!        |«      D �]  \  }}|�At
        j"                  j$                  j'                  |j)                  d«      ||   d
¬«      d   }|�Ž|j                  \  }}}|j                  ddd«      j                  ||d|«      }||	z
  }t        j*                  |d«      }t        j,                  |d¬«      }|j/                  d¬«      }|j                  ddd«       }n"|j1                  d¬«      j3                  «       }d}|j5                  t7        ||dœ¬«      «       �Œ |s|D �cg c]  }|j8                  ‘Œ }}|S c c}w )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.
            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_labels+1, height, width)`
                of negative squared L2 distances to each palette color, or `None` when `num_labels` is not provided).

        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_labels+1, height, width)`). In both cases,
            `(height, width)` corresponds to the target size (if `target_sizes` is specified).
        r{   NzTMake sure that you pass in as many target sizes as the batch dimension of the logitsr   r   r   r   r   )rl   rE   Únearest)r‹   Úmodeéÿÿÿÿ)Údim)ÚsegmentationÚsegmentation_scores)rr   )r   Ú
pred_masksrv   rf   rI   r{   Útensorr�   Útorl   r�   ÚpermuterR   rA   ÚfloatÚviewÚ	enumerateÚnnr   ÚinterpolateÚ	unsqueezeÚpowÚsumÚargminÚmeanr   r   r   r£   )r>   Úoutputsrœ   r   r�   ÚmasksÚstdr²   Úsemantic_segmentationÚpalette_tensorrC   Ú_Únum_channelsÚidxrB   Úchannelsr8   r9   ÚdistÚpredr¤   Úitems                         r*   Ú"post_process_semantic_segmentationz7SegGptImageProcessor.post_process_semantic_segmentationÿ   sÀ  € ôF 	˜$  	Ô*ð ×"Ñ"ˆàÐ#¬¨E«
´c¸,Ó6GÒ(GÜÐsÓtÐtð ’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Ó%Ø% cÑ*Ø"ð 7ó ð ñ	�ð Ð%Ø*.¯*©*Ñ'�˜& %Ø—|‘| A q¨!Ó,×1Ñ1°&¸%ÀÀHÓM�Ø˜nÑ,�Ü—y‘y  qÓ)�Ü—y‘y ¨2Ô.�Ø—{‘{ r�{Ó*�Ø'+§|¡|°A°q¸!Ó'<Ð&<Ñ#à—y‘y Q�yÓ'×+Ñ+Ó-�Ø&*Ð#à!×(Ñ(Ü7Ø*.ÐGZÑ[ôöð)	ñ4 *ØCXÖ$Y¸4 T×%6Ó%6Ð$YÐ!Ð$Yà$Ð$ùò %Zs   ËKra   )NNN)NN)NNF)*r/   r0   r1   r.   Úvalid_kwargsr   ÚBICUBICrq   r   r�   r   r�   r‹   rŠ   rŒ   rŽ   ri   r   r   r=   r   ru   ÚtuplerA   rJ   Úndarrayr[   r   r   r   rg   Úboolr   Ústrr   r   r‰   r   r©   rx   r¿   Ú__classcell__)r?   s   @r*   r7   r7   H   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ðF 6:Ø!%Ø+0ñ\%ð ˜5  c ™?Ñ+¨dÑ2ð\%ð ˜$‘Jð	\%ð
 %)ð\%ð 
N÷\%r,   r7   )'r2   Útypingr   rz   rJ   r{   Ú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   ru   rÂ   r+   r.   r7   Ú__all__r4   r,   r*   ú<module>rÒ      s’   ðñ (å ã Û Ý 7å ;Ý OÝ 2ß E÷÷ ÷ 5ß BÑ Bð
˜cð  d¨5°°c¸3°Ñ+?Ñ&@ó ô(	 °Uõ 	ð ôR%Ð-ó R%ó ðR%ðj "Ð
"�r,   