o
    _{j  ã                   @   s\   d dl Z d dlmZ d dlm  mZ ddlmZ ddlmZ e 	¡ G dd„ dej
ƒƒZdS )é    Né   )ÚLOSSESé   ©Úbinary_logistic_regression_lossc                   @   sR   e Zd ZdZedd„ ƒZe		ddd„ƒZe				
ddd„ƒZ			ddd„ZdS )ÚBMNLossa
  BMN Loss.

    From paper https://arxiv.org/abs/1907.09702,
    code https://github.com/JJBOY/BMN-Boundary-Matching-Network.
    It will calculate loss for BMN Model. This loss is a weighted sum of

        1) temporal evaluation loss based on confidence score of start and
        end positions.
        2) proposal evaluation regression loss based on confidence scores of
        candidate proposals.
        3) proposal evaluation classification loss based on classification
        results of candidate proposals.
    c                 C   s    t | |ƒ}t ||ƒ}|| }|S )aF  Calculate Temporal Evaluation Module Loss.

        This function calculate the binary_logistic_regression_loss for start
        and end respectively and returns the sum of their losses.

        Args:
            pred_start (torch.Tensor): Predicted start score by BMN model.
            pred_end (torch.Tensor): Predicted end score by BMN model.
            gt_start (torch.Tensor): Groundtruth confidence score for start.
            gt_end (torch.Tensor): Groundtruth confidence score for end.

        Returns:
            torch.Tensor: Returned binary logistic loss.
        r   )Ú
pred_startÚpred_endÚgt_startÚgt_endÚ
loss_startÚloss_endÚloss© r   ú5/root/disk_3tb/HAC/mmaction/models/losses/bmn_loss.pyÚtem_loss   s   

zBMNLoss.tem_lossçffffffæ?ç333333Ó?c                 C   sô   ||k  ¡ }||k||k@   ¡ }||k|dk@   ¡ }|| }t |¡}t |¡}	t |¡}
||	 }t |¡}|| }|d| k  ¡ }||
 }t |¡}|| }|d| k  ¡ }|| | }t | | || ¡}dt |t |¡ ¡ t |¡ }|S )aK  Calculate Proposal Evaluation Module Regression Loss.

        Args:
            pred_score (torch.Tensor): Predicted temporal_iou score by BMN.
            gt_iou_map (torch.Tensor): Groundtruth temporal_iou score.
            mask (torch.Tensor): Boundary-Matching mask.
            high_temporal_iou_threshold (float): Higher threshold of
                temporal_iou. Default: 0.7.
            low_temporal_iou_threshold (float): Higher threshold of
                temporal_iou. Default: 0.3.

        Returns:
            torch.Tensor: Proposal evalutaion regression loss.
        g        ç      ð?ç      à?)ÚfloatÚtorchÚsumÚ	rand_likeÚFÚmse_lossÚ	ones_like)Ú
pred_scoreÚ
gt_iou_mapÚmaskZhigh_temporal_iou_thresholdZlow_temporal_iou_thresholdÚu_hmaskÚu_mmaskÚu_lmaskÚnum_hÚnum_mÚnum_lÚr_mÚu_smmaskÚr_lÚu_slmaskÚweightsr   r   r   r   Úpem_reg_loss.   s>   ÿÿÿÿ




ÿÿzBMNLoss.pem_reg_lossçÍÌÌÌÌÌì?©gÍÌÌÌÌÌð?é   çñhãˆµøä>c                 C   s¾   ||k  ¡ }||k  ¡ }|| }tt |¡dƒ}|t |¡ }	|	| }
t |
|d |d ¡}
d|
 |
d  }d|
 }|t | | ¡ | }|t d|  | ¡ | }dt || ¡ |	 }|S )a�  Calculate Proposal Evaluation Module Classification Loss.

        Args:
            pred_score (torch.Tensor): Predicted temporal_iou score by BMN.
            gt_iou_map (torch.Tensor): Groundtruth temporal_iou score.
            mask (torch.Tensor): Boundary-Matching mask.
            threshold (float): Threshold of temporal_iou for positive
                instances. Default: 0.9.
            ratio_range (tuple): Lower bound and upper bound for ratio.
                Default: (1.05, 21)
            eps (float): Epsilon for small value. Default: 1e-5

        Returns:
            torch.Tensor: Proposal evalutaion classification loss.
        r   r   r   r   éÿÿÿÿ)r   Úmaxr   r   ÚclampÚlog)r   r   r   Ú	thresholdÚratio_rangeÚepsÚpmaskÚnmaskÚnum_positiveÚnum_entriesÚratioÚcoef_0Úcoef_1Zloss_posZloss_negr   r   r   r   Úpem_cls_loss_   s   zBMNLoss.pem_cls_lossr   ç      $@c                 C   s€   |dd…df   ¡ }|dd…df   ¡ }|| }|  |||¡}|  |||¡}|  ||||¡}|| |	|  |
|  }||||fS )a
  Calculate Boundary Matching Network Loss.

        Args:
            pred_bm (torch.Tensor): Predicted confidence score for boundary
                matching map.
            pred_start (torch.Tensor): Predicted confidence score for start.
            pred_end (torch.Tensor): Predicted confidence score for end.
            gt_iou_map (torch.Tensor): Groundtruth score for boundary matching
                map.
            gt_start (torch.Tensor): Groundtruth temporal_iou score for start.
            gt_end (torch.Tensor): Groundtruth temporal_iou score for end.
            bm_mask (torch.Tensor): Boundary-Matching mask.
            weight_tem (float): Weight for tem loss. Default: 1.0.
            weight_pem_reg (float): Weight for pem regression loss.
                Default: 10.0.
            weight_pem_cls (float): Weight for pem classification loss.
                Default: 1.0.

        Returns:
            tuple([torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]):
                (loss, tem_loss, pem_reg_loss, pem_cls_loss). Loss is the bmn
                loss, tem_loss is the temporal evaluation loss, pem_reg_loss is
                the proposal evaluation regression loss, pem_cls_loss is the
                proposal evaluation classification loss.
        Nr   r   )Ú
contiguousr+   r>   r   )ÚselfZpred_bmr   r	   r   r
   r   Úbm_maskZ
weight_temZweight_pem_regZweight_pem_clsZpred_bm_regZpred_bm_clsr+   r>   r   r   r   r   r   Úforward†   s   $ÿÿzBMNLoss.forwardN)r   r   )r,   r-   r/   )r   r?   r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r+   r>   rC   r   r   r   r   r   	   s"    
ü0û.ör   )r   Útorch.nnÚnnÚtorch.nn.functionalÚ
functionalr   Úregistryr   r   Úregister_moduleÚModuler   r   r   r   r   Ú<module>   s    