
    |-jM              
          d dl Zd dlZd dlmZ d dlmZmZ ddl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 d
ej        dej        dedej        fdZd
ej        dej        dej        fdZd
ededefdZ	 d dej        dej        dej        fdZdededededef
dZ G d de          Z G d de          Zd Z	 	 	 	 	 	 d!dZdS )"    N)Tensornn   )center_to_corners_format)is_scipy_available   )HungarianMatcher	dice_lossgeneralized_box_iou)LwDetrImageLosslinear_sum_assignmentinputslabels	num_masksreturnc                     t          j        d          } || |          }|                    d                                          |z  }|S )a|  
    Args:
        inputs (`torch.Tensor`):
            A float tensor of arbitrary shape.
        labels (`torch.Tensor`):
            A tensor with the same shape as inputs. Stores the binary classification labels for each element in inputs
            (0 for the negative class and 1 for the positive class).

    Returns:
        loss (`torch.Tensor`): The computed loss.
    none	reductionr   )r   BCEWithLogitsLossmeansum)r   r   r   	criterioncross_entropy_losslosss         ^/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/transformers/loss/loss_rf_detr.pysigmoid_cross_entropy_lossr   #   sR     $v666I"6622""1%%))++i7DK    c                 F   | j         d         }t          j        d          } || t          j        |                     } || t          j        |                     }t          j        ||z  |j                  }t          j        ||z  d|z
  j                  }||z   }|S )a  
    A pair wise version of the cross entropy loss, see `sigmoid_cross_entropy_loss` for usage.

    Args:
        inputs (`torch.Tensor`):
            A tensor representing a mask.
        labels (`torch.Tensor`):
            A tensor with the same shape as inputs. Stores the binary classification labels for each element in inputs
            (0 for the negative class and 1 for the positive class).

    Returns:
        loss (`torch.Tensor`): The computed loss between each pairs.
    r   r   r   )shaper   r   torch	ones_like
zeros_likematmulT)	r   r   height_and_widthr   cross_entropy_loss_poscross_entropy_loss_negloss_posloss_negr   s	            r   $pair_wise_sigmoid_cross_entropy_lossr,   7   s     |A$v666I&Yvuv/F/FGG&Yvu/?/G/GHH|25EEvxPPH|25EEF
~VVHhDKr   c                 (   |                                                      d          } dt          j        | |j                  z  }|                     d          dddf         |                    d          dddf         z   }d|dz   |dz   z  z
  }|S )a  
    A pair wise version of the dice loss, see `dice_loss` for usage.

    Args:
        inputs (`torch.Tensor`):
            A tensor representing a mask
        labels (`torch.Tensor`):
            A tensor with the same shape as inputs. Stores the binary classification labels for each element in inputs
            (0 for the negative class and 1 for the positive class).

    Returns:
        `torch.Tensor`: The computed loss between each pairs.
    r   r   N)sigmoidflattenr"   r%   r&   r   )r   r   	numeratordenominatorr   s        r   pair_wise_dice_lossr3   S   s     ^^%%a((FEL222I**R..D)FJJrNN47,CCK	A+/22DKr   Finput_featurespoint_coordinatesc                     |                                 dk    rd}|                    d          }t          j        j        j        | d|z  dz
  fi |}|r|                    d          }|S )a(  
    A wrapper around `torch.nn.functional.grid_sample` to support 3D point_coordinates tensors.

    Args:
        input_features (`torch.Tensor` of shape (batch_size, channels, height, width)):
            A tensor that contains features map on a height * width grid
        point_coordinates (`torch.Tensor` of shape (batch_size, num_points, 2) or (batch_size, grid_height, grid_width,:
        2)):
            A tensor that contains [0, 1] * [0, 1] normalized point coordinates
        add_dim (`bool`):
            boolean value to keep track of added dimension

    Returns:
        point_features (`torch.Tensor` of shape (batch_size, channels, num_points) or (batch_size, channels,
        height_grid, width_grid):
            A tensor that contains features for points in `point_coordinates`.
       Tr          @g      ?)dim	unsqueezer"   r   
functionalgrid_samplesqueeze)r4   r5   add_dimkwargspoint_featuress        r   sample_pointrA   j   s    ( !##-77:: X(4^SK\E\_bEbmmflmmN 3'//22r   logits
num_pointsoversample_ratioimportance_sample_ratioc           	      <   | j         d         }t          ||z            }t          j        ||d| j                  }t          | |d          }t          j        |           }t          ||z            }	||	z
  }
t          j        |dddddf         |	d          d         }t          j        |d|	                    d	          
                    d	d	d                    }|
dk    r3t          j        |t          j        ||
d| j                  gd
          }|S )a8  
    This function is meant for sampling points in [0, 1] * [0, 1] coordinate space based on their uncertainty. The
    uncertainty is calculated for each point using the passed `uncertainty function` that takes points logit
    prediction as input.

    Args:
        logits (`float`):
            Logit predictions for P points.
        uncertainty_function:
            A function that takes logit predictions for P points and returns their uncertainties.
        num_points (`int`):
            The number of points P to sample.
        oversample_ratio (`int`):
            Oversampling parameter.
        importance_sample_ratio (`float`):
            Ratio of points that are sampled via importance sampling.

    Returns:
        point_coordinates (`torch.Tensor`):
            Coordinates for P sampled points.
    r   r   deviceFalign_cornersNr   )kr9   r.   r9   )r!   intr"   randrH   rA   abstopkgatherr:   expandcat)rB   rC   rD   rE   	num_boxesnum_points_sampledr5   point_logitspoint_uncertaintiesnum_uncertain_pointsnum_random_pointsidxs               r   sample_points_using_uncertaintyr[      s=   2 QIZ*::;; 
9.@!FMZZZ(9OOOL!Il3346CDD"%99
*(Aqqq15Iq
Q
Q
QRS
TC%63==;L;L;S;STVXZ\];^;^__1!I
96GSYS` a a ab
 
 
 r   c                   v     e Zd Z	 	 	 	 	 	 ddedededededef fd	Z ej                    d
             Z xZ	S )RfDetrHungarianMatcherr      
class_cost	bbox_cost	giou_costmask_point_sample_ratiocost_mask_class_costcost_mask_dice_costc                 x    t                                          |||           || _        || _        || _        d S N)super__init__rb   cost_mask_classcost_mask_dice)selfr_   r`   ra   rb   rc   rd   	__class__s          r   rh   zRfDetrHungarianMatcher.__init__   s?     	Y	:::'>$31r   c                   #$ |d         j         dd         \  }}|d                             dd                                          }|d                             dd          }|d                             dd          }t          j        d |D                       }	t          j        d	 |D                       }
t          j        d
 |D                       }d}d}d|z
  ||z  z  d|z
  dz                                    z  }|d|z
  |z  z  |dz                                    z  }|dd|	f         |dd|	f         z
  }t          j        |                    t          j                  |
                    t          j                  d          	                    |          }t          t          |          t          |
                     }|j         dd         \  }}||z  | j        z  }t          j        d|d|j                  }|                    |j         d         dd          }|                    d          }t#          ||d          }t          j        |d          }|                    |j                  }|                    |j         d         dd          }|                    d          }t#          ||dd          }t          j        |d          }t)          ||          }t+          ||          }| j        |z  | j        |z  z   | j        |z  z   | j        |z  z   | j        |z  z   }|                    ||d                                          }t          j        |j                  j        ||                                |                                 z  <   d |D             }g }||z  $|!                    $d          }tE          |          D ]]#|#         } d tG          | !                    |d                    D             }!#dk    r|!}@#$fdtI          ||!          D             }^d |D             }"|"S )a  
        Differences:
        - out_prob = outputs["logits"].flatten(0, 1).sigmoid() instead of softmax
        - class_cost uses alpha and gamma
        - Additionally, mask cost is computed using pair-wise sigmoid cross entropy loss and dice loss
        rB   Nr   r   r   
pred_boxes
pred_masksc                     g | ]
}|d          S )class_labels .0vs     r   
<listcomp>z2RfDetrHungarianMatcher.forward.<locals>.<listcomp>   s    CCCa. 1CCCr   c                     g | ]
}|d          S )boxesrr   rs   s     r   rv   z2RfDetrHungarianMatcher.forward.<locals>.<listcomp>   s     = = =7 = = =r   c                     g | ]
}|d          S masksrr   rs   s     r   rv   z2RfDetrHungarianMatcher.forward.<locals>.<listcomp>   s    !>!>!>!G*!>!>!>r   g      ?r8   g:0yE>)prG   FrI   )r.   r   nearestrJ   moder.   c                 8    g | ]}t          |d                    S rz   lenrs   s     r   rv   z2RfDetrHungarianMatcher.forward.<locals>.<listcomp>  s"    222QQwZ222r   rL   c                 >    g | ]\  }}t          ||                   S rr   r   )rt   ics      r   rv   z2RfDetrHungarianMatcher.forward.<locals>.<listcomp>  s)    sssTQ21Q488sssr   c                     g | ]O\  }}t          j        |d          |d          z  z   g          t          j        |d         |d         g          fPS )r   r   )npconcatenate)rt   indice1indice2group_idgroup_num_queriess      r   rv   z2RfDetrHungarianMatcher.forward.<locals>.<listcomp>  sq       
 ) 
GAJARU]A]4]'^__
GAJ'?@@  r   c                     g | ]E\  }}t          j        |t           j                   t          j        |t           j                   fFS ))dtype)r"   	as_tensorint64)rt   r   js      r   rv   z2RfDetrHungarianMatcher.forward.<locals>.<listcomp>  sR     
 
 
_c_`bcU_Qek222EOAU[4Y4Y4YZ
 
 
r   )%r!   r0   r/   r"   rS   logcdisttofloat32type_asr   r   rb   rN   rH   repeatr:   rA   r=   r   r,   r3   r`   r_   ra   ri   rj   viewcpufinfomaxisinfisnansplitrange	enumeratezip)%rk   outputstargets
group_detr
batch_sizenum_queriesout_probout_bbox	out_masks
target_idstarget_bboxtarget_masksalphagammaneg_cost_classpos_cost_classr_   r`   ra   heightwidthrC   point_coordspred_point_coordspred_masks_logitstarget_point_coordsri   rj   cost_matrixsizesindicescost_matrix_listgroup_cost_matrixgroup_indicesmatched_indicesr   r   s%                                      @@r   forwardzRfDetrHungarianMatcher.forward   s    #*("3"9"1""=
K 8$,,Q22::<<<(00A66L)11!Q77	 YCC7CCCDD
i = =W = = =>>y!>!>g!>!>!>?? e)%8a(lT>Q=V=V=X=X<XY1x<E"9:4?T?T?V?V>VW#AAAzM2^AAAzM5RR
 KEM : :KNN5=<Y<Y]^___gghpqq	 ))A()K)KMefqMrMrsss	 !rr*e^t'CC
z!Z9;KLLL(//	0BAqII''**	(4EUZ[[[!M*;WEE#y77*11,2DQ2GANN#--a00#L2EUZajkkk}\7;;>?PR^__,->MM NY&o
*+ny() "_45 !N2	3 	 "&&z;CCGGII BG[M^A_A_AcK%%''+*;*;*=*==> 32'222':5&,,->A,FFj)) 	 	H 0 :ssYO`OfOfglnpOqOqErErsssM1}}'    
 -0,G,G  
 
gn
 
 
 r   )r   r   r   r^   r   r   )
__name__
__module____qualname__floatrM   rh   r"   no_gradr   __classcell__rl   s   @r   r]   r]      s         ')&'%&2 22 2 	2
 "%2 $2 #2 2 2 2 2 2 U]__U U _U U U U Ur   r]   c                   *     e Zd Z fdZd Zd Z xZS )RfDetrImageLossc                 `    t                                          |||||           || _        d S rf   )rg   rh   rb   )rk   matchernum_classesfocal_alphalossesr   rb   rl   s          r   rh   zRfDetrImageLoss.__init__&  s1    +{FJOOO'>$$$r   c                    d|vrt          d          |                     |          }|d         |         }|                                dk    r)t          j        |          t          j        |          dS t          j        d t          ||          D             d          }|                    d          }|                    d                                          }t          |j
        d         |j
        d         |j
        d	         z  | j        z            }t          j                    5  t          ||d
d          }	t          ||	dd                              d          }
ddd           n# 1 swxY w Y   t          ||	d                              d          }t!          ||
|          t#          ||
|          d}|S )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].
        ro   z#No predicted masks found in outputsr   )loss_mask_celoss_mask_dicec                 6    g | ]\  }\  }}|d          |         S rz   rr   )rt   t_r   s       r   rv   z.RfDetrImageLoss.loss_masks.<locals>.<listcomp><  s(    !W!W!WIAv1!G*Q-!W!W!Wr   rL   r   r.   r7   g      ?Fr}   r~   NrI   )KeyError_get_source_permutation_idxnumelr"   r$   rS   r   r:   r   r   r!   rb   r   r[   rA   r=   r   r
   )rk   r   r   r   rT   
source_idxsource_masksr   rC   r   point_labelsrV   r   s                r   
loss_maskszRfDetrImageLoss.loss_masks*  s*    w&&@AAA55g>>
|,Z81$$ % 0 > >"'"2<"@"@   y!W!WWgAVAV!W!W!W]^___#--a00#--a006688 r"L$6r$:\=OPR=S$SW[Ws$s
 

 ]__ 	t 	t:<UVX\]]L'lRW^ghhhppqrssL		t 	t 	t 	t 	t 	t 	t 	t 	t 	t 	t 	t 	t 	t 	t $L,eTTT\\]^__ 7|\S\]]'lINN
 
 s   19E66E:=E:c           
      
   | j         r| j        nd}d |                                D             }|                     |||          }t	          d |D                       }||z  }t          j        |gt
          j        t          t          |
                                                    j                  }d}t          j                    rKt          j                    r8t          j        |t          j        j                   t          j                    }t          j        ||z  d                                          }i }| j        D ].}	|                    |                     |	||||                     /d|v rt1          |d                   D ]t\  }
|                     |
||          }| j        D ]P}	|                     |	|
|||          }fd|                                D             }|                    |           Qud	|v rv|d	         }|                     |||
          }| j        D ]N}	|                     |	||||          }d |                                D             }|                    |           O|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.
        r   c                 2    i | ]\  }}|d k    |dk    ||S )enc_outputsauxiliary_outputsrr   rt   rK   ru   s      r   
<dictcomp>z+RfDetrImageLoss.forward.<locals>.<dictcomp>`  s:     '
 '
 '
Q]0B0BqL_G_G_AqG_G_G_r   c              3   @   K   | ]}t          |d                    V  dS )rq   Nr   )rt   r   s     r   	<genexpr>z*RfDetrImageLoss.forward.<locals>.<genexpr>h  s/      @@1An-..@@@@@@r   )r   rH   )op)minr   c                 (    i | ]\  }}|d  z   |S r   rr   rt   rK   ru   r   s      r   r   z+RfDetrImageLoss.forward.<locals>.<dictcomp>}  s)    HHHAa'a''k1HHHr   r   )r   c                      i | ]\  }}|d z   |S _encrr   r   s      r   r   z+RfDetrImageLoss.forward.<locals>.<dictcomp>  s"    CCCDAq!f*aCCCr   )trainingr   itemsr   r   r"   r   r   nextitervaluesrH   distis_availableis_initialized
all_reduceReduceOpSUMget_world_sizeclampitemr   updateget_lossr   )rk   r   r   r   outputs_without_aux_and_encr   rT   
world_sizer   r   r   l_dictr   r   s                @r   r   zRfDetrImageLoss.forwardT  s    )-<T__1
'
 '
$]]__'
 '
 '
#
 ,,:GZPP @@@@@@@	
*	OYKu{4PTU\UcUcUeUePfPfKgKgKnooo	
 	/4#6#8#8 	/OI$-*;<<<<,..JK	J 6A>>>CCEE	 K 	U 	UDMM$--gwSSTTTT '))(1':M2N(O(O * *$$,,'8':NN K * *D!]]41BGWV_``FHHHHHHHFMM&))))*
 G##!-0Kll;JlOOG & &t['7IVVCCFLLNNCCCf%%%%r   )r   r   r   rh   r   r   r   r   s   @r   r   r   %  sW        ? ? ? ? ?( ( (T4 4 4 4 4 4 4r   r   c                 h    d t          | d d         |d d         |d d                   D             S )Nc                 "    g | ]\  }}}|||d S )rB   rn   ro   rr   )rt   abr   s       r   rv   z!_set_aux_loss.<locals>.<listcomp>  s8       Aq! AQ77  r   r.   )r   )outputs_classoutputs_coordoutputs_maskss      r   _set_aux_lossr    sM     ="-}SbS/A=QTRTQTCUVV   r   c                 P   t          |j        |j        |j        |j        |j        |j                  }g d}t          ||j        |j	        ||j
        |j                  }|                    |           i }d }| |d<   ||d<   ||d<   |	|
|d|d<   |j        rt          |||          }||d	<    |||          |j        |j        d
|j        d<   |j        d<   |j        d<   |j        ri }t#          |j        dz
            D ]5|                    fd                                D                        6|                    d                                 D                                            |           t+          fdD                       }||fS )N)r_   r`   ra   rb   rc   rd   )r   rx   cardinalityr{   )r   r   r   r   r   rb   rB   rn   ro   r   r   r   )loss_ce	loss_bbox	loss_giour   r   r   c                 (    i | ]\  }}|d  z   |S r   rr   r   s      r   r   z-RfDetrForSegmentationLoss.<locals>.<dictcomp>  s)    #S#S#Stq!AAK#S#S#Sr   c                      i | ]\  }}|d z   |S r   rr   r   s      r   r   z-RfDetrForSegmentationLoss.<locals>.<dictcomp>  s"    NNN$!QF
ANNNr   c              3   B   K   | ]}|v |         |         z  V  d S rf   rr   )rt   rK   	loss_dictweight_dicts     r   r   z,RfDetrForSegmentationLoss.<locals>.<genexpr>  s:      TT1CSCSy|k!n,CSCSCSCSTTr   )r]   r_   r`   ra   rb   mask_class_loss_coefficientmask_dice_loss_coefficientr   
num_labelsr   r   r   auxiliary_lossr  class_loss_coefficientbbox_loss_coefficientgiou_loss_coefficientr   decoder_layersr   r   r   )rB   r   rH   rn   ro   configr  r  r  enc_outputs_classenc_outputs_coordenc_outputs_masksr?   r   r   r   outputs_lossr   aux_weight_dictr   r   r  r  s                       @@@r   RfDetrForSegmentationLossr    s*     %$"" & >#?"=  G 988F%&$ & >  I LLL#L!+L!+L#''# #L
  >)-VV,=()	,//I$;&JfggK%;K"("DK$*$EK ! ,v,q011 	U 	UA""#S#S#S#S{?P?P?R?R#S#S#STTTTNN+:K:K:M:MNNNOOO?+++TTTTTiTTTTTD---r   )F)NNNNNN)numpyr   r"   torch.distributeddistributedr   r   r   image_transformsr   utilsr   loss_for_object_detectionr	   r
   r   loss_lw_detrr   scipy.optimizer   rM   r   r,   r3   rA   r   r[   r]   r   r  r  rr   r   r   <module>r(     s                            7 7 7 7 7 7 & & & & & &         
 * ) ) ) ) )  5444444u| U\ VY ^c^j    ( u| X]Xd    8  6    0 LQ L5:\
\   B.. #.7:.UZ.. . . .bf f f f f- f f fRc c c c co c c cL   ?. ?. ?. ?. ?. ?.r   