o
    _{j  ã                	   @   sš   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 z
d dlmZ dZW n eefy7   dZY nw G dd„ de	jƒZerKe ¡ eƒ dS dS )é    N)Úget_dist_info)ÚSHARED_HEADSTFc                       sD   e Zd ZdZ				d‡ fdd„	Zdd	d
„Zdd„ Zdd„ Z‡  ZS )ÚLFBInferHeada¦  Long-Term Feature Bank Infer Head.

    This head is used to derive and save the LFB without affecting the input.

    Args:
        lfb_prefix_path (str): The prefix path to store the lfb.
        dataset_mode (str, optional): Which dataset to be inferred. Choices are
            'train', 'val' or 'test'. Default: 'train'.
        use_half_precision (bool, optional): Whether to store the
            half-precision roi features. Default: True.
        temporal_pool_type (str): The temporal pool type. Choices are 'avg' or
            'max'. Default: 'avg'.
        spatial_pool_type (str): The spatial pool type. Choices are 'avg' or
            'max'. Default: 'max'.
    ÚtrainTÚavgÚmaxc                    sÆ   t ƒ  ¡  tƒ \}}|dkr$t |¡s td|› d�ƒ t |¡ tdƒ |dv s*J ‚|dv s0J ‚|| _|| _	|| _
|dkrDt d¡| _nt d¡| _|dkrUt d¡| _nt d¡| _g | _g | _d S )	Nr   zlfb prefix path z' does not exist. Creating the folder...z
Inferring LFB...)r   r   r   )é   NN)Nr   r   )ÚsuperÚ__init__r   ÚospÚexistsÚprintÚmmcvÚmkdir_or_existÚlfb_prefix_pathÚdataset_modeÚuse_half_precisionÚnnÚAdaptiveAvgPool3dÚtemporal_poolÚAdaptiveMaxPool3dÚspatial_poolÚall_featuresÚall_metadata)Úselfr   r   r   Útemporal_pool_typeÚspatial_pool_typeÚrankÚ
world_size©Ú	__class__© ú:/root/disk_3tb/HAC/mmaction/models/heads/lfb_infer_head.pyr
   !   s(   




zLFBInferHead.__init__Nc                 C   s   d S ©Nr!   )r   Ú
pretrainedr!   r!   r"   Úinit_weightsC   s   zLFBInferHead.init_weightsc                 C   sn   |   |¡}|  |¡}| jr| ¡ }|d d …df  tj¡}|D ]}| j || d ¡ q|  j	t
|ƒ7  _	|S )Nr   Úimg_key)r   r   r   ÚhalfÚtypeÚtorchÚint64r   Úappendr   Úlist)r   ÚxÚroisÚ	img_metasÚfeaturesÚindsÚindr!   r!   r"   ÚforwardG   s   

zLFBInferHead.forwardc                 C   sÖ  t | jƒt | jƒksJ dƒ‚tƒ \}}|dkrt ¡  i }t| j| jƒD ]/\}}| d¡\}}t|ƒ}||vr;i ||< ||| vrGg || |< || |  	t
 |¡¡ q$t t | jd| j› d|› d�¡¡}t
 ||¡ tt | jƒ› dt |ƒ› d|› d	|› d
�ƒ |dkr‰t ¡  |dkr�d S tdƒ i }	t|ƒD ]2}
t t | jd| j› d|
› d�¡¡}t
 |¡}|D ]}||	vrÁ|| |	|< q´|	|  || ¡ q´q™t t | jd| j› d�¡¡}t
 |	|¡ td|› d�ƒ d S )Nz.features and metadata are not equal in length!r   Ú,Z_lfb_Ú_z.pklz features from z videos on GPU z have been stored in Ú.r   z!Gathering all the roi features...Úlfb_zLFB has been constructed in ú!)Úlenr   r   r   ÚdistÚbarrierÚzipÚsplitÚintr+   r)   Úsqueezer   ÚnormpathÚjoinr   r   Úsaver   ÚrangeÚloadÚupdate)r   r   r   Z_lfbÚfeatureÚmetadataÚvideo_idÚ	timestampZ_lfb_file_pathÚlfbZrank_idZlfb_file_pathr!   r!   r"   Ú__del__V   sb   ÿ
ÿÿÿ
ÿÿÿ
üÿzLFBInferHead.__del__)r   Tr   r   r#   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r%   r3   rK   Ú__classcell__r!   r!   r   r"   r      s    û
"r   )Úos.pathÚpathr   r   r)   Útorch.distributedÚdistributedr:   Útorch.nnr   Úmmcv.runnerr   Zmmdet.models.builderr   ZMMDET_SHARED_HEADSÚmmdet_importedÚImportErrorÚModuleNotFoundErrorÚModuler   Úregister_moduler!   r!   r!   r"   Ú<module>   s"    ÿ ÿ