o
    oi"                     @   s4   d dl Z 	d
de jde jdedede jf
dd	ZdS )    NFpredtargetnum_classes
normalizedreturnc                 C   sT  t | s| jt jurtdt|  t |s(|jt jur(tdt| | j|jks:td| j d|j | j|jksLtd| j d|j t	|t
rU|dk r\td| | jd	 }| ||  }||d
}g }t|D ]}|| }	t j|	|d d}
||
 qst |}||||t j}|rt j|ddd}||d  }|S )a	  Compute confusion matrix to evaluate the accuracy of a classification.

    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.
        normalized: whether to return the confusion matrix normalized.

    Returns:
        a tensor containing the confusion matrix with shape
        :math:`(B, K, K)` where K is the number of classes.

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

    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   )	minlength   T)dimkeepdimgư>)torch	is_tensordtypeint64	TypeErrortypeshape
ValueErrordevice
isinstanceintviewrangebincountappendstacktofloat32sum)r   r   r   r   
batch_sizepre_bincountpre_bincount_vecconfusion_listiter_idpb	bin_countconfusion_vecconfusion_matnorm_val r*   S/home/ubuntu/.local/lib/python3.10/site-packages/kornia/metrics/confusion_matrix.pyconfusion_matrix   s0   

r,   )F)r   Tensorr   boolr,   r*   r*   r*   r+   <module>   s   