
    ^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y)!    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      } || |      }|j                  d      j                         |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         i/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/loss/loss_rf_detr.pysigmoid_cross_entropy_lossr   #   sD     $$v6I"662""1%))+i7DK    c                 \   | 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6I&vuv/FG&vu/?/?/GH||25EEvxxPH||25EEF
~~VHhDKr   c                    | j                         j                  d      } dt        j                  | |j                        z  }| j                  d      dddf   |j                  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_lossr2   S   s|     ^^%%a(FELL22I**R.D)FJJrN47,CCK	A+/22DKr   input_featurespoint_coordinatesc                     |j                         dk(  rd}|j                  d      }t        j                  j                  j
                  | d|z  dz
  fi |}|r|j                  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)r3   r4   add_dimkwargspoint_featuress        r   sample_pointr@   j   st    ( !#-77: XX((44^SK\E\_bEbmflmN'//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|j                  d	      j                  d	d	d            }|
dkD  r:t        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   )kr8   r-   r8   )r    intr!   randrG   r@   abstopkgatherr9   expandcat)rA   rB   rC   rD   	num_boxesnum_points_sampledr4   point_logitspoint_uncertaintiesnum_uncertain_pointsnum_random_pointsidxs               r   sample_points_using_uncertaintyrZ      s   2 QIZ*::; 

9.@!FMMZ(9OL!IIl346CD"%99
**(Aq15Iq
QRS
TC%63==;L;S;STVXZ\];^_1!II

96GSYS`S` ab
 r   c                   t     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 )
RfDetrHungarianMatcher
class_cost	bbox_cost	giou_costmask_point_sample_ratiocost_mask_class_costcost_mask_dice_costc                 R    t         |   |||       || _        || _        || _        y N)super__init__r`   cost_mask_classcost_mask_dice)selfr]   r^   r_   r`   ra   rb   	__class__s          r   rf   zRfDetrHungarianMatcher.__init__   s.     	Y	:'>$31r   c                 	   |d   j                   dd \  }}|d   j                  dd      j                         }|d   j                  dd      }|d   j                  dd      }t        j                  |D 	cg c]  }	|	d   	 c}	      }
t        j                  |D 	cg c]  }	|	d	   	 c}	      }t        j                  |D 	cg c]  }	|	d
   	 c}	      }d}d}d|z
  ||z  z  d|z
  dz   j                          z  }|d|z
  |z  z  |dz   j                          z  }|dd|
f   |dd|
f   z
  }t        j                  |j                  t        j                        |j                  t        j                        d      j                  |      }t        t        |      t        |             }|j                   dd \  }}||z  | j                  z  }t        j                  d|d|j                        }|j                  |j                   d   dd      }|j!                  d      }t#        ||d      }t        j$                  |d      }|j                  |j&                        }|j                  |j                   d   dd      }|j!                  d      }t#        ||dd      }t        j$                  |d      }t)        ||      }t+        ||      }| j,                  |z  | j.                  |z  z   | j0                  |z  z   | j2                  |z  z   | j4                  |z  z   }|j7                  ||d      j9                         }t        j:                  |j&                        j<                  ||j?                         |jA                         z  <   |D 	cg c]  }	tC        |	d
          }}	g }||z  } |jE                  | d      }!tG        |      D ]  }"|!|"   }#tI        |#jE                  |d            D $%cg c]  \  }$}%tK        |%|$          }&}$}%|"dk(  r|&}ItM        ||&      D '(cg c]E  \  }'}(tO        jP                  |'d   |(d   | |"z  z   g      tO        jP                  |'d   |(d   g      fG }}'}( |D $)cg c]O  \  }$})t        jR                  |$t        jT                        t        jR                  |)t        jT                        fQ }*}$})|*S c c}	w c c}	w c c}	w c c}	w c c}%}$w c c}(}'w c c})}$w )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
        rA   Nr   r   r   
pred_boxes
pred_masksclass_labelsboxesmasksg      ?r7   g:0yE>)prF   FrH   )r-   r   nearestrI   moder-   rK   )dtype)+r    r/   r.   r!   rR   logcdisttofloat32type_asr   r   r`   rM   rG   repeatr9   r@   r<   ru   r+   r2   r^   r]   r_   rg   rh   viewcpufinfomaxisinfisnanlensplitrange	enumerater   zipnpconcatenate	as_tensorint64)+ri   outputstargets
group_detr
batch_sizenum_queriesout_probout_bbox	out_masksv
target_idstarget_bboxtarget_masksalphagammaneg_cost_classpos_cost_classr]   r^   r_   heightwidthrB   point_coordspred_point_coordspred_masks_logitstarget_point_coordsrg   rh   cost_matrixsizesindicesgroup_num_queriescost_matrix_listgroup_idgroup_cost_matrixicgroup_indicesindice1indice2jmatched_indicess+                                              r   forwardzRfDetrHungarianMatcher.forward   s    #*("3"9"9"1"=
K 8$,,Q2::<<(00A6L)11!Q7	 YY7Ca. 1CD
iiW =7 =>yyg!>!G*!>? e)%8a(lT>Q=V=V=X<XY1x<E"9:4?T?T?V>VW#AzM2^AzM5RR
 KKEMM :KNN5==<Y]^_gghpq	 ))A()KMefqMrss	 !r*e^t'C'CC
zz!Z9;K;KL(//	0BAqI''*	(4EUZ[!MM*;WE#y7*11,2D2DQ2GAN#--a0#L2EUZajk}}\7;>?PR^_,->M NNY&oo
*+nny() ""_45 !!N2	3 	 "&&z;CGGI BG[M^M^A_AcAcK%%'+*;*;*==> +22QQwZ22':5&,,->A,Fj) 	H 0 :ENO`OfOfglnpOqErsTQ21Q48sMs1}' -0,G
 ) 
GAJARU]A]4]'^_
GAJ'?@ 	 ho
_c_`bcU__Qekk2EOOAU[[4YZ
 
 M  D =!>` 3 t
s,   6SSSS#5S('A
S.;AS4)r   r   r      r   r   )
__name__
__module____qualname__floatrL   rf   r!   no_gradr   __classcell__rj   s   @r   r\   r\      st     ')&'%&22 2 	2
 "%2 $2 #2 U]]_U Ur   r\   c                   *     e Zd Z fdZd Zd Z xZS )RfDetrImageLossc                 :    t         |   |||||       || _        y rd   )re   rf   r`   )ri   matchernum_classesfocal_alphalossesr   r`   rj   s          r   rf   zRfDetrImageLoss.__init__&  s     +{FJO'>$r   c           	      ^   d|vrt        d      | j                  |      }|d   |   }|j                         dk(  r+t        j                  |      t        j                  |      dS t        j
                  t        ||      D 	cg c]  \  }\  }}	|d   |	    c}	}}d      }
|j                  d      }|
j                  d      j                         }
t        |j                  d   |j                  d   |j                  d	   z  | j                  z        }t        j                         5  t        ||d
d      }t        |
|dd      j                  d      }ddd       t        |d      j                  d      }t!        ||      t#        |||      d}|S c c}	}}w # 1 sw Y   JxY w)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].
        rm   z#No predicted masks found in outputsr   )loss_mask_celoss_mask_dicerp   rK   r   r-   r6   g      ?Frr   rs   NrH   )KeyError_get_source_permutation_idxnumelr!   r#   rR   r   r9   r   r   r    r`   r   rZ   r@   r<   r   r
   )ri   r   r   r   rS   
source_idxsource_maskst_r   r   rB   r   point_labelsrU   r   s                   r   
loss_maskszRfDetrImageLoss.loss_masks*  s    w&@AA55g>
|,Z81$ % 0 0 >"'"2"2<"@  yyWgAV!W!WIAv1!G*Q-!W]^_#--a0#--a0668 r"L$6$6r$:\=O=OPR=S$SW[WsWs$s

 ]]_ 	t:<UVX\]L'lRW^ghppqrsL		t $L,eT\\]^_ 7|\S\]'lIN
 - "X	t 	ts   F--F##F,c           
      X   | j                   r| j                  nd}|j                         D ci c]  \  }}|dk7  s|dk7  s|| }}}| j                  |||      }t	        d |D              }||z  }t        j                  |gt
        j                  t        t        |j                                     j                        }d}	t        j                         rWt        j                         rCt        j                  |t        j                   j"                         t        j$                         }	t        j&                  ||	z  d      j)                         }i }
| j*                  D ]&  }|
j-                  | j/                  |||||             ( d|v rt1        |d         D ]x  \  }}| j                  |||      }| j*                  D ]Q  }| j/                  |||||      }|j                         D ci c]  \  }}|d| z   | }}}|
j-                  |       S z d|v rv|d   }| j                  |||	      }| j*                  D ]N  }| j/                  |||||      }|j                         D ci c]  \  }}|d
z   | }}}|
j-                  |       P |
S 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.
        r   enc_outputsauxiliary_outputsc              3   8   K   | ]  }t        |d            yw)rn   N)r   ).0r   s     r   	<genexpr>z*RfDetrImageLoss.forward.<locals>.<genexpr>h  s     @1An-.@s   )ru   rG   )op)minr   )r   _enc)trainingr   itemsr   r   r!   r   r   nextitervaluesrG   distis_availableis_initialized
all_reduceReduceOpSUMget_world_sizeclampitemr   updateget_lossr   )ri   r   r   r   rJ   r   outputs_without_aux_and_encr   rS   
world_sizer   r   r   r   l_dictr   s                   r   r   zRfDetrImageLoss.forwardT  s    )-T__1
$]]_'
Q]0BqL_G_AqD'
# '

 ,,:GZP @@@	
*	OOYKu{{4PTU\UcUcUePfKgKnKno	
4#6#6#8OOI$--*;*;<,,.JKK	J 6A>CCE	 KK 	UDMM$--gwST	U
 ')(1':M2N(O *$$,,'8':N KK *D!]]41BGWV_`F9?HAaAaS'k1nHFHMM&)** G#!-0Kll;JlOG &t['7IV4:LLNCDAq!f*a-CCf%&
 Q'
: I Ds   JJJ3J 
2J&)r   r   r   rf   r   r   r   r   s   @r   r   r   %  s    ?(T4r   r   c           	      p    t        | d d |d d |d d       D cg c]  \  }}}|||d c}}}S c c}}}w )Nr-   rA   rl   rm   )r   )outputs_classoutputs_coordoutputs_masksabr   s         r   _set_aux_lossr     sV     ="-}Sb/A=QTRTCUV Aq! AQ7  s   1c           
         t        |j                  |j                  |j                  |j                  |j
                  |j                        }g d}t        ||j                  |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 ];  }|j'                  j)                         D ci c]  \  }}|d| z   | c}}       = |j'                  j)                         D ci c]  \  }}|dz   | c}}       j'                  |       t+        fdD              }||fS c c}}w c c}}w )N)r]   r^   r_   r`   ra   rb   )r   ro   cardinalityrp   )r   r   r   r   r   r`   rA   rl   rm   r   r   r   )loss_ce	loss_bbox	loss_giour   r   r   r   r   c              3   >   K   | ]  }|v s|   |   z    y wrd    )r   rJ   	loss_dictweight_dicts     r   r   z,RfDetrForSegmentationLoss.<locals>.<genexpr>  s%     T1CSy|k!n,Ts   	)r\   r]   r^   r_   r`   mask_class_loss_coefficientmask_dice_loss_coefficientr   
num_labelsr   r   rx   auxiliary_lossr   class_loss_coefficientbbox_loss_coefficientgiou_loss_coefficientr   decoder_layersr   r   r   )rA   r   rG   rl   rm   configr   r   r   enc_outputs_classenc_outputs_coordenc_outputs_masksr>   r   r   r   outputs_lossr   aux_weight_dictr   rJ   r   r   r   r   s                          @@r   RfDetrForSegmentationLossr    s
     %$$"""" & > >#??"==G 9F%%&&$$ & > >I LLL#L!+L!+L#''#L
 )-V,=(),/I$;;&JfJfgK%;;K"("D"DK$*$E$EK !v,,q01 	UA""{?P?P?R#Stq!A!A3KN#ST	U+:K:K:MN$!QF
ANO?+TiTTD---	 $TNs   .G,)G2
)F)NNNNNN)numpyr   r!   torch.distributeddistributedr   r   r   image_transformsr   utilsr   loss_for_object_detectionr	   r
   r   loss_lw_detrr   scipy.optimizer   rL   r   r+   r2   r@   r   rZ   r\   r   r   r  r   r   r   <module>r     sC        7 & 
 * 4u|| U\\ VY ^c^j^j ( u|| X]XdXd 8  6 0 LQLL5:\\
\\B.. #.7:.UZ..bf- fRco cL ?.r   