+
    œ°³jã.  ã                   ó¶   € ^ RI t ^ RIHt ^RIHt ^RIHt ^RIHtH	t	H
t
Ht ]! 4       '       d   ^ RIHt RR R llt ! R R	]4      t ! R
 R]	4      tRR ltR# )é    N)Úcenter_to_corners_format)Úis_scipy_available)ÚHungarianMatcherÚ	ImageLossÚ_set_aux_lossÚgeneralized_box_iou)Úlinear_sum_assignmentc          
      ó|   € V ^8„  d   QhR\         P                  R\         P                  R\        R\        R\        /# )é   ÚinputsÚtargetsÚ	num_boxesÚalphaÚgamma)ÚtorchÚTensorÚintÚfloat)Úformats   "Úf/home/luca/screen-checking/.venv/lib/python3.14/site-packages/transformers/loss/loss_grounding_dino.pyÚ__annotate__r      sB   € ÷ $"ñ $"Ü�L‰Lð$"ä�\‰\ð$"ô ð$"ô ð	$"ô
 ñ$"ó    c                ón  € V P                  4       p\        P                  P                  WRR7      pWQ,          ^V,
          ^V,
          ,          ,           pV^V,
          V,          ,          pV^ 8¼  d-   W1,          ^V,
          ^V,
          ,          ,           p	W˜,          pVP	                  4       V,          # )a  
Loss used in RetinaNet for dense detection: https://huggingface.co/papers/1708.02002.

Args:
    inputs (`torch.FloatTensor` of arbitrary shape):
        The predictions for each example.
    targets (`torch.FloatTensor` with the same shape as `inputs`)
        A tensor storing the binary classification label for each element in the `inputs` (0 for the negative class
        and 1 for the positive class).
    num_boxes (`int`):
        The total number of boxes in the batch.
    alpha (`float`, *optional*, defaults to 0.25):
        Optional weighting factor in the range (0,1) to balance positive vs. negative examples.
    gamma (`int`, *optional*, defaults to 2):
        Exponent of the modulating factor (1 - p_t) to balance easy vs hard examples.

Returns:
    Loss tensor
Únone)Ú	reduction)ÚsigmoidÚnnÚ
functionalÚ binary_cross_entropy_with_logitsÚsum)
r   r   r   r   r   ÚprobÚce_lossÚp_tÚlossÚalpha_ts
   &&&&&     r   Úsigmoid_focal_lossr&      sŽ   € ð4 �>‰>Ó€DÜ�m‰m×<Ñ<¸VÐX^Ð<Ó_€Gà
�.˜A �H¨¨W­Õ5Õ
5€CØ�q˜3•w 5Õ(Õ)€Dà�„zØ•/ Q¨¥Y°1°wµ;Õ$?Õ?ˆØ�~ˆà�8‰8‹:˜	Õ!Ð!r   c                   óN   a € ] tR t^Ct o ]P
                  ! 4       R 4       tRtV tR# )ÚGroundingDinoHungarianMatcherc           
     óâ  € VR,          P                   R,          w  r4VR,          P                  ^ ^4      P                  4       pVR,          P                  ^ ^4      pVR,          p\        P                  ! \        Wr4       UU	u. uF  w  r‰W‰R,          ,          NK  	  up	p4      pWwP                  RRR7      ,          p\        P                  ! V U
u. uF  qªR,          NK  	  up
4      pR	pR
p^V,
          W],          ,          ^V,
          R,           P                  4       ) ,          pV^V,
          V,          ,          VR,           P                  4       ) ,          pWþ,
          VP                  4       ,          p\        P                  ! Wk^R7      p\        \        V4      \        V4      4      ) pV P                  V,          V P                  V,          ,           V P                  V,          ,           pVP                  W4R4      P!                  4       pV U
u. uF  p
\#        V
R,          4      NK  	  pp
\%        VP'                  VR4      4       UUu. uF  w  pp\)        VV,          4      NK  	  pppV UUu. uFR  w  pp\        P*                  ! V\        P,                  R7      \        P*                  ! V\        P,                  R7      3NKT  	  upp# u up	pi u up
i u up
i u uppi u uppi )ad  
Args:
    outputs (`dict`):
        A dictionary 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.
        * "label_maps": Tuple of tensors of dim [num_classes, hidden_dim].
    targets (`list[dict]`):
        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:
    `list[Tuple]`: 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)
Úlogits:Nr   NÚ
pred_boxesÚ
label_mapsÚclass_labelsT)ÚdimÚkeepdimÚboxesç      Ð?ç       @g:Œ0âŽyE>)Úp)Údtypeéÿÿÿÿ)ÚshapeÚflattenr   r   ÚcatÚzipr    ÚlogÚtÚcdistr   r   Ú	bbox_costÚ
class_costÚ	giou_costÚviewÚcpuÚlenÚ	enumerateÚsplitr	   Ú	as_tensorÚint64)ÚselfÚoutputsr   Ú
batch_sizeÚnum_queriesÚout_probÚout_bboxr,   Ú	label_mapÚtargetÚvÚtarget_bboxr   r   Úneg_cost_classÚpos_cost_classr>   r=   r?   Úcost_matrixÚsizesÚiÚcÚindicesÚjs   &&&                      r   ÚforwardÚ%GroundingDinoHungarianMatcher.forwardD   sg  € ð, #*¨(Õ"3×"9Ñ"9¸"Õ"=Ñˆ
ð ˜8Õ$×,Ñ,¨Q°Ó2×:Ñ:Ó<ˆØ˜<Õ(×0Ñ0°°AÓ6ˆØ˜\Õ*ˆ
ô —Y’YÔ[^Ð_iÔ[sÔtÑ[sÑFWÀi 	°Õ*@× AÐ AÑ[sÒtÓuˆ
à§.¡.°RÀ .Ó"FÕFˆ
ô —i’i±WÓ =±W° 7§ ±WÑ =Ó>ˆð ˆØˆØ˜e�)¨­Õ8¸aÀ(½lÈTÕ>Q×=VÑ=VÓ=XÐ<XÕYˆØ 1 x¥<°EÕ"9Õ:ÀÈ4Å×?TÑ?TÓ?VÐ>VÕWˆà$Õ5¸¿¹»ÕGˆ
ô —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ùó7  uùò
 !>ùò( 3ùÛcùÛss   ÂK
ÃKÈK ÉK%É9AK+© N)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r   Úno_gradrY   Ú__static_attributes__Ú__classdictcell__©Ú__classdict__s   @r   r(   r(   C   s   ø‡ € Ø
‡]‚]ƒ_ñ8tó ö8tr   r(   c                   ód   a € ] tR t^€t o RtR t]P                  ! 4       R 4       tR t	R t
RtV tR# )ÚGroundingDinoImageLossaZ  
This class computes the losses for `GroundingDinoForObjectDetection`. 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 (`GroundingDinoHungarianMatcher`):
        Module able to compute a matching between targets and proposals.
    focal_alpha (`float`):
        Alpha parameter in focal loss.
    losses (`list[str]`):
        List of all the losses to be applied. See `get_loss` for a list of all available losses.
c                óh   € \         P                  P                  V 4       Wn        W n        W0n        R # ©N)r   ÚModuleÚ__init__ÚmatcherÚfocal_alphaÚlosses)rG   rk   rl   rm   s   &&&&r   rj   ÚGroundingDinoImageLoss.__init__�   s$   € Ü
�	‰	×Ñ˜4Ô ØŒØ&ÔØŽr   c           	     ó   € VR,          pVP                   p\        P                  ! V Uu. uF  p\        VR,          4      NK  	  upVR7      pVP	                  4       P                  R4      P                  R8„  P                  ^4      p	\        P                  P                  V	P                  4       VP                  4       4      p
RV
/pV# u upi )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*   r-   )Údeviceg      à?Úcardinality_errorr5   )rp   r   rE   rB   r   ÚmaxÚvaluesr    r   r   Úl1_lossr   )rG   rH   r   rW   r   r*   rp   rO   Útarget_lengthsÚ	card_predÚcard_errrm   s   &&&&&       r   Úloss_cardinalityÚ'GroundingDinoImageLoss.loss_cardinality•   s©   € ð ˜Õ"ˆØ—‘ˆÜŸšÉ'Ó)RÉ'ÀQ¬#¨a°Õ.?Ö*@É'Ñ)RÐ[aÔbˆà—^‘^Ó%×)Ñ)¨"Ó-×4Ñ4°sÑ:×?Ñ?ÀÓBˆ	Ü—=‘=×(Ñ(¨¯©Ó):¸N×<PÑ<PÓ<RÓSˆØ% xÐ0ˆØˆùò *Ss   ªCc                óH  € VR,          p\         P                  ! \        \        W#4      4       UUUUu. uFP  w  pw  pw  rxV^ 8”  d/   VR,          V,          \	        VR,          V,          4      ,           MVR,          V,          NKR  	  upppp4      p	\         P                  ! VR,          ^ R7      p
V P                  V4      p\         P                  ! WDP                  \         P                  R7      pW©,          P                  \         P                  4      WË&   V# u uppppi )z.
Create one_hot based on the matching indices
r*   r-   r,   )r.   )rp   r4   )
r   r8   rC   r9   rB   Ú_get_source_permutation_idxÚ
zeros_likerp   ÚlongÚto)rG   rH   r   rW   r*   rU   rN   Ú_ÚJr-   r,   ÚidxÚtarget_classes_onehots   &&&&         r   Ú_get_target_classes_one_hotÚ2GroundingDinoImageLoss._get_target_classes_one_hot¥   só   € ð ˜Õ"ˆä—y’yô ,5´S¸Ó5JÔ+Köá+KÑ'�AÑ'˜¡ ð NOÐQRÌU��~Õ& qÕ)¬C°¸Õ0EÀaÕ0HÓ,IÖIÐX^Ð_mÕXnÐopÕXqÒqÙ+Kôó
ˆô —Y’Y˜w |Õ4¸!Ô<ˆ
à×.Ñ.¨wÓ7ˆÜ %× 0Ò 0°ÇÁÔUZ×U_ÑU_Ô `ÐØ%/Õ%=×%@Ñ%@ÄÇÁÓ%LÐÑ"à$Ð$ùõs   ³ADc                óJ  € RV9  d   \        R4      hRV9  d   \        R4      hV P                  WV4      pVR,          pVR,          p\        P                  ! Wg4      p\        P                  ! WW4      pVP	                  4       p\        VVVV P                  ^R7      pRV/p	V	# )zˆ
Classification loss (Binary focal loss) targets dicts must contain the key "class_labels" containing a tensor
of dim [nb_target_boxes]
r*   z#No logits were found in the outputsÚ	text_maskz&No text_mask were found in the outputs)r   r   r   r   r   Úloss_ce)ÚKeyErrorrƒ   r   Úmasked_selectr   r&   rl   )
rG   rH   r   rW   r   r‚   Úsource_logitsr†   r‡   rm   s
   &&&&&     r   Úloss_labelsÚ"GroundingDinoImageLoss.loss_labels¹   s·   € ð
 ˜7Ô"ÜÐ@ÓAÐAØ˜gÔ%ÜÐCÓDÐDà $× @Ñ @ÀÐSZÓ [ÐØ Õ)ˆØ˜KÕ(ˆ	ô ×+Ò+¨MÓEˆÜ %× 3Ò 3Ð4IÓ UÐà 5× ;Ñ ;Ó =ÐÜ$Ø Ø)ØØ×"Ñ"Øô
ˆð ˜WÐ%ˆàˆr   )rl   rm   rk   N)r\   r]   r^   r_   Ú__doc__rj   r   r`   rx   rƒ   r‹   ra   rb   rc   s   @r   rf   rf   €   s8   ø‡ € ñòð ‡]‚]ƒ_ñó ðò%÷(ð r   rf   c           
      óZ  aa€ \        VP                  VP                  VP                  R 7      p. ROp\	        VVP
                  VR7      pVP                  V4       / pWR&   W>R&   W^R&   WnR&   RpVP                  '       d#   \        Wx4      pV F  pVVR&   VVR&   K  	  WþR&   V! Wá4      oVP                  '       dQ   RV	RV
RVRV/pV! VV4      pVP                  4        UUu/ uF  w  ppVR,           VbK  	  pppSP                  V4       R	R
RVP                  RVP                  /oVP                  '       d>   SP                  4        UUu/ uF  w  ppVR,           VbK  	  pppSP                  V4       VP                  '       dv   / p\        VP                  ^,
          4       FB  pTP                  SP                  4        UUu/ uF  w  ppVRV 2,           VbK  	  upp4       KD  	  SP                  V4       \!        VV3R lS 4       4      pVSV3# u uppi u uppi u uppi ))r>   r=   r?   )rk   rl   rm   r*   r+   r,   r†   NÚauxiliary_outputsÚ_encr‡   r2   Ú	loss_bboxÚ	loss_giour   c              3   ób   <"  € T F$  qS9   g   K  SV,          SV,          ,          x € K&  	  R # 5irh   r[   )Ú.0ÚkÚ	loss_dictÚweight_dicts   & €€r   Ú	<genexpr>Ú6GroundingDinoForObjectDetectionLoss.<locals>.<genexpr>  s)   øé € ÐT±i°ÈÑCSÔ,ˆy˜�|˜k¨!�n×,Ò,³iùs   ƒ/�/)Úlabelsr0   Úcardinality)r(   r>   r=   r?   rf   rl   r~   Úauxiliary_lossr   Ú	two_stageÚitemsÚupdateÚbbox_loss_coefficientÚgiou_loss_coefficientÚrangeÚdecoder_layersr    )r*   rš   rp   r+   Úconfigr,   r†   Úoutputs_classÚoutputs_coordÚencoder_logitsÚencoder_pred_boxesrk   rm   Ú	criterionÚoutputs_lossr�   Ú
aux_outputÚencoder_outputs_lossÚencoder_loss_dictr•   rO   Úenc_weight_dictÚaux_weight_dictrU   r$   r–   r—   s   &&&&&&&&&&&              @@r   Ú#GroundingDinoForObjectDetectionLossr°   Ù   sM  ù€ ô ,Ø×$Ñ$°×0@Ñ0@ÈF×L\ÑL\ô€Gò 0€FÜ&ØØ×&Ñ&Øô€Ið
 ‡L�L�Ôà€LØ#�ÑØ!+�ÑØ!+�ÑØ )�ÑàÐØ××ÐÜ)¨-ÓGÐÛ+ˆJØ'1ˆJ�|Ñ$Ø&/ˆJ�{Ó#ñ ,ð ->Ð(Ñ)á˜,Ó/€Ià××Ðà�nØÐ,Ø˜*Ø˜ð	 
Ðñ &Ð&:¸FÓCÐØ7H×7NÑ7NÔ7PÔQÑ7P©t¨q°!˜Q �Z¨š]Ñ7PÐÑQØ×ÑÐ*Ô+ð 	�3Ø�V×1Ñ1Ø�V×1Ñ1ð€Kð ××ÐØ5@×5FÑ5FÔ5HÔIÑ5H©T¨Q°˜1˜v�: qš=Ñ5HˆÑIØ×Ñ˜?Ô+à××ÐØˆÜ�v×,Ñ,¨qÕ0Ö1ˆAØ×"Ñ"¸{×?PÑ?PÔ?RÔ#SÑ?R±t°q¸! A¨!¨A¨3¨¥K°¢NÑ?RÒ#SÖTñ 2à×Ñ˜?Ô+äÕT±iÓTÓT€DØ�Ð-Ð-Ð-ùó) Rùó Jùó $Ts   Ã$HÅH!Ç
H')r1   r   )NNNN)r   Útorch.nnr   Úimage_transformsr   Úutilsr   Úloss_for_object_detectionr   r   r   r   Úscipy.optimizer	   r&   r(   rf   r°   r[   r   r   Ú<module>r¶      sR   ðó Ý å 7Ý &ß fÓ fñ ×ÒÝ4÷
$"ôN:tÐ$4ô :tôzV˜Yô VörF.r   