o
    _{j+  ã                   @   sB   d dl Z d dlmZmZ edƒZdd„ Ze ¡ G dd„ dƒƒZdS )é    N)ÚRegistryÚbuild_from_cfgÚmodule_hooksc                 C   sÆ   g }|D ]\}|  dd¡}t| |ƒst| j› d|› d�ƒ‚t| |ƒ}|  dd¡}|dkr6| t|tƒ ¡ ¡}n%|dkrE| 	t|tƒ ¡ ¡}n|dkrT| 
t|tƒ ¡ ¡}ntd	|› �ƒ‚| |¡ q|S )
NÚhooked_moduleÚbackbonez has no ú!Úhook_posZforward_preÚforwardÚbackwardzAhook_pos must be `forward_pre`, `forward` or `backward`, but get )ÚpopÚhasattrÚ
ValueErrorÚ	__class__ÚgetattrÚregister_forward_pre_hookr   ÚMODULE_HOOKSÚ	hook_funcÚregister_forward_hookÚregister_backward_hookÚappend)ÚModuleZmodule_hooks_listÚhandlesZmodule_hook_cfgZhooked_module_namer   r   Úhandle© r   ú1/root/disk_3tb/HAC/mmaction/utils/module_hooks.pyÚregister_module_hooks   s8   
ÿ
ÿÿÿÿÿr   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚGPUNormalizeaí  Normalize images with the given mean and std value on GPUs.

    Call the member function ``hook_func`` will return the forward pre-hook
    function for module registration.

    GPU normalization, rather than CPU normalization, is more recommended in
    the case of a model running on GPUs with strong compute capacity such as
    Tesla V100.

    Args:
        mean (Sequence[float]): Mean values of different channels.
        std (Sequence[float]): Std values of different channels.
    c                 C   s.  |dvrt d|› d�ƒ‚|| _t |¡}t |¡}|dkr7|d d d …d d d f | _|d d d …d d d f | _d S |dkrS|d d d …d d f | _|d d d …d d f | _d S |dkro|d d d …d d f | _|d d d …d d f | _d S |dkr�|d d d d d …d d f | _|d d d d d …d d f | _d S t d|› d�ƒ‚)N)ÚNCTHWÚNCHWÚ	NCHW_FlowÚNPTCHWzThe input format z is invalid.r   r   r   r    )r   Úinput_formatÚtorchÚtensorÚ_meanÚ_std)Úselfr!   ÚmeanÚstdr$   r%   r   r   r   Ú__init__2   s$   

zGPUNormalize.__init__c                    s   ‡ fdd„}|S )Nc                    s�   |d }|j tjksJ d|j › �ƒ‚ˆ j |j¡}ˆ j |j¡}t ¡ � | ¡  	|¡ 
|¡}W d   ƒ n1 s9w   Y  |g|dd … ¢R S )Nr   zVThe previous augmentation should use uint8 data type to speed up computation, but get é   )Údtyper"   Úuint8r$   ÚtoÚdevicer%   Úno_gradÚfloatÚsub_Údiv_)r   ÚinputÚxr'   r(   ©r&   r   r   Únormalize_hookI   s   ÿÿ
ÿz.GPUNormalize.hook_func.<locals>.normalize_hookr   )r&   r6   r   r5   r   r   G   s   zGPUNormalize.hook_funcN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r)   r   r   r   r   r   r   "   s    r   )r"   Z
mmcv.utilsr   r   r   r   Úregister_moduler   r   r   r   r   Ú<module>   s    