
    Z j<                     "   S SK rS SKrS SKJr  SSKJrJrJr  SSK	J
r
JrJrJrJrJrJr  \" 5       (       a  S SKJr  \" 5       (       a  S SKJr  \" 5       (       a  S SKJr  S S	KJr   " S
 S\
5      r " S S\R4                  5      r    SS jrg)    N   )is_accelerate_availableis_scipy_availableis_vision_available   )HungarianMatcher_set_aux_lossbox_iou	dice_lossgeneralized_box_iounested_tensor_from_tensor_listsigmoid_focal_loss)center_to_corners_format)linear_sum_assignment)PartialState)reducec                   B    \ rS rSr\R
                  " 5       S 5       rSrg)LwDetrHungarianMatcher*   c                    US   R                   SS u  pEUS   R                  SS5      R                  5       nUS   R                  SS5      n[        R                  " U Vs/ s H  oS   PM	     sn5      n	[        R                  " U Vs/ s H  oS   PM	     sn5      n
S	nS
nSU-
  Xl-  -  SU-
  S-   R                  5       * -  nUSU-
  U-  -  US-   R                  5       * -  nUSS2U	4   USS2U	4   -
  nUR                  nUR                  [        R                  5      nU
R                  [        R                  5      n
[        R                  " XzSS9nUR                  U5      n[        [        U5      [        U
5      5      * nU R                  U-  U R                  U-  -   U R                  U-  -   nUR                  XES5      R!                  5       nU Vs/ s H  n[#        US   5      PM     nn/ nXS-  nUR%                  USS9n['        U5       H  nUU   n[)        UR%                  US5      5       VVs/ s H  u  nn[+        UU   5      PM     nnnUS:X  a  UnMN  [-        UU5       VVs/ s HH  u  nn[.        R0                  " US   US   UU-  -   /5      [.        R0                  " US   US   /5      4PMJ     nnnM     U VVs/ s HL  u  nn[        R2                  " U[        R4                  S9[        R2                  " U[        R4                  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 s  snnf )zz
Differences:
- out_prob = outputs["logits"].flatten(0, 1).sigmoid() instead of softmax
- class_cost uses alpha and gamma
logitsNr   r   r   
pred_boxesclass_labelsboxesg      ?g       @g:0yE>)pdim)dtype)shapeflattensigmoidtorchcatlogr   tofloat32cdistr   r   	bbox_cost
class_cost	giou_costviewcpulensplitrange	enumerater   zipnpconcatenate	as_tensorint64) selfoutputstargets
group_detr
batch_sizenum_queriesout_probout_bboxv
target_idstarget_bboxalphagammaneg_cost_classpos_cost_classr*   r   r)   r+   cost_matrixsizesindicesgroup_num_queriescost_matrix_listgroup_idgroup_cost_matrixicgroup_indicesindice1indice2js                                    o/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/loss/loss_lw_detr.pyforwardLwDetrHungarianMatcher.forward+   sF    #*("3"9"9"1"=
 8$,,Q2::<<(00A6 YY7C7a. 17CD
iiW =W7W => e)8a(lT>Q=V=V=X<XY1x<E"9:4?T?T?V>VW#AzM2^AzM5RR
 ;;u}}-!nnU]]3KK;	LL'	 ))A()KMefqMrss	 nny04??Z3OORVR`R`clRll!&&zCGGI*12'QQwZ'2'5&,,->A,Fj)H 0 :ENO`OfOfglnpOqErsErTQ21Q48ErMs1}' -0,G
 -H( 
GAJARU]A]4]'^_
GAJ'?@ -H   * lsskrcgcdfg%++6QVQ\Q\8]^krssS  D =. 3 t ts&   !L$
L)L.;L32AL9AL? N)__name__
__module____qualname____firstlineno__r#   no_gradrT   __static_attributes__rV       rS   r   r   *   s    
]]_6t 6tr]   r   c                      ^  \ rS rSrU 4S jrS r\R                  " 5       S 5       rS r	S r
S rS rS	 rS
 rSrU =r$ )LwDetrImageLosse   c                 ^   > [         TU ]  5         Xl        X l        X0l        X@l        XPl        g N)super__init__matchernum_classesfocal_alphalossesr:   )r7   re   rf   rg   rh   r:   	__class__s         rS   rd   LwDetrImageLoss.__init__f   s)    &&$r]   c           	         SU;  a  [        S5      eUS   nUR                  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U R                  nS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[        R                  " [        [        UR                  5       5      [        U5      5      S   5      nUR                  U5      nUR                  5       R                  5       nUR                  5       n[        R                  " U5      nUR                  U5      R                  U5      nX{4-   nUU   R                  U5      UR                  S	U-
  5      -  n[        R                   " US
5      R                  5       R                  U5      nUUU'   S	U-
  UU'   U* UR#                  5       -  US	U-
  R#                  5       -  -
  nUR%                  5       U-  nSU0nU$ s  sn
n	nf s  snn	nf )Nr   z#No logits were found in the outputsr   r   r   r   r   r   r   g{Gz?loss_ce)KeyErrorr   _get_source_permutation_idxr#   r$   r2   rg   diagr
   r   detachr&   cloner"   
zeros_likepowclampr%   sum)r7   r8   r9   rH   	num_boxessource_logitsr   idxt_Jtarget_classes_orB   rC   	src_boxesrM   target_boxesiou_targetspos_iousprobpos_weightsneg_weightspos_indpos_qualityrl   rh   s                             rS   loss_labelsLwDetrImageLoss.loss_labelso   s   7"@AA)##..w7 99CPWLa%bLayq&1a&7&:La%bc  L)#.	yyWAV!WAVIAv!G*Q-AV!W]^_jj,Y-=-=-?@BZ[gBhijkl
 "nnU+$$&--/$$&&&}5hhuo((/++7m''.a%i1HHkk+t4;;=@@G*G ;G,+kQXNN<L.LL++-)+W%7 &c "Xs   H2(H9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   r   )devicer   g      ?r   cardinality_error)r   r#   r5   r.   r"   maxvaluesru   nn
functionall1_lossfloat)r7   r8   r9   rH   rv   r   r   r?   target_lengths	card_predcard_errrh   s               rS   loss_cardinality LwDetrImageLoss.loss_cardinality   s     "')R'Q#a.?*@')R[ab^^%))"-44s:??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  pUS   U	   PM     sn	nnSS9n
[
        R                  R                  XjSS9n0 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   z#No predicted boxes found in outputsr   r   r   none)	reduction	loss_bboxr   	loss_giou)rm   rn   r#   r$   r2   r   r   r   ru   ro   r   r   )r7   r8   r9   rH   rv   rx   source_boxesry   rz   rM   r~   r   rh   r   s                 rS   
loss_boxesLwDetrImageLoss.loss_boxes   s     w&@AA..w7|,S1yyWAV!WAVIAv!G*Q-AV!W]^_MM)),PV)W	'mmo	9{

 8 FH`amHno
 
	 (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)rm   rn   _get_target_permutation_idxr   	decomposer&   r   r   interpolater    r!   r,   r   r   )r7   r8   r9   rH   rv   
source_idx
target_idxsource_masksry   r   target_masksvalidrh   s                rS   
loss_masksLwDetrImageLoss.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                    [         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 rb   r#   r$   r1   	full_like)r7   rH   rM   sourcerz   	batch_idxr   s          rS   rn   +LwDetrImageLoss._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 rb   r   )r7   rH   rM   rz   targetr   r   s          rS   r   +LwDetrImageLoss._get_target_permutation_idx   sh    IIPYZaPbcPbna!uv9Pbcd	YY'B';A'BC
$$ dBr   c                     U R                   U R                  U R                  U R                  S.nX;  a  [	        SU S35      eXa   " X#XE5      $ )N)labelscardinalityr   r   zLoss z not supported)r   r   r   r   
ValueError)r7   lossr8   r9   rH   rv   loss_maps          rS   get_lossLwDetrImageLoss.get_loss   sT    &&00____	
 uTF.9::~gCCr]   c           
      $   U R                   (       a  U R                  OSnUR                  5        VVs0 s H  u  pEUS:w  d  M  US:w  d  M  XE_M     nnnU R                  XbU5      n[	        S U 5       5      nX-  n[
        R                  " U/[
        R                  [        [        UR                  5       5      5      R                  S9nSn	[        5       (       a3  [        R                  0 :w  a  [        U5      n[        5       R                   n	[
        R"                  " X-  SS9R%                  5       n0 n
U R&                   H%  nU
R)                  U R+                  XX'U5      5        M'     SU;   a  [-        US   5       H  u  pU R                  XU5      nU R&                   HZ  nUS:X  a  M  U R+                  XX'U5      nUR                  5        VVs0 s H  u  pEUSU 3-   U_M     nnnU
R)                  U5        M\     M     SU;   at  US   nU R                  XUS	9nU R&                   HO  nU R+                  XX'U5      nUR                  5        VVs0 s H  u  pEUS
-   U_M     nnnU
R)                  U5        MQ     U
$ s  snnf s  sn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.
r   enc_outputsauxiliary_outputsc              3   >   #    U  H  n[        US    5      v   M     g7f)r   N)r.   ).0ry   s     rS   	<genexpr>*LwDetrImageLoss.forward.<locals>.<genexpr>  s     @1An-..s   )r   r   )minr   rz   )r:   _enc)trainingr:   itemsre   ru   r#   r5   r   nextiterr   r   r   r   _shared_stater   num_processesrt   itemrh   updater   r1   )r7   r8   r9   r:   kr?   outputs_without_aux_and_encrH   rv   
world_sizerh   r   rM   r   l_dictr   s                   rS   rT   LwDetrImageLoss.forward   sP    )-T__1
$]]_'
,TQ]0BDqL_G_DAD_ 	$ '

 ,,:ZP @@@	*	OOYKu{{4PTU\UcUcUePfKgKnKno	
"$$))R/"9-	)^99
KK	 6A>CCE	 KKDMM$--wST   ')(1':M2N(O$,,'8:N KKDw !]]4GV_`F9?HaAaS'k1nFHMM&) ( )P G#!-0Kll;JlOGt'IV4:LLNCNDA!f*a-NCf% $
 W'
@ I Ds   J J J J
J)rg   r:   rh   re   rf   )rW   rX   rY   rZ   rd   r   r#   r[   r   r   r   rn   r   r   rT   r\   __classcell__)ri   s   @rS   r_   r_   e   sM    %"H ]]_  2D%%	D7 7r]   r_   c	           
      L  ^^ [        UR                  UR                  UR                  S9n
/ SQn[	        U
UR
                  UR                  UUR                  S9nUR                  U5        0 nS nXS'   X=S'   UUS.US'   UR                  (       a  [        XV5      nXS'   U" X5      mS	UR                  S
.mUR                  TS'   UR                  (       an  0 n[        UR                  S	-
  5       H?  nUR                  TR!                  5        VVs0 s H  u  nnUSU 3-   U_M     snn5        MA     TR                  U5        TR!                  5        VVs0 s H  u  nnUS-   U_M     nnnTR                  U5        [#        UU4S jT 5       5      nUTU4$ s  snnf s  snnf )N)r*   r)   r+   )r   r   r   )re   rf   rg   rh   r:   r   r   )r   r   r   r   r   )rl   r   r   rz   r   c              3   H   >#    U  H  oT;   d  M
  TU   TU   -  v   M     g 7frb   rV   )r   r   	loss_dictweight_dicts     rS   r   /LwDetrForObjectDetectionLoss.<locals>.<genexpr>b  s&     TiCS,y|k!n,is   	"")r   r*   r)   r+   r_   
num_labelsrg   r:   r&   auxiliary_lossr	   bbox_loss_coefficientgiou_loss_coefficientr0   decoder_layersr   r   ru   )r   r   r   r   configoutputs_classoutputs_coordenc_outputs_classenc_outputs_coordkwargsre   rh   	criterionoutputs_lossr   aux_weight_dictrM   r   r?   enc_weight_dictr   r   r   s                        @@rS   LwDetrForObjectDetectionLossr   1  s    %$$0@0@FL\L\G 0F%%&&$$I LLL#!+#'#L )-G,=(),/If.J.JKK%;;Kv,,q01A""{?P?P?R#S?Rtq!A!A3KN?R#ST 2?+1<1B1B1DE1DAq6z1}1DOE'TiTTD--- $TEs   FF )NNNN)numpyr3   r#   torch.nnr   utilsr   r   r   loss_for_object_detectionr   r	   r
   r   r   r   r   transformers.image_transformsr   scipy.optimizer   
accelerater   accelerate.utilsr   r   Moduler_   r   rV   r]   rS   <module>r      s       T T   F 4''8t- 8tvIbii Id 2.r]   