o
    _{j}’  ã                   @   sº   d dl Z d dlmZ d dlZd dlmZ d dlZd dlZ	d dl
mZ ddlmZ ddlmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ G dd„ dƒZe ¡ G dd„ deƒƒZdS )é    N)ÚOrderedDict)Ú_pairé   )Úsoftmax)Úeval_apÚload_localize_proposal_fileÚperform_regressionÚtemporal_iouÚtemporal_nms)Úget_root_loggeré   )ÚBaseDataset)ÚDATASETSc                   @   s(   e Zd ZdZ			ddd„Zdd„ ZdS )	ÚSSNInstancea  Proposal instance of SSN.

    Args:
        start_frame (int): Index of the proposal's start frame.
        end_frame (int): Index of the proposal's end frame.
        num_video_frames (int): Total frames of the video.
        label (int | None): The category label of the proposal. Default: None.
        best_iou (float): The highest IOU with the groundtruth instance.
            Default: 0.
        overlap_self (float): Percent of the proposal's own span contained
            in a groundtruth instance. Default: 0.
    Nr   c                 C   s^   || _ t||ƒ| _|| _|d ur|nd| _|| | | _|| _|| _d | _d | _	ddg| _
d S )Néÿÿÿÿç        )Ústart_frameÚminÚ	end_frameÚnum_video_framesÚlabelÚcoverageÚbest_iouÚoverlap_selfÚloc_regÚsize_regÚregression_targets)Úselfr   r   r   r   r   r   © r   ú3/root/disk_3tb/HAC/mmaction/datasets/ssn_dataset.pyÚ__init__    s   zSSNInstance.__init__c                    s¨   ‡ fdd„|D ƒ}|t  |¡ }ˆ jˆ j d d }|j|j d d }ˆ jˆ j }|j|j }|| | ˆ _t  || ¡ˆ _ˆ jdurMˆ jˆ jgˆ _dS ddgˆ _dS )zˆCompute regression targets of positive proposals.

        Args:
            gt_list (list): The list of groundtruth instances.
        c                    s"   g | ]}t ˆ jˆ j|j|jƒ‘qS r   )r	   r   r   )Ú.0Úgt©r   r   r   Ú
<listcomp>9   s    ÿÿÿz:SSNInstance.compute_regression_targets.<locals>.<listcomp>r   r   Nr   )ÚnpÚargmaxr   r   r   Úlogr   r   )r   Zgt_listÚiousZbest_gtZproposal_centerZ	gt_centerZproposal_sizeZgt_sizer   r#   r   Úcompute_regression_targets2   s   
þ
ÿÿz&SSNInstance.compute_regression_targets)Nr   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r)   r   r   r   r   r      s    
úr   c                       sÌ   e Zd ZdZ										
					d.‡ fdd„	Zdd„ Zd/dd„Zdeedd�d�dfdd„Zdd„ Z	dd„ Z
ed0dd„ƒZe		d1d d!„ƒZd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Z‡  ZS )2Ú
SSNDataseta{  Proposal frame dataset for Structured Segment Networks.

    Based on proposal information, the dataset loads raw frames and applies
    specified transforms to return a dict containing the frame tensors and
    other information.

    The ann_file is a text file with multiple lines and each
    video's information takes up several lines. This file can be a normalized
    file with percent or standard file with specific frame indexes. If the file
    is a normalized file, it will be converted into a standard file first.

    Template information of a video in a standard file:
    .. code-block:: txt
        # index
        video_id
        num_frames
        fps
        num_gts
        label, start_frame, end_frame
        label, start_frame, end_frame
        ...
        num_proposals
        label, best_iou, overlap_self, start_frame, end_frame
        label, best_iou, overlap_self, start_frame, end_frame
        ...

    Example of a standard annotation file:
    .. code-block:: txt
        # 0
        video_validation_0000202
        5666
        1
        3
        8 130 185
        8 832 1136
        8 1303 1381
        5
        8 0.0620 0.0620 790 5671
        8 0.1656 0.1656 790 2619
        8 0.0833 0.0833 3945 5671
        8 0.0960 0.0960 4173 5671
        8 0.0614 0.0614 3327 5671

    Args:
        ann_file (str): Path to the annotation file.
        pipeline (list[dict | callable]): A sequence of data transforms.
        train_cfg (dict): Config for training.
        test_cfg (dict): Config for testing.
        data_prefix (str): Path to a directory where videos are held.
        test_mode (bool): Store True when building test or validation dataset.
            Default: False.
        filename_tmpl (str): Template for each filename.
            Default: 'img_{:05}.jpg'.
        start_index (int): Specify a start index for frames in consideration of
            different filename format. Default: 1.
        modality (str): Modality of data. Support 'RGB', 'Flow'.
            Default: 'RGB'.
        video_centric (bool): Whether to sample proposals just from
            this video or sample proposals randomly from the entire dataset.
            Default: True.
        reg_normalize_constants (list): Regression target normalized constants,
            including mean and standard deviation of location and duration.
        body_segments (int): Number of segments in course period.
            Default: 5.
        aug_segments (list[int]): Number of segments in starting and
            ending period. Default: (2, 2).
        aug_ratio (int | float | tuple[int | float]): The ratio of the length
            of augmentation to that of the proposal. Defualt: (0.5, 0.5).
        clip_len (int): Frames of each sampled output clip.
            Default: 1.
        frame_interval (int): Temporal interval of adjacent sampled frames.
            Default: 1.
        filter_gt (bool): Whether to filter videos with no annotation
            during training. Default: True.
        use_regression (bool): Whether to perform regression. Default: True.
        verbose (bool): Whether to print full information or not.
            Default: False.
    Fúimg_{:05d}.jpgr   ÚRGBTNé   ©r   r   ©ç      à?r4   c                    s°  t ƒ ˆ _tƒ j||||||	d� |ˆ _|ˆ _|jjˆ _|jjˆ _|jj	ˆ _	|ˆ _
|ˆ _|s/|s9dd„ tˆ jƒD ƒ}ˆ j t|ƒ› dtˆ jƒ› d�¡ ‡ fdd„|D ƒˆ _g ˆ _g ˆ _g ˆ _ˆ  ¡  |d u rkˆ  ¡ ˆ _n|ˆ _|
ˆ _|ˆ _|ˆ _t|ƒˆ _t ˆ jttf¡sŽtdt |ƒ› �ƒ‚tˆ jƒdks—J ‚ˆ jj!ˆ jj" ˆ jj# }tˆ jj$ˆ jj!|  ƒˆ _%tˆ jj$ˆ jj"|  ƒˆ _&ˆ jj$ˆ j% ˆ j& ˆ _'ˆ jjjj(ˆ _(|ˆ _)|ˆ _*|ˆ _+|ˆ _,|ˆ _-ˆ j
�rKˆ j d	ˆ j.› d
tˆ jƒtˆ jƒ tˆ jƒ › dtˆ jƒ› dtˆ jƒ› dtˆ jƒ› dtˆ jƒ› dˆ j%› dˆ j&› dˆ j'› dˆ j› dˆ jd d d›dˆ jd d d›dˆ jd d d›dˆ jd d d›d�¡ d S ˆ j dˆ j.› d�¡ d S )N)Údata_prefixÚ	test_modeÚstart_indexÚmodalityc                 S   s$   g | ]\}}t |d  ƒdkr|‘qS )Úgtsr   )Úlen)r!   ÚiÚ
video_infor   r   r   r$   Ç   s
    ÿz'SSNDataset.__init__.<locals>.<listcomp>z out of z videos are valid.c                    s   g | ]}ˆ j | ‘qS r   )Úvideo_infos)r!   r;   r#   r   r   r$   Í   s    zAaug_ratio should be int, floator tuple of int and float, but got r   z'
            SSNDataset: proposal file z  parsed.

            There are z usable proposals from z videos.
            z  positive proposals
            z" incomplete proposals
            zL background proposals

            Sample config:
            FG/BG/INCOMP: ú/z)  # noqa:E501
            Video Centric: zM

            Regression Normalization Constants:
            Location: mean r   z.05fz std r   z) # noqa: E501
            Duration: mean z # noqa: E501
            zSSNDataset: proposal file z parsed.)/r   ÚloggerÚsuperr    Ú	train_cfgÚtest_cfgZssnÚassignerÚsamplerÚ	evaluaterÚverboseÚfilename_tmplÚ	enumerater=   Úinfor:   Úpositive_poolÚbackground_poolÚincomplete_poolÚconstruct_proposal_poolsÚ _compute_reg_normalize_constantsÚreg_norm_constsÚvideo_centricÚbody_segmentsÚaug_segmentsr   Ú	aug_ratioÚmmcvÚis_tuple_ofÚintÚfloatÚ	TypeErrorÚtypeZpositive_ratioZbackground_ratioZincomplete_ratioZnum_per_videoÚpositive_per_videoÚbackground_per_videoÚincomplete_per_videoÚtest_intervalÚclip_lenÚframe_intervalÚ	filter_gtÚuse_regressionr6   Úproposal_file)r   Úann_fileÚpipelinerA   rB   r5   r6   rG   r7   r8   rP   Zreg_normalize_constantsrQ   rR   rS   r^   r_   r`   ra   rF   Z
valid_indsZtotal_ratio©Ú	__class__r#   r   r    ¢   sÂ   ú


ÿ"
þÿÿ
ÿÿ
ÿÿÿÿÿÿýüûúù
ö
ö
öõòòññÿzSSNDataset.__init__c           
      C   s�  g }d| j v r| j  dd¡| _t | j¡std| j › d�ƒ‚n| j | _t| jƒ}|D ]›}| jdur:t | j|d ¡}t	|d ƒ}g }|d D ]0}t	|d ƒt	|d ƒkrvt	|d ƒ|k rvt
t	|d ƒt	|d ƒ|t	|d ƒd	d
�}| |¡ qFg }	|d D ]9}t	|d ƒt	|d ƒkr¶t	|d ƒ|k r¶t
t	|d ƒt	|d ƒ|t	|d ƒt|d ƒt|d ƒd�}|	 |¡ q}| t||d |||	d�¡ q*|S )z.Load annotation file to get video information.Znormalized_Ú z6Please refer to `$MMACTION2/tools/data` todenormalize Ú.Nr   r   r   ç      ð?)r   r   é   é   )r   r   r   )Ú	frame_dirÚvideo_idÚtotal_framesr9   Ú	proposals)rc   Úreplacerb   ÚospÚexistsÚ	Exceptionr   r5   ÚjoinrV   r   ÚappendrW   Údict)
r   r=   Zproposal_infosZproposal_inforl   Ú
num_framesr9   ÚxZssn_instancero   r   r   r   Úload_annotations  sb   

ÿÿ

(


û
€(




ú
€ûÿzSSNDataset.load_annotationséÐ  c              
   K   sR  |d d j d d }dd„ t|ƒD ƒ}tt| ƒƒD �]}| j| d }|| d }t|d j ƒdkr9t |d¡}|| d }	|| d	 }
|| d
 }|du r\tjt|ƒ|dftjd�}| d|df¡}|dkr¼t	|	dd…dd…f dd�t 
|
¡ }t|ƒD ];}|dd…|df dd…df }|dd…|df dd…df }tj||dd…|f dd…df ||fdd�|| |< qqt	|	dd…dd…f dd�t 
|
¡ }t | ¡ ¡| d… }|D ]H}|| }|| }||df ||df |||f |||df |||df g}||| v�rt |g¡|| |< qÝt || | |g¡|| |< qÝq|S )zéConvert prediction results into detections.

        Args:
            results (list): Prediction results.
            top_k (int): Number of top results. Default: 2000.

        Returns:
            list: Detection results.
        r   Úactivity_scoresr   c                 S   s   g | ]}t ƒ ‘qS r   )rv   )r!   Ú_r   r   r   r$   U  s    z4SSNDataset.results_to_detections.<locals>.<listcomp>rm   Úrelative_proposal_listrj   Úcompleteness_scoresZ
bbox_predsNr   ©Údtyper   )Údim©Úaxis)ÚshapeÚranger:   r=   r%   ÚsqueezeÚzerosÚfloat32Úreshaper   ÚexpÚconcatenateÚargsortÚravelÚarrayÚvstack)r   ÚresultsÚtop_kÚkwargsÚnum_classesÚ
detectionsÚidxrm   Zrelative_proposalsr{   r~   Zregression_scoresZcombined_scoresr;   Zcenter_scoresZduration_scoresZkeep_idxÚkÚ	class_idxZproposal_idxÚnew_itemr   r   r   Úresults_to_detectionsJ  sx   
þÿÿÿýý	ÿÿ
ÿÿÿÿú	ÿñz SSNDataset.results_to_detectionsÚmAPÚthumos14)Úeval_dataset)rš   c                    sx  t  |¡}|i krt d¡ t|d fi |¤Ž|d< t|tƒs(tdt|ƒ› �ƒ‚t	|ƒt	ˆƒks>J dt	|ƒ› dt	ˆƒ› �ƒ‚t|tt
fƒrG|n|g}dg}|D ]}||vr]td|› d�ƒ‚qOˆj|fi ˆj¤Ž}ˆjr�ˆj d¡ t|ƒD ]\‰ }	d	d
„ |ˆ   ¡ D ƒ|ˆ < quˆj d¡ ˆj d¡ t|ƒD ]\‰ }	‡fdd
„|ˆ   ¡ D ƒ|ˆ < q—ˆj d¡ ˆ ¡ }
t|ƒD ]\‰ }	ˆ |
vrÆtƒ |
ˆ < q¹i }t|ƒD ]$\‰ }	g }|ˆ   ¡ D ]\‰}| ‡ ‡fdd„| ¡ D ƒ¡ qÙ||ˆ < qÍtƒ }|D ]B}|dk�r9| di ¡ dd¡}|dk�r9t ddd¡}t||
|ƒ}|jdd�}ˆj d¡ t||ƒD ]\}}||d|d›�< �q+q÷|S )aÖ  Evaluation in SSN proposal dataset.

        Args:
            results (list[dict]): Output results.
            metrics (str | sequence[str]): Metrics to be performed.
                Defaults: 'mAP'.
            metric_options (dict): Dict for metric options. Options are
                ``eval_dataset`` for ``mAP``.
                Default: ``dict(mAP=dict(eval_dataset='thumos14'))``.
            logger (logging.Logger | None): Logger for recording.
                Default: None.
            deprecated_kwargs (dict): Used for containing deprecated arguments.
                See 'https://github.com/open-mmlab/mmaction2/pull/286'.

        Returns:
            dict: Evaluation results for evaluation metrics.
        zŠOption arguments for metrics has been changed to `metric_options`, See 'https://github.com/open-mmlab/mmaction2/pull/286' for more detailsrš   z results must be a list, but got z7The length of results is not equal to the dataset len: z != zmetric z is not supportedzPerforming location regressionc                 S   s   i | ]	\}}|t |ƒ“qS r   )r   ©r!   r–   Úvr   r   r   Ú
<dictcomp>½  s    ÿÿz'SSNDataset.evaluate.<locals>.<dictcomp>zRegression finishedzPerforming NMSc                    s    i | ]\}}|t |ˆ jjƒ“qS r   )r
   rE   Únmsr�   r#   r   r   rŸ   Å  s    ÿÿzNMS finishedc                    s    g | ]}ˆˆ g|d d…  ‘qS )Nrj   r   ©r!   rx   )r—   Úvideor   r   r$   Ö  s    ÿz'SSNDataset.evaluate.<locals>.<listcomp>rœ   r›   gš™™™™™¹?ri   r   r‚   zEvaluation finishedzmAP@z.02f)ÚcopyÚdeepcopyÚwarningsÚwarnrv   Ú
isinstanceÚlistrX   rY   r:   ÚtupleÚKeyErrorr™   rE   ra   r?   rI   rH   ÚitemsÚget_all_gtsÚextendÚtolistr   Ú
setdefaultr%   Úaranger   ÚmeanÚzip)r   r�   ÚmetricsÚmetric_optionsr?   Údeprecated_kwargsÚallowed_metricsÚmetricr”   r|   Zall_gtsZplain_detectionsZdetection_listZdetsÚeval_resultsrœ   Z	iou_rangeZ	ap_valuesZmap_iousÚiouZmap_iour   )r—   r   r¢   r   ÚevaluateŠ  s‚   
ÿ
ÿ
ÿÿÿÿ
þ

þ
€
ÿ

ÿ
€zSSNDataset.evaluatec                    s¨   | j D ]N‰ |  ˆ d ˆ d | jj| jj¡}| j ‡ fdd„|D ƒ¡ |  ˆ d | jj	| jj
| jj| jj¡\}}| j ‡ fdd„|D ƒ¡ | j ‡ fdd„|D ƒ¡ qdS )	zuConstruct positve proposal pool, incomplete proposal pool and
        background proposal pool of the entire dataset.r9   ro   c                    ó   g | ]}ˆ d  |f‘qS ©rm   r   ©r!   Úproposal©r<   r   r   r$   ò  ó    ÿz7SSNDataset.construct_proposal_pools.<locals>.<listcomp>c                    r»   r¼   r   r½   r¿   r   r   r$   û  rÀ   c                 3   s   � | ]	}ˆ d  |gV  qdS )rm   Nr   r½   r¿   r   r   Ú	<genexpr>ý  s   € ÿz6SSNDataset.construct_proposal_pools.<locals>.<genexpr>N)r=   Úget_positivesrC   Úpositive_iou_thresholdrD   Úadd_gt_as_proposalsrJ   r­   Úget_negativesÚincomplete_iou_thresholdÚbackground_iou_thresholdÚbackground_coverage_thresholdÚincomplete_overlap_thresholdrL   rK   )r   Ú	positivesÚincompletesÚbackgroundsr   r¿   r   rM   ê  s.   
ýÿûÿ
ÿðz#SSNDataset.construct_proposal_poolsc                 C   sj   i }| j D ]-}|d }|d D ]"}|jd }|j|d  |j|d  g}| |i ¡ |g ¡ |¡ qq|S )z2Fetch groundtruth instances of the entire dataset.rm   r9   r   rn   )r=   r   r   r   r¯   ru   )r   r9   r<   r¢   r"   r—   Zgt_infor   r   r   r¬      s   

þÿ
ø
zSSNDataset.get_all_gtsc                    s8   ‡ fdd„|D ƒ}|r|  | ¡ |D ]}| | ¡ q|S )ai  Get positive/foreground proposals.

        Args:
            gts (list): List of groundtruth instances(:obj:`SSNInstance`).
            proposals (list): List of proposal instances(:obj:`SSNInstance`).
            positive_threshold (float): Minimum threshold of overlap of
                positive/foreground proposals and groundtruths.
            with_gt (bool): Whether to include groundtruth instances in
                positive proposals. Default: True.

        Returns:
            list[:obj:`SSNInstance`]: (positives), positives is a list
                comprised of positive proposal instances.
        c                    s   g | ]	}|j ˆ kr|‘qS r   )r   r½   ©Úpositive_thresholdr   r   r$   !  s
    
ÿz,SSNDataset.get_positives.<locals>.<listcomp>)r­   r)   )r9   ro   rÎ   Zwith_gtrÊ   r¾   r   rÍ   r   rÂ     s   
ÿ
zSSNDataset.get_positivesç{®Gáz„?çffffffæ?c                 C   sX   g }g }| D ]!}|j |k r|j|kr| |¡ q|j |k r'|j|kr'| |¡ q||fS )aõ  Get negative proposals, including incomplete proposals and
        background proposals.

        Args:
            proposals (list): List of proposal instances(:obj:`SSNInstance`).
            incomplete_iou_threshold (float): Maximum threshold of overlap
                of incomplete proposals and groundtruths.
            background_iou_threshold (float): Maximum threshold of overlap
                of background proposals and groundtruths.
            background_coverage_threshold (float): Minimum coverage
                of background proposals in video duration. Default: 0.01.
            incomplete_overlap_threshold (float): Minimum percent of incomplete
                proposals' own span contained in a groundtruth instance.
                Default: 0.7.

        Returns:
            list[:obj:`SSNInstance`]: (incompletes, backgrounds), incompletes
                and backgrounds are lists comprised of incomplete
                proposal instances and background proposal instances.
        )r   r   ru   r   )ro   rÆ   rÇ   rÈ   rÉ   rË   rÌ   r¾   r   r   r   rÅ   .  s   




€zSSNDataset.get_negativesc                 C   s´   |   |d |d | jj| jj¡}|  |d | jj| jj| jj| jj	¡\}}dd„ }g }| 
|d|d || j| jƒ¡ | 
|d|d || j| jƒ¡ | 
|d|d || j| jƒ¡ |S )	a`  Sample proposals from the this video instance.

        Args:
            record (dict): Information of the video instance(video_info[idx]).
                key: frame_dir, video_id, total_frames,
                gts: List of groundtruth instances(:obj:`SSNInstance`).
                proposals: List of proposal instances(:obj:`SSNInstance`).
        r9   ro   c                    sn   t ˆƒdkrtjjt ˆ ƒ|dd�}‡ ‡fdd„|D ƒS t ˆƒ|k }tjjt ˆƒ||d�}‡‡‡fdd„|D ƒS )aÞ  This method will sample proposals from the this video pool. If
            the video pool is empty, it will fetch from the dataset pool
            (collect proposal of the entire dataset).

            Args:
                proposal_type (int): Type id of proposal.
                    Positive/Foreground: 0
                    Negative:
                        Incomplete: 1
                        Background: 2
                video_id (str): Name of the video.
                video_pool (list): Pool comprised of proposals in this video.
                num_requested_proposals (int): Number of proposals
                    to be sampled.
                dataset_pool (list): Proposals of the entire dataset.

            Returns:
                list[(str, :obj:`SSNInstance`), int]:
                    video_id (str): Name of the video.
                    :obj:`SSNInstance`: Instance of class SSNInstance.
                    proposal_type (int): Type of proposal.
            r   F©rp   c                    s   g | ]}ˆ | ˆf‘qS r   r   r¡   )Údataset_poolÚproposal_typer   r   r$   ƒ  s    zVSSNDataset._video_centric_sampling.<locals>.sample_video_proposals.<locals>.<listcomp>c                    s   g | ]
}ˆˆ| fˆ f‘qS r   r   r¡   )rÓ   rm   Ú
video_poolr   r   r$   ˆ  s    )r:   r%   ÚrandomÚchoice)rÓ   rm   rÔ   Znum_requested_proposalsrÒ   r•   Ú	replicater   )rÒ   rÓ   rm   rÔ   r   Úsample_video_proposalsg  s   
ÿ
ÿzBSSNDataset._video_centric_sampling.<locals>.sample_video_proposalsr   rm   r   r   )rÂ   rC   rÃ   rD   rÄ   rÅ   rÆ   rÇ   rÈ   rÉ   r­   rZ   rJ   r\   rL   r[   rK   )r   ÚrecordrÊ   rË   rÌ   rØ   Úout_proposalsr   r   r   Ú_video_centric_samplingU  s>   	þü#þÿþÿþÿz"SSNDataset._video_centric_samplingc                    sÂ   g }t jjtˆ jƒˆ jtˆ jƒˆ jk d�}| ‡ fdd„|D ƒ¡ t jjtˆ jƒˆ jtˆ jƒˆ jk d�}| ‡ fdd„|D ƒ¡ t jjtˆ j	ƒˆ j
tˆ j	ƒˆ j
k d�}| ‡ fdd„|D ƒ¡ |S )z2Randomly sample proposals from the entire dataset.rÑ   c                    ó   g | ]	}ˆ j | d f‘qS )r   )rJ   r¡   r#   r   r   r$   ¢  ó    ÿz/SSNDataset._random_sampling.<locals>.<listcomp>c                    rÜ   )r   )rL   r¡   r#   r   r   r$   ¨  rÝ   c                    rÜ   )r   )rK   r¡   r#   r   r   r$   ®  rÝ   )r%   rÕ   rÖ   r:   rJ   rZ   r­   rL   r\   rK   r[   )r   rÚ   Zpositive_idxZincomplete_idxZbackground_idxr   r#   r   Ú_random_samplingš  s4   ýÿýÿýÿzSSNDataset._random_samplingc                 C   sî   |j }|j}| j| j }|| }|dksJ ‚td|t|| jd  ƒ ƒ}t|| d |d t|| jd  ƒ ƒ}|| | }	|| d | }
|	| d || jd   }|
| d || jd   }| j\}}||| j	 || j	 | g}|||fS )aÍ  Fetch the scale factor of starting and ending stage and get the
        stage split.

        Args:
            proposal (:obj:`SSNInstance`): Proposal instance.
            num_frames (int): Total frames of the video.

        Returns:
            tuple[float, float, list]: (starting_scale_factor,
                ending_scale_factor, stage_split), starting_scale_factor is
                the ratio of the effective sampling length to augment length
                in starting stage, ending_scale_factor is the ratio of the
                effective sampling length to augment length in ending stage,
                stage_split is  ending segment id of starting, course and
                ending stage.
        r   r   )
r   r   r^   r_   ÚmaxrV   rS   r   rR   rQ   )r   r¾   rw   r   r   Úori_clip_lenÚdurationÚvalid_startingÚvalid_endingÚvalid_starting_lengthÚvalid_ending_lengthÚstarting_scale_factorÚending_scale_factorZ	aug_startZaug_endZstage_splitr   r   r   Ú
_get_stage³  s0   ÿÿ
ÿ
ÿ

þ
zSSNDataset._get_stagec                 C   sz   | j r	| j d¡ g }| jD ]}|  |d |d | jjd¡}|D ]
}| t|j	ƒ¡ q qt
 t
j|dd�t
j|dd�f¡S )z/Compute regression target normalized constants.z.Compute regression target normalized constantsr9   ro   Fr   r‚   )rF   r?   rI   r=   rÂ   rC   rÃ   ru   r¨   r   r%   rŽ   r±   Ústd)r   Útargetsr<   rÊ   Úpositiver   r   r   rN   á  s   
þÿ"z+SSNDataset._compute_reg_normalize_constantsc                 C   sü  t  | j| ¡}| j|d< | j|d< | j|d< | jr%|  | j| ¡|d< n|  ¡ |d< g }g }g }g }t	|d ƒD ]�\}}|d d j
}|  |d d |¡\}	}
}|d dkr_|d d j}n |d dkrm|d d j}n|d dkrvd}n	td|d › �ƒ‚| |	|
g¡ t|tƒs”td	t|ƒ› �ƒ‚| |¡ | |d ¡ |d d j}|d dkrÑ|d | jd d  | jd d  |d | jd d  | jd d  f}| |¡ q9tj|tjd
�|d< tj|tjd
�|d< t |¡|d< t |¡|d< |  |¡S )z0Prepare the frames for training given the index.rG   r8   r7   rÚ   r   r   r   z+Proposal type should be 0, 1, or 2,but got z&proposal_label must be an int,but got r   Úreg_targetsZproposal_scale_factorZproposal_labelsrÓ   )r£   r¤   r=   rG   r8   r7   rP   rÛ   rÞ   rH   r   rè   r   Ú
ValueErrorru   r§   rV   rX   rY   r   rO   r%   rŽ   rˆ   rd   )r   r•   r�   Zout_proposal_scale_factorZout_proposal_typeZout_proposal_labelsZout_proposal_reg_targetsr¾   rw   ræ   rç   r|   r   rì   r   r   r   Úprepare_train_framesï  sj   


ÿ	ÿÿ
ÿ
ÿÿþ
ÿ
ÿ
zSSNDataset.prepare_train_framesc                 C   sÊ  t  | j| ¡}| j|d< | j|d< | j|d< |d }|d }| j| j }tj	d|| | j
td�d }t|ƒ}t|ƒdkrH| td|d |ƒ¡ g }g }	g }
|D ]l}|j| |j| f}|d |d  }|| jd  }|| jd  }|d | }|d | }td	|ƒ}td
|ƒ}|d | | }||d  | }|g|¢|‘R }t |¡|  tj¡}| |¡ |	 |¡ |
 ||f¡ qPtj|tjd�|d< tj|
tjd�|d< tj|	tjd�|d< | j|d< |  |¡S )z/Prepare the frames for testing given the index.rG   r8   r7   ro   rn   r   r   r   r   ri   r}   Úscale_factor_listÚproposal_tick_listrO   )r£   r¤   r=   rG   r8   r7   r^   r_   r%   r°   r]   rV   r:   ru   r   r   r   rS   rß   r   rŽ   ÚastypeÚint32rˆ   rO   rd   )r   r•   r�   ro   rw   rà   Zframe_ticksZnum_sampled_framesr}   rð   rï   r¾   Zrelative_proposalZrelative_durationZrelative_starting_durationZrelative_ending_durationZrelative_startingZrelative_endingZreal_relative_startingZreal_relative_endingræ   rç   Zproposal_rangesZproposal_ticksr   r   r   Úprepare_test_frames5  s|   


ÿÿÿ
ÿ


ÿÿ
ÿÿÿÿÿ

ÿ
ÿ
ÿ
ÿ

zSSNDataset.prepare_test_frames)Fr/   r   r0   TNr1   r2   r3   r   r   TTF)rz   )T)rÏ   rÐ   )r*   r+   r,   r-   r    ry   r™   rv   rº   rM   r¬   ÚstaticmethodrÂ   rÅ   rÛ   rÞ   rè   rN   rî   ró   Ú__classcell__r   r   re   r   r.   Q   sJ    Uít
4B
ü`ü&E.Fr.   )r£   Úos.pathÚpathrq   r¥   Úcollectionsr   rT   Únumpyr%   Ztorch.nn.modules.utilsr   Úcorer   Zlocalizationr   r   r   r	   r
   Úutilsr   Úbaser   Úregistryr   r   Úregister_moduler.   r   r   r   r   Ú<module>   s    ?