o
    _{jM  ã                	   @   s„   d dl Z d dlmZ d dlZd dlZd dlZd dlm	Z
 d dlmZ zd dlZdZW n eefy7   dZY nw G dd„ deƒZdS )é    N)Úget_dist_infoTFc                   @   sP   e Zd ZdZ							dd	d
„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z	dS )ÚLFBaä  Long-Term Feature Bank (LFB).

    LFB is proposed in `Long-Term Feature Banks for Detailed Video
    Understanding <https://arxiv.org/abs/1812.05038>`_

    The ROI features of videos are stored in the feature bank. The feature bank
    was generated by inferring with a lfb infer config.

    Formally, LFB is a Dict whose keys are video IDs and its values are also
    Dicts whose keys are timestamps in seconds. Example of LFB:

    .. code-block:: Python
        {
            '0f39OWEqJ24': {
                901: tensor([[ 1.2760,  1.1965,  ...,  0.0061, -0.0639],
                    [-0.6320,  0.3794,  ..., -1.2768,  0.5684],
                    [ 0.2535,  1.0049,  ...,  0.4906,  1.2555],
                    [-0.5838,  0.8549,  ..., -2.1736,  0.4162]]),
                ...
                1705: tensor([[-1.0169, -1.1293,  ...,  0.6793, -2.0540],
                    [ 1.2436, -0.4555,  ...,  0.2281, -0.8219],
                    [ 0.2815, -0.0547,  ..., -0.4199,  0.5157]]),
                ...
            },
            'xmqSaQPzL1E': {
                ...
            },
            ...
        }

    Args:
        lfb_prefix_path (str): The storage path of lfb.
        max_num_sampled_feat (int): The max number of sampled features.
            Default: 5.
        window_size (int): Window size of sampling long term feature.
            Default: 60.
        lfb_channels (int): Number of the channels of the features stored
            in LFB. Default: 2048.
        dataset_modes (tuple[str] | str): Load LFB of datasets with different
            modes, such as training, validation, testing datasets. If you don't
            do cross validation during training, just load the training dataset
            i.e. setting `dataset_modes = ('train')`.
            Default: ('train', 'val').
        device (str): Where to load lfb. Choices are 'gpu', 'cpu' and 'lmdb'.
            A 1.65GB half-precision ava lfb (including training and validation)
            occupies about 2GB GPU memory. Default: 'gpu'.
        lmdb_map_size (int): Map size of lmdb. Default: 4e9.
        construct_lmdb (bool): Whether to construct lmdb. If you have
            constructed lmdb of lfb, you can set to False to skip the
            construction. Default: True.
    é   é<   é   ©ÚtrainÚvalÚgpuç    eÍíATc	                 C   s@  t  |¡std|› d�ƒ‚|| _|| _|| _|| _t|tƒs(t|t	ƒs%J ‚|f}|| _
|| _tƒ \}	}
| jdkrB|  d|	› �¡ d S | jdkrX|
dkrQt dt¡ |  d¡ d S | jdkr–tscJ d	ƒ‚|| _|| _t  t  | jd¡¡| _|	d
krƒ| jrƒtdƒ |  ¡  |
dkr‹t ¡  tj| jdd�| _d S tdd| j› d�ƒ‚)Nzlfb prefix path z does not exist!r
   zcuda:Úcpué   z~If distributed training is used with multi-GPUs, lfb will be loaded multiple times on RAM. In this case, 'lmdb' is recomended.Úlmdbz*Please install `lmdb` to load lfb on lmdb!r   zConstructing LFB lmdb...T)Úreadonlyz'Device must be 'gpu', 'cpu' or 'lmdb', zbut get Ú.)ÚospÚexistsÚ
ValueErrorÚlfb_prefix_pathÚmax_num_sampled_featÚwindow_sizeÚlfb_channelsÚ
isinstanceÚtupleÚstrÚdataset_modesÚdevicer   Úload_lfbÚwarningsÚwarnÚUserWarningÚlmdb_importedÚlmdb_map_sizeÚconstruct_lmdbÚnormpathÚjoinÚlfb_lmdb_pathÚprintÚload_lfb_on_lmdbÚdistÚbarrierr   ÚopenÚlmdb_env)Úselfr   r   r   r   r   r   r"   r#   ÚrankÚ
world_size© r0   ú0/root/disk_3tb/HAC/mmaction/models/common/lfb.pyÚ__init__F   sR   
	
ÿ



ý
ÿÿÿzLFB.__init__c              	   C   sh   i | _ | jD ]#}t t | jd|› d�¡¡}td|› d�ƒ | j  tj	||d�¡ qtd|› d�ƒ d S )NÚlfb_ú.pklzLoading LFB from z...©Úmap_locationzLFB has been loaded on r   )
Úlfbr   r   r$   r%   r   r'   ÚupdateÚtorchÚload)r-   r6   Údataset_modeÚlfb_pathr0   r0   r1   r   |   s   
ÿzLFB.load_lfbc           	   	   C   sÈ   i }| j D ]}t t | jd|› d�¡¡}| tj|dd�¡ qtj	| j
| jd�}| ¡ D ]+\}}|jdd�}t ¡ }t ||¡ | d¡ | | ¡ | ¡ ¡ | ¡  | ¡  q-td	| j
› d
�ƒ d S )Nr3   r4   r   r5   )Zmap_sizeT©Úwriter   z!LFB lmdb has been constructed on ú!)r   r   r$   r%   r   r8   r9   r:   r   r+   r&   r"   ÚitemsÚbeginÚioÚBytesIOÚsaveÚseekÚputÚencodeÚreadÚcommitÚcloser'   )	r-   r7   r;   r<   r,   ÚkeyÚvalueÚtxnÚbuffr0   r0   r1   r(   …   s    
ÿ

zLFB.load_lfb_on_lmdbc                 C   s  | j dkr,| jjdd��}| | ¡ ¡}t t |¡¡}W d   ƒ n1 s&w   Y  n| j	| }| j
| j}}||d  }t || | j¡}	tt||| ƒƒD ]3\}
}||v rƒt|| ƒ}t||ƒ}tjjt|ƒ|dd�}t|ƒD ]\}}|| | |	|
| | < qrqP|	S )Nr   Fr=   é   )Úreplace)r   r,   rA   ÚgetrG   r9   r:   rB   rC   r7   r   r   Úzerosr   Ú	enumerateÚrangeÚlenÚminÚnpÚrandomÚchoice)r-   Úvideo_idÚ	timestamprM   ÚbufZvideo_featuresr   ÚKÚstartZlt_featsÚidxÚsecZnum_featZnum_feat_sampledZrandom_lfb_indicesÚkZrand_idxr0   r0   r1   Úsample_long_term_features˜   s*   
þ€


ÿ€zLFB.sample_long_term_featuresc                 C   s   |  d¡\}}|  |t|ƒ¡S )zhSample long term features like `lfb['0f39OWEqJ24,0902']` where `lfb`
        is a instance of class LFB.Ú,)Úsplitrb   Úint)r-   Úimg_keyrZ   r[   r0   r0   r1   Ú__getitem__´   s   zLFB.__getitem__c                 C   s
   t | jƒS )z:The number of videos whose ROI features are stored in LFB.)rU   r7   )r-   r0   r0   r1   Ú__len__º   s   
zLFB.__len__N)r   r   r   r   r
   r   T)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r   r(   rb   rg   rh   r0   r0   r0   r1   r      s    6
ø6	r   )rB   Úos.pathÚpathr   r   ÚnumpyrW   r9   Útorch.distributedÚdistributedr)   Úmmcv.runnerr   r   r!   ÚImportErrorÚModuleNotFoundErrorÚobjectr   r0   r0   r0   r1   Ú<module>   s    ÿ