
    |-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dS )    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
        | _
        d S )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     ]/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/transformers/loss/loss_deimv2.pyr   zDeimv2Loss.__init__   sr       .00..
 
 0//)$*$?!!!    c                    |                      |          }|d         |         }t          j        d t          ||          D             d          }t	          t          |          t          |                    \  }}	t          j        |                                          }|d         }
t          j        d t          ||          D                       }t          j        |
j	        dd         | j
        t          j        |
j        	          }|||<   t          j        || j
        d
z             dddf         }t          j        ||
j                  }|                    |j                  ||<   |                    d          |z  }t          j        |
                                          }|                    | j                  }| j        ,| j        |                    | j                  z  d
|z
  z  |z   }n#|                    | j                  d
|z
  z  |z   }t          j        |
||d          }|                    d
                                          |
j	        d
         z  |z  }d|iS )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_boxesc                 6    g | ]\  }\  }}|d          |         S )r    .0t_is       r$   
<listcomp>z.Deimv2Loss.loss_labels_mal.<locals>.<listcomp>1   s(    !W!W!WIAv1!G*Q-!W!W!Wr%   r   dimlogitsc                 6    g | ]\  }\  }}|d          |         S )class_labelsr)   r*   s       r$   r/   z.Deimv2Loss.loss_labels_mal.<locals>.<listcomp>6   s*    ,i,i,iia!QQ~->q-A,i,i,ir%   Nr   dtypedevicer   )num_classes.)r6   none)weight	reductionr   )_get_source_permutation_idxtorchcatzipr	   r
   diagdetachfullshaper8   int64r7   Fone_hot
zeros_liker6   to	unsqueezesigmoidpowgammar    binary_cross_entropy_with_logitsmeansum)r!   outputstargetsindices	num_boxesidx	src_boxestarget_boxesiousr-   
src_logitstarget_classes_originaltarget_classestargettarget_score_originaltarget_score
pred_scorer;   losss                      r$   loss_labels_malzDeimv2Loss.loss_labels_mal*   sY    ..w77L)#.	y!W!WWgAVAV!W!W!W]^___29==?WXd?e?effaz$&&((X&
"'),i,iSVW^`gShSh,i,i,i"j"jRaR $"2%+jN_
 
 
 6s>t7G!7KLLLSRUSURUXV % 0zGW X X X%)WW-B-H%I%Ic",66r::VCYz**1133
#''
33>%^jnnTZ&@&@@AJORXXFF^^DJ//1v:>GF1*lSYeklllyy||!!J$4Q$77)CD!!r%   c                    g }|D ]@}d t          |                                |                                          D             }Ad |D             D ].}t          j        |dd          \  }}t          j        |d          }||         }	i }
|	D ]?}|d                                         |d                                         }}||
vr||
|<   @t          j        t          |
                                          |j	                  }t          j        t          |

                                          |j	                  }|                    |                                |                                f           0|S )	Nc                     g | ]I\  }}t          j        |d          |d          g          t          j        |d         |d         g          fJS )r   r   r>   r?   )r+   idx1idx2s      r$   r/   z5Deimv2Loss._get_dense_o2o_indices.<locals>.<listcomp>O   s_       D$ DGT!W-..	47DG:L0M0MN  r%   c                 |    g | ]9}t          j        |d          dddf         |d         dddf         gd          :S )r   Nr   rd   )r+   rU   s     r$   r/   z5Deimv2Loss._get_dense_o2o_indices.<locals>.<listcomp>T   sI    ZZZ3eiQ4#a&D/ BAFFZZZr%   Tr   )return_countsr1   )
descendingr   )r7   )r@   copyr>   uniqueargsortitemtensorlistkeysr7   valuesappendlong)r!   rS   indices_aux_listresultsindices_auxindexrk   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 "%gllnnk6F6F6H6H"I"I  GG
 [ZRYZZZ 	C 	CE"\%tKKKNFF!&v$!G!G!G"#56MM) 5 5#+A;#3#3#5#5x{7G7G7I7I-//-4M'*d=+=+=+?+?&@&@VVVJd=+?+?+A+A&B&B5<XXXJNNJOO--z/@/@ABBBBr%   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_vflra   
ValueError)r!   r`   rQ   rR   rS   rT   loss_maps          r$   get_losszDeimv2Loss.get_lossb   sq    0__+''
 
 x9T999:::x~gwCCCr%   c           
      `     j         s"t                                          ||          S d |                                D             }                     ||          }t          d |D                       }t          j        |gt          j        t          t          |                                                    j                  }t          j        |d                                          }g }g }d|v rK|d         D ]B}                     ||          }	|                    |	           |                    |	           C                     ||          }
t          d |
D                       }t          j        |gt          j        t          t          |                                                    j                  }t          j        |d                                          }i } j        D ]O}|dv }|r|
n|}|r|n|}                     |||||           fd	D             |                               Pd|v rt)          |d                   D ]\  } j        D ]u}|dv }|r|
n|         }|r|n|}                     |||||           fd
D             fd                                D             |                               vd|v rd|vrt+          d                               |d         |          }||d         d         z  }t)          |d                   D ]l\  } j        D ]_}                     |||||           fdD             fd                                D             |                               `m|S )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.
        c                 "    i | ]\  }}d |v	||S )auxiliary_outputsr)   )r+   kvs      r$   
<dictcomp>z&Deimv2Loss.forward.<locals>.<dictcomp>~   s*    ```1CV^_C_C_q!C_C_C_r%   c              3   @   K   | ]}t          |d                    V  dS )r4   Nlen)r+   r,   s     r$   	<genexpr>z%Deimv2Loss.forward.<locals>.<genexpr>   s/      @@1An-..@@@@@@r%   r5   r   )minr   c              3   @   K   | ]}t          |d                    V  dS )r   Nr   )r+   xs     r$   r   z%Deimv2Loss.forward.<locals>.<genexpr>   s,      993qt99999999r%   )r   r   c                 P    i | ]"}|j         v ||         j         |         z  #S r)   r   r+   r   l_dictr!   s     r$   r   z&Deimv2Loss.forward.<locals>.<dictcomp>   s:    bbbQAQUQaLaLaaT%5a%88LaLaLar%   c                 P    i | ]"}|j         v ||         j         |         z  #S r)   r   r   s     r$   r   z&Deimv2Loss.forward.<locals>.<dictcomp>   ;    jjjQTUY]YiTiTiaT-=a-@!@TiTiTir%   c                 (    i | ]\  }}|d  z   |S )_aux_r)   r+   r   r   r.   s      r$   r   z&Deimv2Loss.forward.<locals>.<dictcomp>   s)    LLLTQa+!++oqLLLr%   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_groupc                 P    i | ]"}|j         v ||         j         |         z  #S r)   r   r   s     r$   r   z&Deimv2Loss.forward.<locals>.<dictcomp>   r   r%   c                 (    i | ]\  }}|d  z   |S )_dn_r)   r   s      r$   r   z&Deimv2Loss.forward.<locals>.<dictcomp>   s)    KKKDAqa***naKKKr%   )r    r   forwarditemsmatcherrP   r>   	as_tensorfloatnextiterrq   r7   clamprm   rr   r   r   r   update	enumerater   get_cdn_matched_indices)r!   rQ   rR   outputs_without_auxrS   rT   cached_indicesrt   r   aux_indices
indices_gonum_boxes_gor   r`   	use_union
indices_innum_boxes_in
dn_indicesdn_num_boxesr.   r   r#   s   `                  @@r$   r   zDeimv2Loss.forwardo   sL    ( 	577??7G444 a````,,2G<< @@@@@@@	OYKu{4PTU\UcUcUeUePfPfKgKgKnooo	K	q1116688	 '))%,-@%A 5 5!"ll+<gFF%%k222 ''4444 00:JKK
99j99999~U[QUVZ[b[i[i[k[kVlVlQmQmQtuuu{<Q777<<>> K 	" 	"D 22I'0=gJ+4C<<)L]]4':|TTFbbbbb&bbbFMM&!!!! '))(1':M2N(O(O * *$$ K * *D $(: :I/8!OnQ>OJ3<#K<<)L!]]41BGZYeffFjjjjj&jjjFLLLLV\\^^LLLFMM&))))* "W,,&g55 ^   55g>U6VX_``J$w/F'G'WWL(1':P2Q(R(R * *$$ K * *D!]]41BGZYeffFjjjjj&jjjFKKKKFLLNNKKKFMM&))))	* r%   )	__name__
__module____qualname__r   ra   r   r   r   __classcell__)r#   s   @r$   r   r      s        @ @ @ @ @ "  "  "D  ,D D DL L L L L L L L Lr%   r   c           
          t          |          }|                    |           | |                    dd          d}d }|j        r|	t	          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<   |                    dd          |d	<   n|                    dd          }|}|
}|}t          |d d d d
f                             dd          |d d d d
f                             dd          |d d d d
f                             dd          |d d d d
f                             dd          |d d d
f         |d d d
f                   }||d<   |d                             t          |g|                    dd          g                     |	t          |                    dd          |                    dd          |                    dd          |                    dd          |d d d
f         |d d d
f                   }||d<   |	|d<    |||          }t          |                                          }|||fS )Nr   r   )r   max)r2   r'   dn_num_splitr   r0   r2   r'   r9   r   r   r   )r   rI   r   auxiliary_lossr>   splitr   	transposeextendr   rP   rq   )r2   labelsr7   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_classr-   normal_logitsnormal_pred_boxesdn_out_cornersout_cornersdn_out_refsout_refsr   	loss_dictr`   s                                 r$   Deimv2ForObjectDetectionLossr      s    6""ILL$J4D4DPQ4D4R4RSSL -J ,-2[##q#113H3X^_. . .*L* .3[H]^lHmst-u-u-u*L*  %{63H3X^_```A}#(;z;PQ_;`fg#h#h#h A */+6GI^_mIntu*v*v*v'NK$)K0HJ_`nJouv$w$w$w!K%2L"):)@)@QA)@)N)NL&&,22qa2@@,+K/H*QQQV$..q!44QQQV$..q!443B3))!Q//QQQV&&q!,,2QQQU#
 
 ->()()00?+o.C.Cq.C.Q.Q-RSS	
 	
 	
 !,#1&&q!,,&&q!,,((A..%%a++qqq"u%QQQU#$ $  4HL/04IL01	,//Iy!!""D---r%   )NNNNNNN)r>   torch.nn.functionalnn
functionalrF   utilsr   loss_d_finer   r   r   loss_for_object_detectionr	   transformers.image_transformsr
   r   r   r)   r%   r$   <module>r      s              ' ' ' ' ' ' A A A A A A A A A A . . . . . .  GFFFFFF_ _ _ _ _ _ _ _P !G. G. G. G. G. G.r%   