
    3j                        S SK r SSKJr  SS\ R                  S\ R                  S\S\S\ R                  4
S	 jjrS
\ R                  S\S\ R                  4S jrSS\ R                  S\ R                  S\S\ R                  4S jjrg)    N   )confusion_matrixpredtargetnum_classesepsreturnc                 H   [         R                  " U 5      (       d4  U R                  [         R                  La  [	        S[        U 5       35      e[         R                  " U5      (       d4  UR                  [         R                  La  [	        S[        U5       35      eU R                  UR                  :X  d%  [        SU R                   SUR                   35      eU R                  UR                  :X  d%  [        SU R                   SUR                   35      e[        U[        5      (       a  US:  a  [        SU 35      e[        XU5      n[         R                  " US	S
9n[         R                  " USS
9n[         R                  " USSS9nXV-   U-
  nXs-   X-   -  n	U	$ )a  Calculate mean Intersection-Over-Union (mIOU).

The function internally computes the confusion matrix.

Args:
    pred : tensor with estimated targets returned by a
      classifier. The shape can be :math:`(B, *)` and must contain integer
      values between 0 and K-1.
    target: tensor with ground truth (correct) target
      values. The shape can be :math:`(B, *)` and must contain integer
      values between 0 and K-1, where targets are assumed to be provided as
      one-hot vectors.
    num_classes: total possible number of classes in target.
    eps: epsilon for numerical stability.

Returns:
    a tensor representing the mean intersection-over union
    with shape :math:`(B, K)` where K is the number of classes.

Example:
    >>> logits = torch.tensor([[0, 1, 0]])
    >>> target = torch.tensor([[0, 1, 0]])
    >>> mean_iou(logits, target, num_classes=3)
    tensor([[1., 1., 1.]])

zBInput pred type is not a torch.Tensor with torch.int64 dtype. Got zDInput target type is not a torch.Tensor with torch.int64 dtype. Got z6Inputs pred and target must have the same shape. Got: z and z(Inputs must be in the same device. Got: z -    z?The number of classes must be an integer bigger than two. Got: r   dim)dim1dim2)torch	is_tensordtypeint64	TypeErrortypeshape
ValueErrordevice
isinstanceintr   sumdiagonal)
r   r   r   r   conf_matsum_over_rowsum_over_colconf_mat_diagdenominatoriouss
             Q/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/metrics/mean_iou.pymean_iour&      st   6 ??4  TZZu{{%B\]abf]g\hijj??6""v||5;;'F^_cdj_k^lmnn::%QRVR\R\Q]]bcicocobpqrr;;&--'CDKK=PSTZTaTaSbcddk3'';?Z[fZghii .dKHH 99X1-L99X1-LNN8"2>M-=K K$56DK    boxes
box_formatc                    US:X  a  U $ US:X  aL  U SS2SS24   U SS2SS24   U SS2SS24   U SS2SS24   4u  p#pEX$-   nX5-   n[         R                  " X#Xg/SS	9$ US
:X  a`  U SS2SS24   U SS2SS24   U SS2SS24   U SS2SS24   4u  ppEXS-  -
  n
XS-  -
  nXS-  -   nXS-  -   n[         R                  " XXg/SS	9$ [        SU S35      e)zConvert bounding boxes from various formats to xyxy format.

Args:
    boxes: tensor of bounding boxes in shape (N, 4).
    box_format: box format - one of 'xyxy', 'xywh', or 'cxcywh'.

Returns:
    boxes in xyxy format (x1, y1, x2, y2).
xyxyxywhNr   r   r         r   cxcywhzUnsupported box format: z-. Must be one of 'xyxy', 'xywh', or 'cxcywh'.)r   catr   )r(   r)   xywhx2y2cxcyx1y1s               r%   _convert_boxes_to_xyxyr;   N   s     V	v	1ac6]E!QqS&M5AaC=%1Q3-O
aUUyy!Q//	x	Q!V}eAqsFmU1ac6]E!QqS&MQa%Za%Za%Za%Zyy"")q113J<?lmnnr'   boxes_1boxes_2c                    [        X5      n[        X5      nUSS2S4   USS2S4   -
  S:  R                  5       (       a)  USS2S4   USS2S4   -
  S:  R                  5       (       d  [        S5      eUSS2S4   USS2S4   -
  S:  R                  5       (       a)  USS2S4   USS2S4   -
  S:  R                  5       (       d  [        S5      e[        R                  " USS2SS24   R                  S5      USS2SS24   R                  S5      5      n[        R                  " USS2SS24   R                  S5      USS2SS24   R                  S5      5      n[        R                  " Xe-
  SS9nUSS2SS2S4   USS2SS2S4   -  nUSS2S4   USS2S4   -
  USS2S4   USS2S4   -
  -  n	USS2S4   USS2S4   -
  USS2S4   USS2S4   -
  -  n
U	R                  S5      U
R                  S5      -   U-
  nX-  $ )	a/  Compute the IoU of the cartesian product of two sets of boxes.

Args:
    boxes_1: a tensor of bounding boxes in :math:`(B1, 4)`.
    boxes_2: a tensor of bounding boxes in :math:`(B2, 4)`.
    box_format: the bounding box format. Supported formats are:
        - 'xyxy': (x1, y1, x2, y2) where (x1, y1) is top-left and (x2, y2) is bottom-right
        - 'xywh': (x, y, w, h) where (x, y) is top-left, w is width, h is height
        - 'cxcywh': (cx, cy, w, h) where (cx, cy) is center, w is width, h is height
        Default: 'xyxy'.

Returns:
    a tensor in dimensions :math:`(B1, B2)`, representing the
    intersection of each of the boxes in set 1 with respect to each of the boxes in set 2.

Example:
    >>> # XYXY format
    >>> boxes_1 = torch.tensor([[40, 40, 60, 60], [30, 40, 50, 60]])
    >>> boxes_2 = torch.tensor([[40, 50, 60, 70], [30, 40, 40, 50]])
    >>> mean_iou_bbox(boxes_1, boxes_2)
    tensor([[0.3333, 0.0000],
            [0.1429, 0.2500]])
    >>> # XYWH format
    >>> boxes_1_xywh = torch.tensor([[40, 40, 20, 20], [30, 40, 20, 20]])
    >>> boxes_2_xywh = torch.tensor([[40, 50, 20, 20], [30, 40, 10, 10]])
    >>> mean_iou_bbox(boxes_1_xywh, boxes_2_xywh, box_format='xywh')
    tensor([[0.3333, 0.0000],
            [0.1429, 0.2500]])
    >>> # CXCYWH format
    >>> boxes_1_cxcywh = torch.tensor([[50, 50, 20, 20], [40, 50, 20, 20]])
    >>> boxes_2_cxcywh = torch.tensor([[50, 60, 20, 20], [35, 45, 10, 10]])
    >>> mean_iou_bbox(boxes_1_cxcywh, boxes_2_cxcywh, box_format='cxcywh')
    tensor([[0.3333, 0.0000],
            [0.1429, 0.2500]])

Nr   r   r-   r   z0Boxes_1 contains invalid boxes after conversion.z0Boxes_2 contains invalid boxes after conversion.)min)r;   allAssertionErrorr   max	unsqueezer?   clamp)r<   r=   r)   boxes_1_xyxyboxes_2_xyxylower_boundsupper_boundsintersection_dimsintersectionareas_set_1areas_set_2unions               r%   mean_iou_bboxrN   l   sM   L *'>L)'>L q!t
|AqD1
1Q	6;;==LQRTUQUDVYefgijfjYkDkopCpBuBuBwBwOPP
q!t
|AqD1
1Q	6;;==LQRTUQUDVYefgijfjYkDkopCpBuBuBwBwOPP 99\!RaR%0::1=|ArPQrE?R?\?\]^?_`L99\!QR%0::1=|AqrE?R?\?\]^?_`LL$?QG$Q1W-0A!Q'0JJL  1%QT(::|AqD?QT`abdeaeTf?fgK1%QT(::|AqD?QT`abdeaeTf?fgK !!!${'<'<Q'??,NEr'   )gư>)r+   )	r   r   Tensorr   floatr&   strr;   rN    r'   r%   <module>rS      s   $  .45<< 4 4C 4e 4_d_k_k 4no%,, oC oELL o<@ 5<< @ %,, @ C @ ]b]i]i @ r'   