§
    ’©ºj1�  ã            &       ó0  — d dl 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
mZmZmZmZmZ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mZmZ esesd
dgZ	 	 d7deeeef                  deeeef                  deed         e ed         df         f         de!ddf
d„Z"dedefd„Z#	 d8deed         e ed         df         f         de ed         df         fd„Z$ded         de e%e%ef         fd„Z& G d„ d¦  «        Z'de(ddfd„Z)	 d9deed         eed         df         f         d ed!ee(         d"e'd#eee
f         d$e!de ee         ee          f         fd%„Z*d&ee         d'ee         dee(         fd(„Z+d"e'd'ee         d)ee         d*ee         d+ee         d,ee         d&ee         d-ee         d.ee         d/ee         deed         eed         df         f         d0ed1         d2ee,         d3ee,         d!ee(         d4e!d5e!deeef         f$d6„Z-dS ):é    N)ÚSequence)Úversion)Ú
ModuleType)ÚAnyÚDictÚListÚLiteralÚOptionalÚTupleÚUnion)Úapply_to_collection)ÚTensor)Úrank_zero_warn)Ú_FASTER_COCO_EVAL_AVAILABLEÚ_PYCOCOTOOLS_AVAILABLEÚ _PYCOCOTOOLS_GREATER_EQUAL_2_0_9zCocoBackend.tm_to_cocozCocoBackend.coco_to_tmÚbboxFÚpredsÚtargetsÚiou_type©r   Úsegm.Úignore_scoreÚreturnc                 ó¨  ‡‡‡	— t          |t          ¦  «        r|f}dddœŠ	t          ˆ	fd„|D ¦   «         ¦  «        rt          d|› d�¦  «        ‚ˆ	fd„|D ¦   «         }t          | t          ¦  «        st          d| › �¦  «        ‚t          |t          ¦  «        st          d	|› �¦  «        ‚t          | ¦  «        t          |¦  «        k    r/t          d
t          | ¦  «        › dt          |¦  «        › �¦  «        ‚g |¢d‘|sdgng z   D ]0Št          ˆfd„| D ¦   «         ¦  «        rt          d‰› d�¦  «        ‚Œ1g |¢d‘D ]0Št          ˆfd„|D ¦   «         ¦  «        rt          d‰› d�¦  «        ‚Œ1|D ]0Št          ˆfd„| D ¦   «         ¦  «        st          d‰› d�¦  «        ‚Œ1|s(t          d„ | D ¦   «         ¦  «        st          d¦  «        ‚t          d„ | D ¦   «         ¦  «        st          d¦  «        ‚|D ]0Št          ˆfd„|D ¦   «         ¦  «        st          d‰› d�¦  «        ‚Œ1t          d„ |D ¦   «         ¦  «        st          d¦  «        ‚t          |¦  «        D ]�\  }}|D ]ˆŠ|‰          	                    d¦  «        |d          	                    d¦  «        k    rNt          d‰› d |› d!|‰          	                    d¦  «        › d"|d          	                    d¦  «        › d#�	¦  «        ‚Œ‰Œ‘|rd$S t          | ¦  «        D ]Î\  }}|D ]ÆŠ|‰          	                    d¦  «        |d          	                    d¦  «        cxk    r|d          	                    d¦  «        k    sln t          d‰› d%|› d&|‰          	                    d¦  «        › d'|d          	                    d¦  «        › d(|d          	                    d¦  «        › d#�¦  «        ‚ŒÇŒÏd$S ))z9Ensure the correct input format of `preds` and `targets`.ÚboxesÚmasksr   c              3   ó    •K  — | ]}|‰vV — Œ	d S ©N© ©Ú.0ÚtpÚname_maps     €ún/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/detection/helpers.pyú	<genexpr>z#_input_validator.<locals>.<genexpr>4   s(   øè è € Ð
1Ð
1 "ˆ2�XÐÐ
1Ð
1Ð
1Ð
1Ð
1Ð
1ó    z	IOU type z is not supportedc                 ó    •— g | ]
}‰|         ‘ŒS r    r    r!   s     €r%   ú
<listcomp>z$_input_validator.<locals>.<listcomp>6   s   ø€ Ð5Ð5Ð5 b�X˜b”\Ð5Ð5Ð5r'   z:Expected argument `preds` to be of type Sequence, but got z;Expected argument `target` to be of type Sequence, but got zHExpected argument `preds` and `target` to have the same length, but got z and ÚlabelsÚscoresc              3   ó    •K  — | ]}‰|vV — Œ	d S r   r    ©r"   ÚpÚks     €r%   r&   z#_input_validator.<locals>.<genexpr>B   s'   øè è € Ð)Ð)˜aˆq˜ˆzÐ)Ð)Ð)Ð)Ð)Ð)r'   z.Expected all dicts in `preds` to contain the `z` keyc              3   ó    •K  — | ]}‰|vV — Œ	d S r   r    r-   s     €r%   r&   z#_input_validator.<locals>.<genexpr>F   s'   øè è € Ð+Ð+˜aˆq˜ˆzÐ+Ð+Ð+Ð+Ð+Ð+r'   z/Expected all dicts in `target` to contain the `c              3   óN   •K  — | ]}t          |‰         t          ¦  «        V — Œ d S r   ©Ú
isinstancer   )r"   ÚpredÚivns     €r%   r&   z#_input_validator.<locals>.<genexpr>J   s1   øè è € ÐCÐC°T•:˜d 3œi­Ñ0Ô0ÐCÐCÐCÐCÐCÐCr'   zExpected all z  in `preds` to be of type Tensorc              3   óL   K  — | ]}t          |d          t          ¦  «        V — Œ dS )r+   Nr2   ©r"   r4   s     r%   r&   z#_input_validator.<locals>.<genexpr>L   s0   è è € Ð#YÐ#YÈ4¥J¨t°H¬~½vÑ$FÔ$FÐ#YÐ#YÐ#YÐ#YÐ#YÐ#Yr'   z3Expected all scores in `preds` to be of type Tensorc              3   óL   K  — | ]}t          |d          t          ¦  «        V — Œ dS ©r*   Nr2   r7   s     r%   r&   z#_input_validator.<locals>.<genexpr>N   s0   è è € ÐDÐD°d�z˜$˜xœ.­&Ñ1Ô1ÐDÐDÐDÐDÐDÐDr'   z3Expected all labels in `preds` to be of type Tensorc              3   óN   •K  — | ]}t          |‰         t          ¦  «        V — Œ d S r   r2   )r"   Útargetr5   s     €r%   r&   z#_input_validator.<locals>.<genexpr>Q   s1   øè è € ÐIÐI°v•:˜f Sœk­6Ñ2Ô2ÐIÐIÐIÐIÐIÐIr'   z! in `target` to be of type Tensorc              3   óL   K  — | ]}t          |d          t          ¦  «        V — Œ dS r9   r2   )r"   r;   s     r%   r&   z#_input_validator.<locals>.<genexpr>S   s1   è è € ÐJÐJ¸�z˜& Ô*­FÑ3Ô3ÐJÐJÐJÐJÐJÐJr'   z4Expected all labels in `target` to be of type Tensorr   zInput 'z' and labels of sample z. in targets have a different length (expected z labels, got ú)Nz', labels and scores of sample z2 in predictions have a different length (expected z labels and scores, got z labels and )
r3   ÚstrÚanyÚ	Exceptionr   Ú
ValueErrorÚlenÚallÚ	enumerateÚsize)
r   r   r   r   Úitem_val_nameÚiÚitemr5   r/   r$   s
          @@@r%   Ú_input_validatorrI   )   sd  øøø€ õ �(�CÑ Ô ð Ø�;ˆà¨Ð1Ð1€HÝ
Ð
1Ð
1Ð
1Ð
1¨Ð
1Ñ
1Ô
1Ñ1Ô1ð AÝÐ? HÐ?Ð?Ð?Ñ@Ô@Ð@Ø5Ð5Ð5Ð5¨HÐ5Ñ5Ô5€Må�e�XÑ&Ô&ð _ÝÐ]ÐV[Ð]Ð]Ñ^Ô^Ð^Ý�g�xÑ(Ô(ð bÝÐ`ÐW^Ð`Ð`ÑaÔaÐaÝ
ˆ5�z„z•S˜‘\”\Ò!Ð!ÝØvÕWZÐ[`ÑWaÔWaÐvÐvÕhkÐlsÑhtÔhtÐvÐvñ
ô 
ð 	
ð (ˆ}Ð'˜hÐ'¸\Ð+Q¨H¨:¨:ÈrÑRð Xð XˆÝÐ)Ð)Ð)Ð) 5Ð)Ñ)Ô)Ñ)Ô)ð 	XÝÐVÈaÐVÐVÐVÑWÔWÐWð	Xð (ˆ}Ð'˜hÐ'ð Yð YˆÝÐ+Ð+Ð+Ð+ 7Ð+Ñ+Ô+Ñ+Ô+ð 	YÝÐWÈqÐWÐWÐWÑXÔXÐXð	Yð ð Tð TˆÝÐCÐCÐCÐC¸UÐCÑCÔCÑCÔCð 	TÝÐR¨SÐRÐRÐRÑSÔSÐSð	Tàð P¥Ð#YÐ#YÐSXÐ#YÑ#YÔ#YÑ YÔ Yð PÝÐNÑOÔOÐOÝÐDÐD¸eÐDÑDÔDÑDÔDð PÝÐNÑOÔOÐOØð Uð UˆÝÐIÐIÐIÐIÀÐIÑIÔIÑIÔIð 	UÝÐS¨SÐSÐSÐSÑTÔTÐTð	UåÐJÐJÀ'ÐJÑJÔJÑJÔJð QÝÐOÑPÔPÐPå˜WÑ%Ô%ð ð ‰ˆˆ4Ø ð 	ð 	ˆCØ�CŒy�~Š~˜aÑ Ô  D¨¤N×$7Ò$7¸Ñ$:Ô$:Ò:Ð:Ý ðm˜cð mð m¸!ð mð mØ37¸´9·>²>À!Ñ3DÔ3Dðmð mØSWÐX`ÔSa×SfÒSfÐghÑSiÔSiðmð mð mñô ð ð ;ð	ð ð ØˆÝ˜UÑ#Ô#ð ð ‰ˆˆ4Ø ð 	ð 	ˆCØ˜”I—N’N 1Ñ%Ô%¨¨h¬×)<Ò)<¸QÑ)?Ô)?ÐYÐYÒYÐYÀ4ÈÄ>×CVÒCVÐWXÑCYÔCYÒYÐYÐYÐYÝ ðZ˜cð Zð ZÀ!ð Zð ZØ37¸´9·>²>À!Ñ3DÔ3DðZð Zà  œN×/Ò/°Ñ2Ô2ðZð Zà@DÀXÄ×@SÒ@SÐTUÑ@VÔ@VðZð Zð Zñô ð ð Zð	ðð r'   r   c                 óv   — |                       ¦   «         dk    r | j        dk    r|                      d¦  «        S | S )zIEmpty tensors can cause problems in DDP mode, this methods corrects them.r   é   )ÚnumelÚndimÚ	unsqueeze)r   s    r%   Ú_fix_empty_tensorsrO   i   s6   € à‡{‚{�}„}˜ÒÐ˜eœj¨Ašo˜oØ�Š˜qÑ!Ô!Ð!Ø€Lr'   c                 óœ   ‡— dŠt          | t          ¦  «        r| f} t          ˆfd„| D ¦   «         ¦  «        rt          d‰› d| › �¦  «        ‚| S )z+Validate that iou type argument is correct.)r   r   c              3   ó    •K  — | ]}|‰vV — Œ	d S r   r    )r"   r#   Úallowed_iou_typess     €r%   r&   z)_validate_iou_type_arg.<locals>.<genexpr>w   s)   øè è € Ð
:Ð
:¨2ˆ2Ð&Ð&Ð
:Ð
:Ð
:Ð
:Ð
:Ð
:r'   z*Expected argument `iou_type` to be one of z or a tuple of, but got )r3   r>   r?   rA   )r   rR   s    @r%   Ú_validate_iou_type_argrS   p   sw   ø€ ð )ÐÝ�(�CÑ Ô ð Ø�;ˆÝ
Ð
:Ð
:Ð
:Ð
:°Ð
:Ñ
:Ô
:Ñ:Ô:ð 
ÝØnÐ9JÐnÐnÐdlÐnÐnñ
ô 
ð 	
ð €Or'   Úbackend©ÚpycocotoolsÚfaster_coco_evalc                 óÂ   — | dk    r-t           st          d¦  «        ‚ddlm} ddlm} ddlm} |||fS t          st          d¦  «        ‚ddl	m} ddl	m
} dd	lm} |||fS )
z-Load the backend tools for the given backend.rV   z¿Backend `pycocotools` in metric `MeanAveragePrecision`  metric requires that `pycocotools` is installed. Please install with `pip install pycocotools` or `pip install torchmetrics[detection]`r   N)ÚCOCO)ÚCOCOevalz¦Backend `faster_coco_eval` in metric `MeanAveragePrecision`  metric requires that `faster-coco-eval` is installed. Please install with `pip install faster-coco-eval`.)ÚCOCOeval_faster)Úmask)r   ÚModuleNotFoundErrorÚpycocotools.maskr\   Úpycocotools.cocorY   Úpycocotools.cocoevalrZ   r   rW   r[   Úfaster_coco_eval.core)rT   Ú
mask_utilsrY   rZ   s       r%   Ú_load_coco_backend_toolsrc   ~   s×   € à�-ÒÐÝ%ð 	Ý%ðuñô ð ð 	.Ð-Ð-Ð-Ð-Ð-Ø)Ð)Ð)Ð)Ð)Ð)Ø1Ð1Ð1Ð1Ð1Ð1à�X˜zÐ)Ð)å&ð 
Ý!ðNñ
ô 
ð 	
ð &Ð%Ð%Ð%Ð%Ð%Ø<Ð<Ð<Ð<Ð<Ð<Ø8Ð8Ð8Ð8Ð8Ð8à�˜:Ð%Ð%r'   c                   ó†  — e Zd ZdZded         ddfd„Zedefd„¦   «         Zedefd„¦   «         Z	edefd	„¦   «         Z
	 	 d.dee         dee         dee         dee         dee         dee         dee         dee         dee         deed         eed         df         f         ded         deeef         fd„Zdee         dedee         deeef         fd„Ze	 	 d/d ed!edeed         eed         df         f         ded         deeeeef                  eeeef                  f         f
d"„¦   «         Z	 	 	 d0dee         dee         dee         dee         dee         dee         dee         dee         dee         d$edeed         eed         df         f         ded         ddfd%„Z	 	 	 	 	 	 	 d1d&ee         d'ee         d(eee                  d)eee                  d*eee                  d+eee                  d,eee                  deed         eed         df         f         ded         defd-„ZdS )2ÚCocoBackendaI  Backend implementation for COCO-style Mean Average Precision (mAP) calculation.

    This class provides the core functionality for evaluating object detection and instance
    segmentation predictions using the Common Objects in Context (COCO) evaluation protocol.
    It supports both the standard 'pycocotools' and optimized 'faster_coco_eval' backends.

    It's used for calculation of mAP in MeanAveragePrecision class. It's a backend that abstracts
    away the mAP calculation with coco package

    Args:
        backend (str): Either 'pycocotools' or 'faster_coco_eval'

    rT   rU   r   Nc                 ó@   — |dvrt          d|› �¦  «        ‚|| _        d S )NrU   zUExpected argument `backend` to be one of ('pycocotools', 'faster_coco_eval') but got )rA   rT   )ÚselfrT   s     r%   Ú__init__zCocoBackend.__init__§   s9   € ØÐ=Ð=Ð=ÝØqÐhoÐqÐqñô ð ð ˆŒˆˆr'   c                 ó6   — t          | j        ¦  «        \  }}}|S )z.Returns the coco module for the given backend.©rc   rT   )rg   ÚcocoÚ_s      r%   rk   zCocoBackend.coco®   s   € õ .¨d¬lÑ;Ô;‰
ˆˆa�Øˆr'   c                 ó6   — t          | j        ¦  «        \  }}}|S )z3Returns the coco eval module for the given backend.rj   )rg   rl   Úcocoevals      r%   rn   zCocoBackend.cocoeval´   s   € õ 2°$´,Ñ?Ô?‰ˆˆ8�QØˆr'   c                 ó6   — t          | j        ¦  «        \  }}}|S )z4Returns the mask utils object for the given backend.rj   )rg   rl   rb   s      r%   rb   zCocoBackend.mask_utilsº   s    € õ 4°D´LÑAÔAÑˆˆ1ˆjØÐr'   ©r   ÚmicroÚgroundtruth_labelsÚgroundtruth_boxÚgroundtruth_maskÚgroundtruth_crowdsÚgroundtruth_areaÚdetection_labelsÚdetection_boxÚdetection_maskÚdetection_scoresr   r   .Úaverage©Úmacrorq   c           
      ó¤  — |dk    r.t          |t          d„ ¦  «        }t          |t          d„ ¦  «        }|                      ¦   «         |                      ¦   «         }}t          |¦  «        dk    st          |¦  «        dk    rMt	          j        ||z   ¦  «                             ¦   «                              ¦   «                              ¦   «         ng }|  	                    |t          |¦  «        dk    r|ndt          |¦  «        dk    r|nd|||
||¬¦  «        |_
        |  	                    |t          |¦  «        dk    r|ndt          |¦  «        dk    r|nd|	|
||¬¦  «        |_
        t          j        t          j        ¦   «         ¦  «        5  |                     ¦   «          |                     ¦   «          ddd¦  «         n# 1 swxY w Y   ||fS )z=Returns the coco datasets for the target and the predictions.rq   c                 ó*   — t          j        | ¦  «        S r   ©ÚtorchÚ
zeros_like©Úxs    r%   ú<lambda>z0CocoBackend._get_coco_datasets.<locals>.<lambda>Ñ   s   € Õ[`Ô[kÐlmÑ[nÔ[n€ r'   c                 ó*   — t          j        | ¦  «        S r   r€   rƒ   s    r%   r…   z0CocoBackend._get_coco_datasets.<locals>.<lambda>Ò   s   € ÕW\ÔWgÐhiÑWjÔWj€ r'   r   N)r*   r   r   ÚcrowdsÚarear   Ú
all_labelsr{   )r*   r   r   r+   r   r‰   r{   )r   r   rk   rB   r�   ÚcatÚuniqueÚcpuÚtolistÚ_get_coco_formatÚdatasetÚ
contextlibÚredirect_stdoutÚioÚStringIOÚcreateIndex)rg   rr   rs   rt   ru   rv   rw   rx   ry   rz   r   r{   Úcoco_targetÚ
coco_predsr‰   s                  r%   Ú_get_coco_datasetszCocoBackend._get_coco_datasetsÀ   s)  € ð �gÒÐå!4Ð5GÍÐQnÐQnÑ!oÔ!oÐÝ2Ð3CÅVÐMjÐMjÑkÔkÐà"&§)¢)¡+¤+¨t¯yªy©{¬{�Zˆõ
 Ð#Ñ$Ô$ qÒ(Ð(­CÐ0BÑ,CÔ,CÀaÒ,GÐ,Gõ ŒIÐ&Ð);Ñ;Ñ<Ô<×CÒCÑEÔE×IÒIÑKÔK×RÒRÑTÔTÐTàð 	ð
 #×3Ò3Ø%Ý%(¨Ñ%9Ô%9¸AÒ%=Ð%=�/�/À4Ý&)Ð*:Ñ&;Ô&;¸aÒ&?Ð&?Ð"Ð"ÀTØ%Ø!ØØ!Øð 4ñ 	
ô 	
ˆÔð "×2Ò2Ø#Ý#& }Ñ#5Ô#5¸Ò#9Ð#9�-�-¸tÝ$'¨Ñ$7Ô$7¸!Ò$;Ð$;�.�.ÀØ#ØØ!Øð 3ñ 
ô 
ˆ
Ôõ Ô'­¬©¬Ñ6Ô6ð 	%ð 	%Ø×#Ò#Ñ%Ô%Ð%Ø×"Ò"Ñ$Ô$Ð$ð	%ð 	%ð 	%ñ 	%ô 	%ð 	%ð 	%ð 	%ð 	%ð 	%ð 	%øøøð 	%ð 	%ð 	%ð 	%ð ˜;Ð&Ð&s   Æ)GÇGÇ
GÚstatsÚprefixÚmax_detection_thresholdsc                 ó*  — |}|› d�t          j        |d         gt           j        ¬¦  «        |› d�t          j        |d         gt           j        ¬¦  «        |› d�t          j        |d         gt           j        ¬¦  «        |› d�t          j        |d	         gt           j        ¬¦  «        |› d
�t          j        |d         gt           j        ¬¦  «        |› d�t          j        |d         gt           j        ¬¦  «        |› d|d         › �t          j        |d         gt           j        ¬¦  «        |› d|d         › �t          j        |d         gt           j        ¬¦  «        |› d|d         › �t          j        |d         gt           j        ¬¦  «        |› d�t          j        |d         gt           j        ¬¦  «        |› d�t          j        |d         gt           j        ¬¦  «        |› d�t          j        |d         gt           j        ¬¦  «        iS )z;Converts the output of COCOeval.stats to a dict of tensors.Úmapr   ©ÚdtypeÚmap_50rK   Úmap_75é   Ú	map_smallé   Ú
map_mediumé   Ú	map_largeé   Úmar_é   é   é   Ú	mar_smallé	   Ú
mar_mediumé
   Ú	mar_largeé   ©r�   ÚtensorÚfloat32)rg   r˜   r™   rš   Úmdts        r%   Ú_coco_stats_to_tensor_dictz&CocoBackend._coco_stats_to_tensor_dictö   sç  € ð 'ˆàˆNˆNˆN�EœL¨%°¬(¨½5¼=ÐIÑIÔIØÐÐÐ�uœ|¨U°1¬X¨J½e¼mÐLÑLÔLØÐÐÐ�uœ|¨U°1¬X¨J½e¼mÐLÑLÔLØÐ Ð Ð ¥%¤,°°a´¨zÅÄÐ"OÑ"OÔ"OØÐ!Ð!Ð!¥5¤<°°q´°
Å%Ä-Ð#PÑ#PÔ#PØÐ Ð Ð ¥%¤,°°a´¨zÅÄÐ"OÑ"OÔ"OØÐ#Ð#˜3˜qœ6Ð#Ð#¥U¤\°5¸´8°*ÅEÄMÐ%RÑ%RÔ%RØÐ#Ð#˜3˜qœ6Ð#Ð#¥U¤\°5¸´8°*ÅEÄMÐ%RÑ%RÔ%RØÐ#Ð#˜3˜qœ6Ð#Ð#¥U¤\°5¸´8°*ÅEÄMÐ%RÑ%RÔ%RØÐ Ð Ð ¥%¤,°°a´¨zÅÄÐ"OÑ"OÔ"OØÐ!Ð!Ð!¥5¤<°°r´°Å5Ä=Ð#QÑ#QÔ#QØÐ Ð Ð ¥%¤,°°b´	¨{Å%Ä-Ð"PÑ"PÔ"Pð
ð 	
r'   rV   r–   r•   c                 ón
  — t          |¦  «        }t          |¦  «        \  }}}t          j        t	          j        ¦   «         ¦  «        5   ||¦  «        }|                     | ¦  «        }ddd¦  «         n# 1 swxY w Y   |j        d         }|j        d         }	i }
|D �]<}|d         |
vr9g g g dœ|
|d         <   d|v rg |
|d                  d<   d|v rg |
|d                  d<   d|v r-|
|d                  d                              |d         ¦  «         d|v r:|
|d                  d                              | 	                    |¦  «        ¦  «         |
|d                  d	                              |d
         ¦  «         |
|d                  d                              |d         ¦  «         |
|d                  d                              |d         ¦  «         �Œ>i }|	D �]}|d         |vr8g g dœ||d         <   d|v rg ||d                  d<   d|v rg ||d                  d<   d|v r-||d                  d                              |d         ¦  «         d|v r:||d                  d                              | 	                    |¦  «        ¦  «         ||d                  d                              |d         ¦  «         ||d                  d	                              |d
         ¦  «         �Œ|
D ],}||vr&g g dœ||<   d|v rg ||         d<   d|v rg ||         d<   Œ-g g }}|
D �]}t          j        ||         d         t          j        ¬¦  «        t          j        ||         d	         t          j        ¬¦  «        dœ}d|v rAt          j        t          j        ||         d         ¦  «        t          j        ¬¦  «        |d<   d|v rAt          j        t          j        ||         d         ¦  «        t          j        ¬¦  «        |d<   |                     |¦  «         t          j        |
|         d	         t          j        ¬¦  «        t          j        |
|         d         t          j        ¬¦  «        t          j        |
|         d         t          j        ¬¦  «        dœ}d|v r/t          j        |
|         d         t          j        ¬¦  «        |d<   d|v rAt          j        t          j        |
|         d         ¦  «        t          j        ¬¦  «        |d<   |                     |¦  «         �Œ||fS )aÏ  Utility function for converting .json coco format files to the input format of the mAP metric.

        The function accepts a file for the predictions and a file for the target in coco format and converts them to
        a list of dictionaries containing the boxes, labels and scores in the input format of mAP metric.

        Args:
            coco_preds: Path to the json file containing the predictions in coco format
            coco_target: Path to the json file containing the targets in coco format
            iou_type: Type of input, either `bbox` for bounding boxes or `segm` for segmentation masks
            backend: Backend to use for the conversion. Either `pycocotools` or `faster_coco_eval`.

        Returns:
            A tuple containing the predictions and targets in the input format of mAP metric. Each element of the
            tuple is a list of dictionaries containing the boxes, labels and scores.

        Example:
            >>> # File formats are defined at https://cocodataset.org/#format-data
            >>> # Example files can be found at
            >>> # https://github.com/cocodataset/cocoapi/tree/master/results
            >>> from torchmetrics.detection import MeanAveragePrecision
            >>> preds, target = MeanAveragePrecision().coco_to_tm(
            ...   "instances_val2014_fakebbox100_results.json",
            ...   "val2014_fake_eval_res.txt.json"
            ...   iou_type="bbox"
            ... )  # doctest: +SKIP

        NÚannotationsÚimage_id)r*   Úiscrowdrˆ   r   r   r   r   r*   Úcategory_idrº   rˆ   )r+   r*   r+   Úscorer�   )rS   rc   r�   r‘   r’   r“   ÚloadResr�   ÚappendÚ	annToMaskr�   r³   r´   Úint32ÚnpÚarrayÚuint8)r–   r•   r   rT   rk   rl   ÚgtÚdtÚ
gt_datasetÚ
dt_datasetr;   Útr   r.   r/   Úbatched_predsÚbatched_targetÚkeyÚbpÚbts                       r%   Ú
coco_to_tmzCocoBackend.coco_to_tm
  sŒ  € õD *¨(Ñ3Ô3ˆÝ-¨gÑ6Ô6‰
ˆˆa�åÔ'­¬©¬Ñ6Ô6ð 	(ð 	(Ø��kÑ"Ô"ˆBØ—’˜JÑ'Ô'ˆBð	(ð 	(ð 	(ñ 	(ô 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(øøøð 	(ð 	(ð 	(ð 	(ð ”Z Ô.ˆ
Ø”Z Ô.ˆ
àˆØð 	<ñ 	<ˆAØ�Œ} FÐ*Ð*à Ø!Øð)ð )��q˜”}Ñ%ð
 ˜XÐ%Ð%Ø57�F˜1˜Zœ=Ô)¨'Ñ2Ø˜XÐ%Ð%Ø57�F˜1˜Zœ=Ô)¨'Ñ2à˜Ð!Ð!Ø�q˜”}Ô% gÔ.×5Ò5°a¸´iÑ@Ô@Ð@Ø˜Ð!Ð!Ø�q˜”}Ô% gÔ.×5Ò5°b·l²lÀ1±o´oÑFÔFÐFØ�1�Z”=Ô! (Ô+×2Ò2°1°]Ô3CÑDÔDÐDØ�1�Z”=Ô! )Ô,×3Ò3°A°i´LÑAÔAÐAØ�1�Z”=Ô! &Ô)×0Ò0°°6´Ñ;Ô;Ð;Ñ;àˆØð 	Dñ 	DˆAØ�Œ} EÐ)Ð)Ø24ÀÐ'CÐ'C��a˜
”mÑ$Ø˜XÐ%Ð%Ø46�E˜!˜Jœ-Ô(¨Ñ1Ø˜XÐ%Ð%Ø46�E˜!˜Jœ-Ô(¨Ñ1Ø˜Ð!Ð!Ø�a˜
”mÔ$ WÔ-×4Ò4°Q°v´YÑ?Ô?Ð?Ø˜Ð!Ð!Ø�a˜
”mÔ$ WÔ-×4Ò4°R·\²\À!±_´_ÑEÔEÐEØ�!�J”-Ô  Ô*×1Ò1°!°G´*Ñ=Ô=Ð=Ø�!�J”-Ô  Ô*×1Ò1°!°MÔ2BÑCÔCÐCÑCØð 	+ð 	+ˆAØ˜ˆ~ˆ~Ø&(°BÐ7Ð7��a‘Ø˜XÐ%Ð%Ø(*�E˜!”H˜WÑ%Ø˜XÐ%Ð%Ø(*�E˜!”H˜WÑ%øà(*¨B�~ˆØð 	&ñ 	&ˆCåœ, u¨S¤z°(Ô';Å5Ä=ÐQÑQÔQÝœ, u¨S¤z°(Ô';Å5Ä;ÐOÑOÔOðð ˆBð ˜Ð!Ð!Ý#œl­2¬8°E¸#´J¸wÔ4GÑ+HÔ+HÕPUÔP]Ð^Ñ^Ô^��7‘Ø˜Ð!Ð!Ý#œl­2¬8°E¸#´J¸wÔ4GÑ+HÔ+HÕPUÔP[Ð\Ñ\Ô\��7‘Ø× Ò  Ñ$Ô$Ð$õ  œ, v¨c¤{°8Ô'<ÅEÄKÐPÑPÔPÝ œ<¨¨s¬°IÔ(>ÅeÄkÐRÑRÔRÝœ V¨C¤[°Ô%8ÅÄÐNÑNÔNðð ˆBð
 ˜Ð!Ð!Ý#œl¨6°#¬;°wÔ+?ÅuÄ}ÐUÑUÔU��7‘Ø˜Ð!Ð!Ý#œl­2¬8°F¸3´KÀÔ4HÑ+IÔ+IÕQVÔQ\Ð]Ñ]Ô]��7‘Ø×!Ò! "Ñ%Ô%Ð%Ñ%à˜nÐ,Ð,s   Á!A5Á5A9Á<A9Útm_map_inputÚnamec           
      ó   — t          |¦  «        dk    st          |¦  «        dk    rMt          j        ||z   ¦  «                             ¦   «                              ¦   «                              ¦   «         ng }|                      |t          |¦  «        dk    r|ndt          |¦  «        dk    r|nd|||||¬¦  «        }|                      |t          |¦  «        dk    r|ndt          |¦  «        dk    r|nd|	|||¬¦  «        }d|v r”t          |d         t          d„ ¬¦  «        |d<   t          |d         t          j
        t          j        fd	„ ¬¦  «        |d<   t          |t          d
„ ¬¦  «        }t          |t          j
        t          j        fd„ ¬¦  «        }t          j        |d         d¬¦  «        }t          j        |d¬¦  «        }t          |
› d�d¦  «        5 }|                     |¦  «         ddd¦  «         n# 1 swxY w Y   t          |
› d�d¦  «        5 }|                     |¦  «         ddd¦  «         dS # 1 swxY w Y   dS )aÁ  Utility function for converting the input for mAP metric to coco format and saving it to a json file.

        This function should be used after calling `.update(...)` or `.forward(...)` on all data that should be written
        to the file, as the input is then internally cached. The function then converts to information to coco format
        and writes it to json files.

        Args:
            groundtruth_labels: List of tensors containing the ground truth labels
            groundtruth_box: List of tensors containing the ground truth bounding boxes
            groundtruth_mask: List of tensors containing the ground truth segmentation masks
            groundtruth_crowds: List of tensors indicating whether ground truth annotations are crowd annotations
            groundtruth_area: List of tensors containing the area of ground truth annotations
            detection_labels: List of tensors containing the predicted labels
            detection_box: List of tensors containing the predicted bounding boxes
            detection_mask: List of tensors containing the predicted segmentation masks
            detection_scores: List of tensors containing the confidence scores for predictions
            name: Name of the output file, which will be appended with "_preds.json" and "_target.json"
            iou_type: Type of IoU calculation to use. Can be either "bbox" for bounding box or "segm" for segmentation
            average: Type of averaging to use. Can be either "macro" or "micro"

        Example:
            >>> from torch import tensor
            >>> from torchmetrics.detection import MeanAveragePrecision
            >>> preds = [
            ...   dict(
            ...     boxes=tensor([[258.0, 41.0, 606.0, 285.0]]),
            ...     scores=tensor([0.536]),
            ...     labels=tensor([0]),
            ...   )
            ... ]
            >>> target = [
            ...   dict(
            ...     boxes=tensor([[214.0, 41.0, 562.0, 285.0]]),
            ...     labels=tensor([0]),
            ...   )
            ... ]
            >>> metric = MeanAveragePrecision(iou_type="bbox")
            >>> metric.update(preds, target)
            >>> metric.tm_to_coco("tm_map_input")

        r   N)r*   r   r   r‡   rˆ   r‰   r   r{   )r*   r   r   r+   r‰   r   r{   r   r¸   c                 ó,   — |                       d¦  «        S ©Nzutf-8©Údecoderƒ   s    r%   r…   z(CocoBackend.tm_to_coco.<locals>.<lambda>Í  s   € ÈaÏhÊhÐW^ÑN_ÔN_€ r'   )rž   Úfunctionc                 ó    — t          | ¦  «        S r   ©Úintrƒ   s    r%   r…   z(CocoBackend.tm_to_coco.<locals>.<lambda>Ò  s   € ¥3 q¡6¤6€ r'   c                 ó,   — |                       d¦  «        S rÓ   rÔ   rƒ   s    r%   r…   z(CocoBackend.tm_to_coco.<locals>.<lambda>Ô  s   € Ðab×aiÒaiÐjqÑarÔar€ r'   c                 ó    — t          | ¦  «        S r   rØ   rƒ   s    r%   r…   z(CocoBackend.tm_to_coco.<locals>.<lambda>Ö  s   € ÕQTÐUVÑQWÔQW€ r'   r¥   )Úindentz_preds.jsonÚwz_target.json)rB   r�   rŠ   r‹   rŒ   r�   rŽ   r   ÚbytesrÁ   Úuint32Úuint64ÚjsonÚdumpsÚopenÚwrite)rg   rr   rs   rt   ru   rv   rw   rx   ry   rz   rÐ   r   r{   r‰   Útarget_datasetÚpreds_datasetÚ
preds_jsonÚtarget_jsonÚfs                      r%   Ú
tm_to_cocozCocoBackend.tm_to_cocoz  s  € õt Ð#Ñ$Ô$ qÒ(Ð(­CÐ0BÑ,CÔ,CÀaÒ,GÐ,Gõ ŒIÐ&Ð);Ñ;Ñ<Ô<×CÒCÑEÔE×IÒIÑKÔK×RÒRÑTÔTÐTàð 	ð
 ×.Ò.Ø%Ý%(¨Ñ%9Ô%9¸AÒ%=Ð%=�/�/À4Ý&)Ð*:Ñ&;Ô&;¸aÒ&?Ð&?Ð"Ð"ÀTØ%Ø!Ø!ØØð /ñ 	
ô 	
ˆð ×-Ò-Ø#Ý#& }Ñ#5Ô#5¸Ò#9Ð#9�-�-¸tÝ$'¨Ñ$7Ô$7¸!Ò$;Ð$;�.�.ÀØ#Ø!ØØð .ñ 
ô 
ˆð �XÐÐå+>Ø˜mÔ,µEÐD_ÐD_ð,ñ ,ô ,ˆM˜-Ñ(õ ,?Ø˜mÔ,Ý”y¥"¤)Ð,Ø)Ð)ð,ñ ,ô ,ˆM˜-Ñ(õ
 1°ÅuÐWrÐWrÐsÑsÔsˆNÝ0Ø¥r¤yµ"´)Ð&<ÐGWÐGWðñ ô ˆNõ ”Z ¨mÔ <ÀQÐGÑGÔGˆ
Ý”j ¸Ð:Ñ:Ô:ˆå�TÐ&Ð&Ð&¨Ñ,Ô,ð 	 °Ø�GŠG�JÑÔÐð	 ð 	 ð 	 ñ 	 ô 	 ð 	 ð 	 ð 	 ð 	 ð 	 ð 	 øøøð 	 ð 	 ð 	 ð 	 õ �TÐ'Ð'Ð'¨Ñ-Ô-ð 	!°Ø�GŠG�KÑ Ô Ð ð	!ð 	!ð 	!ñ 	!ô 	!ð 	!ð 	!ð 	!ð 	!ð 	!ð 	!ð 	!øøøð 	!ð 	!ð 	!ð 	!ð 	!ð 	!s$   Ç HÈHÈ	HÈ IÉIÉ
Ir*   r‰   r   r   r+   r‡   rˆ   c
                 ó„  — g }
g }d}t          |¦  «        D �]l\  }}|�.||         }|                     ¦   «                              ¦   «         }|�||         }t          |¦  «        dk    r|€ŒV|                     ¦   «                              ¦   «         }|
                     d|i¦  «         d|v rNt          |¦  «        dk    r;|d         d         d         |d         d         d         c|
d         d<   |
d         d<   t          |¦  «        D �]v\  }}|�||         }|�,t          |¦  «        dk    r||         }|d         |d         d	œ}d
|v r9t          |¦  «        dk    r&t          d|› d|› dt          |¦  «        › d�¦  «        ‚t          |t          ¦  «        s&t          d|› d|› dt          |¦  «        › d�¦  «        ‚d}d}|�i||         |                              ¦   «                              ¦   «         dk    r3||         |                              ¦   «                              ¦   «         }nmd|v r| j	         
                    |¦  «        n|d         |d         z  }t          |¦  «        dk    r+|d         |d         z  }| j	         
                    |¦  «        }|||||�2||         |                              ¦   «                              ¦   «         nddœ}|�
||d<   ||d<   |�||d
<   |�||d<   |�r||         |                              ¦   «                              ¦   «         }t          |t          ¦  «        s&t          d|› d|› dt          |¦  «        › d�¦  «        ‚||d<   |                     |¦  «         |dz  }�Œx�Œn|	dk    rd„ |D ¦   «         ndddœg}|
||dœ}t          rd d!t          d"¦  «        › �i|d#<   |S )$z�Transforms and returns all cached targets or predictions in COCO format.

        Format is defined at
        https://cocodataset.org/#format-data

        rK   Nr   Úidr   éÿÿÿÿÚheightÚwidth)rE   Úcountsr   r¥   zInvalid input box of sample z
, element z (expected 4 values, got r=   zInvalid input class of sample z+ (expected value of type integer, got type r¡   r£   )rì   r¹   rˆ   r»   rº   Ú	area_bboxÚ	area_segmÚsegmentationzInvalid input score of sample z) (expected value of type float, got type r¼   rq   c                 ó2   — g | ]}|t          |¦  «        d œ‘ŒS )©rì   rÐ   )r>   )r"   rG   s     r%   r)   z0CocoBackend._get_coco_format.<locals>.<listcomp>=  s&   € ÐAÐAÐA°˜!¥S¨¡V¤VÐ,Ð,ÐAÐAÐAr'   Ú0rõ   )Úimagesr¸   Ú
categoriesÚdescriptionz:Dummy info generated by tm_to_coco to support pycocotools rV   Úinfo)rD   rŒ   r�   rB   r¾   rA   r3   rÙ   Útyperb   rˆ   Úfloatr   r   )rg   r*   r‰   r   r   r+   r‡   rˆ   r   r{   r÷   r¸   Úannotation_idr¹   Úimage_labelsÚimage_boxesÚimage_masksr/   Úimage_labelÚ	image_boxÚ
image_maskÚarea_stat_boxÚarea_stat_maskÚ	area_statÚ
annotationr¼   ÚclassesÚresults                               r%   rŽ   zCocoBackend._get_coco_formatâ  sÈ  € ð$ ˆØˆØˆå&/°Ñ&7Ô&7ð C	#ñ C	#Ñ"ˆH�lØÐ Ø# Hœo�Ø)ŸošoÑ/Ô/×6Ò6Ñ8Ô8�ØÐ Ø# Hœo�Ý�{Ñ#Ô# qÒ(Ð(¨U¨]ØØ'×+Ò+Ñ-Ô-×4Ò4Ñ6Ô6ˆLà�MŠM˜4 Ð*Ñ+Ô+Ð+Ø˜Ð!Ð!¥c¨+Ñ&6Ô&6¸Ò&:Ð&:Ø<GÈ¼NÈ1Ô<MÈaÔ<PÐR]Ð^_ÔR`ÐabÔRcÐdeÔRfÐ9��r”
˜8Ñ$ f¨R¤j°Ñ&9å"+¨LÑ"9Ô"9ð 5#ñ 5#‘��;ØÐ$Ø +¨A¤�IØÐ$­¨[Ñ)9Ô)9¸AÒ)=Ð)=Ø!,¨Q¤�JØ*4°Q¬-À:ÈaÄ=Ð!QÐ!Q�Jà˜XÐ%Ð%­#¨i©.¬.¸AÒ*=Ð*=Ý$Øx°xÐxÐxÈ1ÐxÐxÕgjÐktÑguÔguÐxÐxÐxñô ð õ " +­sÑ3Ô3ð Ý$ð[¸ð [ð [ÈQð [ð [ÝFJÈ;ÑFWÔFWð[ð [ð [ñô ð ð
 !%�Ø!%�ØÐ#¨¨X¬°qÔ(9×(=Ò(=Ñ(?Ô(?×(FÒ(FÑ(HÔ(HÈ1Ò(LÐ(LØ $ X¤¨qÔ 1× 5Ò 5Ñ 7Ô 7× >Ò >Ñ @Ô @�I�IàDJÈhÐDVÐDV ¤× 4Ò 4°ZÑ @Ô @Ð @Ð\eÐfgÔ\hÐktÐuvÔkwÑ\w�IÝ˜8‘}”} qÒ(Ð(Ø(1°!¬°yÀ´|Ñ(C˜Ø)-¬×)=Ò)=¸jÑ)IÔ)I˜ð (Ø (Ø%Ø#.ØEKÐEW˜v hÔ/°Ô2×6Ò6Ñ8Ô8×?Ò?ÑAÔAÐAÐ]^ðð �
ð !Ð,Ø.;�J˜{Ñ+Ø.<�J˜{Ñ+àÐ$Ø)2�J˜vÑ&ØÐ$Ø1;�J˜~Ñ.àÐ%Ø" 8Ô,¨QÔ/×3Ò3Ñ5Ô5×<Ò<Ñ>Ô>�EÝ% e­UÑ3Ô3ð Ý(ðW¸Xð Wð WÐQRð Wð WÝHLÈUÉÌðWð Wð Wñô ð ð +0�J˜wÑ'Ø×"Ò" :Ñ.Ô.Ð.Ø Ñ"�‘ñk5#ðn FMÐPWÒEWÐEWÐAÐA°jÐAÑAÔAÐAÐefÐpsÐ^tÐ^tÐ]uˆàØ&Ø!ð
ð 
ˆõ
 ,ð 	àÐtÕ\cÐdqÑ\rÔ\rÐtÐtðˆF�6‰Nð ˆr'   )rp   rq   )rp   rV   )rÏ   rp   rq   )NNNNNrp   rq   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   rh   ÚpropertyÚobjectrk   rn   rb   r   r   r   Útupler—   Úlistrü   r>   rÙ   Údictr¶   ÚstaticmethodrÎ   rê   r
   rŽ   r    r'   r%   re   re   ˜   sœ  € € € € € ðð ð Ð(IÔ Jð Ètð ð ð ð ð ð�fð ð ð ñ „Xðð
 ð˜&ð ð ð ñ „Xðð
 ð˜Fð ð ð ñ „Xðð  YbØ-4ð4'ð 4'à  œLð4'ð ˜fœð4'ð ˜vœ,ð	4'ð
 ! œLð4'ð ˜vœ,ð4'ð ˜vœ,ð4'ð ˜F”|ð4'ð ˜Vœð4'ð ˜vœ,ð4'ð ˜ Ô/°°w¸~Ô7NÐPSÐ7SÔ1TÐTÔUð4'ð Ð)Ô*ð4'ð 
ˆv�vˆ~Ô	ð4'ð 4'ð 4'ð 4'ðl
Ø˜%”[ð
Ø*-ð
ØIMÈcÌð
à	ˆc�6ˆkÔ	ð
ð 
ð 
ð 
ð( ð YbØ>Kð	m-ð m-Øðm-àðm-ð ˜ Ô/°°w¸~Ô7NÐPSÐ7SÔ1TÐTÔUðm-ð Ð:Ô;ð	m-ð
 
ˆt�D˜˜f˜Ô%Ô&¨¨T°#°v°+Ô->Ô(?Ð?Ô	@ðm-ð m-ð m-ñ „\ðm-ðt #ØXaØ-4ðf!ð f!à  œLðf!ð ˜fœðf!ð ˜vœ,ð	f!ð
 ! œLðf!ð ˜vœ,ðf!ð ˜vœ,ðf!ð ˜F”|ðf!ð ˜Vœðf!ð ˜vœ,ðf!ð ðf!ð ˜ Ô/°°w¸~Ô7NÐPSÐ7SÔ1TÐTÔUðf!ð Ð)Ô*ðf!ð 
ðf!ð f!ð f!ð f!ðX )-Ø(,Ø)-Ø)-Ø'+ØXaØ-4ðeð eà�V”ðeð ˜”Lðeð ˜˜VœÔ%ð	eð
 ˜˜VœÔ%ðeð ˜˜fœÔ&ðeð ˜˜fœÔ&ðeð �t˜F”|Ô$ðeð ˜ Ô/°°w¸~Ô7NÐPSÐ7SÔ1TÐTÔUðeð Ð)Ô*ðeð 
ðeð eð eð eð eð er'   re   Úlimitc                 ó8   — t          d| › d�t          ¦  «         d S )NzEncountered more than aY   detections in a single image. This means that certain detections with the lowest scores will be ignored, that may have an undesirable impact on performance. Please consider adjusting the `max_detection_threshold` to suit your use case. To disable this warning, set attribute class `warn_on_many_detections=False`, after initializing the metric.)r   ÚUserWarning)r  s    r%   Ú_warning_on_too_many_detectionsr  J  s=   € Ýð	K ð 	Kð 	Kð 	Kõ 	ñô ð ð ð r'   Ú
box_formatrš   Úcoco_backendrH   Úwarnc                 óp  ‡‡— ddl m} ddgŠd| v r@t          |d         ¦  «        }|                     ¦   «         dk    r |||d¬¦  «        }|‰d<   d| v rŸg }|d	                              ¦   «                              ¦   «         D ]^}	|j                             t          j	        |	¦  «        ¦  «        }
| 
                    t          |
d
         ¦  «        |
d         f¦  «         Œ_t          |¦  «        ‰d<   dt          dt          fˆˆfd„}|r+ |d¦  «        s |d¦  «        rt          ‰d         ¦  «         ‰S )a.  Convert and return the boxes or masks from the item depending on the iou_type.

    Args:
        iou_type:
            Type of input to process. Supported types are:
                - "bbox": Process bounding boxes
                - "segm": Process segmentation masks
        box_format:
            Input format of given boxes. Supported formats are:
                - 'xyxy': boxes are represented via corners, x1, y1 being top left and x2, y2 being bottom right.
                - 'xywh': boxes are represented via corner, width and height, x1, y2 being top left, w, h being
                  width and height.
                - 'cxcywh': boxes are represented via centre, width and height, cx, cy being center of box, w, h being
                  width and height.
        max_detection_thresholds:
            List of thresholds on maximum detections per image. Used to determine if warnings should be raised
            when the number of detections exceeds these thresholds.
        coco_backend:
            The COCO evaluation backend class type to use for processing the items.
        item:
            Input dictionary containing the boxes or masks to be processed, along with other detection information.
        warn:
            Whether to warn if the number of boxes or masks exceeds the max_detection_thresholds.
            Default is False.

    Returns:
        A tuple containing processed boxes or masks depending on the iou_type. The first element is the
        tensor representation, and the second element contains additional metadata if applicable.

    r   )Úbox_convertNr   r   Úxywh)Úin_fmtÚout_fmtr   r   rE   rð   rK   Úidxr   c                 óN   •— ‰|          }|€dS t          |¦  «        ‰d         k    S )NFrí   )rB   )r   Úvalrš   Úoutputs     €€r%   Ú_valid_output_lenz0_get_safe_item_values.<locals>._valid_output_len‰  s-   ø€ Ø�SŒkˆØˆ;Ø�5Ý�3‰xŒxÐ2°2Ô6Ò6Ð6r'   rí   )Útorchvision.opsr  rO   rL   rŒ   Únumpyrb   ÚencoderÁ   Úasfortranarrayr¾   r  rÙ   Úboolr  )r   r  rš   r  rH   r  r  r   r   rG   Úrler$  r#  s     `         @r%   Ú_get_safe_item_valuesr+  T  s‚  øø€ ðL ,Ð+Ð+Ð+Ð+Ð+à�Dˆ\€FØ�ÐÐÝ" 4¨¤=Ñ1Ô1ˆØ�;Š;‰=Œ=˜1ÒÐØ�K ¨jÀ&ÐIÑIÔIˆEØˆˆq‰	Ø�ÐÐØˆØ�g”×"Ò"Ñ$Ô$×*Ò*Ñ,Ô,ð 	>ð 	>ˆAØÔ)×0Ò0µÔ1BÀ1Ñ1EÔ1EÑFÔFˆCØ�LŠL�%  F¤Ñ,Ô,¨c°(¬mÐ<Ñ=Ô=Ð=Ð=Ý˜%‘L”Lˆˆq‰	ð7�sð 7¥tð 7ð 7ð 7ð 7ð 7ð 7ð 7ð ð FÐ"Ð" 1Ñ%Ô%ð FÐ):Ð):¸1Ñ)=Ô)=ð FÝ'Ð(@ÀÔ(DÑEÔEÐEØ€Mr'   rw   rr   c                 óì   — t          | ¦  «        dk    st          |¦  «        dk    rMt          j        | |z   ¦  «                             ¦   «                              ¦   «                              ¦   «         S g S )Nr   )rB   r�   rŠ   r‹   rŒ   r�   ©rw   rr   s     r%   Ú_get_classesr.  ”  si   € Ý
ÐÑÔ˜qÒ Ð ¥CÐ(:Ñ$;Ô$;¸aÒ$?Ð$?ÝŒyÐ)Ð,>Ñ>Ñ?Ô?×FÒFÑHÔH×LÒLÑNÔN×UÒUÑWÔWÐWØ€Ir'   rs   rt   ru   rv   rx   ry   rz   r{   r|   Úiou_thresholdsÚrec_thresholdsÚclass_metricsÚextended_summaryc                 ó–
  — |                       |||||||||	|
|¬¦  «        \  }}i }t          j        t          j        ¦   «         ¦  «        5  |
D �]�}t          |
¦  «        dk    rdn|› d�}t          |
¦  «        dk    r|j        d         D ]}|d|› �         |d<   Œt          |j        ¦  «        dk    st          |j        ¦  «        dk    r0|                     |  	                    d	d
gz  ||¬¦  «        ¦  «         Œ®|  
                    |||¬¦  «        }t          j        |t          j        ¬¦  «        |j        _        t          j        |t          j        ¬¦  «        |j        _        ||j        _        |                     ¦   «          |                     ¦   «          |                     ¦   «          |j        }|                     |  	                    |||¬¦  «        ¦  «         i }|rŒ|› d�t-          |j        t          j        d„ ¦  «        |› d�t3          j        |j        d         ¦  «        |› d�t3          j        |j        d         ¦  «        |› d�t3          j        |j        d         ¦  «        i}|                     |¦  «         |�rÝ|                       |||||||||	|
d¬¦  «        \  }}|  
                    |||¬¦  «        }t          j        |t          j        ¬¦  «        |j        _        t          j        |t          j        ¬¦  «        |j        _        ||j        _        g }g }t9          ||¬¦  «        D ]ë}|g|j        _        t          j        t          j        ¦   «         ¦  «        5  |                     ¦   «          |                     ¦   «          |                     ¦   «          |j        }d d d ¦  «         n# 1 swxY w Y   |                     t3          j        |d         g¦  «        ¦  «         |                     t3          j        |d         g¦  «        ¦  «         Œìt3          j        |t2          j        ¬¦  «        }t3          j        |t2          j        ¬¦  «        }nBt3          j        dgt2          j        ¬¦  «        }t3          j        dgt2          j        ¬¦  «        }t          |
¦  «        dk    rdn|› d�}|                     |› d�||› d|d         › d�|i¦  «         �ŒŸ	 d d d ¦  «         n# 1 swxY w Y   |                     dt3          j        t9          ||¬¦  «        t2          j         ¬¦  «        i¦  «         |S )N)r{   rK   Ú rl   r¸   Úarea_rˆ   r   é   g      ð¿)r™   rš   )ÚiouTyper�   Úiousc                 óB   — t          j        | t           j        ¬¦  «        S )Nr�   r²   rƒ   s    r%   r…   z*_calculate_map_with_coco.<locals>.<lambda>ß  s   € Å%Ä,ÈqÕX]ÔXeÐBfÑBfÔBf€ r'   Ú	precisionÚrecallr+   r}   r-  r«   rí   Úmap_per_classr¨   Ú
_per_classr  )!r—   r�   r‘   r’   r“   rB   r�   ÚimgsÚupdater¶   rn   rÁ   rÂ   Úfloat64ÚparamsÚiouThrsÚrecThrsÚmaxDetsÚevaluateÚ
accumulateÚ	summarizer˜   r   r8  Úndarrayr�   r³   Úevalr.  ÚcatIdsr¾   r´   rÀ   ) r  rr   rs   rt   ru   rv   rw   rx   ry   rz   r   r{   r/  r0  rš   r1  r2  r–   r•   Úresult_dictÚi_typer™   ÚannoÚ	coco_evalr˜   ÚsummaryÚmap_per_class_listÚmar_per_class_listÚclass_idÚclass_statsÚmap_per_class_valuesÚmar_per_class_valuess                                    r%   Ú_calculate_map_with_cocorV  š  sG  € ð& +×=Ò=ØØØØØØØØØØØð >ñ ô Ñ€J�ð €KÝ	Ô	#¥B¤K¡M¤MÑ	2Ô	2ð [ð [Øð Z	ñ Z	ˆFÝ˜x™=œ=¨AÒ-Ð-�R�R°f°<°<°<ˆFÝ�8‰}Œ}˜qÒ Ð ð 'Ô.¨}Ô=ð :ð :�DØ#'Ð(8°Ð(8Ð(8Ô#9�D˜‘L�Lå�:”?Ñ#Ô# qÒ(Ð(­C°Ô0@Ñ,AÔ,AÀQÒ,FÐ,FØ×"Ò"Ø ×;Ò;Ø˜d˜V™¨FÐMeð <ñ ô ñô ð ð ð )×1Ò1°+¸zÐSYÐ1ÑZÔZ�	Ý+-¬8°NÍ"Ì*Ð+UÑ+UÔ+U�	Ô Ô(Ý+-¬8°NÍ"Ì*Ð+UÑ+UÔ+U�	Ô Ô(Ø+C�	Ô Ô(à×"Ò"Ñ$Ô$Ð$Ø×$Ò$Ñ&Ô&Ð&Ø×#Ò#Ñ%Ô%Ð%Ø!œ�Ø×"Ò"Ø ×;Ò;Ø fÐG_ð <ñ ô ñô ð ð �Ø#ð à!˜˜˜Õ)<Ø%œN­B¬JÐ8fÐ8fñ*ô *ð "Ð,Ð,Ð,­e¬l¸9¼>È+Ô;VÑ.WÔ.WØ!Ð)Ð)Ð)­5¬<¸	¼ÀxÔ8PÑ+QÔ+QØ!Ð)Ð)Ð)­5¬<¸	¼ÀxÔ8PÑ+QÔ+Qð�Gð ×"Ò" 7Ñ+Ô+Ð+ð !ñ (Sð /;×.MÒ.MØ*Ø'Ø(Ø*Ø(Ø(Ø%Ø&Ø(Ø Ø 'ð /Nñ /ô /Ñ+�J ð !-× 5Ò 5°kÀ:ÐW]Ð 5Ñ ^Ô ^�IÝ/1¬x¸ÍbÌjÐ/YÑ/YÔ/Y�IÔ$Ô,Ý/1¬x¸ÍbÌjÐ/YÑ/YÔ/Y�IÔ$Ô,Ø/G�IÔ$Ô,à)+Ð&Ø)+Ð&Ý$0Ø)9ÐN`ð%ñ %ô %ð Rð R˜ð 4<°*˜	Ô(Ô/Ý'Ô7½¼¹¼ÑFÔFð :ð :Ø%×.Ò.Ñ0Ô0Ð0Ø%×0Ò0Ñ2Ô2Ð2Ø%×/Ò/Ñ1Ô1Ð1Ø*3¬/˜Kð	:ð :ð :ñ :ô :ð :ð :ð :ð :ð :ð :øøøð :ð :ð :ð :ð +×1Ò1µ%´,ÀÈAÄÐ?OÑ2PÔ2PÑQÔQÐQØ*×1Ò1µ%´,ÀÈAÄÐ?OÑ2PÔ2PÑQÔQÐQÐQå+0¬<Ð8JÕRWÔR_Ð+`Ñ+`Ô+`Ð(Ý+0¬<Ð8JÕRWÔR_Ð+`Ñ+`Ô+`Ð(Ð(å+0¬<¸¸ÅEÄMÐ+RÑ+RÔ+RÐ(Ý+0¬<¸¸ÅEÄMÐ+RÑ+RÔ+RÐ(Ý" 8™}œ}°Ò1Ð1˜˜¸&°|°|°|�Ø×"Ò"à!Ð0Ð0Ð0Ð2FØ!ÐOÐOÐ'?ÀÔ'CÐOÐOÐOÐQeðñô ð ñ ðkZ	ð[ð [ð [ñ [ô [ð [ð [ð [ð [ð [ð [øøøð [ð [ð [ð [ðx ×ÒØ•5”<ÝÐ*:ÐOaÐbÑbÔbÕjoÔjuð
ñ 
ô 
ðñ ô ð ð
 Ðs9   ÁL#S:Í.AN>	Î2S:Î>OÏS:ÏOÏD'S:Ó:S>ÔS>)r   Frp   )F).r�   r’   rá   Úcollections.abcr   Úimportlib.metadatar   Útypesr   Útypingr   r   r   r	   r
   r   r   r&  rÁ   r�   Úlightning_utilitiesr   r   Útorchmetrics.utilitiesr   Útorchmetrics.utilities.importsr   r   r   Ú__doctest_skip__r  r>   r  r)  rI   rO   rS   r  rc   re   rÙ   r  r+  r.  rü   rV  r    r'   r%   ú<module>r_     sÝ  ðð Ð Ð Ð Ø 	€	€	€	Ø €€€Ø $Ð $Ð $Ð $Ð $Ð $Ø &Ð &Ð &Ð &Ð &Ð &Ø Ð Ð Ð Ð Ð Ø CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ Cà Ð Ð Ð Ø €€€Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø Ð Ð Ð Ð Ð à 1Ð 1Ð 1Ð 1Ð 1Ð 1ðð ð ð ð ð ð ð ð ð ð 	ð Ð"=ð à Ø ðÐð U[Øð	=ð =Ø�D˜˜f˜Ô%Ô&ð=à�d˜3 ˜;Ô'Ô(ð=ð �G˜NÔ+¨U°7¸>Ô3JÈCÐ3OÔ-PÐPÔQð=ð ð	=ð
 
ð=ð =ð =ð =ð@˜fð ¨ð ð ð ð ð U[ðð Ø�G˜NÔ+¨U°7¸>Ô3JÈCÐ3OÔ-PÐPÔQðà
ˆ7�>Ô" CÐ'Ô(ðð ð ð ð& gÐ.OÔ&Pð &ÐUZÐ[aÐciÐkuÐ[uÔUvð &ð &ð &ð &ð4oð oð oð oð oñ oô oð oðd¨3ð °4ð ð ð ð ð  ð=ð =Ø�G˜NÔ+¨U°7¸>Ô3JÈCÐ3OÔ-PÐPÔQð=àð=ð # 3œið=ð ð	=ð
 ˆs�CˆxŒ.ð=ð ð=ð ˆ8�FÔ˜X eœ_Ð,Ô-ð=ð =ð =ð =ð@ 4¨¤<ð ÀTÈ&Ä\ð ÐVZÐ[^ÔV_ð ð ð ð ðCØðCà˜VœðCð ˜&”\ðCð ˜6”lð	Cð
 ˜VœðCð ˜6”lðCð ˜6”lðCð ˜”<ðCð ˜”LðCð ˜6”lðCð �G˜NÔ+¨U°7¸>Ô3JÈCÐ3OÔ-PÐPÔQðCð Ð%Ô&ðCð ˜”KðCð ˜”KðCð # 3œiðCð  ð!Cð" ð#Cð$ 
ˆ#ˆvˆ+Ôð%Cð Cð Cð Cð Cð Cr'   