ó
    pyüi0V  ã                   ó  • S SK r S SKJr  S SKJs  Jr  SSKJrJrJ	r	  SSK
JrJrJrJrJr  \" 5       (       a  S SKJr  \" 5       (       a  S SKJr  S r " S	 S
\R*                  5      r " S S\R*                  5      r     SS jrg)é    Né   )Úis_scipy_availableÚis_vision_availableÚrequires_backendsé   )Úbox_iouÚ	dice_lossÚgeneralized_box_iouÚnested_tensor_from_tensor_listÚsigmoid_focal_loss)Úlinear_sum_assignment)Úcenter_to_corners_formatc                 óR   • [        X5       VVs/ s H	  u  p#X#S.PM     snn$ s  snnf )N)ÚlogitsÚ
pred_boxes)Úzip)Úoutputs_classÚoutputs_coordÚaÚbs       Ú[/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/loss/loss_rt_detr.pyÚ_set_aux_lossr   &   s&   € Ü7:¸=Ô7XÔYÒ7X©t¨q�qÔ*Ñ7XÒYÐYùÓYs   �#c                   óZ   ^ • \ rS rSrSrU 4S jr\R                  " 5       S 5       rSr	U =r
$ )ÚRTDetrHungarianMatcheré*   aƒ  This class computes an assignment between the targets and the predictions of the network

For efficiency reasons, the targets don't include the no_object. Because of this, in general, there are more
predictions than targets. In this case, we do a 1-to-1 matching of the best predictions, while the others are
un-matched (and thus treated as non-objects).

Args:
    config: RTDetrConfig
c                 óˆ  >• [         TU ]  5         [        U S/5        UR                  U l        UR
                  U l        UR                  U l        UR                  U l	        UR                  U l        UR                  U l        U R                  U R                  s=:X  a  U R                  s=:X  a  S:X  a  O  g [        S5      eg )NÚscipyr   z#All costs of the Matcher can't be 0)ÚsuperÚ__init__r   Úmatcher_class_costÚ
class_costÚmatcher_bbox_costÚ	bbox_costÚmatcher_giou_costÚ	giou_costÚuse_focal_lossÚmatcher_alphaÚalphaÚmatcher_gammaÚgammaÚ
ValueError)ÚselfÚconfigÚ	__class__s     €r   r   ÚRTDetrHungarianMatcher.__init__5   s™   ø€ Ü‰ÑÔÜ˜$  	Ô*à ×3Ñ3ˆŒØ×1Ñ1ˆŒØ×1Ñ1ˆŒà$×3Ñ3ˆÔØ×)Ñ)ˆŒ
Ø×)Ñ)ˆŒ
à�?‰?˜dŸn™nÕC°·±ÕCÀ!ÖCÜÐBÓCÐCð Dó    c           
      ó`  • US   R                   SS u  p4US   R                  SS5      n[        R                  " U Vs/ s H  ofS   PM	     sn5      n[        R                  " U Vs/ s H  ofS   PM	     sn5      nU R                  (       a¡  [
        R                  " US   R                  SS5      5      n	U	SS2U4   n	SU R                  -
  X�R                  -  -  SU	-
  S	-   R                  5       * -  n
U R                  SU	-
  U R                  -  -  U	S	-   R                  5       * -  nXº-
  nO.US   R                  SS5      R                  S
5      n	U	SS2U4   * n[        R                  " XXSS9n[        [        U5      [        U5      5      * nU R                  U-  U R                  U-  -   U R                   U-  -   nUR#                  X4S
5      R%                  5       nU Vs/ s H  n['        US   5      PM     nn[)        UR+                  US
5      5       VVs/ s H  u  nn[-        UU   5      PM     nnnU VVs/ s HL  u  nn[        R.                  " U[        R0                  S9[        R.                  " U[        R0                  S94PMN     snn$ s  snf s  snf s  snf s  snnf s  snnf )a  Performs the matching

Params:
    outputs: This is a dict that contains at least these entries:
         "logits": Tensor of dim [batch_size, num_queries, num_classes] with the classification logits
         "pred_boxes": Tensor of dim [batch_size, num_queries, 4] with the predicted box coordinates

    targets: This is a list of targets (len(targets) = batch_size), where each target is a dict containing:
         "class_labels": Tensor of dim [num_target_boxes] (where num_target_boxes is the number of ground-truth
                   objects in the target) containing the class labels
         "boxes": Tensor of dim [num_target_boxes, 4] containing the target box coordinates

Returns:
    A list of size batch_size, containing tuples of (index_i, index_j) where:
        - index_i is the indices of the selected predictions (in order)
        - index_j is the indices of the corresponding selected targets (in order)
    For each batch element, it holds:
        len(index_i) = len(index_j) = min(num_queries, num_target_boxes)
r   Nr   r   r   r   Úclass_labelsÚboxesg:Œ0âŽyE>éÿÿÿÿ)Úp©Údtype)ÚshapeÚflattenÚtorchÚcatr&   ÚFÚsigmoidr(   r*   ÚlogÚsoftmaxÚcdistr
   r   r#   r!   r%   ÚviewÚcpuÚlenÚ	enumerateÚsplitr   Ú	as_tensorÚint64)r,   ÚoutputsÚtargetsÚ
batch_sizeÚnum_queriesÚout_bboxÚvÚ
target_idsÚtarget_bboxÚout_probÚneg_cost_classÚpos_cost_classr!   r#   r%   Úcost_matrixÚsizesÚiÚcÚindicesÚjs                        r   ÚforwardÚRTDetrHungarianMatcher.forwardD   ss  € ð* #*¨(Ñ"3×"9Ñ"9¸"¸1Ð"=Ñˆ
ð ˜<Ñ(×0Ñ0°°AÓ6ˆä—Y’Y¹7ÓCº7°a .Ô 1¹7ÑCÓDˆ
Ü—i’i±WÓ =²W° 7¤±WÑ =Ó>ˆð ××Ü—y’y ¨Ñ!2×!:Ñ!:¸1¸aÓ!@ÓAˆHØ¢ : Ñ.ˆHØ $§*¡*™n°¿:¹:Ñ1EÑFÈAÐPXÉLÐ[_ÑL_×KdÑKdÓKfÐJfÑgˆNØ!ŸZ™Z¨A°©L¸T¿Z¹ZÑ+GÑHÈhÐY]Éo×MbÑMbÓMdÐLdÑeˆNØ'Ñ8‰Jà˜xÑ(×0Ñ0°°AÓ6×>Ñ>¸rÓBˆHØ"¢1 j =Ñ1Ð1ˆJô —K’K ¸Ñ;ˆ	ä(Ô)AÀ(Ó)KÔMeÐfqÓMrÓsÐsˆ	à—n‘n yÑ0°4·?±?ÀZÑ3OÑOÐRV×R`ÑR`ÐclÑRlÑlˆØ!×&Ñ& zÀÓC×GÑGÓIˆá*1Ó2ª' Q”�Q�w‘Z–©'ˆÐ2Ü;DÀ[×EVÑEVÐW\Ð^`ÓEaÔ;bÔcÒ;b±4°1°aÔ(¨¨1©Ö.Ñ;bˆÑcákrÔsÒkrÑcgÐcdÐfg”—’ ¬%¯+©+Ñ6¼¿ºÈÔQV×Q\ÑQ\Ñ8]Ó^ÑkrÒsÐsùò5  DùÚ =ùò, 3ùÛcùãss   ¾JÁ'JÇ#JÈJ$È>AJ*)r(   r#   r!   r*   r%   r&   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r:   Úno_gradrY   Ú__static_attributes__Ú__classcell__©r.   s   @r   r   r   *   s*   ø† ñõDð ‡]‚]ƒ_ñ3tó ö3tr0   r   c                   ó¶   ^ • \ rS rSrSrU 4S jrSS jrSS jr\R                  " 5       S 5       r
S rS rSS	 jrS
 rS rSS jrS r\S 5       rS rSrU =r$ )Ú
RTDetrLossé{   a  
This class computes the losses for RTDetr. The process happens in two steps: 1) we compute hungarian assignment
between ground truth boxes and the outputs of the model 2) we supervise each pair of matched ground-truth /
prediction (supervise class and box).

Args:
    matcher (`DetrHungarianMatcher`):
        Module able to compute a matching between targets and proposals.
    weight_dict (`Dict`):
        Dictionary relating each loss with its weights. These losses are configured in RTDetrConf as
        `weight_loss_vfl`, `weight_loss_bbox`, `weight_loss_giou`
    losses (`list[str]`):
        List of all the losses to be applied. See `get_loss` for a list of all available losses.
    alpha (`float`):
        Parameter alpha used to compute the focal loss.
    gamma (`float`):
        Parameter gamma used to compute the focal loss.
    eos_coef (`float`):
        Relative classification weight applied to the no-object category.
    num_classes (`int`):
        Number of object categories, omitting the special no-object category.
c                 ó¶  >• [         TU ]  5         [        U5      U l        UR                  U l        UR                  UR                  UR                  S.U l	        SS/U l
        UR                  U l        [        R                  " UR                  S-   5      nU R                  US'   U R                  SU5        UR                   U l        UR$                  U l        g )N)Úloss_vflÚ	loss_bboxÚ	loss_giouÚvflr3   r   r4   Úempty_weight)r   r   r   ÚmatcherÚ
num_labelsÚnum_classesÚweight_loss_vflÚweight_loss_bboxÚweight_loss_giouÚweight_dictÚlossesÚeos_coefficientÚeos_coefr:   ÚonesÚregister_bufferÚfocal_loss_alphar(   Úfocal_loss_gammar*   )r,   r-   rl   r.   s      €r   r   ÚRTDetrLoss.__init__“   s¹   ø€ Ü‰ÑÔä-¨fÓ5ˆŒØ!×,Ñ,ˆÔà×.Ñ.Ø×0Ñ0Ø×0Ñ0ñ
ˆÔð
 ˜gÐ&ˆŒØ×.Ñ.ˆŒÜ—z’z &×"3Ñ"3°aÑ"7Ó8ˆØŸ=™=ˆ�RÑØ×Ñ˜^¨\Ô:Ø×,Ñ,ˆŒ
Ø×,Ñ,ˆ�
r0   c           	      óü  • SU;  a  [        S5      eSU;  a  [        S5      eU R                  U5      nUS   U   n[        R                  " [	        X#5       VV	V
s/ s H  u  nu  pšUS   U
   PM     sn
n	nSS9n[        [        UR                  5       5      [        U5      5      u  pÉ[        R                  " U5      nUS   nUR                  n[        R                  " [	        X#5       VV	V
s/ s H  u  nu  pšUS   U
   PM     sn
n	n5      n[        R                  " UR                  S S	 U R                  [        R                  UR                  S
9nUUU'   [        R                   " UU R                  S-   S9SS S24   n[        R"                  " UUS9nUR%                  U5      UU'   UR'                  S5      U-  n[        R(                  " UR                  5       5      nU R*                  UR-                  U R.                  5      -  SU-
  -  U-   R%                  U5      n[        R0                  " UUUSS9nUR3                  S5      R5                  5       UR                  S   -  U-  nSU0$ s  sn
n	nf s  sn
n	nf )Nr   ú#No predicted boxes found in outputsr   z$No predicted logits found in outputsr3   r   ©Údimr2   r   ©r7   Údevicer   ©ro   .r4   r6   Únone)ÚweightÚ	reductionrh   )ÚKeyErrorÚ_get_source_permutation_idxr:   r;   r   r   r   ÚdetachÚdiagr7   Úfullr8   ro   rG   r�   r<   Úone_hotÚ
zeros_likeÚtoÚ	unsqueezer=   r(   Úpowr*   Ú binary_cross_entropy_with_logitsÚmeanÚsum)r,   rH   rI   rW   Ú	num_boxesr>   ÚidxÚ	src_boxesÚ_targetÚ_rU   Útarget_boxesÚiousÚ
src_logitsr7   Útarget_classes_originalÚtarget_classesÚtargetÚtarget_score_originalÚtarget_scoreÚ
pred_scorer„   Úlosss                          r   Úloss_labels_vflÚRTDetrLoss.loss_labels_vfl¥   sM  € Ø˜wÓ&ÜÐ@ÓAÐAØ˜7Ó"ÜÐAÓBÐBØ×.Ñ.¨wÓ7ˆà˜LÑ)¨#Ñ.ˆ	Ü—y’yÌSÐQXÔMbÕ!cÒMb¹/¸'Á6ÀA '¨'Ñ"2°1Ô"5ÑMbÓ!cÐijÑkˆÜÔ2°9×3CÑ3CÓ3EÓFÔH`ÐamÓHnÓo‰ˆÜ�zŠz˜$Óˆà˜XÑ&ˆ
Ø× Ñ ˆÜ"'§)¢)Ô_bÐcjÔ_tÕ,uÒ_tÉOÈGÑU[ÐVW¨W°^Ñ-DÀQÔ-GÑ_tÓ,uÓ"vÐÜŸšØ×Ñ˜R˜aÐ  $×"2Ñ"2¼%¿+¹+Èj×N_ÑN_ñ
ˆð 6ˆ�sÑÜ—’˜>°t×7GÑ7GÈ!Ñ7KÑLÈSÐRUÐSUÐRUÈXÑVˆä %× 0Ò 0°ÀuÑ MÐØ%)§W¡W¨U£^Ð˜cÑ"Ø,×6Ñ6°rÓ:¸VÑCˆä—Y’Y˜z×0Ñ0Ó2Ó3ˆ
à—*‘*˜zŸ~™~¨d¯j©jÓ9Ñ9¸QÀ¹ZÑHÈ<ÑW×[Ñ[Ð\aÓbˆä×1Ò1°*¸lÐSYÐekÑlˆØ�y‰y˜‹|×ÑÓ! J×$4Ñ$4°QÑ$7Ñ7¸)ÑCˆØ˜DÐ!Ð!ùô/ "dùô -vs   ÁI0Ã.I7c           	      óÞ  • SU;  a  [        S5      eUS   nU R                  U5      n[        R                  " [	        X#5       VV	V
s/ s H  u  nu  pšUS   U
   PM     sn
n	n5      n[        R
                  " UR                  SS U R                  [        R                  UR                  S9nX¼U'   [        R                  " UR                  SS5      XÀR                  5      nSU0nU$ s  sn
n	nf )	zyClassification loss (NLL)
targets dicts must contain the key "class_labels" containing a tensor of dim [nb_target_boxes]
r   z#No logits were found in the outputsr2   Nr   r€   r   Úloss_ce)r†   r‡   r:   r;   r   rŠ   r8   ro   rG   r�   r<   Úcross_entropyÚ	transposeÚclass_weight)r,   rH   rI   rW   r“   r>   rš   r”   r–   r—   rU   r›   rœ   r¥   rt   s                  r   Úloss_labelsÚRTDetrLoss.loss_labelsÆ   sá   € ð ˜7Ó"ÜÐ@ÓAÐAà˜XÑ&ˆ
à×.Ñ.¨wÓ7ˆÜ"'§)¢)Ô_bÐcjÔ_tÕ,uÒ_tÉOÈGÑU[ÐVW¨W°^Ñ-DÀQÔ-GÑ_tÓ,uÓ"vÐÜŸšØ×Ñ˜R˜aÐ  $×"2Ñ"2¼%¿+¹+Èj×N_ÑN_ñ
ˆð 6�sÑä—/’/ *×"6Ñ"6°q¸!Ó"<¸n×N_ÑN_Ó`ˆØ˜WÐ%ˆØˆùô -vs   ÁC(c           	      óŒ  • US   nUR                   n[        R                  " U Vs/ s H  n[        US   5      PM     snUS9nUR	                  5       R                  S5      R                  S:„  R                  S5      n	[        R                  R                  U	R                  5       UR                  5       5      n
SU
0nU$ s  snf )zÉ
Compute the cardinality error, i.e. the absolute error in the number of predicted non-empty boxes. This is not
really a loss, it is intended for logging purposes only. It doesn't propagate gradients.
r   r2   )r�   r4   g      à?r   Úcardinality_error)r�   r:   rF   rC   r=   ÚmaxÚvaluesr’   ÚnnÚ
functionalÚl1_lossÚfloat)r,   rH   rI   rW   r“   r   r�   rM   Útarget_lengthsÚ	card_predÚcard_errrt   s               r   Úloss_cardinalityÚRTDetrLoss.loss_cardinalityÚ   s©   € ð ˜Ñ"ˆØ—‘ˆÜŸšÉ'Ó)RÊ'ÀQ¬#¨a°Ñ.?Ö*@É'Ñ)RÐ[aÑbˆà—^‘^Ó%×)Ñ)¨"Ó-×4Ñ4°sÑ:×?Ñ?ÀÓBˆ	Ü—=‘=×(Ñ(¨¯©Ó):¸N×<PÑ<PÓ<RÓSˆØ% xÐ0ˆØˆùò *Ss   ¦Cc           	      óÔ  • SU;  a  [        S5      eU R                  U5      nUS   U   n[        R                  " [	        X#5       VVV	s/ s H  u  nu  p‰US   U	   PM     sn	nnSS9n
0 n[
        R                  " XjSS9nUR                  5       U-  US'   S	[        R                  " [        [        U5      [        U
5      5      5      -
  nUR                  5       U-  US
'   U$ s  sn	nnf )a  
Compute the losses related to the bounding boxes, the L1 regression loss and the GIoU loss. Targets dicts must
contain the key "boxes" containing a tensor of dim [nb_target_boxes, 4]. The target boxes are expected in
format (center_x, center_y, w, h), normalized by the image size.
r   r}   r3   r   r~   rƒ   ©r…   ri   r   rj   )r†   r‡   r:   r;   r   r<   r±   r’   r‰   r
   r   )r,   rH   rI   rW   r“   r”   r•   Útr—   rU   r˜   rt   ri   rj   s                 r   Ú
loss_boxesÚRTDetrLoss.loss_boxesé   sä   € ð ˜wÓ&ÜÐ@ÓAÐAØ×.Ñ.¨wÓ7ˆØ˜LÑ)¨#Ñ.ˆ	Ü—y’yÄÀWÔAVÕ!WÒAV±I°A±v¸ ! G¡*¨Q¤-ÑAVÓ!WÐ]^Ñ_ˆàˆä—I’I˜iÀÑHˆ	Ø'Ÿm™m›o°	Ñ9ˆˆ{ÑàœŸ
š
ÜÔ 8¸Ó CÔE]Ð^jÓEkÓló
ñ 
ˆ	ð (Ÿm™m›o°	Ñ9ˆˆ{ÑØˆùô "Xs   Á
C#c                 ó2  • SU;  a  [        S5      eU R                  U5      nU R                  U5      nUS   nXu   nU Vs/ s H  oˆS   PM	     n	n[        U	5      R	                  5       u  p«U
R                  U5      n
X¦   n
[        R                  R                  USS2S4   U
R                  SS SSS9nUSS2S	4   R                  S
5      nU
R                  S
5      n
U
R                  UR                  5      n
[        XzU5      [        XzU5      S.nU$ s  snf )z«
Compute the losses related to the masks: the focal loss and the dice loss. Targets dicts must contain the key
"masks" containing a tensor of dim [nb_target_boxes, h, w].
Ú
pred_masksz#No predicted masks found in outputsÚmasksNéþÿÿÿÚbilinearF)ÚsizeÚmodeÚalign_cornersr   r   )Ú	loss_maskÚ	loss_dice)r†   r‡   Ú_get_target_permutation_idxr   Ú	decomposer�   r¯   r°   Úinterpolater8   r9   rA   r   r	   )r,   rH   rI   rW   r“   Ú
source_idxÚ
target_idxÚsource_masksrº   r¿   Útarget_masksÚvalidrt   s                r   Ú
loss_masksÚRTDetrLoss.loss_masks   s/  € ð
 ˜wÓ&ÜÐ@ÓAÐAà×5Ñ5°gÓ>ˆ
Ø×5Ñ5°gÓ>ˆ
Ø˜|Ñ,ˆØ#Ñ/ˆÙ%,Ó-¢W �7”¡WˆÐ-Ü<¸UÓC×MÑMÓOÑˆØ#—‘ |Ó4ˆØ#Ñ/ˆô —}‘}×0Ñ0Øš˜D˜Ñ!¨×(:Ñ(:¸2¸3Ð(?ÀjÐ`eð 1ð 
ˆð $¢A q DÑ)×1Ñ1°!Ó4ˆà#×+Ñ+¨AÓ.ˆØ#×(Ñ(¨×);Ñ);Ó<ˆä+¨LÈ	ÓRÜ" <¸yÓIñ
ˆð ˆùò# .s   ÁDc           	      ó:  • US   nU R                  U5      n[        R                  " [        X#5       VV	V
s/ s H  u  nu  pšUS   U
   PM     sn
n	n5      n[        R                  " UR
                  S S U R                  [        R                  UR                  S9nX¼U'   [        R                  " XÀR                  S-   S9SS S24   n[        R                  " XmS	-  S
S9nUR                  S5      R                  5       UR
                  S   -  U-  nSU0$ s  sn
n	nf )Nr   r2   r   r€   r   r‚   .r4   g      ð?rƒ   r¹   Úloss_bce)r‡   r:   r;   r   rŠ   r8   ro   rG   r�   r<   r‹   r�   r‘   r’   ©r,   rH   rI   rW   r“   r>   rš   r”   r–   r—   rU   r›   rœ   r�   r¡   s                  r   Úloss_labels_bceÚRTDetrLoss.loss_labels_bce  s  € Ø˜XÑ&ˆ
Ø×.Ñ.¨wÓ7ˆÜ"'§)¢)Ô_bÐcjÔ_tÕ,uÒ_tÉOÈGÑU[ÐVW¨W°^Ñ-DÀQÔ-GÑ_tÓ,uÓ"vÐÜŸšØ×Ñ˜R˜aÐ  $×"2Ñ"2¼%¿+¹+Èj×N_ÑN_ñ
ˆð 6�sÑä—’˜>×7GÑ7GÈ!Ñ7KÑLÈSÐRUÐSUÐRUÈXÑVˆÜ×1Ò1°*Às¹lÐV\Ñ]ˆØ�y‰y˜‹|×ÑÓ! J×$4Ñ$4°QÑ$7Ñ7¸)ÑCˆØ˜DÐ!Ð!ùô -vs   ¶Dc                 ó  • [         R                  " [        U5       VVVs/ s H  u  nu  p4[         R                  " X25      PM      snnn5      n[         R                  " U VVs/ s H  u  p4UPM	     snn5      nXV4$ s  snnnf s  snnf ©N©r:   r;   rD   Ú	full_like)r,   rW   rU   Úsourcer—   Ú	batch_idxrÊ   s          r   r‡   Ú&RTDetrLoss._get_source_permutation_idx-  sh   € ä—I’IÔPYÐZaÔPbÕcÒPb¹n¸aÁÀ&œuŸš¨vÖ9ÑPbÓcÓdˆ	Ü—Y’Y¹'ÔBº'©;¨F£¹'ÒBÓCˆ
ØÐ$Ð$ùô dùÛBó    %A<Á#B
c                 ó  • [         R                  " [        U5       VVVs/ s H  u  nu  p4[         R                  " XB5      PM      snnn5      n[         R                  " U VVs/ s H  u  p4UPM	     snn5      nXV4$ s  snnnf s  snnf r×   rØ   )r,   rW   rU   r—   r�   rÛ   rË   s          r   rÇ   Ú&RTDetrLoss._get_target_permutation_idx3  sh   € ä—I’IÔPYÐZaÔPbÕcÒPb¹n¸aÁÀ!œuŸš¨vÖ9ÑPbÓcÓdˆ	Ü—Y’Y¹'ÔBº'©;¨A£¹'ÒBÓCˆ
ØÐ$Ð$ùô dùÛBrÝ   c           	      ón  • SU;  a  [        S5      eUS   nU R                  U5      n[        R                  " [	        X#5       VV	V
s/ s H  u  nu  pšUS   U
   PM     sn
n	n5      n[        R
                  " UR                  S S U R                  [        R                  UR                  S9nX¼U'   [        R                  " XÀR                  S-   S9SS S	24   n[        XmU R                  U R                  5      nUR                  S5      R!                  5       UR                  S   -  U-  nS
U0$ s  sn
n	nf )Nr   zNo logits found in outputsr2   r   r€   r   r‚   .r4   Ú
loss_focal)r†   r‡   r:   r;   r   rŠ   r8   ro   rG   r�   r<   r‹   r   r(   r*   r‘   r’   rÓ   s                  r   Úloss_labels_focalÚRTDetrLoss.loss_labels_focal9  s"  € Ø˜7Ó"ÜÐ7Ó8Ð8à˜XÑ&ˆ
à×.Ñ.¨wÓ7ˆÜ"'§)¢)Ô_bÐcjÔ_tÕ,uÒ_tÉOÈGÑU[ÐVW¨W°^Ñ-DÀQÔ-GÑ_tÓ,uÓ"vÐÜŸšØ×Ñ˜R˜aÐ  $×"2Ñ"2¼%¿+¹+Èj×N_ÑN_ñ
ˆð 6�sÑä—’˜>×7GÑ7GÈ!Ñ7KÑLÈSÐRUÐSUÐRUÈXÑVˆÜ! *°d·j±jÀ$Ç*Á*ÓMˆØ�y‰y˜‹|×ÑÓ! J×$4Ñ$4°QÑ$7Ñ7¸)ÑCˆØ˜dÐ#Ð#ùô -vs   ÁD0c                 óà   • U R                   U R                  U R                  U R                  U R                  U R
                  U R                  S.nX;  a  [        SU S35      eXa   " X#XE5      $ )N)ÚlabelsÚcardinalityr3   r¿   ÚbceÚfocalrk   zLoss z not supported)r©   r¶   r»   rÏ   rÔ   râ   r¢   r+   )r,   r¡   rH   rI   rW   r“   Úloss_maps          r   Úget_lossÚRTDetrLoss.get_lossK  so   € à×&Ñ&Ø×0Ñ0Ø—_‘_Ø—_‘_Ø×'Ñ'Ø×+Ñ+Ø×'Ñ'ñ
ˆð ÓÜ˜u T F¨.Ð9Ó:Ð:ØŠ~˜g°ÓCÐCr0   c           
      ó8  • U S   U S   p2U Vs/ s H  n[        US   5      PM     nnUS   S   R                  n/ n[        U5       HÊ  u  p‰U	S:”  ah  [        R                  " U	[        R
                  US9n
U
R                  U5      n
[        X(   5      [        U
5      :X  d   eUR                  X(   U
45        Ms  UR                  [        R                  " S[        R
                  US9[        R                  " S[        R
                  US945        MÌ     U$ s  snf )NÚdn_positive_idxÚdn_num_groupr2   r   r€   )	rC   r�   rD   r:   ÚarangerG   ÚtileÚappendÚzeros)Údn_metarI   rí   rî   rº   Únum_gtsr�   Údn_match_indicesrU   Únum_gtÚgt_idxs              r   Úget_cdn_matched_indicesÚ"RTDetrLoss.get_cdn_matched_indicesY  s   € à(/Ð0AÑ(BÀGÈNÑD[˜Ù3:Ó;²7¨a”3�q˜Ñ(Ö)±7ˆÐ;Ø˜‘˜NÑ+×2Ñ2ˆàÐÜ" 7Ö+‰IˆAØ˜‹zÜŸš f´E·K±KÈÑO�ØŸ™ \Ó2�Ü˜?Ñ-Ó.´#°f³+Ó=Ð=Ð=Ø ×'Ñ'¨Ñ);¸VÐ(DÖEà ×'Ñ'äŸš A¬U¯[©[ÀÑHÜŸš A¬U¯[©[ÀÑHðöñ ,ð  Ðùò% <s   ŽDc           
      ó&  • UR                  5        VVs0 s H  u  p4SU;  d  M  X4_M     nnnU R                  XR5      n[        S U 5       5      n[        R                  " U/[        R
                  [        [        UR                  5       5      5      R                  S9n[        R                  " USS9R                  5       n0 nU R                   H]  n	U R                  X‘X&U5      n
U
 Vs0 s H)  o3U R                  ;   d  M  X:U   U R                  U   -  _M+     n
nUR                  U
5        M_     SU;   aÈ  [!        US   5       H¶  u  p¼U R                  XÂ5      nU R                   H�  n	U	S:X  a  M  U R                  XœX&U5      n
U
 Vs0 s H)  o3U R                  ;   d  M  X:U   U R                  U   -  _M+     n
nU
R                  5        VVs0 s H  u  p4USU 3-   U_M     n
nnUR                  U
5        M’     M¸     SU;   aë  S	U;  a  [#        S
5      eU R%                  US	   U5      nXqS	   S   -  n[!        US   5       H©  u  p¼U R                   H”  n	U	S:X  a  M  0 nU R                  " XœX&U40 UD6n
U
 Vs0 s H)  o3U R                  ;   d  M  X:U   U R                  U   -  _M+     n
nU
R                  5        VVs0 s H  u  p4USU 3-   U_M     n
nnUR                  U
5        M–     M«     U$ s  snnf s  snf s  snf s  snnf s  snf s  snnf )aj  
This performs the loss computation.

Args:
     outputs (`dict`, *optional*):
        Dictionary of tensors, see the output specification of the model for the format.
     targets (`list[dict]`, *optional*):
        List of dicts, such that `len(targets) == batch_size`. The expected keys in each dict depends on the
        losses applied, see each loss' doc.
Úauxiliary_outputsc              3   ó>   #   • U  H  n[        US    5      v •  M     g7f)r2   N)rC   )Ú.0rº   s     r   Ú	<genexpr>Ú%RTDetrLoss.forward.<locals>.<genexpr>�  s   é € Ð@º°1œ˜A˜nÑ-×.Ð.ºùs   ‚r€   r   )Úminr¿   Ú_aux_Údn_auxiliary_outputsÚdenoising_meta_valuesz}The output must have the 'denoising_meta_values` key. Please, ensure that 'outputs' includes a 'denoising_meta_values' entry.rî   Ú_dn_)Úitemsrm   r’   r:   rF   r²   ÚnextÚiterr®   r�   ÚclampÚitemrt   rê   rs   ÚupdaterD   r+   rø   )r,   rH   rI   ÚkrM   Úoutputs_without_auxrW   r“   rt   r¡   Úl_dictrU   rû   Úkwargss                 r   rY   ÚRTDetrLoss.forwardp  s  € ð 18·±´Ô`²©¨ÐCVÐ^_ÑC_›t˜qšt±ÐÑ`ð —,‘,Ð2Ó<ˆô Ñ@¹Ó@Ó@ˆ	Ü—O’O Y K´u·{±{Ì4ÔPTÐU\×UcÑUcÓUeÓPfÓKg×KnÑKnÑoˆ	Ü—K’K 	¨qÑ1×6Ñ6Ó8ˆ	ð ˆØ—K”KˆDØ—]‘] 4°'ÀIÓNˆFÙBHÓbÂ&¸QÐQU×QaÑQaÑLaÓ8�a ™ T×%5Ñ%5°aÑ%8Ñ8Ò8Á&ˆFÐbØ�M‰M˜&Ö!ñ  ð  'Ó)Ü(1°'Ð:MÑ2NÖ(OÑ$�ØŸ,™,Ð'8ÓB�Ø ŸKœK�DØ˜w“á Ø!Ÿ]™]¨4ÀGÐV_Ó`�FÙJPÓjÊ&ÀQÐY]×YiÑYiÑTiÓ@˜a¨¡¨T×-=Ñ-=¸aÑ-@Ñ!@Ò@É&�FÐjØ=C¿\¹\¼^ÔLº^±T°Q˜a E¨!¨ +™o¨qÒ0¹^�FÑLØ—M‘M &Ö)ó (ñ )Pð " WÓ,Ø&¨gÓ5Ü ð Tóð ð ×2Ñ2°7Ð;RÑ3SÐU\Ó]ˆGØ!Ð,CÑ$DÀ^Ñ$TÑTˆIä(1°'Ð:PÑ2QÖ(RÑ$�à ŸKœK�DØ˜w“á Ø�FØ!Ÿ]š]¨4ÀGÐV_ÑjÐciÑj�FÙJPÓjÊ&ÀQÐY]×YiÑYiÑTiÓ@˜a¨¡¨T×-=Ñ-=¸aÑ-@Ñ!@Ò@É&�FÐjØ<B¿L¹L¼NÔKºN±D°A˜a D¨¨ *™n¨aÒ/¹N�FÑKØ—M‘M &Ö)ó (ñ )Sð ˆùóg aùò cùò kùÛLùò( kùÛKs:   ”K2¤K2Ã*K8ÄK8Æ	K=Æ K=ÇL
É:LÊLÊ?L
)r(   rv   r*   rt   rm   ro   rs   )T)r[   r\   r]   r^   r_   r   r¢   r©   r:   r`   r¶   r»   rÏ   rÔ   r‡   rÇ   râ   rê   Ústaticmethodrø   rY   ra   rb   rc   s   @r   re   re   {   st   ø† ñõ.-ô$"ôBð( ‡]‚]ƒ_ñó ðòò.ô>"ò%ò%ô$ò$Dð ñ ó ð ÷,>ð >r0   re   c
                 ó^  • [        U5      nUR                  U5        0 nXS'   X<S'   S nUR                  (       aÏ  U	b4  [        R                  " XiS   SS9u  pæ[        R                  " XYS   SS9u  põ[        US S 2S S24   R                  SS5      US S 2S S24   R                  SS5      5      nXÜS	'   US	   R                  [        U/U/5      5        U	b3  [        WR                  SS5      WR                  SS5      5      US
'   XœS'   U" XÁ5      n[        UR                  5       5      nUUU4$ )Nr   r   Údn_num_splitr   r~   r4   r   r   rû   r  r  )
re   r�   Úauxiliary_lossr:   rE   r   r§   Úextendr’   r®   )r   rå   r�   r   r-   r   r   Úenc_topk_logitsÚenc_topk_bboxesr  r  Ú	criterionÚoutputs_lossrû   Údn_out_coordÚdn_out_classÚ	loss_dictr¡   s                     r   ÚRTDetrForObjectDetectionLossr  ±  sX  € ô ˜6Ó"€IØ‡L�L�Ôà€LØ#�ÑØ!+�ÑØÐØ××Ø Ñ,Ü*/¯+ª+°mÐ[iÑEjÐpqÑ*rÑ'ˆLÜ*/¯+ª+°mÐ[iÑEjÐpqÑ*rÑ'ˆLä)¨-º¸3¸B¸3¸Ñ*?×*IÑ*IÈ!ÈQÓ*OÐQ^Ò_`ÐbeÐceÐbeÐ_eÑQf×QpÑQpÐqrÐtuÓQvÓwÐØ,=Ð(Ñ)ØÐ(Ñ)×0Ñ0´ÀÐ?PÐSbÐRcÓ1dÔeØ Ñ,Ü3@Ø×&Ñ& q¨!Ó,¨l×.DÑ.DÀQÈÓ.Jó4ˆLÐ/Ñ0ð 5JÐ0Ñ1á˜,Ó/€Iäˆy×ÑÓ!Ó"€DØ�Ð-Ð-Ð-r0   )NNNNN)r:   Útorch.nnr¯   Útorch.nn.functionalr°   r<   Úutilsr   r   r   Úloss_for_object_detectionr   r	   r
   r   r   Úscipy.optimizer   Útransformers.image_transformsr   r   ÚModuler   re   r  © r0   r   Ú<module>r%     sƒ   ðó Ý ß Ð ç NÑ N÷õ ñ ×ÑÝ4ñ ×ÑÝFòZôNt˜RŸY™Yô Ntôbs�—‘ô sðx	 ØØØØõ%.r0   