a
    dY                     @   sN   d dl Z ddlmZ de je jeee jdddZe je je jdd	d
ZdS )    N   )confusion_matrixư>)inputtargetnum_classesepsreturnc           
      C   s  t | s(| jt jur(tdt| t |sP|jt jurPtdt|| j|jksptd| j|j| j	|j	kstd| j	|j	t
|tr|dk rtd|t| ||}t j|dd}t j|dd}t j|d	d
d}|| | }|| ||  }	|	S )a  Calculate mean Intersection-Over-Union (mIOU).

    The function internally computes the confusion matrix.

    Args:
        input : 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.

    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.]])
    zEInput input type is not a torch.Tensor with torch.int64 dtype. Got {}zFInput target type is not a torch.Tensor with torch.int64 dtype. Got {}z@Inputs input and target must have the same shape. Got: {} and {}z/Inputs must be in the same device. Got: {} - {}   zAThe number of classes must be an integer bigger than two. Got: {}r   )Zdim)Zdim1Zdim2)torchZ	is_tensorZdtypeint64	TypeErrorformattypeshape
ValueErrorZdevice
isinstanceintr   sumZdiagonal)
r   r   r   r   Zconf_matZsum_over_rowZsum_over_colZconf_mat_diagdenominatorZious r   `/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/metrics/mean_iou.pymean_iou   s*    r   )boxes_1boxes_2r	   c           	      C   s  | dddf | dddf  dk  sX| dddf | dddf  dk  sXtd|dddf |dddf  dk  s|dddf |dddf  dk  stdt| ddddf d|ddddf d}t| ddddf d|ddddf d}tj|| dd}|dddddf |dddddf  }| dddf | dddf  | dddf | dddf   }|dddf |dddf  |dddf |dddf   }|d|d | }|| S )	a  Compute the IoU of the cartesian product of two sets of boxes.

    Each box in each set shall be (x1, y1, x2, y2).

    Args:
        boxes_1: a tensor of bounding boxes in :math:`(B1, 4)`.
        boxes_2: a tensor of bounding boxes in :math:`(B2, 4)`.

    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:
        >>> 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]])
    Nr
   r      r   z0Boxes_1 does not follow (x1, y1, x2, y2) format.z0Boxes_2 does not follow (x1, y1, x2, y2) format.)min)allAssertionErrorr   maxZ	unsqueezer   clamp)	r   r   Zlower_boundsZupper_boundsZintersection_dimsintersectionZareas_set_1Zareas_set_2unionr   r   r   mean_iou_bbox?   s    PP88,@@r%   )r   )r   r   ZTensorr   floatr   r%   r   r   r   r   <module>   s   9