
    eThrV                     B   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  \ R(                  R*                  S 5       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       V/var/www/auris/envauris/lib/python3.13/site-packages/transformers/loss/loss_rt_detr.py_set_aux_lossr   &   s(    
 8;=7XY7Xtqq*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__9   s    $	* 331111$33))
))
??dnn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                  -
  XR                  -  -  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:0yE>)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.forwardH   ss   * #*("3"9"9"1"=
 <(00A6YY7C7a. 17CD
iiW =W7W => yy!2!:!:1a!@AH:.H$**n::1EFAPXL[_L_KdKdKfJfgN!ZZALTZZ+GHhY]oMbMbMdLdeN'8Jx(00A6>>rBH"1j=11J KK;	()A()KMefqMrss	nny04??Z3OORVR`R`clRll!&&zCGGI*12'QQwZ'2;D[EVEVW\^`Ea;bc;b41a(1.;bckrskrcgcdfg%++6QVQ\Q\8]^krss5  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&..zz&"3"3a"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  pU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[        R                  " [	        X#5       VV	V
s/ s H  u  nu  pUS   U
   PM     sn
n	n5      n[        R                  " UR                  S S	 U R                  [        R                  UR                  S
9nXU'   [        R                  " XR                  S-   S9SS S24   n[        R                   " XR"                  S9nUR%                  UR"                  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-   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diagfullr8   ro   rG   r   r<   one_hot
zeros_liker7   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_logitstarget_classes_originaltarget_classestargettarget_score_originaltarget_score
pred_scorer   losss                         r   loss_labels_vflRTDetrLoss.loss_labels_vfl   s>   w&@AA7"ABB..w7L)#.	yySQXMb!cMb/'6A''"21"5Mb!cijk293C3C3EFH`amHnozz$X&
"'))_bcj_t,u_tOGU[VWW^-DQ-G_t,u"vRa $"2"2%++jN_N_
 6s>7G7G!7KLSRUSURUXV % 0 0GWGW X%)WW-B-H-H%Ic",66r:VCYYz0023
jnnTZZ88AJG,V11*lSYeklyy|!J$4$4Q$77)CD!!+ "d
 -vs   I&"I-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  pUS   U
   PM     sn
n	n5      n[        R
                  " UR                  SS U R                  [        R                  UR                  S9nXU'   [        R                  " UR                  SS5      XR                  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"'))_bcj_t,u_tOGU[VWW^-DQ-G_t,u"vRa $"2"2%++jN_N_
 6s//*"6"6q!"<nN_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	                  S5      UR
                  S   S-
  :g  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   r   cardinality_error)r   r:   rF   rC   argmaxr8   r   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]]2&&,,r*:Q*>>CCAF	==(():N<P<P<RS%x0 *Ss   B9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  pU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)#.	yyWAV!WAVIAv!G*Q-AV!W]^_IIiH	'mmo	9{

 8 CE]^jEkl
 
	 (mmo	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  oS   PM	     n	n[        U	5      R	                  5       u  pU
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5g>
55g>
|,#/%,-W7W-<UCMMO#|4#/ }}00D!(:(:23(?j`e 1 
 $AqD)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  pUS   U
   PM     sn
n	n5      n[        R                  " UR
                  S S U R                  [        R                  UR                  S9nXU'   [        R                  " XR                  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"'))_bcj_t,u_tOGU[VWW^-DQ-G_t,u"vRa $"2"2%++jN_N_
 6s>7G7G!7KLSRUSURUXV11*slV\]yy|!J$4$4Q$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    IIPYZaPbcPbna&uv9Pbcd	YY'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_idx5  sh    IIPYZaPbcPbna!uv9Pbcd	YY'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  pUS   U
   PM     sn
n	n5      n[        R
                  " UR                  S S U R                  [        R                  UR                  S9nXU'   [        R                  " XR                  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_focal;  s"   7"788X&
..w7"'))_bcj_t,u_tOGU[VWW^-DQ-G_t,u"vRa $"2"2%++jN_N_
 6s>7G7G!7KLSRUSURUXV!*djj$**Myy|!J$4$4Q$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_lossM  so    &&00____''++''
 uTF.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  pU	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_indices[  s    (/0A(BGND[3:;7a3q()7;N+22"7+IAzfEKKO\2?-.#f+=== '');V(DE ''AU[[HAU[[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                  XX&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  pU R                  X5      nU R                   H  n	U	S:X  a  M  U R                  XX&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  pU R                   H  n	U	S:X  a  M  0 nU R                  " XX&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An-..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itervaluesr   clampitemrt   r   rs   updaterD   r+   r   )r,   rH   rI   krM   outputs_without_auxrW   r   rt   r   l_dictrU   r   kwargss                 r   rY   RTDetrLoss.forwardr  s    18`CV^_C_tqt` ,,2< @@@	OOYKu{{4PTU\UcUcUePfKgKnKno	KK	q1668	 KKD]]4'INFBHb&QQUQaQaLa8aT%5%5a%888&FbMM&!   ')(1':M2N(O$,,'8B KKDw !]]4GV_`FJPj&QY]YiYiTi@aT-=-=a-@!@@&Fj=C\\^L^TQaE!+oq0^FLMM&) ( )P "W,&g5  T  227;R3SU\]G!,C$D^$TTI(1':P2Q(R$ KKDw F!]]4GV_jcijFJPj&QY]YiYiTi@aT-=-=a-@!@@&Fj<BLLNKNDAaD*na/NFKMM&) ( )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      ss    .-$">( ]]_ .>"%%$$D    ,> >r0   re   c
                 Z   [        U5      nUR                  U5        0 nXS'   X<S'   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XS	'   US	   R                  [        U/U/5      5        U	b3  [        WR                  SS5      WR                  SS5      5      US
'   XS'   U" X5      n[        UR                  5       5      nUUW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_lossdn_out_coorddn_out_classr   	loss_dictr   s                     r   RTDetrForObjectDetectionLossr    sR    6"ILLL#!+ ,*/++m[iEjpq*r'L*/++m[iEjpq*r'L)-3B3*?*I*I!Q*OQ^_`becebe_eQfQpQpqrtuQvw,=()()00?PSbRc1de ,3@&&q!,l.D.DQ.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   jitunusedr   Moduler   re   r   r0   r   <module>r'     s        N N  4 F Z ZNtRYY Ntbq qt	 $.r0   