o
    _{jèP  ã                   @   s”   d dl Zd dd„Zdd„ Zd!dd„Zd	d
„ Zdd„ Zdd„ Z	d"dd„Zde 	ddd¡fdd„Z
dd„ Zddd„Zdd„ Ze 	ddd¡fdd„ZdS )#é    Nc                 C   s¸  |dvrt dƒ‚t| tƒrt | ¡} t| tjƒs!tdt| ƒ› �ƒ‚| jtj	ks/td| j› �ƒ‚t|tƒr9t |¡}t|tjƒsHtdt|ƒ› �ƒ‚|jtj	ksVtd|j› �ƒ‚t 
t | |f¡¡}t|ƒ}|d }tj|d tj	d	�}t|ƒD ]\}}|||< qv||  }	|| }
tj||
 |	 |d
 d� ||¡}tjdd��4 |dkr­||jddd� }n|dkr»||jddd� }n
|dkrÅ|| ¡  }t |¡}W d  ƒ |S 1 sÕw   Y  |S )a÷  Compute confusion matrix.

    Args:
        y_pred (list[int] | np.ndarray[int]): Prediction labels.
        y_real (list[int] | np.ndarray[int]): Ground truth labels.
        normalize (str | None): Normalizes confusion matrix over the true
            (rows), predicted (columns) conditions or all the population.
            If None, confusion matrix will not be normalized. Options are
            "true", "pred", "all", None. Default: None.

    Returns:
        np.ndarray: Confusion matrix.
    )ÚtrueÚpredÚallNz6normalize must be one of {'true', 'pred', 'all', None}z+y_pred must be list or np.ndarray, but got z'y_pred dtype must be np.int64, but got z+y_real must be list or np.ndarray, but got z'y_real dtype must be np.int64, but got éÿÿÿÿé   ©Údtypeé   )Ú	minlengthÚignore)r   r   T©ÚaxisÚkeepdimsr   r   r   N)Ú
ValueErrorÚ
isinstanceÚlistÚnpÚarrayÚndarrayÚ	TypeErrorÚtyper   Úint64ÚuniqueÚconcatenateÚlenÚzerosÚ	enumerateÚbincountÚreshapeÚerrstateÚsumÚ
nan_to_num)Zy_predZy_realÚ	normalizeZ	label_setZ
num_labelsZ	max_labelZ	label_mapÚiÚlabelZy_pred_mappedZy_real_mappedZconfusion_mat© r%   ú7/root/disk_3tb/HAC/mmaction/core/evaluation/accuracy.pyÚconfusion_matrix   sd   

ÿ
ÿ

ÿ
ÿ

þþÿÿ
÷õr'   c                 C   sR   t j| dd�}t||ƒ t¡}|jdd�}t  |¡}t  dd„ t||ƒD ƒ¡}|S )zÛCalculate mean class accuracy.

    Args:
        scores (list[np.ndarray]): Prediction scores for each class.
        labels (list[int]): Ground truth labels.

    Returns:
        np.ndarray: Mean class accuracy.
    r   ©r   c                 S   s    g | ]\}}|r|| nd ‘qS )ç        r%   )Ú.0ÚcntÚhitr%   r%   r&   Ú
<listcomp>U   s     z'mean_class_accuracy.<locals>.<listcomp>)	r   Úargmaxr'   ÚastypeÚfloatr    ÚdiagÚmeanÚzip)ÚscoresÚlabelsr   Zcf_matZcls_cntZcls_hitZmean_class_accr%   r%   r&   Úmean_class_accuracyD   s   

ÿr6   ©r   c                 C   sŒ   g }t  |¡dd…t jf }|D ]3}t j| dd�dd…| d…f dd…ddd…f }t jj||kdd�}| ¡ |jd  }| |¡ q|S )a0  Calculate top k accuracy score.

    Args:
        scores (list[np.ndarray]): Prediction scores for each class.
        labels (list[int]): Ground truth labels.
        topk (tuple[int]): K value for top_k_accuracy. Default: (1, ).

    Returns:
        list[float]: Top k accuracy score for each k.
    Nr   r(   r   r   )	r   r   ÚnewaxisÚargsortÚ
logical_orÚreducer    ÚshapeÚappend)r4   r5   ÚtopkÚresÚkZmax_k_predsZmatch_arrayZtopk_acc_scorer%   r%   r&   Útop_k_accuracyZ   s   2rA   c           	      C   s`   g }t | |ƒD ]#\}}t||ƒ\}}}t t |¡t |¡dd…  ¡ }| |¡ qt |¡S )a  Mean average precision for multi-label recognition. Used for reporting
    MMIT style mAP on Multi-Moments in Times. The difference is that this
    method calculates average-precision for each sample and averages them among
    samples.

    Args:
        scores (list[np.ndarray]): Prediction scores of different classes for
            each sample.
        labels (list[np.ndarray]): Ground truth many-hot vector for each
            sample.

    Returns:
        np.float: The MMIT style mean average precision.
    Nr   )r3   Úbinary_precision_recall_curver   r    Údiffr   r=   r2   ©	r4   r5   ÚresultsÚscorer$   Ú	precisionÚrecallÚ_Úapr%   r%   r&   Úmmit_mean_average_precisionp   s   $
rK   c           	      C   s”   g }t  | ¡j} t  |¡j}t| |ƒD ]#\}}t||ƒ\}}}t  t  |¡t  |¡dd…  ¡ }| |¡ qdd„ |D ƒ}|g krEt j	S t  
|¡S )aC  Mean average precision for multi-label recognition.

    Args:
        scores (list[np.ndarray]): Prediction scores of different classes for
            each sample.
        labels (list[np.ndarray]): Ground truth many-hot vector for each
            sample.

    Returns:
        np.float: The mean average precision.
    Nr   c                 S   s   g | ]	}t  |¡s|‘qS r%   )r   Úisnan)r*   Úxr%   r%   r&   r-   ›   s    z*mean_average_precision.<locals>.<listcomp>)r   ÚstackÚTr3   rB   r    rC   r   r=   Únanr2   rD   r%   r%   r&   Úmean_average_precision‡   s   $
rQ   c                 C   s  t | tjƒsJ ‚t |tjƒsJ ‚| j|jksJ ‚|dk}tj| dd�ddd… }| | } || }t t | ¡¡d }tj||jd f }t 	|¡| }d| | }| | }|||  }d|t 
|¡< ||d  }	| |d ¡}
t|
ddƒ}tj|| df tj|	| df || fS )a%  Calculate the binary precision recall curve at step thresholds.

    Args:
        y_score (np.ndarray): Prediction scores for each class.
            Shape should be (num_classes, ).
        y_true (np.ndarray): Ground truth many-hot vector.
            Shape should be (num_classes, ).

    Returns:
        precision (np.ndarray): The precision of different thresholds.
        recall (np.ndarray): The recall of different thresholds.
        thresholds (np.ndarray): Different thresholds at which precison and
            recall are tested.
    r   Ú	mergesort)ÚkindNr   r   )r   r   r   r<   r9   ÚwhererC   Úr_ÚsizeÚcumsumrL   ÚsearchsortedÚslice)Zy_scoreZy_trueZdesc_score_indicesZdistinct_value_indsZthreshold_indsZtpsÚfpsZ
thresholdsrG   rH   Zlast_indÚslr%   r%   r&   rB   ¡   s$   *rB   Fc                 C   s˜  | j }|j dks|dvrtdƒ‚|dkr| tjdd…f } |jd | jd }}tj||ftjd�}|r>tj||ftjd�}t|ƒD ]k}| |dd…f }	t |	d |dd…df ¡}
t 	|	d |dd…df ¡}||
  
d¡}|dd…df |dd…df  |	d |	d   | }| t¡| |dd…|f< |r­|	d |	d  }| t¡| |dd…|f< qB|dkr¹tj|dd�}|rÊ|dkrÆtj|dd�}||fS |S )	a»  Compute intersection over union between segments.

    Args:
        candidate_segments (np.ndarray): 1-dim/2-dim array in format
            ``[init, end]/[m x 2:=[init, end]]``.
        target_segments (np.ndarray): 2-dim array in format
            ``[n x 2:=[init, end]]``.
        calculate_overlap_self (bool): Whether to calculate overlap_self
            (union / candidate_length) or not. Default: False.

    Returns:
        t_iou (np.ndarray): 1-dim array [n] /
            2-dim array [n x m] with IoU ratio.
        t_overlap_self (np.ndarray, optional): 1-dim array [n] /
            2-dim array [n x m] with overlap_self, returns when
            calculate_overlap_self is True.
    r	   )r   r	   z#Dimension of arguments is incorrectr   Nr   r   r(   )Úndimr   r   r8   r<   ÚemptyÚfloat32ÚrangeÚmaximumÚminimumÚclipr/   r0   Úsqueeze)Zcandidate_segmentsZtarget_segmentsZcalculate_overlap_selfZcandidate_segments_ndimÚnÚmÚt_iouZt_overlap_selfr#   Zcandidate_segmentZtt1Ztt2Zsegments_intersectionZsegments_unionZcandidate_lengthr%   r%   r&   Úpairwise_temporal_iouÍ   s@   ÿþÿ€rg   g      à?gffffffî?é
   c           !   	   C   sº  t | ƒ}|st|ƒ| }|t|ƒ | }g }d}| D ]”}	||	 }
|
dd…dd…f }|
dd…df  ¡ ddd… }||dd…f  tj¡}| |	 }|dd…dd…f  tj¡}|jd dkrn|jd }| t |df¡¡ q|j	dkrztj
|dd�}|j	dkr†tj
|dd�}t t|jd | ƒ|jd ¡}||7 }|d|…dd…f }t||ƒ}| |¡ qt dd¡d |t|ƒ |  }t ||jd f¡}t |¡}t |jd |jd f¡}t|ƒD ]Y\}}t|ƒD ]@\}}|jd ||< ||k}t |jd |  tj¡|jd ¡}t|ƒD ]\}}t |dd…d|…f jdd�¡|||f< �qqç|jdd�| ¡  ||dd…f< qß|jdd�}|t|ƒ|  }t ||¡}dt|ƒ |d  } |||| fS )	a¹  Computes the average recall given an average number (percentile) of
    proposals per video.

    Args:
        ground_truth (dict): Dict containing the ground truth instances.
        proposals (dict): Dict containing the proposal instances.
        total_num_proposals (int): Total number of proposals in the
            proposal dict.
        max_avg_proposals (int | None): Max number of proposals for one video.
            Default: None.
        temporal_iou_thresholds (np.ndarray): 1D array with temporal_iou
            thresholds. Default: ``np.linspace(0.5, 0.95, 10)``.

    Returns:
        tuple([np.ndarray, np.ndarray, np.ndarray, float]):
            (recall, average_recall, proposals_per_video, auc)
            In recall, ``recall[i,j]`` is recall at i-th temporal_iou threshold
            at the j-th average number (percentile) of average number of
            proposals per video. The average_recall is recall averaged
            over a list of temporal_iou threshold (1D array). This is
            equivalent to ``recall.mean(axis=0)``. The ``proposals_per_video``
            is the average number of proposals per video. The auc is the area
            under ``AR@AN`` curve.
    r   Nr	   r   r   r(   ée   g      Y@)r   r0   r9   r/   r   r^   r<   r=   r   r\   Úexpand_dimsra   Úintrg   Úaranger]   r   Úcount_nonzeror    r2   Útrapz)!Úground_truthÚ	proposalsZtotal_num_proposalsZmax_avg_proposalsÚtemporal_iou_thresholdsZtotal_num_videosÚratioÚ
score_listZtotal_num_retrieved_proposalsZvideo_idZproposals_video_idZthis_video_proposalsÚsort_idxZground_truth_video_idZthis_video_ground_truthrd   Znum_retrieved_proposalsrf   Zpcn_listÚmatchesZ	positivesrH   ZridxZtemporal_iour#   rF   Ztrue_positives_temporal_iouZpcn_proposalsÚjZ
avg_recallZproposals_per_videoZarea_under_curveÚaucr%   r%   r&   Úaverage_recall_at_avg_proposals	  sŽ   ÿÿ


ÿþÿÿÿ
ÿÿ
ÿÿÿý"
ÿrx   c                 C   st   t | ƒt |ƒks
J ‚t | d ƒ}tdt | ƒƒD ]}t | | ƒ|ks#J ‚qt | ¡}t |¡}tt |j|¡jƒ}|S )ay  Get weighted score with given scores and coefficients.

    Given n predictions by different classifier: [score_1, score_2, ...,
    score_n] (score_list) and their coefficients: [coeff_1, coeff_2, ...,
    coeff_n] (coeff_list), return weighted score: weighted_score =
    score_1 * coeff_1 + score_2 * coeff_2 + ... + score_n * coeff_n

    Args:
        score_list (list[list[np.ndarray]]): List of list of scores, with shape
            n(number of predictions) X num_samples X num_classes
        coeff_list (list[float]): List of coefficients, with shape n.

    Returns:
        list[np.ndarray]: List of weighted scores.
    r   r   )r   r_   r   r   r   ÚdotrO   )rs   Z
coeff_listÚnum_samplesr#   r4   ÚcoeffZweighted_scoresr%   r%   r&   Úget_weighted_score…  s   

r|   r   c                 C   s,   t  | t j| |dd� ¡}||j|dd� S )z4Compute softmax values for each sets of scores in x.Tr   )r   ÚexpÚmaxr    )rM   ÚdimZe_xr%   r%   r&   Úsoftmax   s   r€   c                 C   s®   t  dg| dgg¡}t  dg|dgg¡}tt|ƒd ƒddd… D ]}t|| ||d  ƒ||< q!t  |dd… |dd… k¡d d }t  || ||d   ||  ¡}|S )uú   Interpolated AP - VOCdevkit from VOC 2011.

    Args:
        precision (np.ndarray): The precision of different thresholds.
        recall (np.ndarray): The recall of different thresholds.

    Returnsï¼š
        float: Average precision score.
    r   r   Nr   )r   Úhstackr_   r   r~   rT   r    )rG   rH   Z
mprecisionZmrecallr#   ÚidxrJ   r%   r%   r&   Úinterpolated_precision_recall¦  s   
&"rƒ   c                 C   sl  t jt|ƒt jd�}t|ƒdk r|S d}tƒ }| D ]}t  t|ƒt| | ƒf¡d ||< |t| | ƒ7 }qt  |¡}|dd…df  t¡}t  	|¡ddd… }|| }t jt|ƒt|ƒft j
d�}	t jt|ƒt|ƒft j
d�}
t|ƒD ]ƒ\}}|d | v r‰t j| |d  td�}n	d|
dd…|f< qst|dd…  t¡|ƒ}| 	¡ ddd… }t|ƒD ]J\}}|D ]-}|| |k rÁd|
||f<  n||d  ||f dkrÎq±d|	||f< |||d  ||f<  |
||f dkrõ|	||f dkrõd|
||f< q«qst j|	dd	� t j¡}t j|
dd	� t j¡}|| }|||  }tt|ƒƒD ]}t||dd…f ||dd…f ƒ||< �q|S )
aS  Compute average precision (in detection task) between ground truth and
    predicted data frames. If multiple predictions match the same predicted
    segment, only the one with highest score is matched as true positive. This
    code is greatly inspired by Pascal VOC devkit.

    Args:
        ground_truth (dict): Dict containing the ground truth instances.
            Key: 'video_id'
            Value (np.ndarray): 1D array of 't-start' and 't-end'.
        prediction (np.ndarray): 2D array containing the information of
            proposal instances, including 'video_id', 'class_id', 't-start',
            't-end' and 'score'.
        temporal_iou_thresholds (np.ndarray): 1D array with temporal_iou
            thresholds. Default: ``np.linspace(0.5, 0.95, 10)``.

    Returns:
        np.ndarray: 1D array of average precision score.
    r   r   r)   r   Né   r   r	   r(   )r   r   r   r^   ÚdictÚonesr   r/   r0   r9   Úint32r   rg   rW   r_   rƒ   )ro   Z
predictionrq   rJ   Únum_gtsZlock_gtÚkeyr4   rt   ÚtpÚfpr‚   Z	this_predZthis_gtrf   Zt_iou_sorted_idxZt_idxZt_iou_thresholdZjdxZ	tp_cumsumZ	fp_cumsumZrecall_cumsumZprecision_cumsumr%   r%   r&   Ú!average_precision_at_temporal_iou¹  sh   ÿÿ
ÿÿ €óÿrŒ   )N)r7   )F)Únumpyr   r'   r6   rA   rK   rQ   rB   rg   Úlinspacerx   r|   r€   rƒ   rŒ   r%   r%   r%   r&   Ú<module>   s*    
@
.
þ?ÿ
ü|
ÿþ