
    |h;                     X   d dl mZmZmZmZmZ d dlZd dlmZ d dl	mc m
Z d dlmZ d dlmZ d dlmZmZ  G d dej&                        Z	 	 	 	 ddeeef   d	ed
edej.                  dededededeeej.                     eej.                     eej.                     eeeef      f   fdZy)    )AnyDictListOptionalTupleN)linear_sum_assignment)bbox_iou)	xywh2xyxy	xyxy2xywhc                   P    e Zd ZdZ	 	 	 	 	 	 ddeeeef      dedede	dedef fdZ
	 	 dd	ej                  d
ej                  dej                  dej                  dee	   deej                     deeej                        deeej                  ej                  f      fdZ xZS )HungarianMatchera  
    A module implementing the HungarianMatcher for optimal assignment between predictions and ground truth.

    HungarianMatcher performs optimal bipartite assignment over predicted and ground truth bounding boxes using a cost
    function that considers classification scores, bounding box coordinates, and optionally mask predictions. This is
    used in end-to-end object detection models like DETR.

    Attributes:
        cost_gain (Dict[str, float]): Dictionary of cost coefficients for 'class', 'bbox', 'giou', 'mask', and 'dice'
            components.
        use_fl (bool): Whether to use Focal Loss for classification cost calculation.
        with_mask (bool): Whether the model makes mask predictions.
        num_sample_points (int): Number of sample points used in mask cost calculation.
        alpha (float): Alpha factor in Focal Loss calculation.
        gamma (float): Gamma factor in Focal Loss calculation.

    Methods:
        forward: Compute optimal assignment between predictions and ground truths for a batch.
        _cost_mask: Compute mask cost and dice cost if masks are predicted.

    Examples:
        Initialize a HungarianMatcher with custom cost gains
        >>> matcher = HungarianMatcher(cost_gain={"class": 2, "bbox": 5, "giou": 2})

        Perform matching between predictions and ground truth
        >>> pred_boxes = torch.rand(2, 100, 4)  # batch_size=2, num_queries=100
        >>> pred_scores = torch.rand(2, 100, 80)  # 80 classes
        >>> gt_boxes = torch.rand(10, 4)  # 10 ground truth boxes
        >>> gt_classes = torch.randint(0, 80, (10,))
        >>> gt_groups = [5, 5]  # 5 GT boxes per image
        >>> indices = matcher(pred_boxes, pred_scores, gt_boxes, gt_classes, gt_groups)
    	cost_gainuse_fl	with_masknum_sample_pointsalphagammac                     t         |           |dddddd}|| _        || _        || _        || _        || _        || _        y)a  
        Initialize HungarianMatcher for optimal assignment of predicted and ground truth bounding boxes.

        Args:
            cost_gain (Dict[str, float], optional): Dictionary of cost coefficients for different matching cost
                components. Should contain keys 'class', 'bbox', 'giou', 'mask', and 'dice'.
            use_fl (bool): Whether to use Focal Loss for classification cost calculation.
            with_mask (bool): Whether the model makes mask predictions.
            num_sample_points (int): Number of sample points used in mask cost calculation.
            alpha (float): Alpha factor in Focal Loss calculation.
            gamma (float): Gamma factor in Focal Loss calculation.
        N         )classbboxgioumaskdice)super__init__r   r   r   r   r   r   )selfr   r   r   r   r   r   	__class__s          [/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/models/utils/ops.pyr   zHungarianMatcher.__init__0   sQ    * 	"#Q1aPI""!2

    pred_bboxespred_scores	gt_bboxesgt_cls	gt_groupsmasksgt_maskreturnc           
         |j                   \  }}	}
t        |      dk(  rat        |      D cg c]L  }t        j                  g t        j
                        t        j                  g t        j
                        fN c}S |j                         j                  d|
      }| j                  rt        j                  |      nt        j                  |d      }|j                         j                  dd      }|dd|f   }| j                  rqd| j                  z
  || j                  z  z  d|z
  dz   j                          z  }| j                  d|z
  | j                  z  z  |dz   j                          z  }||z
  }n| }|j                  d      |j                  d      z
  j!                         j                  d      }d	t#        |j                  d      |j                  d      d
d
      j%                  d      z
  }| j&                  d   |z  | j&                  d   |z  z   | j&                  d   |z  z   }| j(                  r|| j+                  ||||      z  }d||j-                         |j/                         z  <   |j                  ||	d      j1                         }t3        |j5                  |d            D cg c]  \  }}t7        ||          }}}t        j8                  dg|dd       j;                  d      }t3        |      D cg c]X  \  }\  }}t        j                  |t        j
                        t        j                  |t        j
                        ||   z   fZ c}}}S c c}w c c}}w c c}}}w )a  
        Compute optimal assignment between predictions and ground truth using Hungarian algorithm.

        This method calculates matching costs based on classification scores, bounding box coordinates, and optionally
        mask predictions, then finds the optimal bipartite assignment between predictions and ground truth.

        Args:
            pred_bboxes (torch.Tensor): Predicted bounding boxes with shape (batch_size, num_queries, 4).
            pred_scores (torch.Tensor): Predicted classification scores with shape (batch_size, num_queries,
                num_classes).
            gt_bboxes (torch.Tensor): Ground truth bounding boxes with shape (num_gts, 4).
            gt_cls (torch.Tensor): Ground truth class labels with shape (num_gts,).
            gt_groups (List[int]): Number of ground truth boxes for each image in the batch.
            masks (torch.Tensor, optional): Predicted masks with shape (batch_size, num_queries, height, width).
            gt_mask (List[torch.Tensor], optional): Ground truth masks, each with shape (num_masks, Height, Width).

        Returns:
            (List[Tuple[torch.Tensor, torch.Tensor]]): A list of size batch_size, each element is a tuple
                (index_i, index_j), where index_i is the tensor of indices of the selected predictions (in order)
                and index_j is the tensor of indices of the corresponding selected ground truth targets (in order).
                For each batch element, it holds: len(index_i) = len(index_j) = min(num_queries, num_target_boxes).
        r   dtypedim   Nr   g:0yE>      ?T)xywhGIoUr   r   r           )shapesumrangetorchtensorlongdetachviewr   Fsigmoidsoftmaxr   r   log	unsqueezeabsr	   squeezer   r   
_cost_maskisnanisinfcpu	enumeratesplitr   	as_tensorcumsum_)r   r#   r$   r%   r&   r'   r(   r)   bsnqnc_neg_cost_classpos_cost_class
cost_class	cost_bbox	cost_giouCicindiceskjs                          r!   forwardzHungarianMatcher.forwardO   s'   @ !&&
By>QfklnfopabU\\"EJJ7bPUPZPZ9[\pp "((*//B704aii,;\^A_!((*//A6 "!V),;;$**ndjj1HIqS^aeOeNjNjNlMlmN!ZZAO

+JKQ\_cQcPhPhPjOjkN'.8J%J !**1-	0C0CA0FFKKMQQRTU	 (;#8#8#;Y=P=PQR=SZ^eijrrsuvv	 NN7#j0nnV$y01nnV$y01 	
 >>Yw??A $'!'')aggi
 FF2r2""$;DQWWYXZE[;\]41a(1.]]OOQ$83B$89AA!D	 'w/
 
6Aq \\!5::.Qejj0QT]^_T`0`a
 	
O qJ ^
s   AM4>M9AM?)NTFi 1  g      ?       @)NN)__name__
__module____qualname____doc__r   r   strfloatboolintr   r9   Tensorr   r   r\   __classcell__)r    s   @r!   r   r      s   F 15!&De,-  	
   L )-04M
\\M
 \\M
 <<	M

 M
 9M
 %M
 $u||,-M
 
eELL%,,./	0M
r"   r   batchnum_classesnum_queriesclass_embednum_dncls_noise_ratiobox_noise_scaletrainingr*   c           
      	   |r|dk  s| y| d   }t        |      }	t        |      }
|
dk(  ry||
z  }|dk(  rdn|}t        |      }| d   }| d   }| d   }|j                  d	|z        }|j                  d	|z  d      }|j                  d	|z        j	                  d
      }t        j                  |	|z  t
        j                  |j                        ||	z  z   }|dkD  r|t        j                  |j                        |dz  k  }t        j                  |      j                  d
      }t        j                  |d||j                  |j                        }|||<   |dkD  rt        |      }|dd	df   dz  j                  dd	      |z  }t        j                  |dd	      dz  dz
  }t        j                   |      }||xx   dz  cc<   ||z  }|||z  z  }|j#                  dd       t%        |      }t        j&                  |d      }t)        |
d	z  |z        }||   }t        j*                  |||j                  d
   |j                        }t        j*                  ||d|j                        }t        j,                  |D cg c]0  }t        j.                  t1        |      t
        j                        2 c}      }t        j2                  t1        |      D  cg c]
  } ||
| z  z    c} d      }!t        j,                  t1        d	|z        D  cg c]
  } ||
| z  z    c}       }||||f<   ||||f<   ||z   }"t        j*                  |"|"gt
        j4                        }#d|#|dd|f<   t1        |      D ]  } | dk(  r#d|#|
d	z  | z  |
d	z  | dz   z  |
d	z  | dz   z  |f<   | |dz
  k(  r!d|#|
d	z  | z  |
d	z  | dz   z  d|
| z  d	z  f<   Td|#|
d	z  | z  |
d	z  | dz   z  |
d	z  | dz   z  |f<   d|#|
d	z  | z  |
d	z  | dz   z  d|
d	z  | z  f<    |!j7                         j9                  t;        |      d      D $cg c]  }$|$j=                  d
       c}$|||gd}%|j?                  |j                        |j?                  |j                        |#j?                  |j                        |%fS c c}w c c} w c c} w c c}$w )a  
    Generate contrastive denoising training group with positive and negative samples from ground truths.

    This function creates denoising queries for contrastive denoising training by adding noise to ground truth
    bounding boxes and class labels. It generates both positive and negative samples to improve model robustness.

    Args:
        batch (Dict[str, Any]): Batch dictionary containing 'gt_cls' (torch.Tensor with shape (num_gts,)),
            'gt_bboxes' (torch.Tensor with shape (num_gts, 4)), and 'gt_groups' (List[int]) indicating number of
            ground truths per image.
        num_classes (int): Total number of object classes.
        num_queries (int): Number of object queries.
        class_embed (torch.Tensor): Class embedding weights to map labels to embedding space.
        num_dn (int): Number of denoising queries to generate.
        cls_noise_ratio (float): Noise ratio for class labels.
        box_noise_scale (float): Noise scale for bounding box coordinates.
        training (bool): Whether model is in training mode.

    Returns:
        padding_cls (torch.Tensor | None): Modified class embeddings for denoising with shape (bs, num_dn, embed_dim).
        padding_bbox (torch.Tensor | None): Modified bounding boxes for denoising with shape (bs, num_dn, 4).
        attn_mask (torch.Tensor | None): Attention mask for denoising with shape (tgt_size, tgt_size).
        dn_meta (Dict[str, Any] | None): Meta information dictionary containing denoising parameters.

    Examples:
        Generate denoising group for training
        >>> batch = {
        ...     "cls": torch.tensor([0, 1, 2]),
        ...     "bboxes": torch.rand(3, 4),
        ...     "batch_idx": torch.tensor([0, 0, 1]),
        ...     "gt_groups": [2, 1],
        ... }
        >>> class_embed = torch.rand(80, 256)  # 80 classes, 256 embedding dim
        >>> cdn_outputs = get_cdn_group(batch, 80, 100, class_embed, training=True)
    r   N)NNNNr'   r   clsbboxes	batch_idxr   r.   )r-   device      ?.r]   r2   r5   )minmaxgư>)eps)rt   r1   r,   r/   T)
dn_pos_idxdn_num_groupdn_num_split) r7   rw   lenrepeatr=   r9   aranger;   rt   randr6   nonzerorD   randint_liker-   r
   	rand_likeclip_r   logitre   zeroscatr:   r8   stackrd   rH   rJ   listreshapeto)&rh   ri   rj   rk   rl   rm   rn   ro   r'   	total_nummax_nums	num_grouprM   r&   gt_bboxb_idxdn_clsdn_bboxdn_b_idxneg_idxr   idx	new_label
known_bboxdiff	rand_sign	rand_partdn_cls_embedpadding_clspadding_bboxnummap_indicesrW   pos_idxtgt_size	attn_maskpdn_metas&                                         r!   get_cdn_groupr      s   Z 1%k"III9~H1}%("I!^I	YB5\FHoG+E ]]1y=)FnnQ]A.G||A	M*//3H ll9y0

7>>Z]fir]rrGzz&,,'?S+@AmmD!))"-&&sA{&,,W]WdWde	sw'
QR 3&..q!4F&&w15;cA	OOG,	'c!Y	i$&&
Sc*J'++g40A	)*Fv&L++b&,*<*<R*@WK;;r61W^^DL))S\]CU\\%*EJJG]^KkkuY?OP!;A5PVWXG))q9}AUVA[8a<7VWK+7K;'(,3L(K()#HXx0

CI"&Ifgww9 \6dhIhlQ&AQ)??AQRUVQVAWZ`A``a	AW[IhlQ&AQ)??AS8a<RSCSASSTdhIhlQ&AQ)??AQRUVQVAWZ`A``aW[IhlQ&AQ)??AS8a<RSCSASST\ /6kkm.A.A$y/WX.A.YZqyy}Z!-G 	{))***+[''(	 5 ^PV$ [s   5R2R7R< S)d   ru   r2   F)typingr   r   r   r   r   r9   torch.nnnntorch.nn.functional
functionalr>   scipy.optimizer   ultralytics.utils.metricsr	   ultralytics.utils.opsr
   r   Moduler   rb   re   rf   rc   rd   r    r"   r!   <module>r      s    4 3     0 . 6N
ryy N
h   @S>@@ @ 	@
 @ @ @ @ 8ELL!8ELL#98ELL;QS[\`adfiai\jSkkl@r"   