
    gT                         d dl Z d dlmZ d dlmc mZ ddlmZmZm	Z	 ddl
mZmZmZmZmZmZ  e            rd dlmZ  e            rd dlmZ  G d d	ej                  Z G d
 dej                  Z	 	 	 	 	 ddZdS )    N   )is_scipy_availableis_vision_availablerequires_backends   )_set_aux_lossbox_iou	dice_lossgeneralized_box_iounested_tensor_from_tensor_listsigmoid_focal_losslinear_sum_assignment)center_to_corners_formatc                   R     e Zd ZdZ fdZ ej                    d             Z xZS )RTDetrHungarianMatchera  This class computes an assignment between the targets and the predictions of the network

    For efficiency reasons, the targets don't include the no_object. Because of this, in general, there are more
    predictions than targets. In this case, we do a 1-to-1 matching of the best predictions, while the others are
    un-matched (and thus treated as non-objects).

    Args:
        config: RTDetrConfig
    c                 f   t                                                       t          | dg           |j        | _        |j        | _        |j        | _        |j	        | _	        |j
        | _        |j        | _        | j        | j        cxk    r| j        cxk    rdk    rn d S t          d          d S )Nscipyr   z#All costs of the Matcher can't be 0)super__init__r   matcher_class_cost
class_costmatcher_bbox_cost	bbox_costmatcher_giou_cost	giou_costuse_focal_lossmatcher_alphaalphamatcher_gammagamma
ValueError)selfconfig	__class__s     Z/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/loss/loss_rt_detr.pyr   zRTDetrHungarianMatcher.__init__1   s    $	*** 311$3)
)
?dnCCCCCCCC!CCCCCCBCCC DC    c                 F   |d         j         dd         \  }}|d                             dd          }t          j        d |D                       }t          j        d |D                       }| j        rt          j        |d                             dd                    }|dd|f         }d| j        z
  || j        z  z  d|z
  d	z   	                                 z  }	| j        d|z
  | j        z  z  |d	z   	                                 z  }
|
|	z
  }n<|d                             dd          
                    d
          }|dd|f          }t          j        ||d          }t          t          |          t          |                     }| j        |z  | j        |z  z   | j        |z  z   }|                    ||d
                                          }d |D             }d t'          |                    |d
                    D             }d |D             S )a  Performs the matching

        Params:
            outputs: This is a dict that contains at least these entries:
                 "logits": Tensor of dim [batch_size, num_queries, num_classes] with the classification logits
                 "pred_boxes": Tensor of dim [batch_size, num_queries, 4] with the predicted box coordinates

            targets: This is a list of targets (len(targets) = batch_size), where each target is a dict containing:
                 "class_labels": Tensor of dim [num_target_boxes] (where num_target_boxes is the number of ground-truth
                           objects in the target) containing the class labels
                 "boxes": Tensor of dim [num_target_boxes, 4] containing the target box coordinates

        Returns:
            A list of size batch_size, containing tuples of (index_i, index_j) where:
                - index_i is the indices of the selected predictions (in order)
                - index_j is the indices of the corresponding selected targets (in order)
            For each batch element, it holds:
                len(index_i) = len(index_j) = min(num_queries, num_target_boxes)
        logitsNr   
pred_boxesr   r   c                     g | ]
}|d          S class_labels .0vs     r&   
<listcomp>z2RTDetrHungarianMatcher.forward.<locals>.<listcomp>Z   s    CCCa. 1CCCr'   c                     g | ]
}|d          S boxesr.   r/   s     r&   r2   z2RTDetrHungarianMatcher.forward.<locals>.<listcomp>[   s     = = =7 = = =r'   g:0yE>)pc                 8    g | ]}t          |d                    S r4   lenr/   s     r&   r2   z2RTDetrHungarianMatcher.forward.<locals>.<listcomp>q   s"    222QQwZ222r'   c                 >    g | ]\  }}t          ||                   S r.   r   )r0   ics      r&   r2   z2RTDetrHungarianMatcher.forward.<locals>.<listcomp>r   s)    ccc41a(1..cccr'   c                     g | ]E\  }}t          j        |t           j                   t          j        |t           j                   fFS )dtype)torch	as_tensorint64)r0   r<   js      r&   r2   z2RTDetrHungarianMatcher.forward.<locals>.<listcomp>t   sH    ssscgcdfg%+666QVQ\8]8]8]^sssr'   )shapeflattenrA   catr   Fsigmoidr   r!   logsoftmaxcdistr   r   r   r   r   viewcpu	enumeratesplit)r#   outputstargets
batch_sizenum_queriesout_bbox
target_idstarget_bboxout_probneg_cost_classpos_cost_classr   r   r   cost_matrixsizesindicess                    r&   forwardzRTDetrHungarianMatcher.forward@   sE   * #*("3"9"1""=
K <(00A66YCC7CCCDD
i = =W = = =>>  	2y!2!:!:1a!@!@AAH:.H$*n4:1EFAPXL[_L_KdKdKfKfJfgN!ZALTZ+GHhY]oMbMbMdMdLdeN'.8JJx(00A66>>rBBH"111j=11J K+;;;	()A()K)KMefqMrMrsss	ny04?Z3OORVR`clRll!&&z;CCGGII22'222cc9[EVEVW\^`EaEa;b;bcccsskrssssr'   )	__name__
__module____qualname____doc__r   rA   no_gradr^   __classcell__r%   s   @r&   r   r   &   sq         D D D D D U]__3t 3t _3t 3t 3t 3t 3tr'   r   c                        e Zd ZdZ fdZddZddZ ej                    d             Z	d Z
d Zdd	Zd
 Zd ZddZd Zed             Zd Z xZS )
RTDetrLossah  
    This class computes the losses for RTDetr. The process happens in two steps: 1) we compute hungarian assignment
    between ground truth boxes and the outputs of the model 2) we supervise each pair of matched ground-truth /
    prediction (supervise class and box).

    Args:
        matcher (`DetrHungarianMatcher`):
            Module able to compute a matching between targets and proposals.
        weight_dict (`Dict`):
            Dictionary relating each loss with its weights. These losses are configured in RTDetrConf as
            `weight_loss_vfl`, `weight_loss_bbox`, `weight_loss_giou`
        losses (`List[str]`):
            List of all the losses to be applied. See `get_loss` for a list of all available losses.
        alpha (`float`):
            Parameter alpha used to compute the focal loss.
        gamma (`float`):
            Parameter gamma used to compute the focal loss.
        eos_coef (`float`):
            Relative classification weight applied to the no-object category.
        num_classes (`int`):
            Number of object categories, omitting the special no-object category.
    c                    t                                                       t          |          | _        |j        | _        |j        |j        |j        d| _	        ddg| _
        |j        | _        t          j        |j        dz             }| j        |d<   |                     d|           |j        | _        |j        | _        d S )N)loss_vfl	loss_bbox	loss_giouvflr5   r   r6   empty_weight)r   r   r   matcher
num_labelsnum_classesweight_loss_vflweight_loss_bboxweight_loss_giouweight_dictlosseseos_coefficienteos_coefrA   onesregister_bufferfocal_loss_alphar   focal_loss_gammar!   )r#   r$   rm   r%   s      r&   r   zRTDetrLoss.__init__   s    -f55!,.00
 

 g&.z&"3a"788=R^\:::,
,


r'   Tc                    d|vrt          d          d|vrt          d          |                     |          }|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                  z  d|z
  z  |z   }t          j        |||d          }|                    d                                          |j
        d         z  |z  }d|iS )Nr*   #No predicted boxes found in outputsr)   z$No predicted logits found in outputsc                 6    g | ]\  }\  }}|d          |         S r4   r.   r0   _target_r<   s       r&   r2   z.RTDetrLoss.loss_labels_vfl.<locals>.<listcomp>   s*    !c!c!c/'6Aq''"21"5!c!c!cr'   r   dimc                 6    g | ]\  }\  }}|d          |         S r,   r.   r   s       r&   r2   z.RTDetrLoss.loss_labels_vfl.<locals>.<listcomp>   -    ,u,u,uOGU[VWYZW^-DQ-G,u,u,ur'   r   r@   devicer   rp   .r6   r?   none)weight	reductionri   )KeyError_get_source_permutation_idxrA   rG   zipr	   r   diagdetachfullrE   rp   rC   r   rH   one_hot
zeros_liker@   to	unsqueezerI   r   powr!    binary_cross_entropy_with_logitsmeansum)r#   rQ   rR   r]   	num_boxesrJ   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_vflzRTDetrLoss.loss_labels_vfl   sL   w&&@AAA7""ABBB..w77L)#.	y!c!cSQXZaMbMb!c!c!cijkkk29==?WXd?e?effaz$&&((X&
"'),u,u_bcjls_t_t,u,u,u"v"v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
jnnTZ888AJG,V1*lSYeklllyy||!!J$4Q$77)CD!!r'   c                    d|vrt          d          |d         }|                     |          }t          j        d t	          ||          D                       }t          j        |j        dd         | j        t          j        |j	                  }	||	|<   t          j        |                    dd          |	| j                  }
d|
i}|S )	zClassification loss (NLL)
        targets dicts must contain the key "class_labels" containing a tensor of dim [nb_target_boxes]
        r)   z#No logits were found in the outputsc                 6    g | ]\  }\  }}|d          |         S r,   r.   r   s       r&   r2   z*RTDetrLoss.loss_labels.<locals>.<listcomp>   r   r'   Nr   r   r   loss_ce)r   r   rA   rG   r   r   rE   rp   rC   r   rH   cross_entropy	transposeclass_weight)r#   rQ   rR   r]   r   rJ   r   r   r   r   r   ru   s               r&   loss_labelszRTDetrLoss.loss_labels   s     7""@AAAX&
..w77"'),u,u_bcjls_t_t,u,u,u"v"vRaR $"2%+jN_
 
 
 6s/*"6"6q!"<"<ndN_``W%r'   c                 h   |d         }|j         }t          j        d |D             |          }|                    d          |j        d         dz
  k                        d          }t          j                            |	                                |	                                          }	d|	i}
|
S )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)   c                 8    g | ]}t          |d                    S r,   r9   r/   s     r&   r2   z/RTDetrLoss.loss_cardinality.<locals>.<listcomp>   s%    )R)R)RQ#a.?*@*@)R)R)Rr'   )r   r6   r   cardinality_error)
r   rA   rB   argmaxrE   r   nn
functionall1_lossfloat)r#   rQ   rR   r]   r   r)   r   target_lengths	card_predcard_errru   s              r&   loss_cardinalityzRTDetrLoss.loss_cardinality   s     ")R)R')R)R)R[abbb]]2&&&,r*:Q*>>CCAFF	=(():):N<P<P<R<RSS%x0r'   c           	         d|vrt          d          |                     |          }|d         |         }t          j        d t	          ||          D             d          }i }t          j        ||d          }	|	                                |z  |d<   d	t          j        t          t          |          t          |                              z
  }
|
                                |z  |d
<   |S )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*   r}   c                 6    g | ]\  }\  }}|d          |         S r4   r.   )r0   tr   r<   s       r&   r2   z)RTDetrLoss.loss_boxes.<locals>.<listcomp>   s(    !W!W!WIAv1!G*Q-!W!W!Wr'   r   r   r   r   rj   r   rk   )r   r   rA   rG   r   rH   r   r   r   r   r   )r#   rQ   rR   r]   r   r   r   r   ru   rj   rk   s              r&   
loss_boxeszRTDetrLoss.loss_boxes   s     w&&@AAA..w77L)#.	y!W!WWgAVAV!W!W!W]^___IiHHH	'mmoo	9{
 8 C CE]^jEkEkll
 
 
	 (mmoo	9{r'   c                    d|vrt          d          |                     |          }|                     |          }|d         }||         }d |D             }t          |                                          \  }	}
|	                    |          }	|	|         }	t          j                            |dddf         |	j	        dd         dd          }|ddd	f         
                    d
          }|	
                    d
          }	|	                    |j	                  }	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].
        
pred_masksz#No predicted masks found in outputsc                     g | ]
}|d          S )masksr.   r0   r   s     r&   r2   z)RTDetrLoss.loss_masks.<locals>.<listcomp>  s    ---7---r'   NbilinearF)sizemodealign_cornersr   r   )	loss_mask	loss_dice)r   r   _get_target_permutation_idxr   	decomposer   r   r   interpolaterE   rF   rM   r   r
   )r#   rQ   rR   r]   r   
source_idx
target_idxsource_masksr   target_masksvalidru   s               r&   
loss_maskszRTDetrLoss.loss_masks   sa   
 w&&@AAA55g>>
55g>>
|,#J/--W---<UCCMMOOe#|44#J/ }00D!(:233(?j`e 1 
 
 $AAAqD)11!44#++A..#(();<<+L,	RR"<yII
 
 r'   c                    |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        ||
d	z  d
          }|                    d                                          |j        d         z  |z  }d|iS )Nr)   c                 6    g | ]\  }\  }}|d          |         S r,   r.   r   s       r&   r2   z.RTDetrLoss.loss_labels_bce.<locals>.<listcomp>  r   r'   r   r   r   r   .r6   g      ?r   r   loss_bce)r   rA   rG   r   r   rE   rp   rC   r   rH   r   r   r   r   r#   rQ   rR   r]   r   rJ   r   r   r   r   r   r   s               r&   loss_labels_bcezRTDetrLoss.loss_labels_bce  s
   X&
..w77"'),u,u_bcjls_t_t,u,u,u"v"vRaR $"2%+jN_
 
 
 6s>t7G!7KLLLSRUSURUXV1*fslV\]]]yy||!!J$4Q$77)CD!!r'   c                     t          j        d t          |          D                       }t          j        d |D                       }||fS )Nc                 D    g | ]\  }\  }}t          j        ||          S r.   rA   	full_like)r0   r<   sourcer   s       r&   r2   z:RTDetrLoss._get_source_permutation_idx.<locals>.<listcomp>)  s,    cccna&!uvq99cccr'   c                     g | ]\  }}|S r.   r.   )r0   r   r   s      r&   r2   z:RTDetrLoss._get_source_permutation_idx.<locals>.<listcomp>*  s    BBB;FABBBr'   rA   rG   rO   )r#   r]   	batch_idxr   s       r&   r   z&RTDetrLoss._get_source_permutation_idx'  S    IccPYZaPbPbcccdd	YBB'BBBCC
*$$r'   c                     t          j        d t          |          D                       }t          j        d |D                       }||fS )Nc                 D    g | ]\  }\  }}t          j        ||          S r.   r   )r0   r<   r   r   s       r&   r2   z:RTDetrLoss._get_target_permutation_idx.<locals>.<listcomp>/  s,    cccna!Vuvq99cccr'   c                     g | ]\  }}|S r.   r.   )r0   r   r   s      r&   r2   z:RTDetrLoss._get_target_permutation_idx.<locals>.<listcomp>0  s    BBB;AvBBBr'   r   )r#   r]   r   r   s       r&   r   z&RTDetrLoss._get_target_permutation_idx-  r   r'   c                 6   d|vrt          d          |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                  }|                    d                                          |j        d         z  |z  }d
|iS )Nr)   zNo logits found in outputsc                 6    g | ]\  }\  }}|d          |         S r,   r.   r   s       r&   r2   z0RTDetrLoss.loss_labels_focal.<locals>.<listcomp>:  r   r'   r   r   r   r   .r6   
loss_focal)r   r   rA   rG   r   r   rE   rp   rC   r   rH   r   r   r   r!   r   r   r   s               r&   loss_labels_focalzRTDetrLoss.loss_labels_focal3  s   7""7888X&
..w77"'),u,u_bcjls_t_t,u,u,u"v"vRaR $"2%+jN_
 
 
 6s>t7G!7KLLLSRUSURUXV!*fdj$*MMyy||!!J$4Q$77)Cd##r'   c                     | j         | j        | j        | j        | j        | j        | j        d}||vrt          d| d           ||         ||||          S )N)labelscardinalityr5   r   bcefocalrl   zLoss z not supported)r   r   r   r   r   r   r   r"   )r#   r   rQ   rR   r]   r   loss_maps          r&   get_losszRTDetrLoss.get_lossE  sw    &0__'+'
 
 x9T999:::x~gwCCCr'   c           	      @   | d         | d         }}d |D             }|d         d         j         }g }t          |          D ]\  }}|dk    r|t          j        |t          j        |          }	|	                    |          }	t          ||                   t          |	          k    sJ |                    ||         |	f           |                    t          j        dt          j        |          t          j        dt          j        |          f           |S )Ndn_positive_idxdn_num_groupc                 8    g | ]}t          |d                    S r,   r9   r   s     r&   r2   z6RTDetrLoss.get_cdn_matched_indices.<locals>.<listcomp>V  s%    ;;;a3q());;;r'   r   r-   r   )	r   rO   rA   arangerC   tiler:   appendzeros)
dn_metarR   r   r   num_gtsr   dn_match_indicesr<   num_gtgt_idxs
             r&   get_cdn_matched_indicesz"RTDetrLoss.get_cdn_matched_indicesS  s(   (/0A(BGND[;;7;;;N+2"7++ 	 	IAvzzfEKOOO\22?1-..#f++==== '');V(DEEEE ''AU[HHHAU[HHH     r'   c           
         
 d |                                 D             }                     ||          }t          d |D                       }t          j        |gt          j        t          t          |                                                    j	                  }t          j
        |d                                          }i } j        D ]?}                     |||||           fdD             |                               @d|v rt          |d                   D ]\  
}                     ||          } j        D ]f}|dk    r	                     |||||           fd	D             
fd
                                 D             |                               gd|v rd|vrt!          d                               |d         |          }||d         d         z  }t          |d                   D ]n\  
} j        D ]a}|dk    r	i }	  j        |||||fi |	 fdD             
fd                                 D             |                               bo|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.   )r0   kr1   s      r&   
<dictcomp>z&RTDetrLoss.forward.<locals>.<dictcomp>u  s*    ```1CV^_C_C_q!C_C_C_r'   c              3   @   K   | ]}t          |d                    V  dS )r-   Nr9   r   s     r&   	<genexpr>z%RTDetrLoss.forward.<locals>.<genexpr>{  s/      @@1An-..@@@@@@r'   r   r   )minc                 P    i | ]"}|j         v ||         j         |         z  #S r.   rt   r0   r  l_dictr#   s     r&   r  z&RTDetrLoss.forward.<locals>.<dictcomp>  s:    bbbQAQUQaLaLaaT%5a%88LaLaLar'   r  r   c                 P    i | ]"}|j         v ||         j         |         z  #S r.   r  r	  s     r&   r  z&RTDetrLoss.forward.<locals>.<dictcomp>  ;    jjjQTUY]YiTiTiaT-=a-@!@TiTiTir'   c                 (    i | ]\  }}|d  z   |S )_aux_r.   r0   r  r1   r<   s      r&   r  z&RTDetrLoss.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.r   c                 P    i | ]"}|j         v ||         j         |         z  #S r.   r  r	  s     r&   r  z&RTDetrLoss.forward.<locals>.<dictcomp>  r  r'   c                 (    i | ]\  }}|d  z   |S )_dn_r.   r  s      r&   r  z&RTDetrLoss.forward.<locals>.<dictcomp>  s)    KKKDAqa***naKKKr'   )itemsrn   r   rA   rB   r   nextitervaluesr   clampitemru   r   updaterO   r"   r   )r#   rQ   rR   outputs_without_auxr]   r   ru   r   r  kwargsr<   r
  s   `         @@r&   r^   zRTDetrLoss.forwardj  s    a```` ,,2G<< @@@@@@@	OYKu{4PTU\UcUcUeUePfPfKgKgKnooo	K	q1116688	 K 	" 	"D]]4'7INNFbbbbb&bbbFMM&!!!! '))(1':M2N(O(O 	* 	*$$,,'8'BB K * *Dw !]]41BGWV_``Fjjjjj&jjjFLLLLV\\^^LLLFMM&))))* "W,,&g55  T   227;R3SU\]]G!G,C$D^$TTI(1':P2Q(R(R 
* 
*$$ K * *Dw F*T]41BGWV_jjcijjFjjjjj&jjjFKKKKFLLNNKKKFMM&))))* r'   )T)r_   r`   ra   rb   r   r   r   rA   rc   r   r   r   r   r   r   r   r   staticmethodr   r^   rd   re   s   @r&   rg   rg   w   s-        .- - - - -$" " " ">   ( U]__  _  .  >" " " "% % %% % %$ $ $ $$D D D     \ ,> > > > > > >r'   rg   c
                    t          |          }|                    |           i }| |d<   ||d<   |j        r|	@t          j        ||	d         d          \  }}t          j        ||	d         d          \  }}t          |d d d df                             dd          |d d d df                             dd                    }||d	<   |d	                             t          |g|g                     |	@t          |                    dd          |                    dd                    |d
<   |	|d<    |||          }t          |	                                          }|||fS )Nr)   r*   dn_num_splitr   r   r6   r   r   r  r  r  )
rg   r   auxiliary_lossrA   rP   r   r   extendr   r  )r)   r   r   r*   r$   outputs_classoutputs_coordenc_topk_logitsenc_topk_bboxesr  r  	criterionoutputs_lossdn_out_coorddn_out_classr  	loss_dictr   s                     r&   RTDetrForObjectDetectionLossr,    s    6""ILLL#L!+L J ,*/+mEZ[iEjpq*r*r*r'L-*/+mEZ[iEjpq*r*r*r'L-)-3B3*?*I*I!Q*O*OQ^_`_`_`becebe_eQfQpQpqrtuQvQvww,=()()00?PSbRc1d1deee ,3@&&q!,,l.D.DQ.J.J4 4L/0 5JL01	,//Iy!!""D---r'   )NNNNN)rA   torch.nnr   torch.nn.functionalr   rH   utilsr   r   r   loss_for_object_detectionr   r	   r
   r   r   r   scipy.optimizer   transformers.image_transformsr   Moduler   rg   r,  r.   r'   r&   <module>r4     s                   N N N N N N N N N N                 5444444  GFFFFFFNt Nt Nt Nt NtRY Nt Nt Ntbq q q q q q q qt	 $. $. $. $. $. $.r'   