ó
    qyüiT¶  ã                   óÖ  • S r SSKrSSKJr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  SS	KJrJr  SS
KJrJrJrJr  SSKJrJrJrJrJrJr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+  \+RX                  " \-5      r.\R^                  \R`                  4r1 " S S\&SS9r2S r3S r4S r5S-S jr6    S.S\7S\7S\8\9   S-  S\:\9\94   S-  4S jjr;S\9S\9S\Rx                  S\Rz                  4S jr>  S/S \?S!\\@-  S-  4S" jjrAS#\Rz                  S\Rz                  4S$ jrBS% rC  S0S&\Rz                  S'\DS(\@\RŠ                  -  S)\?S!\\@-  S\D4S* jjrF\* " S+ S,\5      5       rGS,/rHg)1zImage processor class for DETR.é    N)ÚAnyÚOptional)Únn)Ú
read_image)Ú
functionalé   )ÚTorchvisionBackend)ÚBatchFeatureÚget_size_dict)Úcenter_to_corners_formatÚcorners_to_center_formatÚget_size_with_aspect_ratioÚsafe_squeeze)ÚIMAGENET_DEFAULT_MEANÚIMAGENET_DEFAULT_STDÚAnnotationFormatÚAnnotationTypeÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚSizeDictÚget_image_sizeÚ#get_image_size_for_max_height_widthÚget_max_height_widthÚvalidate_annotations)ÚImagesKwargsÚUnpack)Ú
TensorTypeÚauto_docstringÚloggingc                   ó4   • \ rS rSr% Sr\\-  \S'   \\S'   Sr	g)ÚDetrImageProcessorKwargsé<   a  
format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`):
    Data format of the annotations. One of "coco_detection" or "coco_panoptic".
do_convert_annotations (`bool`, *optional*, defaults to `True`):
    Controls whether to convert the annotations to the format expected by the DETR model. Converts the
    bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`.
    Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method.
ÚformatÚdo_convert_annotations© N)
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ústrr   Ú__annotations__ÚboolÚ__static_attributes__r&   ó    Úk/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/models/detr/image_processing_detr.pyr"   r"   <   s   ‡ ñð Ð"Ñ"Ó"Ø Ö r0   r"   F)Útotalc                 ó*  • SSK Jn  U" U 5      (       a  U R                  5       n U R                  5       n[        R
                  " S/US//5      n[        R                  " USS USS :g  5      S   S-   nUSSS2==   USSS2   -  ss'   [        U5      $ )a¡  
Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format.

Args:
    mask (`torch.Tensor` or `numpy.array`):
        A binary mask tensor of shape `(height, width)` where 0 denotes background and 1 denotes the target
        segment_id or class_id.
Returns:
    `List`: Run-length encoded list of the binary mask. Refer to COCO API for more information about the RLE
    format.
r   )Úis_torch_tensorr   é   Néÿÿÿÿé   )Úutilsr4   ÚnumpyÚflattenÚnpÚconcatenateÚwhereÚlist)Úmaskr4   ÚpixelsÚrunss       r1   Úbinary_mask_to_rlerB   J   s�   € õ )á�t×ÑØ�z‰z‹|ˆà�\‰\‹^€FÜ�^Š^˜a˜S &¨1¨#Ð.Ó/€FÜ�8Š8�F˜1˜2�J &¨¨" +Ñ-Ó.¨qÑ1°AÑ5€DØˆˆˆAˆƒJ�$‘s˜�s‘)ÑƒJÜ�‹:Ðr0   c                 ó´   • [         R                  " U 5      n/ nU H9  n[         R                  " X:H  SS5      n[        U5      nUR	                  U5        M;     U$ )av  
Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format.

Args:
    segmentation (`torch.Tensor` or `numpy.array`):
        A segmentation map of shape `(height, width)` where each value denotes a segment or class id.
Returns:
    `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id.
r5   r   )ÚtorchÚuniquer=   rB   Úappend)ÚsegmentationÚsegment_idsÚrun_length_encodingsÚidxr?   Úrles         r1   Úconvert_segmentation_to_rlerL   b   sW   € ô —,’,˜|Ó,€KàÐÛˆÜ�{Š{˜<Ñ.°°1Ó5ˆÜ  Ó&ˆØ×#Ñ# CÖ(ñ ð
  Ðr0   c                 óÆ   • U R                   S   UR                   S   s=:X  a  UR                   S   :X  d  O  [        S5      eUR                  U5      X:„  -  nX   X   X%   4$ )aÅ  
Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and
`labels`.

Args:
    masks (`torch.Tensor`):
        A tensor of shape `(num_queries, height, width)`.
    scores (`torch.Tensor`):
        A tensor of shape `(num_queries)`.
    labels (`torch.Tensor`):
        A tensor of shape `(num_queries)`.
    object_mask_threshold (`float`):
        A number between 0 and 1 used to binarize the masks.
Raises:
    `ValueError`: Raised when the first dimension doesn't match in all input tensors.
Returns:
    `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region
    < `object_mask_threshold`.
r   z1mask, scores and labels must have the same shape!)ÚshapeÚ
ValueErrorÚne)ÚmasksÚscoresÚlabelsÚobject_mask_thresholdÚ
num_labelsÚto_keeps         r1   Úremove_low_and_no_objectsrW   w   s^   € ð( �K‰K˜‰N˜fŸl™l¨1™oÕ@°·±¸a±Õ@ÜÐLÓMÐMà�i‰i˜
Ó# vÑ'EÑF€Gà‰>˜6™?¨F©OÐ;Ð;r0   c                 ó¾   • X:H  nUR                  5       nX   U:¬  R                  5       nUS:„  =(       a    US:„  nU(       a  Xg-  n	U	R                  5       U:”  d  SnX…4$ )Nr   F)ÚsumÚitem)
Úmask_labelsÚ
mask_probsÚkÚmask_thresholdÚoverlap_mask_area_thresholdÚmask_kÚmask_k_areaÚoriginal_areaÚmask_existsÚ
area_ratios
             r1   Úcheck_segment_validityre   “   sj   € àÑ€FØ—*‘*“,€Kð  ‘] nÑ4×9Ñ9Ó;€MØ ‘/×7 m°aÑ&7€Kö Ø Ñ0ˆ
Ø�‰Ó Ð#>Ó>ØˆKàÐÐr0   r^   r_   Úlabel_ids_to_fuseÚtarget_sizec                 óÄ  • Uc  U R                   S   OUS   nUc  U R                   S   OUS   n[        R                  " Xx4[        R                  U R                  S9n	/ n
Ub2  [
        R                  R                  U R                  S5      USSS9S   n SnXR                  SSS5      -  n U R                  S5      n0 n[        UR                   S   5       H„  nX.   R                  5       nXõ;   n[        XÀXãU5      u  nnU(       d  M2  Xý;   a  Xß   nOUS-  nX¹U'   [        X   R                  5       S	5      nU
R                  UUUUS
.5        U(       d  M€  X½U'   M†     Xš4$ )Nr5   r   r7   ©ÚdtypeÚdeviceÚbilinearF©ÚsizeÚmodeÚalign_cornersr6   é   )ÚidÚlabel_idÚ	was_fusedÚscore)rN   rD   ÚzerosÚint32rk   r   r   ÚinterpolateÚ	unsqueezeÚviewÚargmaxÚrangerZ   re   ÚroundrF   )r\   Úpred_scoresÚpred_labelsr^   r_   rf   rg   ÚheightÚwidthrG   ÚsegmentsÚcurrent_segment_idr[   Ústuff_memory_listr]   Ú
pred_classÚshould_fuserc   r`   Úsegment_scores                       r1   Úcompute_segmentsrˆ   ¥   s–  € ð %0Ñ$7ˆZ×Ñ˜aÒ ¸[È¹^€FØ#.Ñ#6ˆJ×Ñ˜QÒ¸KÈ¹N€Eä—;’; ˜´e·k±kÈ*×J[ÑJ[Ñ\€LØ€HàÑÜ—]‘]×.Ñ.Ø× Ñ  Ó#¨+¸JÐV[ð /ð 
à
ñˆ
ð Ðð ×"Ñ" 2 q¨!Ó,Ñ,€JØ×#Ñ# AÓ&€Kð )+ÐÜ�;×$Ñ$ QÑ'Ö(ˆØ ‘^×(Ñ(Ó*ˆ
Ø Ñ5ˆô 5Ø QÐ8Só
Ñˆ�V÷ ˆ;ØÓ.Ø%6Ñ%BÑ"à" aÑ'Ð"ð $6˜Ñ Ü! +¡.×"5Ñ"5Ó"7¸Ó;ˆMØ�O‰Oà,Ø *Ø!,Ø*ñ	ô÷ ˆ{Ø0B *Ó-ñ7 )ð: Ð!Ð!r0   r€   r�   rk   Úreturnc                 óú  •  SSK Jn  / nU  HŽ  nUR                  XaU5      nUR	                  U5      n[        UR                  5      S:  a  US   n[        R                  " U[        R                  US9n[        R                  " USS9nUR                  U5        M�     U(       a  [        R                  " USS9nU$ [        R                  " SX4[        R                  US9nU$ ! [         a    [        S5      ef = f)	a  
Convert a COCO polygon annotation to a mask.

Args:
    segmentations (`list[list[float]]`):
        List of polygons, each polygon represented by a list of x-y coordinates.
    height (`int`):
        Height of the mask.
    width (`int`):
        Width of the mask.
r   )r?   z1Pycocotools is not installed in your environment.r   ).Nri   r7   )Úaxis)Úpycocotoolsr?   ÚImportErrorÚfrPyObjectsÚdecodeÚlenrN   rD   Ú	as_tensorÚuint8ÚanyrF   Ústackrv   )	Úsegmentationsr€   r�   rk   Ú	coco_maskrQ   ÚpolygonsÚrlesr?   s	            r1   Úconvert_coco_poly_to_maskr™   â   sæ   € ðOÝ1ð €EÛ!ˆØ×$Ñ$ X°uÓ=ˆØ×Ñ Ó%ˆÜˆt�z‰z‹?˜QÓØ˜	‘?ˆDÜ�Š˜t¬5¯;©;¸vÑFˆÜ�yŠy˜ AÑ&ˆØ�‰�TÖñ "ö Ü—’˜E¨Ñ*ˆð €Lô —’˜Q Ð.´e·k±kÈ&ÑQˆà€Løô# ó OÜÐMÓNÐNðOús   ‚C$ Ã$C:Úreturn_segmentation_masksÚinput_data_formatc                 ó$  • U R                  5       SS u  pEUS   n[        R                  " U/[        R                  U R                  S9nUS   n/ n/ n	/ n
/ nU Hl  nSU;  d  US   S:X  d  M  UR                  US   5        U	R                  US	   5        U
R                  US
   5        SU;   d  MX  UR                  US   5        Mn     [        R                  " U[        R                  U R                  S9n[        R                  " U	[        R                  U R                  S9n	[        R                  " U[        R                  U R                  S9n[        R                  " U
[        R                  U R                  S9R                  SS5      n
U
SS2SS24==   U
SS2SS24   -  ss'   U
SS2SSS24   R                  SUS9U
SS2SSS24'   U
SS2SSS24   R                  SUS9U
SS2SSS24'   U
SS2S4   U
SS2S4   :„  U
SS2S4   U
SS2S4   :„  -  nUXŽ   X®   Xž   XÞ   [        R                  " [        U5      [        U5      /[        R                  U R                  S9S.nU(       a_  [        R                  " U[        R                  U R                  S9nX¾   nUR                  S   nU(       a  UR                  S5      OUnX¿S'   U(       a1  U Vs/ s H  oÌS   PM	     nn[        UXEU R                  S9nUU   US'   U$ s  snf )zE
Convert the target in COCO format into the format expected by DETR.
éþÿÿÿNÚimage_idri   ÚannotationsÚiscrowdr   Úcategory_idÚareaÚbboxÚ	keypointsr6   é   r7   )ÚminÚmaxr5   r   )rž   Úclass_labelsÚboxesr¢   r    Ú	orig_size)r6   r   rG   ©rk   rQ   )rn   rD   r‘   Úint64rk   rF   Úfloat32Ú
zeros_likeÚreshapeÚclipÚintrN   r™   )ÚimageÚtargetrš   r›   Úimage_heightÚimage_widthrž   rŸ   Úclassesr¢   r©   r¤   Úobjr    ÚkeepÚ
new_targetÚnum_keypointsÚsegmentation_masksrQ   s                      r1   Ú!prepare_coco_detection_annotationr¼     sá  € ð !&§
¡
£¨R¨SÐ 1Ñ€Là�jÑ!€HÜ�Š ˜z´·±ÀUÇ\Á\ÑR€Hð ˜Ñ'€KØ€GØ€DØ€EØ€IÛˆØ˜CÓ 3 y¡>°QÕ#6Ø�N‰N˜3˜}Ñ-Ô.Ø�K‰K˜˜F™Ô$Ø�L‰L˜˜V™Ô%Ø˜cÕ!Ø× Ñ   [Ñ!1Ö2ñ ô �oŠo˜g¬U¯[©[ÀÇÁÑN€GÜ�?Š?˜4¤u§}¡}¸U¿\¹\ÑJ€DÜ×Ò˜w¬e¯k©kÀ%Ç,Á,ÑO€Gä�OŠO˜E¬¯©¸u¿|¹|ÑL×TÑTÐUWÐYZÓ[€EØ	Š!ˆQ‰Rˆ%ƒL�Eš!˜R˜a˜R˜%‘LÑ ƒLØš1˜a˜d ˜d˜7‘^×(Ñ(¨Q°KÐ(Ð@€EŠ!ˆQˆT�ˆTˆ'�NØš1˜a˜d ˜d˜7‘^×(Ñ(¨Q°LÐ(ÐA€EŠ!ˆQˆT�ˆTˆ'�Nà’!�Q�$‰K˜%¢ 1 ™+Ñ%¨%²°1°©+¸ºaÀ¸d¹Ñ*CÑD€Dð Ø™Ø‘Ø‘
Ø‘=Ü—_’_¤c¨,Ó&7¼¸[Ó9IÐ%JÔRW×R]ÑR]Ðfk×frÑfrÑsñ€Jö Ü—O’O I´U·]±]È5Ï<É<ÑXˆ	à‘Oˆ	Ø!Ÿ™¨Ñ*ˆÞ2?�I×%Ñ% gÔ.ÀYˆ	Ø"+�;Ñæ Ù=HÓIº[°c .Ô1¹[ÐÐIÜ)Ð*<¸lÐ`e×`lÑ`lÑmˆØ# D™kˆ
�7ÑàÐùò	 Js   ËLrQ   c           	      óš  • U R                  5       S:X  a  [        R                  " SU R                  S9$ U R                  SS u  p[        R
                  " SU[        R                  U R                  S9n[        R
                  " SU[        R                  U R                  S9n[        R                  " X4SS9u  p4U [        R                  " US5      -  nUR                  UR                  S   S	5      R                  S	5      S   n[        R                  " XR                  S5      [        R                  " S
U R                  S95      R                  U R                  S   S	5      R                  S	5      S   nU [        R                  " US5      -  nUR                  UR                  S   S	5      R                  S	5      S   n	[        R                  " XR                  S5      [        R                  " S
U R                  S95      R                  U R                  S   S	5      R                  S	5      S   n
[        R                  " XzXi/S5      $ )zÿ
Compute the bounding boxes around the provided panoptic segmentation masks.

Args:
    masks: masks in format `[number_masks, height, width]` where N is the number of masks

Returns:
    boxes: bounding boxes in format `[number_masks, 4]` in xyxy format
r   )r   r¥   r«   r�   Nri   Úij)Úindexingr6   g    „×—Ar5   )ÚnumelrD   rv   rk   rN   Úaranger­   Úmeshgridry   rz   r§   r=   Útensorr¦   r”   )rQ   ÚhÚwÚyÚxÚx_maskÚx_maxÚx_minÚy_maskÚy_maxÚy_mins              r1   Úmasks_to_boxesrÎ   E  sÅ  € ð ‡{�{ƒ}˜ÓÜ�{Š{˜6¨%¯,©,Ñ7Ð7à�;‰;�r�sÐ�D€AÜ�Š�Q˜¤§¡°u·|±|ÑD€AÜ�Š�Q˜¤§¡°u·|±|ÑD€Aä�>Š>˜!¨Ñ.�D€Aà”U—_’_ Q¨Ó*Ñ*€FØ�K‰K˜Ÿ™ Q™¨Ó,×0Ñ0°Ó4°QÑ7€Eä�Š�EŸ;™; q›>¬5¯<ª<¸ÀEÇLÁLÑ+QÓR×WÑWÐX]×XcÑXcÐdeÑXfÐhjÓk×oÑoÐprÓsÐtuÑvð 
ð ”U—_’_ Q¨Ó*Ñ*€FØ�K‰K˜Ÿ™ Q™¨Ó,×0Ñ0°Ó4°QÑ7€Eä�Š�EŸ;™; q›>¬5¯<ª<¸ÀEÇLÁLÑ+QÓR×WÑWÐX]×XcÑXcÐdeÑXfÐhjÓk×oÑoÐprÓsÐtuÑvð 
ô �;Š;˜ eÐ3°QÓ7Ð7r0   c                 ó„  • [        U [        R                  5      (       a‚  [        U R                  5      S:X  ai  U R
                  [        R                  :X  a  U R                  [        R                  5      n U SS2SS2S4   SU SS2SS2S4   -  -   SU SS2SS2S4   -  -   $ [        U S   SU S   -  -   SU S   -  -   5      $ )z"
Converts RGB color to unique ID.
r   Nr   é   r5   i   r7   )
Ú
isinstancerD   ÚTensorr�   rN   rj   r’   Útorw   r±   )Úcolors    r1   Ú	rgb_to_idrÕ   j  s©   € ô �%œŸ™×&Ñ&¬3¨u¯{©{Ó+;¸qÓ+@Ø�;‰;œ%Ÿ+™+Ó%Ø—H‘HœUŸ[™[Ó)ˆEØ’Qš˜1�W‰~  eªAªq°!¨G¡nÑ 4Ñ4°yÀ5ÊÊAÈqÈÁ>Ñ7QÑQÐQÜˆu�Q‰x˜#  a¡™.Ñ(¨9°u¸Q±xÑ+?Ñ?Ó@Ð@r0   r²   r³   Ú
masks_pathÚreturn_masksc                 ó  • [        XS9u  pV[        R                  " U5      US   -  n0 n[        R                  " SU;   a  US   OUS   /[        R
                  U R                  S9US'   [        R                  " XV/[        R
                  U R                  S9US'   [        R                  " XV/[        R
                  U R                  S9US'   SU;   Ga›  [        U5      R                  S	S
S5      R                  [        R                  U R                  S9n	[        U	5      n	[        R                  " US    V
s/ s H  oªS   PM	     sn
U R                  S9nX›SS2SS4   :H  n	U	R                  [        R                  5      n	U(       a  X˜S'   [        U	5      US'   [        R                  " US    V
s/ s H  oªS   PM	     sn
[        R
                  U R                  S9US'   [        R                  " US    V
s/ s H  oªS   PM	     sn
[        R
                  U R                  S9US'   [        R                  " US    V
s/ s H  oªS   PM	     sn
[        R                  U R                  S9US'   U$ s  sn
f s  sn
f s  sn
f s  sn
f )z.
Prepare a coco panoptic annotation for DETR.
)Úchannel_dimÚ	file_namerž   rr   ri   rn   rª   Úsegments_infor5   r7   r   r«   NrQ   r©   r¡   r¨   r    r¢   )r   ÚpathlibÚPathrD   r‘   r¬   rk   r   ÚpermuterÓ   rw   rÕ   r.   rÎ   r­   )r²   r³   rÖ   r×   r›   r´   rµ   Úannotation_pathr¹   rQ   Úsegment_infoÚidss               r1   Ú prepare_coco_panoptic_annotationrâ   u  sG  € ô !/¨uÑ TÑ€LÜ—l’l :Ó.°¸Ñ1DÑD€Oà€JÜ"Ÿ_š_Ø)¨VÓ3ˆ�
Ò	¸À¹ÐFÌeÏkÉkÐbg×bnÑbnñ€JˆzÑô Ÿš¨,Ð)DÌEÏKÉKÐ`e×`lÑ`lÑm€JˆvÑÜ#Ÿošo¨|Ð.IÔQV×Q\ÑQ\Ðej×eqÑeqÑr€Jˆ{Ñà˜&Ô Ü˜?Ó+×3Ñ3°A°q¸!Ó<×?Ñ?ÄeÇkÁkÐZ_×ZfÑZfÐ?ÐgˆÜ˜%Ó ˆä�oŠoÀfÈ_ÒF]Ó^ÒF]°l¨DÔ1ÑF]Ñ^Ðgl×gsÑgsÑtˆØšQ  d˜]Ñ+Ñ+ˆØ—‘œŸ™Ó$ˆÞØ"'�wÑÜ,¨UÓ3ˆ
�7ÑÜ%*§_¢_Ø=CÀOÒ=TÓUÒ=T¨\˜-Ô(Ñ=TÑUÜ—+‘+Ø—<‘<ñ&
ˆ
�>Ñ"ô
 !&§¢Ø9?ÀÒ9PÓQÒ9P¨˜)Ô$Ñ9PÑQÜ—+‘+Ø—<‘<ñ!
ˆ
�9Ñô
 #Ÿ_š_Ø6<¸_Ò6MÓNÒ6M l˜&Ô!Ñ6MÑNÜ—-‘-Ø—<‘<ñ
ˆ
�6Ñð Ðùò- _ùò Vùò
 Rùò
 Os   Ä9I2Æ2I7Ç9I<É Jc            $       ó¸  ^ • \ rS rSr\r\R                  r\	r
\r\R                  rSrSrSrSrSSS.rSrSS/rS	\\   S
S4U 4S jjr    SAS\R4                  S\S\S-  S\S-  S\\R>                  -  S-  S\\ -  S-  S
\4S jjr! SBS\R4                  S\"S\#S   S
\R4                  4U 4S jjjr$S\RJ                  4S\\\&4   S\'\(\(4   S\'\(\(4   S\)S\#S   4
U 4S jjjr*S\S\'\(\(4   S
\4S jr+S\S \'\(\(4   S!\'\(\(4   S
\4S" jr,   SCS\R4                  S#\'\(\(4   S\\\&4   S-  S$\S%\(4
S& jjr-\.   SDS'\/S(\0\1\0   -  S-  S\S-  S\\R>                  -  S-  S	\\   S
\24U 4S) jjj5       r3S'\1S*   S(\0\1\0   -  S-  S\S\\R>                  -  S-  S+\S\"SS,S-\S.\)S/\S0\S1\)\1\)   -  S-  S2\)\1\)   -  S-  S3\S4\"S-  S\\-  S-  S5\\4-  S-  S
\24$S6 jr5 SES\)S7\4\1\'   -  S-  4S8 jjr6SBS7\1\'\(\(4      S-  4S9 jjr7     SFS\)S:\)S;\)S7\1\'\(\(4      S-  S<\S-  S
\1\   4S= jjr8     SGS\)S:\)S;\)S>\9\(   S-  S7\1\'\(\(4      S-  S
\1\   4S? jjr:S@r;U =r<$ )HÚDetrImageProcessori¦  Té   é5  ©Úshortest_edgeÚlongest_edgeFÚpixel_valuesÚ
pixel_maskÚkwargsr‰   Nc                 ó�  >• UR                  SUR                  SU R                  5      5        UR                  SS 5      nUc  S OUR                  SS5      nUb  UOSSS.n[        X#SS	9US'   UR	                  S
5      nUR	                  S5      nUc$  [        U S
S 5      c  Ub  UOU R                  U l        [        TU ]$  " S0 UD6  g )NÚdo_padÚpad_and_return_pixel_maskrn   Úmax_sizeræ   rå   rç   F)rð   Údefault_to_squarer%   Údo_normalizer&   )
Ú
setdefaultÚpoprî   r   ÚgetÚgetattrrò   r%   ÚsuperÚ__init__)Úselfrì   rn   rð   r%   rò   Ú	__class__s         €r1   rø   ÚDetrImageProcessor.__init__µ  sÈ   ø€ Ø×Ñ˜( F§J¡JÐ/JÈDÏKÉKÓ$XÔYà�z‰z˜& $Ó'ˆØ™<‘4¨V¯Z©Z¸
ÀDÓ-IˆØÑ'‰t¸sÐTXÑ-Yˆä& tÐRWÑXˆˆv‰ð "(§¡Ð,DÓ!EÐØ—z‘z .Ó1ˆØ!Ñ)¬g°dÐ<TÐVZÓ.[Ñ.cØ:FÑ:R©,ÐX\×XiÑXiˆDÔ'ä‰ÒÑ"˜6Ó"r0   r²   r³   r$   rš   rÖ   r›   c                 óä   • Ub  UOU R                   nU[        R                  :X  a  Uc  SOUn[        XXFS9nU$ U[        R                  :X  a  Uc  SOUn[        UUUUUS9nU$ [        SU S35      e)z4
Prepare an annotation for feeding into DETR model.
F)r›   T)rÖ   r×   r›   zFormat z is not supported.)r$   r   ÚCOCO_DETECTIONr¼   ÚCOCO_PANOPTICrâ   rO   )rù   r²   r³   r$   rš   rÖ   r›   s          r1   Úprepare_annotationÚ%DetrImageProcessor.prepare_annotationÆ  sž   € ð "Ñ-‘°4·;±;ˆàÔ%×4Ñ4Ó4Ø1JÑ1R©ÐXqÐ%Ü6ØÐ8ñˆFð ˆð Ô'×5Ñ5Ó5Ø0IÑ0Q©ÐWpÐ%Ü5ØØØ%Ø6Ø"3ñˆFð ˆô ˜w v hÐ.@ÐAÓBÐBr0   rn   Úresamplez0PILImageResampling | tvF.InterpolationMode | intc                 ó$  >• UR                   (       a@  UR                  (       a/  [        UR                  SS UR                   UR                  5      nO›UR                  (       a@  UR
                  (       a/  [        UR                  SS UR                  UR
                  5      nOJUR                  (       a*  UR                  (       a  UR                  UR                  4nO[        SU S35      e[        TU ],  " U4[        US   US   S9US.UD6nU$ )	aË  
Resize the image to the given size. Size can be `min_size` (scalar) or `(height, width)` tuple. If size is an
int, smaller edge of the image will be matched to this number.

Args:
    image (`torch.Tensor`):
        Image to resize.
    size (`SizeDict`):
        Size of the image's `(height, width)` dimensions after resizing. Available options are:
            - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`.
                Do NOT keep the aspect ratio.
            - `{"shortest_edge": int, "longest_edge": int}`: The image will be resized to a maximum size respecting
                the aspect ratio and keeping the shortest edge less or equal to `shortest_edge` and the longest edge
                less or equal to `longest_edge`.
            - `{"max_height": int, "max_width": int}`: The image will be resized to the maximum size respecting the
                aspect ratio and keeping the height less or equal to `max_height` and the width less or equal to
                `max_width`.
    resample (`PILImageResampling | tvF.InterpolationMode | int`, *optional*, defaults to `PILImageResampling.BILINEAR`):
        Resampling filter to use if resizing the image.
r�   Nz\Size must contain 'height' and 'width' keys or 'shortest_edge' and 'longest_edge' keys. Got Ú.r   r5   ©r€   r�   ©rn   r  )rè   ré   r   rN   Ú
max_heightÚ	max_widthr   r€   r�   rO   r÷   Úresizer   )rù   r²   rn   r  rì   Únew_sizerú   s         €r1   r  ÚDetrImageProcessor.resizeæ  sæ   ø€ ð6 ×× $×"3×"3ô 2°%·+±+¸b¸cÐ2BÀD×DVÑDVÐX\×XiÑXiÓj‰HØ�_�_ §§Ü:¸5¿;¹;ÀrÀsÐ;KÈTÏ_É_Ð^b×^lÑ^lÓm‰HØ�[�[˜TŸZŸZØŸ™ T§Z¡ZÐ0‰HäØnÐosÐntÐtuÐvóð ô ‘’Øð
Ü ¨°©¸8ÀA¹;ÑGÐRZñ
Ø^dñ
ˆð ˆr0   ç      à?Ú
annotationrª   rg   Ú	thresholdc                 ó~  >• [        X25       VVs/ s H	  u  pgXg-  PM     snnu  p‰0 n
X:S'   UR                  5        Hò  u  p¼US:X  a;  UnU[        R                  " X˜X˜/[        R                  UR
                  S9-  nXêS'   MF  US:X  a  UnXùU-  -  nUU
S'   M\  US:X  a€  USS2S4   nU Vs/ s H$  n[        [        U ]#  U[        US   US   S	9US
9PM&     nn[        R                  " U5      R                  [        R                  5      nUSS2S4   U:„  nUU
S'   Mâ  US:X  a  X:S'   Mî  XÊU'   Mô     U
$ s  snnf s  snf )ak  
Resizes an annotation to a target size.

Args:
    annotation (`dict[str, Any]`):
        The annotation dictionary.
    orig_size (`tuple[int, int]`):
        The original size of the input image.
    target_size (`tuple[int, int]`):
        The target size of the image, as returned by the preprocessing `resize` step.
    threshold (`float`, *optional*, defaults to 0.5):
        The threshold used to binarize the segmentation masks.
    resample (`PILImageResampling | tvF.InterpolationMode | int`, defaults to `tvF.InterpolationMode.NEAREST_EXACT`):
        The resampling filter to use when resizing the masks.
rn   r©   ri   r¢   rQ   Nr   r5   r  r  )ÚzipÚitemsrD   r‘   r­   rk   r÷   rä   r  r   r”   rÓ   )rù   r  rª   rg   r  r  r³   ÚorigÚratio_heightÚratio_widthÚnew_annotationÚkeyÚvaluer©   Úscaled_boxesr¢   Úscaled_arearQ   r?   rú   s                      €r1   Úresize_annotationÚ$DetrImageProcessor.resize_annotation  s}  ø€ ô. HKÈ;ÔGbÔ$cÒGb±|°v V¤]ÑGbÒ$cÑ!ˆàˆØ!,�vÑà$×*Ñ*Ö,‰JˆCØ�g‹~Ø�Ø$¤u§¢Ø °ÐJÔRW×R_ÑR_Ðhm×htÑhtñ(ñ  �ð +7˜wÓ'Ø˜“Ø�Ø"°LÑ&@ÑA�Ø)4�˜vÓ&Ø˜“Øša ˜g™�ñ
 !&ó	ò !&˜ô Ô,¨dÑ:Ø¤8°;¸q±>ÈÐUVÉÑ#XÐckð ;ó ñ !&ð	 ð ô Ÿš EÓ*×-Ñ-¬e¯m©mÓ<�Øša ˜d™ iÑ/�Ø*/�˜wÓ'Ø˜“Ø)4˜vÓ&à&+˜sÓ#ñ3 -ð6 ÐùóA %dùò$s   �D4Â++D:Ú
image_sizec                 óä   • Uu  p40 nUR                  5        HU  u  pgUS:X  aF  Un[        U5      nU[        R                  " XCXC/[        R                  UR
                  S9-  nX…U'   MQ  XuU'   MW     U$ )Nr©   ri   )r  r   rD   r‘   r­   rk   )	rù   r  r  r´   rµ   Únorm_annotationr  r  r©   s	            r1   Únormalize_annotationÚ'DetrImageProcessor.normalize_annotationL  sƒ   € Ø$.Ñ!ˆØˆØ$×*Ñ*Ö,‰JˆCØ�g‹~Ø�Ü0°Ó7�ØœŸšØ °ÐJÔRW×R_ÑR_Ðhm×htÑhtññ �ð (- Ó$à', Ó$ñ -ð Ðr0   Úinput_image_sizeÚoutput_image_sizec                 ó^  • 0 nX6S'   S [        X25       5       u  pxUR                  5        H~  u  pšU	S:X  a*  U
n[        R                  " UUSS9n[	        US5      nX¶S'   M5  U	S:X  a3  U(       a,  U
nU[
        R                  " X‡X‡/UR                  S9-  nXÆS'   Mn  U	S:X  a  X6S'   Mz  X¦U	'   M€     U$ )	z+
Update the annotation for a padded image.
rn   c              3   ó.   #   • U  H  u  pX!-  v •  M     g 7f©Nr&   )Ú.0ÚoutputÚinputs      r1   Ú	<genexpr>ÚIDetrImageProcessor._update_annotation_for_padded_image.<locals>.<genexpr>h  s   é € Ð$rÒIq¹¸ U¦^ÒIqùs   ‚rQ   r   ©Úfillr5   r©   r«   )r  r  ÚtvFÚpadr   rD   r‘   rk   )rù   r  r   r!  ÚpaddingÚupdate_bboxesr  r  r  r  r  rQ   r©   s                r1   Ú#_update_annotation_for_padded_imageÚ6DetrImageProcessor._update_annotation_for_padded_image[  sÈ   € ð ˆØ!2�vÑÙ$rÌÐM^ÔIqÓ$rÑ!ˆà$×*Ñ*Ö,‰JˆCØ�g‹~Ø�ÜŸšØØØñ�ô
 % U¨AÓ.�Ø*/˜wÓ'Ø˜“¦MØ�ØœŸš¨+À[Ð)_Ðhm×htÑhtÑuÑu�Ø*/˜wÓ'Ø˜“Ø):˜vÓ&à&+˜sÓ#ñ# -ð$ Ðr0   Úpadded_sizer/  r+  c                 ó€  • UR                  5       SS  nUS   US   -
  nUS   US   -
  nUS:  d  US:  a  [        SU SU S35      eXb:w  a0  SSX‡/n	[        R                  " XUS9nUb  U R	                  X6X)U5      n[
        R                  " U[
        R                  UR                  S9n
SU
S US   2S US   24'   XU4$ )	Nr�   r   r5   zzPadding dimensions are negative. Please make sure that the padded size is larger than the original size. Got padded size: z, original size: r  r*  ri   )	rn   rO   r,  r-  r0  rD   rv   r¬   rk   )rù   r²   r2  r  r/  r+  Úoriginal_sizeÚpadding_bottomÚpadding_rightr.  rë   s              r1   r-  ÚDetrImageProcessor.pad~  s  € ð Ÿ
™
› R SÐ)ˆØ$ Q™¨-¸Ñ*:Ñ:ˆØ# A™¨°qÑ)9Ñ9ˆØ˜AÓ °Ó!2Üð3Ø3>°-Ð?PÐQ^ÐP_Ð_`ðbóð ð Ó'Ø˜!˜]Ð;ˆGÜ—G’G˜E°Ñ6ˆEØÑ%Ø!×EÑEØ¨{À]ó�
ô
 —[’[ ´E·K±KÈÏÉÑUˆ
Ø=>ˆ
Ð%�] 1Ñ%Ð%Ð'9¨°qÑ)9Ð'9Ð9Ñ:à *Ð,Ð,r0   ÚimagesrŸ   c                 ó(   >• [         TU ]  " XX440 UD6$ )a“  
annotations (`AnnotationType` or `list[AnnotationType]`, *optional*):
    Annotations to transform according to the padding that is applied to the images.
return_segmentation_masks (`bool`, *optional*, defaults to `self.return_segmentation_masks`):
    Whether to return segmentation masks.
masks_path (`str` or `pathlib.Path`, *optional*):
    Path to the directory containing the segmentation masks.
)r÷   Ú
preprocess)rù   r8  rŸ   rš   rÖ   rì   rú   s         €r1   r:  ÚDetrImageProcessor.preprocessœ  s   ø€ ô" ‰wÒ! &Ð7PÑgÐ`fÑgÐgr0   ztorch.TensorÚ	do_resizez7PILImageResampling | tvF.InterpolationMode | int | NoneÚ
do_rescaleÚrescale_factorrò   r%   Ú
image_meanÚ	image_stdrî   Úpad_sizeÚreturn_tensorsc           
      ó  • Ub  [        U[        5      (       a  U/nUb<  [        U5      [        U5      :w  a$  [        S[        U5       S[        U5       S35      e[	        U5      nUb  [        U[        U5        UbQ  U[        R                  :X  a=  [        U[        R                  [        45      (       d  [        S[        U5       S35      e0 n/ n/ n/ n[        Xb  UOS/[        U5      -  5       Hä  u  nnUb"  U R                  UUUUU[        R                  S9nU(       aH  U R!                  UXgS9nUb3  U R#                  UUR%                  5       S	S UR%                  5       S	S S
9nUnU R'                  UX‰X¬U5      nU(       a-  Ub*  U R)                  U[+        U[        R                  5      5      nUR-                  U5        UR-                  U5        Mæ     UnUb  UOSnU(       GaB  Ub  UR.                  UR0                  4nO[3        U5      n/ n/ n[        Xb  UOS/[        U5      -  5       HÇ  u  nnUUR%                  5       S	S :X  aa  UR-                  U5        UR-                  [4        R6                  " U[4        R8                  UR:                  S95        UR-                  U5        M~  U R=                  UUUUS9u  nnnUR-                  U5        UR-                  U5        UR-                  U5        MÉ     UnUb  UOSnUR?                  S[4        R@                  " USS905        UR?                  S[4        R@                  " USS905        [C        UUS9nUb  U Vs/ s H  n[C        UUS9PM     snUS'   U$ s  snf )zO
Preprocess an image or a batch of images so that it can be used by the model.
NzThe number of images (z) and annotations (z) do not match.zxThe path to the directory containing the mask PNG files should be provided as a `pathlib.Path` or string object, but is z	 instead.)rš   rÖ   r›   r  r�   )rª   rg   ri   )r  r/  rë   r   ©Údimrê   )Útensor_typerS   )"rÑ   Údictr�   rO   r   r   ÚSUPPORTED_ANNOTATION_FORMATSrþ   rÜ   rÝ   r,   Útyper  rÿ   r   ÚFIRSTr  r  rn   Úrescale_and_normalizer  r   rF   r€   r�   r   rD   Úonesr¬   rk   r-  Úupdater”   r
   )rù   r8  rŸ   rš   rÖ   r<  rn   r  r=  r>  rò   r%   r?  r@  rî   rA  r$   rB  rì   ÚdataÚprocessed_imagesÚprocessed_annotationsÚpixel_masksr²   r  Úresized_imager2  Úpadded_imagesÚpadded_annotationsrë   Úencoded_inputss                                  r1   Ú_preprocessÚDetrImageProcessor._preprocess¯  s­  € ð0 Ñ"¤z°+¼t×'DÑ'DØ&˜-ˆKàÑ"¤s¨6£{´c¸+Ó6FÓ'FÜØ(¬¨V«¨Ð5HÌÈ[ÓIYÐHZÐZiÐjóð ô " &Ó)ˆØÑ"Ü  Ô)EÀ{ÔSð Ñ"ØÔ*×8Ñ8Ó8Ü˜z¬G¯L©L¼#Ð+>×?Ñ?äð<Ü<@ÀÓ<LÐ;MÈYðXóð ð
 ˆàÐØ "ÐØˆÜ!$ VÑ<S©[ÐZ^ÐY_ÔbeÐflÓbmÑYmÖ!nÑˆE�:àÑ&Ø!×4Ñ4ØØØØ.GØ)Ü&6×&<Ñ&<ð 5ð �
ö Ø $§¡¨E¸ Ð P�ØÑ*Ø!%×!7Ñ!7Ø"Ø"'§*¡*£,¨r¨sÐ"3Ø$1×$6Ñ$6Ó$8¸¸Ð$=ð "8ð "�Jð
 &�à×.Ñ.¨u°jÐR^ÐluÓvˆEÞ%¨+Ñ*AØ!×6Ñ6°zÄ>ÐRWÔYi×YoÑYoÓCpÓq�
à×#Ñ# EÔ*Ø!×(Ñ(¨Ö4ñ7 "oð8 "ˆØ/:Ñ/FÑ+ÈDˆçàÑ#Ø'Ÿ™°·±Ð?‘ä2°6Ó:�àˆMØ!#ÐÜ%(¨Ñ@W±Ð^bÐ]cÔfiÐjpÓfqÑ]qÖ%rÑ!��zà %§*¡*£,¨r¨sÐ"3Ó3Ø!×(Ñ(¨Ô/Ø×&Ñ&¤u§z¢z°+ÄUÇ[Á[ÐY^×YeÑYeÑ'fÔgØ&×-Ñ-¨jÔ9ÙØ04·±Ø˜;°:ÐMcð 19ð 1Ñ-��z :ð ×$Ñ$ UÔ+Ø"×)Ñ)¨*Ô5Ø×"Ñ" :Ö.ñ &sð #ˆFØ0;Ñ0GÑ,ÈTˆKØ�K‰K˜¤u§{¢{°;ÀAÑ'FÐGÔHà�‰�^¤U§[¢[°¸QÑ%?Ð@ÔAÜ% d¸ÑGˆØÑ"áWbó(ÚWbÈ”˜Z°^ÔDÑWbñ(ˆN˜8Ñ$ð Ðùò(s   Í%NÚtarget_sizesc                 ó  • UR                   UR                  pTUb#  [        U5      [        U5      :w  a  [        S5      e[        R
                  R                  US5      nUSSS24   R                  S5      u  px[        U5      n	Ub¹  [        U[        5      (       aS  [        R                  " U V
s/ s H  oªS   PM	     sn
5      n[        R                  " U V
s/ s H  oªS   PM	     sn
5      nOUR                  S5      u  p¼[        R                  " XËXË/SS9R                  U	R                   5      nX�SS2SSS24   -  n	/ n[#        XxU	5       H1  u  nnnXÿU:„     nUXò:„     nUXò:„     nUR%                  UUUS.5        M3     U$ s  sn
f s  sn
f )	a  
Converts the raw output of [`DetrForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y,
bottom_right_x, bottom_right_y) format. Only supports PyTorch.

Args:
    outputs ([`DetrObjectDetectionOutput`]):
        Raw outputs of the model.
    threshold (`float`, *optional*):
        Score threshold to keep object detection predictions.
    target_sizes (`torch.Tensor` 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 unset, predictions will not be resized.
Returns:
    `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image
    in the batch as predicted by the model.
NúTMake sure that you pass in as many target sizes as the batch dimension of the logitsr6   .r   r5   rD  )rR   rS   r©   )ÚlogitsÚ
pred_boxesr�   rO   r   r   Úsoftmaxr§   r   rÑ   r>   rD   rÒ   Úunbindr”   rÓ   rk   r  rF   )rù   Úoutputsr  rX  Ú
out_logitsÚout_bboxÚprobrR   rS   r©   ÚiÚimg_hÚimg_wÚ	scale_fctÚresultsÚsÚlÚbru   ÚlabelÚboxs                        r1   Úpost_process_object_detectionÚ0DetrImageProcessor.post_process_object_detection$  s}  € ð&  'Ÿ~™~¨w×/AÑ/A�HàÑ#Ü�:‹¤# lÓ"3Ó3Ü Øjóð ô �}‰}×$Ñ$ Z°Ó4ˆØ˜c 3 B 3˜h™×+Ñ+¨BÓ/‰ˆô )¨Ó2ˆð Ñ#Ü˜,¬×-Ñ-ÜŸš±LÓ%A²L¨q¨¤d±LÑ%AÓB�ÜŸš±LÓ%A²L¨q¨¤d±LÑ%AÓB‘à+×2Ñ2°1Ó5‘�äŸš U°5Ð$@ÀaÑH×KÑKÈEÏLÉLÓYˆIØ¢a¨ªq jÑ1Ñ1ˆEàˆÜ˜6¨5Ö1‰GˆAˆq�!Ø˜)‘mÑ$ˆEØ�a‘mÑ$ˆEØ�A‘MÑ"ˆCØ�N‰N e°uÀsÑKÖLñ	 2ð ˆùò &BùÚ%As   Â/FÃF	c                 óX  • UR                   nUR                  nUR                  SS9SSS24   nUR                  5       n[        R
                  " SXV5      nUR                  S   nUb„  U[        U5      :w  a  [        S5      e/ n	[        U5       HW  n
[        R                  R                  Xz   R                  SS9X*   SS	S
9nUS   R                  SS9nU	R                  U5        MY     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 [`DetrForSegmentation`] into semantic segmentation maps. Only supports PyTorch.

Args:
    outputs ([`DetrForSegmentation`]):
        Raw outputs of the model.
    target_sizes (`list[tuple[int, int]]`, *optional*):
        A list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the
        batch. If unset, predictions will not be resized.
Returns:
    `list[torch.Tensor]`:
        A list 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.
r6   rD  .Nzbqc, bqhw -> bchwr   rZ  rl   Frm   r5   )r[  Ú
pred_masksr]  ÚsigmoidrD   ÚeinsumrN   r�   rO   r|   r   r   rx   ry   r{   rF   )rù   r_  rX  Úclass_queries_logitsÚmasks_queries_logitsÚmasks_classesÚmasks_probsrG   Ú
batch_sizeÚsemantic_segmentationrJ   Úresized_logitsÚsemantic_maprc  s                 r1   Ú"post_process_semantic_segmentationÚ5DetrImageProcessor.post_process_semantic_segmentationY  s_  € ð   'Ÿ~™~ÐØ&×1Ñ1Ðð -×4Ñ4¸Ð4Ð<¸SÀ#À2À#¸XÑFˆØ*×2Ñ2Ó4ˆô —|’|Ð$7¸ÓTˆØ)×/Ñ/°Ñ2ˆ
ð Ñ#ØœS Ó.Ó.Ü Øjóð ð %'Ð!Ü˜ZÖ(�Ü!#§¡×!:Ñ!:Ø Ñ%×/Ñ/°AÐ/Ð6¸\Ñ=NÐU_Ðotð ";ð "�ð  .¨aÑ0×7Ñ7¸AÐ7Ð>�Ø%×,Ñ,¨\Ö:ñ )ð %Ð$ð %1×$7Ñ$7¸AÐ$7Ð$>Ð!ÜGLÐMb×MhÑMhÐijÑMkÔGlÓ$mÒGlÀ!¸1Ô%=ÑGlÐ!Ð$mà$Ð$ùò %ns   ÄD'r^   r_   Úreturn_coco_annotationc                 óŠ  • UR                   nUR                  nUR                  S   n	UR                  S   S-
  n
UR                  5       n[        R
                  R                  USS9R                  S5      u  pÍ/ n[        U	5       H¹  n[        X¿   XÏ   Xß   X*5      u  nnnUR                  S   S::  aJ  Ub  X_   OUR                  SS u  nn[        R                  " UU45      S-
  nUR                  U/ S.5        Mx  Ub  X_   OSn[        UUUUU/ US9u  nnU(       a  [        U5      nUR                  UUS.5        M»     U$ )a  
Converts the output of [`DetrForSegmentation`] into instance segmentation predictions. Only supports PyTorch.

Args:
    outputs ([`DetrForSegmentation`]):
        Raw outputs of the model.
    threshold (`float`, *optional*, defaults to 0.5):
        The probability score threshold to keep predicted instance masks.
    mask_threshold (`float`, *optional*, defaults to 0.5):
        Threshold to use when turning the predicted masks into binary values.
    overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8):
        The overlap mask area threshold to merge or discard small disconnected parts within each binary
        instance mask.
    target_sizes (`list[Tuple]`, *optional*):
        List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested
        final size (height, width) of each prediction. If unset, predictions will not be resized.
    return_coco_annotation (`bool`, *optional*):
        Defaults to `False`. If set to `True`, segmentation maps are returned in COCO run-length encoding (RLE)
        format.
Returns:
    `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys:
    - **segmentation** -- A tensor of shape `(height, width)` where each pixel represents a `segment_id` or
      `list[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to
      `True`. Set to `None` if no mask if found above `threshold`.
    - **segments_info** -- A dictionary that contains additional information on each segment.
        - **id** -- An integer representing the `segment_id`.
        - **label_id** -- An integer representing the label / semantic class id corresponding to `segment_id`.
        - **score** -- Prediction score of segment with `segment_id`.
r   r6   r5   rD  N©rG   rÛ   ©r\   r~   r   r^   r_   rf   rg   )r[  rp  rN   rq  r   r   r]  r§   r|   rW   rD   rv   rF   rˆ   rL   )rù   r_  r  r^   r_   rX  r}  rs  rt  rw  rU   r\   r~   r   rg  rc  Úmask_probs_itemÚpred_scores_itemÚpred_labels_itemr€   r�   rG   rg   r‚   s                           r1   Ú"post_process_instance_segmentationÚ5DetrImageProcessor.post_process_instance_segmentationˆ  sp  € ðL  'Ÿ~™~ÐØ&×1Ñ1Ðà)×/Ñ/°Ñ2ˆ
Ø)×/Ñ/°Ñ3°aÑ7ˆ
à)×1Ñ1Ó3ˆ
ô $&§=¡=×#8Ñ#8Ð9MÐSUÐ#8Ð#V×#ZÑ#ZÐ[]Ó#^Ñ ˆð 02ˆä�zÖ"ˆAÜB[Ø‘˜{™~¨{©~¸yóCÑ?ˆOÐ-Ð/?ð
 ×$Ñ$ QÑ'¨1Ó,Ø3?Ñ3K ¢ÐQ`×QfÑQfÐghÐgiÐQj‘�˜Ü$Ÿ{š{¨F°E¨?Ó;¸aÑ?�Ø—‘°ÈrÑRÔSÙð .:Ñ-E˜,š/È4ˆKÜ%5Ø*Ø,Ø,Ø-Ø,GØ"$Ø'ñ&Ñ"ˆL˜(ö &Ü:¸<ÓH�à�N‰N¨LÈ8ÑTÖUñ9 #ð: ˆr0   rf   c                 óª  • Uc  [         R                  S5        [        5       nUR                  nUR                  nUR
                  S   n	UR
                  S   S-
  n
UR                  5       n[        R                  R                  USS9R                  S5      u  pÍ/ n[        U	5       H§  n[        X¿   XÏ   Xß   X*5      u  nnnUR
                  S   S::  aJ  Ub  Xo   OUR
                  SS u  nn[        R                  " UU45      S-
  nUR                  U/ S.5        Mx  Ub  Xo   OSn[!        UUUUUUUS9u  nnUR                  UUS.5        M©     U$ )	am  
Converts the output of [`DetrForSegmentation`] into image panoptic segmentation predictions. Only supports
PyTorch.

Args:
    outputs ([`DetrForSegmentation`]):
        The outputs from [`DetrForSegmentation`].
    threshold (`float`, *optional*, defaults to 0.5):
        The probability score threshold to keep predicted instance masks.
    mask_threshold (`float`, *optional*, defaults to 0.5):
        Threshold to use when turning the predicted masks into binary values.
    overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8):
        The overlap mask area threshold to merge or discard small disconnected parts within each binary
        instance mask.
    label_ids_to_fuse (`Set[int]`, *optional*):
        The labels in this state will have all their instances be fused together. For instance we could say
        there can only be one sky in an image, but several persons, so the label ID for sky would be in that
        set, but not the one for person.
    target_sizes (`list[Tuple]`, *optional*):
        List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested
        final size (height, width) of each prediction in batch. If unset, predictions will not be resized.
Returns:
    `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys:
    - **segmentation** -- a tensor of shape `(height, width)` where each pixel represents a `segment_id` or
      `None` if no mask if found above `threshold`. If `target_sizes` is specified, segmentation is resized to
      the corresponding `target_sizes` entry.
    - **segments_info** -- A dictionary that contains additional information on each segment.
        - **id** -- an integer representing the `segment_id`.
        - **label_id** -- An integer representing the label / semantic class id corresponding to `segment_id`.
        - **was_fused** -- a boolean, `True` if `label_id` was in `label_ids_to_fuse`, `False` otherwise.
          Multiple instances of the same class / label were fused and assigned a single `segment_id`.
        - **score** -- Prediction score of segment with `segment_id`.
Nz5`label_ids_to_fuse` unset. No instance will be fused.r   r6   r5   rD  r  r€  )ÚloggerÚwarning_onceÚsetr[  rp  rN   rq  r   r   r]  r§   r|   rW   rD   rv   rF   rˆ   )rù   r_  r  r^   r_   rf   rX  rs  rt  rw  rU   r\   r~   r   rg  rc  r�  r‚  rƒ  r€   r�   rG   rg   r‚   s                           r1   Ú"post_process_panoptic_segmentationÚ5DetrImageProcessor.post_process_panoptic_segmentationÛ  s€  € ðV Ñ$Ü×ÑÐ WÔXÜ #£Ðà&Ÿ~™~ÐØ&×1Ñ1Ðà)×/Ñ/°Ñ2ˆ
Ø)×/Ñ/°Ñ3°aÑ7ˆ
à)×1Ñ1Ó3ˆ
ô $&§=¡=×#8Ñ#8Ð9MÐSUÐ#8Ð#V×#ZÑ#ZÐ[]Ó#^Ñ ˆð 02ˆä�zÖ"ˆAÜB[Ø‘˜{™~¨{©~¸yóCÑ?ˆOÐ-Ð/?ð
 ×$Ñ$ QÑ'¨1Ó,Ø3?Ñ3K ¢ÐQ`×QfÑQfÐghÐgiÐQj‘�˜Ü$Ÿ{š{¨F°E¨?Ó;¸aÑ?�Ø—‘°ÈrÑRÔSÙð .:Ñ-E˜,š/È4ˆKÜ%5Ø*Ø,Ø,Ø-Ø,GØ"3Ø'ñ&Ñ"ˆL˜(ð �N‰N¨LÈ8ÑTÖUñ1 #ð2 ˆr0   )r%   )NNNNr$  )NTr   )NNN)r  N)r  r  çš™™™™™é?NF)r  r  rŒ  NN)=r'   r(   r)   r*   r"   Úvalid_kwargsr   ÚBILINEARr  r   r?  r   r@  r   rý   r$   r<  r=  rò   rî   rn   rñ   Úmodel_input_namesr   rø   rD   rÒ   rG  r.   r,   rÜ   rÝ   r   rÿ   r   r   r  ÚNEARESTr   Útupler±   Úfloatr  r  r0  r-  r   r   r   r>   r
   r:  r   rV  rm  r{  r„  r‰  rŠ  r/   Ú__classcell__)rú   s   @r1   rä   rä   ¦  s@  ø† à+€LØ!×*Ñ*€HØ&€JØ$€IØ×,Ñ,€FØ€IØ€JØ€LØ€FØ °$Ñ7€DØÐØ'¨Ð6Ðð# Ð(@Ñ!Að #Àd÷ #ð* +/Ø15Ø04Ø;?ñà�|‰|ðð ðð ! 4Ñ'ð	ð
 $(¨$¡;ðð ˜'Ÿ,™,Ñ&¨Ñ-ðð Ð!1Ñ1°DÑ8ðð 
õðH RVñ	+à�|‰|ð+ð ð+ð ÐMÑNð	+ð 
�‰÷+ð +ðd ØQc×QkÑQkñ7à˜˜c˜‘Nð7ð ˜˜c˜‘?ð7ð ˜3 ˜8‘_ð	7ð
 ð7ð ÐMÑN÷7ð 7ðr¨tð ÀÀsÈCÀxÁð ÐUYô ð!àð!ð    S ™/ð!ð !  c ™?ð	!ð 
ô!ðN -1Ø"Øñ-à�|‰|ð-ð ˜3 ˜8‘_ð-ð ˜˜c˜‘N TÑ)ð	-ð
 ð-ð õ-ð< ð EIØ15Ø04ñhàðhð $ d¨>Ñ&:Ñ:¸TÑAðhð $(¨$¡;ð	hð
 ˜'Ÿ,™,Ñ&¨Ñ-ðhð Ð1Ñ2ðhð 
÷hó ðhð$rà�^Ñ$ðrð $ d¨>Ñ&:Ñ:¸TÑAðrð $(ð	rð
 ˜'Ÿ,™,Ñ&¨Ñ-ðrð ðrð ðrð Lðrð ðrð ðrð ðrð !%ðrð ˜D ™KÑ'¨$Ñ.ðrð ˜4 ™;Ñ&¨Ñ-ðrð ðrð  ˜T‘/ð!rð" Ð&Ñ&¨Ñ-ð#rð$ ˜jÑ(¨4Ñ/ð%rð( 
ô)rðl `dñ3Ø"'ð3Ø=GÈ$ÈuÉ+Ñ=UÐX\Ñ=\õ3ñj-%ÈÈUÐSVÐX[ÐS[É_ÑH]Ð`dÑHdõ -%ðd Ø #Ø-0Ø59Ø.3ñQð ðQð ð	Qð
 &+ðQð ˜5  c ™?Ñ+¨dÑ2ðQð !% t¡ðQð 
ˆd‰õQðl Ø #Ø-0Ø-1Ø59ñVð ðVð ð	Vð
 &+ðVð ˜s™8 d™?ðVð ˜5  c ™?Ñ+¨dÑ2ðVð 
ˆd‰÷Vó Vr0   rä   )r  rŒ  )r  rŒ  NN)FN)TN)Ir+   rÜ   Útypingr   r   r9   r;   rD   r   Útorchvision.ior   Útorchvision.transforms.v2r   r,  Úimage_processing_backendsr	   Úimage_processing_utilsr
   r   Úimage_transformsr   r   r   r   Úimage_utilsr   r   r   r   r   r   r   r   r   r   r   r   Úprocessing_utilsr   r   r8   r   r   r    Ú
get_loggerr'   r‡  rý   rþ   rH  r"   rB   rL   rW   re   r’  r‰  r±   r‘  rˆ   rk   rÒ   r™   r.   r,   r¼   rÎ   rÕ   rG  rÝ   râ   rä   Ú__all__r&   r0   r1   Ú<module>rž     sì  ðñ &ã ß  ã Û Ý Ý %Ý 7å ;ß A÷ó ÷÷ ÷ ó ÷ 5÷ñ ð 
×	Ò	˜HÓ	%€à 0× ?Ñ ?ÐAQ×A_ÑA_Ð`Ð ô!˜|°5ò !òò0 ò*<ô8ð,  Ø),Ø)-Ø*.ñ9"ð ð	9"ð
 "'ð9"ð ˜3‘x $‘ð9"ð �s˜C�x‘ 4Ñ'õ9"ðz°Sð Àð ÈeÏlÉlð Ð_d×_kÑ_kô ðL ',Ø7;ñ	=ð  $ð=ð (¨#Ñ-°Ñ4õ	=ð@8˜%Ÿ,™,ð 8¨5¯<©<ô 8òJAð Ø04ñ.Ø�<‰<ð.àð.ð �g—l‘lÑ"ð.ð ð	.ð
 (¨#Ñ-ð.ð 
õ.ðb ôJ
Ð+ó J
ó ðJ
ðZ  Ð
 �r0   