o
    _{j‹A  ã                   @   sv   d dl Z d dlmZ d dlmZ ddlmZ dd„ ZG dd„ dejƒZ	G d	d
„ d
ejƒZ
e ¡ G dd„ dejƒƒZdS )é    N)Únormal_inité   )ÚHEADSc                 C   s8   t | tƒr
| f| fS t | tƒr| t| ƒfS td| › �ƒ‚)a=  Parse config of STPP for three stages.

    Args:
        stage_cfg (int | tuple[int]):
            Config of structured temporal pyramid pooling.

    Returns:
        tuple[tuple[int], int]:
            Config of structured temporal pyramid pooling and
            total number of parts(number of multipliers).
    zIncorrect STPP config )Ú
isinstanceÚintÚtupleÚsumÚ
ValueError)Ú	stage_cfg© r   ú4/root/disk_3tb/HAC/mmaction/models/heads/ssn_head.pyÚparse_stage_config   s
   


r   c                       s6   e Zd ZdZd
‡ fdd„	Zedd„ ƒZdd	„ Z‡  ZS )Ú	STPPTraina-  Structured temporal pyramid pooling for SSN at training.

    Args:
        stpp_stage (tuple): Config of structured temporal pyramid pooling.
            Default: (1, (1, 2), 1).
        num_segments_list (tuple): Number of segments to be sampled
            in three stages. Default: (2, 5, 2).
    ©é   )r   r   r   ©r   é   r   c           	         sj   t ƒ  ¡  t|d ƒ\}}t|d ƒ\}}t|d ƒ\}}|| | | _|||f| _|||f| _|| _d S )Nr   r   r   )ÚsuperÚ__init__r   Únum_multipliersÚstpp_stagesÚmultiplier_listÚnum_segments_list)	ÚselfÚ
stpp_stager   Zstarting_partÚstarting_multiplierZcourse_partÚcourse_multiplierZending_partÚending_multiplier©Ú	__class__r   r   r   %   s   

ÿÿ
zSTPPTrain.__init__c                 C   s˜   g }|   d¡}|D ]@}t d|d || ¡ ¡ }t|ƒD ],}	| dd…||	 ||	d  …dd…f jdd�| }
|durC|
| |d¡ }
| |
¡ qq	|S )aû  Extract stage feature based on structured temporal pyramid pooling.

        Args:
            stage_feat (torch.Tensor): Stage features to be STPP.
            stage_parts (tuple): Config of STPP.
            num_multipliers (int): Total number of parts in the stage.
            scale_factors (list): Ratios of the effective sampling lengths
                to augmented lengths.
            num_samples (int): Number of samples.

        Returns:
            torch.Tensor: Features of the stage.
        r   r   çñhãˆµøä>N©Údim)ÚsizeÚtorchÚaranger   ÚrangeÚmeanÚviewÚappend)Z
stage_featZstage_partsr   Úscale_factorsÚnum_samplesZstage_stpp_featZ	stage_lenZ
stage_partÚticksÚiZ	part_featr   r   r   Ú_extract_stage_feature4   s(   
ÿÿ&ÿÿÿúz STPPTrain._extract_stage_featurec              
   C   sV  | j d }|| j d  }|| j d  }| d¡}| d||¡}| d¡}| dd¡}g }| |  |dd…d|…dd…f | jd | jd |dd…df |¡¡ | |  |dd…||…dd…f | jd | jd d|¡¡ | |  |dd…|d…dd…f | jd | jd |dd…df |¡¡ tj|dd�}	|dd…||…dd…f j	dd�}
|
|	fS )a‰  Defines the computation performed at every call.

        Args:
            x (torch.Tensor): The input data.
            scale_factors (list): Ratios of the effective sampling lengths
                to augmented lengths.

        Returns:
            tuple[torch.Tensor, torch.Tensor]:
                Features for predicting activity scores and
                completeness scores.
        r   r   r   éÿÿÿÿNr!   )
r   r#   r(   Úextendr.   r   r   r$   Úcatr'   )r   Úxr*   Úx0Úx1Únum_segmentsÚfeat_dimr+   Zstage_stpp_featsZ	stpp_featZcourse_featr   r   r   ÚforwardR   s:   


$þÿ$
þÿ$þÿ"zSTPPTrain.forward)r   r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústaticmethodr.   r7   Ú__classcell__r   r   r   r   r      s    	
r   c                       s:   e Zd ZdZ		d
‡ fdd„	Zedd„ ƒZdd	„ Z‡  ZS )ÚSTPPTestaV  Structured temporal pyramid pooling for SSN at testing.

    Args:
        num_classes (int): Number of classes to be classified.
        use_regression (bool): Whether to perform regression or not.
            Default: True.
        stpp_stage (tuple): Config of structured temporal pyramid pooling.
            Default: (1, (1, 2), 1).
    Tr   c           
         sü   t ƒ  ¡  |d | _|| _|d | _|| _t|d ƒ\}}t|d ƒ\}}t|d ƒ\}}	|| |	 | _| jrE| j| j| j| j   | _n
| j| j| j  | _|||f| _	t
d| jƒ| _t
| jj| jj| j| j  ƒ| _t
| jj| jj| j| j  ƒ| _d S )Nr   r   r   )r   r   Úactivity_score_lenÚcomplete_score_lenÚreg_score_lenÚuse_regressionr   r   r6   r   ÚsliceÚactivity_sliceÚstopÚcomplete_sliceÚ	reg_slice)
r   Únum_classesrB   r   Zstarting_partsr   Zcourse_partsr   Zending_partsr   r   r   r   r   ‡   s>   



ÿ
ÿÿ
ÿÿ
ÿÿ
ÿ
ÿzSTPPTest.__init__c                 C   sF  d}t |ƒD ]š\}}	|dkr|d }
n|t|ƒd kr |d }
nd}
t|	ƒ}|| }tt|| d ||d  ƒƒ}|dksD|| d¡krI||7 }q|	D ]T}t ||d || | ¡ ¡ }t	|ƒD ]>}|| }||d  }|| dkrš|||…|| |d | …f }|j
dd�|
 }| |dd…f  | ¡  ¡ 7  < |d7 }q`qKq| S )aá  Perform pyramids pooling.

        Args:
            out_scores (torch.Tensor): Scores to be returned.
            index (int): Index of output scores.
            raw_scores (torch.Tensor): Raw scores before STPP.
            ticks (list): Ticks of raw scores.
            scale_factors (list): Ratios of the effective sampling lengths
                to augmented lengths.
            score_len (int): Length of the score.
            stpp_stage (tuple): Config of STPP.
        r   r   g      ð?r    r!   N)Ú	enumerateÚlenr   ÚfloatÚmaxr#   r$   r%   r   r&   r'   ÚdetachÚcpu)Z
out_scoresÚindexZ
raw_scoresr,   r*   Z	score_lenr   ÚoffsetZ	stage_idxr
   Úscale_factorZ	sum_partsZ	tick_leftZ
tick_rightZ	num_partsZ
part_ticksr-   Zpart_tick_leftZpart_tick_rightZ	raw_scoreZraw_scale_scorer   r   r   Ú_pyramids_poolingª   sP   

ÿÿþÿÿÿÿ 
öûzSTPPTest._pyramids_poolingc              
   C   sF  |  d¡| jks
J ‚|  d¡}tj|| jf|jd�}|dd…| jf }tj|| jf|jd�}|dd…| jf }| j	rOtj|| j
f|jd�}	|dd…| jf }
nd}	d}
t|ƒD ]F}|| }||d t|d d |d ƒ…dd…f jdd�||dd…f< |  |||||| | j| j¡}| j	r�|  |	||
||| | j
| j¡}	qW|||	fS )aG  Defines the computation performed at every call.

        Args:
            x (torch.Tensor): The input data.
            proposal_ticks (list): Ticks of proposals to be STPP.
            scale_factors (list): Ratios of the effective sampling lengths
                to augmented lengths.

        Returns:
            tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
                out_activity_scores (torch.Tensor): Activity scores
                out_complete_scores (torch.Tensor): Completeness scores.
                out_reg_scores (torch.Tensor): Regression scores.
        r   r   )ÚdtypeNr   r!   )r#   r6   r$   Úzerosr?   rS   rD   r@   rF   rB   rA   rG   r&   rL   r'   rR   r   )r   r2   Úproposal_ticksr*   Z	num_ticksZout_activity_scoresZraw_activity_scoresZout_complete_scoresZraw_complete_scoresZout_reg_scoresZraw_reg_scoresr-   r,   r   r   r   r7   Ü   sF   
ÿÿÿ$ÿÿþþ€
zSTPPTest.forward)Tr   )	r8   r9   r:   r;   r   r<   rR   r7   r=   r   r   r   r   r>   |   s    ý#
1r>   c                	       sT   e Zd ZdZdddeddddd	�dd
f‡ fdd„	Zdd„ Zdd„ Zddd„Z‡  Z	S )ÚSSNHeadañ  The classification head for SSN.

    Args:
        dropout_ratio (float): Probability of dropout layer. Default: 0.8.
        in_channels (int): Number of channels for input data. Default: 1024.
        num_classes (int): Number of classes to be classified. Default: 20.
        consensus (dict): Config of segmental consensus.
        use_regression (bool): Whether to perform regression or not.
            Default: True.
        init_std (float): Std value for Initiation. Default: 0.001.
    gš™™™™™é?i   é   r   T)r   r   r   r   )ÚtypeZstandalong_classifierZstpp_cfgZnum_seggü©ñÒMbP?c           	         sê   t ƒ  ¡  || _|| _|| _|| _| jdkrtj| jd�| _nd | _| 	¡ }| 
d¡}|dkr8td	i |¤Ž| _n|dkrI| j|d< td	i |¤Ž| _|| _| jj| | _t ||d ¡| _t | j|¡| _| jrst | j|d ¡| _d S d S )
Nr   )ÚprX   r   r>   rH   r   r   r   )r   r   Údropout_ratiorH   rB   Úinit_stdÚnnÚDropoutÚdropoutÚcopyÚpopr   Ú	consensusr>   Zin_channels_activityr   Zin_channels_completeÚLinearÚactivity_fcÚcompleteness_fcÚregressor_fc)	r   rZ   Úin_channelsrH   ra   rB   r[   Z
consensus_Zconsensus_typer   r   r   r     s6   




ÿÿ
ÿÿzSSNHead.__init__c                 C   s>   t | j| jd� t | j| jd� | jrt | j| jd� dS dS )z%Initiate the parameters from scratch.)ÚstdN)r   rc   r[   rd   rB   re   )r   r   r   r   Úinit_weightsK  s
   ÿzSSNHead.init_weightsc           
      C   sR  | j j}| j j| jj|  }| jr|| jj| 7 }t ||¡| _| jj	j
 | jj||¡ dd¡ ¡  d|¡}| jjj
 dd¡ || jj¡ ¡  d¡| }t | j j	j
|f¡}t | j jj
|f¡}| jr�| jj	j
 | jj||¡ dd¡ ¡  d|¡}| jjj
 dd¡ || jj¡ ¡  d¡| }	t ||f¡}t ||	f¡}|| jj	_
|| jj_
dS )a  Reorganize the shape of fully connected layer at testing, in order
        to improve testing efficiency.

        Args:
            stpp_feat_multiplier (int): Total number of parts.

        Returns:
            bool: Whether the shape transformation is ready for testing.
        r   r   r/   T)rc   Úin_featuresÚout_featuresrd   rB   re   r\   rb   Útest_fcÚweightÚdatar(   Ú	transposeÚ
contiguousÚbiasÚexpandr$   r1   )
r   Zstpp_feat_multiplierri   rj   Zcomplete_weightZcomplete_biasrl   rp   Z
reg_weightZreg_biasr   r   r   Úprepare_test_fcR  sV   
ÿÿ
ÿ
þþÿþ
þÿýÿþ

zSSNHead.prepare_test_fcFc                 C   s´   |s@|\}}|   ||¡\}}| jdur|  |¡}|  |¡}|  |¡}|  |¡}| jr9|  |¡}| d| jjd¡}nd}|||fS |\}}	}
|  |¡}|   ||	|
¡\}}}||||fS )z0Defines the computation performed at every call.Nr/   r   )	ra   r^   rc   rd   rB   re   r(   rj   rk   )r   r2   Ú	test_modeÚproposal_scale_factorZactivity_featZcompleteness_featÚactivity_scoresZcomplete_scoresÚ
bbox_predsÚproposal_tick_listÚscale_factor_listZtest_scoresÚcompleteness_scoresr   r   r   r7   €  s4   ÿ





þ


ÿÿzSSNHead.forward)F)
r8   r9   r:   r;   Údictr   rh   rr   r7   r=   r   r   r   r   rV     s"    üö-.rV   )r$   Útorch.nnr\   Úmmcv.cnnr   Úregistryr   r   ÚModuler   r>   Úregister_modulerV   r   r   r   r   Ú<module>   s    a 