
    ^j/                         d dl Z d dlmc mZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ  e       rd dlmZ  G d d	e      Z	 	 	 	 	 	 	 dd
Zy)    N   )is_vision_available   )	DFineLoss_set_aux_loss_set_aux_loss2)box_iou)center_to_corners_formatc                   :     e Zd Z fdZd Zd Zd Z fdZ xZS )
Deimv2Lossc                     t         |   |       |j                  |j                  |j                  |j
                  |j                  d| _        g d| _        |j                  | _	        |j                  | _
        y )N)loss_mal	loss_bbox	loss_giouloss_fglloss_ddf)malboxeslocal)super__init__weight_loss_malweight_loss_bboxweight_loss_giouweight_loss_fglweight_loss_ddfweight_dictlosses	mal_alphause_dense_one_to_one)selfconfig	__class__s     h/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/loss/loss_deimv2.pyr   zDeimv2Loss.__init__   sk     ..0000....
 0))$*$?$?!    c           	      N   | j                  |      }|d   |   }t        j                  t        ||      D 	cg c]  \  }\  }}	|d   |	    c}	}}d      }
t	        t        |      t        |
            \  }}t        j                  |      j                         }|d   }t        j                  t        ||      D 	cg c]  \  }\  }}	|d   |	    c}	}}      }t        j                  |j                  dd | j                  t        j                  |j                  	      }|||<   t        j                  || j                  d
z         dddf   }t        j                  ||j                         }|j#                  |j                         ||<   |j%                  d      |z  }t        j&                  |      j                         }|j)                  | j*                        }| j,                  2| j,                  |j)                  | j*                        z  d
|z
  z  |z   }n$|j)                  | j*                        d
|z
  z  |z   }t        j.                  |||d      }|j1                  d
      j3                         |j                  d
   z  |z  }d|iS c c}	}}w c c}	}}w )zCompute the Matching Aware Loss (MAL), which uses IoU-weighted soft labels
        instead of hard one-hot targets, with focal-style weighting controlled by `mal_alpha`.
        
pred_boxesr   r   dimlogitsclass_labelsNr   dtypedevicer   )num_classes.)r-   none)weight	reductionr   )_get_source_permutation_idxtorchcatzipr	   r
   diagdetachfullshaper/   int64r.   Fone_hot
zeros_liker-   to	unsqueezesigmoidpowgammar    binary_cross_entropy_with_logitsmeansum)r!   outputstargetsindices	num_boxesidx	src_boxest_itarget_boxesious
src_logitstarget_classes_originaltarget_classestargettarget_score_originaltarget_score
pred_scorer2   losss                        r$   loss_labels_malzDeimv2Loss.loss_labels_mal*   s]    ..w7L)#.	yyWgAV!W!WIAv1!G*Q-!W]^_29=?WXd?efazz$&&(X&
"'))SVW^`gSh,i,iia!QQ~->q-A,i"jRa $"2"2%++jN_N_
 6s>t7G7G!7KLSRUSURUXV % 0 0zGWGW X%)WW-B-H-H%Ic",66r:VCYYz*113
#''

3>>%^^jnnTZZ&@@AJORXXF^^DJJ/1v:>GF11*lSYeklyy|!J$4$4Q$77)CD!!3 "X
 -js   JJ c                    g }|D ]t  }t        |j                         |j                               D cg c]?  \  }}t        j                  |d   |d   g      t        j                  |d   |d   g      fA }}}v |D cg c].  }t        j                  |d   d d d f   |d   d d d f   gd      0 c}D ]  }t        j                  |dd      \  }	}
t        j
                  |
d      }|	|   }i }|D ]2  }|d   j                         |d   j                         }}||vs.|||<   4 t        j                  t        |j                               |j                        }t        j                  t        |j                               |j                        }|j                  |j                         |j                         f        |S c c}}w c c}w )Nr   r   T)return_countsr)   )
descending)r.   )r7   copyr5   r6   uniqueargsortitemtensorlistkeysr.   valuesappendlong)r!   rJ   indices_aux_listresultsindices_auxidx1idx2rL   indexr`   countscount_sort_indicesunique_sortedcolumn_to_rowidx_pairrow_idxcol_idx
final_rows
final_colss                      r$   _get_dense_o2o_indicesz!Deimv2Loss._get_dense_o2o_indicesL   s   + 	K #&gllnk6F6F6H"ID$ DGT!W-.		47DG:L0MNG 	 SZZ3eiiQ4#a&D/ BAFZ 	CE"\\%tKNFF!&v$!G"#56MM) 5#+A;#3#3#5x{7G7G7I-/-4M'*5 d=+=+=+?&@VJd=+?+?+A&B5<<XJNNJOO-z/@AB	C #
 [s   AG 3Gc                     | j                   | j                  | j                  | j                  | j                  | j
                  d}||vrt        d| d       ||   ||||      S )N)cardinalityr   r   focalvflr   zLoss z not supported)loss_cardinality
loss_local
loss_boxesloss_labels_focalloss_labels_vflr[   
ValueError)r!   rZ   rH   rI   rJ   rK   loss_maps          r$   get_losszDeimv2Loss.get_lossb   sp    00____++''''
 xuTF.9::x~gwCCr%   c           
         | j                   st        | 	  ||      S |j                         D ci c]  \  }}d|vs|| }}}| j	                  ||      }t        d |D              }t        j                  |gt        j                  t        t        |j                                     j                        }t        j                  |d      j                         }g }g }	d|v r>|d   D ]6  }
| j	                  |
|      }|j                  |       |	j                  |       8 | j!                  ||	      }t        d |D              }t        j                  |gt        j                  t        t        |j                                     j                        }t        j                  |d      j                         }i }| j"                  D ]j  }|dv }|r|n|}|r|n|}| j%                  |||||      }|D ci c]'  }|| j&                  v s|||   | j&                  |   z  ) }}|j)                  |       l d|v rt+        |d         D ]  \  }}
| j"                  D ]  }|dv }|r|n||   }|r|n|}| j%                  ||
|||      }|D ci c]'  }|| j&                  v s|||   | j&                  |   z  ) }}|j                         D ci c]  \  }}|d| z   | }}}|j)                  |         d	|v rd
|vrt-        d      | j/                  |d
   |      }||d
   d   z  }t+        |d	         D ]  \  }}
| j"                  D ]  }| j%                  ||
|||      }|D ci c]'  }|| j&                  v s|||   | j&                  |   z  ) }}|j                         D ci c]  \  }}|d| z   | }}}|j)                  |         |S c c}}w c c}w c c}w c c}}w c c}w c c}}w )a  
        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   8   K   | ]  }t        |d            yw)r+   Nlen).0rN   s     r$   	<genexpr>z%Deimv2Loss.forward.<locals>.<genexpr>   s     @1An-.@   r,   r   )minc              3   8   K   | ]  }t        |d            yw)r   Nr   )r   xs     r$   r   z%Deimv2Loss.forward.<locals>.<genexpr>   s     93qt99r   )r   r   _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.dn_num_group_dn_)r    r   forwarditemsmatcherrG   r5   	as_tensorfloatnextiterrf   r.   clamprb   rg   rx   r   r   r   update	enumerater   get_cdn_matched_indices)r!   rH   rI   kvoutputs_without_auxrJ   rK   cached_indicesri   r   aux_indices
indices_gonum_boxes_gor   rZ   	use_union
indices_innum_boxes_inl_dictrP   
dn_indicesdn_num_boxesr#   s                          r$   r   zDeimv2Loss.forwardo   s;    ((7?7G44 18`1CV^_C_q!t``,,2G< @@@	OOYKu{{4PTU\UcUcUePfKgKnKno	KK	q1668	 ')%,-@%A 5!"ll+<gF%%k2 ''45 00:JK
9j99~U[[QUVZ[b[i[i[kVlQmQtQtu{{<Q7<<> KK 	"D 22I'0gJ+4<)L]]4':|TFBHbQAQUQaQaLaaT%5%5a%888bFbMM&!	" ')(1':M2N(O *$$ KK *D $(: :I/8nQ>OJ3<<)L!]]41BGZYefFJPjQTUY]YiYiTiaT-=-=a-@!@@jFj=C\\^LTQaE!+oq0LFLMM&)** "W,&g5 ^  55g>U6VX_`J$w/F'G'WWL(1':P2Q(R *$$ KK *D!]]41BGZYefFJPjQTUY]YiYiTiaT-=-=a-@!@@jFj<BLLNKDAqaD*na/KFKMM&)	** { a< c kL kKs:   OO:OOO""O"O'
)O-=O-*O2
)	__name__
__module____qualname__r   r[   rx   r   r   __classcell__)r#   s   @r$   r   r      s&    @ "D,DL Lr%   r   c           
         t        |      }|j                  |       | |j                  dd      d}d }|j                  r|	t	        j
                  |j                  dd      |	d   d      \  }}t	        j
                  ||	d   d      \  }}t	        j
                  | |	d   d      \  }}t	        j
                  ||	d   d      \  }}t	        j
                  |
|	d   d      \  }}t	        j
                  ||	d   d      \  }}||d<   |j                  dd      |d	<   n|j                  dd      }|}|
}|}t        |d d d d
f   j                  dd      |d d d d
f   j                  dd      |d d d d
f   j                  dd      |d d d d
f   j                  dd      |d d d
f   |d d d
f         }||d<   |d   j                  t        |g|j                  dd      g             |	ht        j                  dd      j                  dd      j                  dd      j                  dd      |d d d
f   |d d d
f         }||d<   |	|d<    |||      }t        |j                               }|||fS )Nr   r   )r   max)r*   r'   dn_num_splitr   r(   r*   r'   r0   r   r   r   )r   r@   r   auxiliary_lossr5   splitr   	transposeextendr   rG   rf   )r*   labelsr.   r'   r"   outputs_classoutputs_coordenc_topk_logitsenc_topk_bboxesr   predicted_cornersinitial_reference_pointskwargs	criterionoutputs_lossr   dn_out_coordnormal_out_coorddn_out_classnormal_out_classrO   normal_logitsnormal_pred_boxesdn_out_cornersout_cornersdn_out_refsout_refsr   	loss_dictrZ   s                                 r$   Deimv2ForObjectDetectionLossr      s    6"ILL$J4D4DPQ4D4RSL ,-2[[##q#13H3X^_.*L* .3[[H]^lHmst-u*L*  %{{63H3X^_`A}#(;;z;PQ_;`fg#h A */++6GI^_mIntu*v'NK$)KK0HJ_`nJouv$w!K%2L"):)@)@QA)@)NL&,22qa2@,+K/H*QV$..q!4QV$..q!43B3))!Q/QV&&q!,2QU#
 ->()()00?+o.C.Cq.C.Q-RS	
 !,#1&&q!,&&q!,((A.%%a+q"u%QU#$  4HL/04IL01,/Iy!"D---r%   )NNNNNNN)r5   torch.nn.functionalnn
functionalr=   utilsr   loss_d_finer   r   r   loss_for_object_detectionr	   transformers.image_transformsr
   r   r    r%   r$   <module>r      sS        ' A A . F_ _P !G.r%   