o
    _{j!;  ã                   @   sœ   d dl Z d dlmZ d dlZd dlmZ d dlmZ	 d dl
mZ d dlmZ d dlmZ G dd„ deƒZG dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZdS )é    N)Úinf)ÚHook)Ú
_BatchNorm)Ú
DataLoaderc                   @   s    e Zd ZdZdd„ dd„ dœZe edœZg d¢ZdgZ				
		d"dd„Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ ZdS )#ÚEvalHookaJ  Non-Distributed evaluation hook.

    Notes:
        If new arguments are added for EvalHook, tools/test.py,
        tools/eval_metric.py may be effected.

    This hook will regularly perform evaluation in a given interval when
    performing in non-distributed environment.

    Args:
        dataloader (DataLoader): A PyTorch dataloader.
        start (int | None, optional): Evaluation starting epoch. It enables
            evaluation before the training starts if ``start`` <= the resuming
            epoch. If None, whether to evaluate is merely decided by
            ``interval``. Default: None.
        interval (int): Evaluation interval. Default: 1.
        by_epoch (bool): Determine perform evaluation by epoch or by iteration.
            If set to True, it will perform by epoch. Otherwise, by iteration.
            default: True.
        save_best (str | None, optional): If a metric is specified, it would
            measure the best checkpoint during evaluation. The information
            about best checkpoint would be save in best.json.
            Options are the evaluation metrics to the test dataset. e.g.,
             ``top1_acc``, ``top5_acc``, ``mean_class_accuracy``,
            ``mean_average_precision``, ``mmit_mean_average_precision``
            for action recognition dataset (RawframeDataset and VideoDataset).
            ``AR@AN``, ``auc`` for action localization dataset.
            (ActivityNetDataset). ``mAP@0.5IOU`` for spatio-temporal action
            detection dataset (AVADataset). If ``save_best`` is ``auto``, the
            first key of the returned ``OrderedDict`` result will be used.
            Default: 'auto'.
        rule (str | None, optional): Comparison rule for best score. If set to
            None, it will infer a reasonable rule. Keys such as 'acc', 'top'
            .etc will be inferred by 'greater' rule. Keys contain 'loss' will
            be inferred by 'less' rule. Options are 'greater', 'less', None.
            Default: None.
        **eval_kwargs: Evaluation arguments fed into the evaluate function of
            the dataset.
    c                 C   s   | |kS ©N© ©ÚxÚyr   r   ú9/root/disk_3tb/HAC/mmaction/core/evaluation/eval_hooks.pyÚ<lambda>5   ó    zEvalHook.<lambda>c                 C   s   | |k S r   r   r	   r   r   r   r   5   r   )ÚgreaterÚless)ÚaccÚtopzAR@ÚaucÚ	precisionzmAP@zRecall@ÚlossNé   TÚautoc                 K   sÞ   d|v rt dƒ‚t|tƒstdt|ƒ› �ƒ‚|dkr!td|› �ƒ‚t|tƒs(J ‚|d ur<|dk r<t d|› d�t	¡ d}|| _
|| _|| _|| _t|tƒsS|d u sSJ ‚|| _|| _d| _| jd urmd | _|  || j¡ d S d S )	NÚkey_indicatorz†"key_indicator" is deprecated, you need to use "save_best" instead. See https://github.com/open-mmlab/mmaction2/pull/395 for more infoz1dataloader must be a pytorch DataLoader, but got r   z#interval must be positive, but got zThe evaluation start epoch z! is smaller than 0, use 0 insteadT)ÚRuntimeErrorÚ
isinstancer   Ú	TypeErrorÚtypeÚ
ValueErrorÚboolÚwarningsÚwarnÚUserWarningÚ
dataloaderÚintervalÚstartÚby_epochÚstrÚ	save_bestÚeval_kwargsÚinitial_flagÚbest_ckpt_pathÚ
_init_rule)Úselfr"   r$   r#   r%   r'   Úruler(   r   r   r   Ú__init__:   s:   ÿ
ÿ
þ
þzEvalHook.__init__c                    sª   || j vr|durtd|› d�ƒ‚|du r?ˆ dkr?t‡ fdd„| jD ƒƒr(d}nt‡ fdd„| jD ƒƒr7d	}ntd
ˆ › d�ƒ‚|| _ˆ | _| jdurS| j | j | _dS dS )zÿInitialize rule, key_indicator, comparison_func, and best score.

        Args:
            rule (str | None): Comparison rule for best score.
            key_indicator (str | None): Key indicator to determine the
                comparison rule.
        Nz,rule must be greater, less or None, but got Ú.r   c                 3   ó   � | ]}|ˆ v V  qd S r   r   ©Ú.0Úkey©r   r   r   Ú	<genexpr>s   ó   € z&EvalHook._init_rule.<locals>.<genexpr>r   c                 3   r0   r   r   r1   r4   r   r   r5   u   r6   r   zCannot infer the rule for key z), thus a specific rule must be specified.)	Úrule_mapÚKeyErrorÚanyÚgreater_keysÚ	less_keysr   r-   r   Úcompare_func)r,   r-   r   r   r4   r   r+   e   s$   
ÿ
ÿ
ÿzEvalHook._init_rulec                 C   s>   | j d ur|jd u rt d¡ tƒ |_|j dtƒ ¡ d S d S )Nz*runner.meta is None. Creating a empty one.Ú	hook_msgs)r'   Úmetar   r    ÚdictÚ
setdefault©r,   Úrunnerr   r   r   Ú
before_run€   s   


üzEvalHook.before_runc                 C   s>   | j rdS | js
dS | jdur|j| jkr|  |¡ d| _dS )z>Evaluate the model only at the start of training by iteration.NF)r%   r)   r$   ÚiterÚafter_train_iterrA   r   r   r   Úbefore_train_iter‡   ó   

zEvalHook.before_train_iterc                 C   s>   | j sdS | js
dS | jdur|j| jkr|  |¡ d| _dS )z:Evaluate the model only at the start of training by epoch.NF)r%   r)   r$   ÚepochÚafter_train_epochrA   r   r   r   Úbefore_train_epoch‘   rG   zEvalHook.before_train_epochc                 C   s   | j s
|  |¡ dS dS )z9Called after every training iter to evaluate the results.N©r%   Ú_do_evaluaterA   r   r   r   rE   ›   ó   ÿzEvalHook.after_train_iterc                 C   s   | j r
|  |¡ dS dS )z:Called after every training epoch to evaluate the results.NrK   rA   r   r   r   rI       rM   zEvalHook.after_train_epochc                 C   sN   |   |¡sdS ddlm} ||j| jƒ}|  ||¡}| jr%|  ||¡ dS dS )ú!perform evaluation and save ckpt.Nr   )Úsingle_gpu_test)Úevaluation_flagÚmmaction.apisrO   Úmodelr"   Úevaluater'   Ú
_save_ckpt)r,   rB   rO   ÚresultsÚ	key_scorer   r   r   rL   ¥   s   
ÿzEvalHook._do_evaluatec                 C   sl   | j r
|j}| j}n|j}| j}| jdu r||| jƒsdS dS |d | jk r(dS |d | j | j r4dS dS )z„Judge whether to perform_evaluation.

        Returns:
            bool: The flag indicating whether to perform evaluation.
        NFr   T)r%   rH   Úevery_n_epochsrD   Úevery_n_itersr$   r#   )r,   rB   ÚcurrentÚ
check_timer   r   r   rP   °   s   
	øzEvalHook.evaluation_flagc                 C   s*  | j rd|jd › �}d|jd }}nd|jd › �}d|jd }}|jd  d| j| j ¡}|  ||¡r“|}||jd d< | jrOt	 
| j¡rOt | j¡ d| j› d	|› d
�}|j|j|dd� t	 |j|¡| _| j|jd d< |j d|› d�¡ |j d| j› d|d›d|› d|› d�	¡ d S d S )NÚepoch_r   rH   Úiter_rD   r=   Ú
best_scoreÚbest_Ú_z.pthF)Úcreate_symlinkÚ	best_ckptz Now best checkpoint is saved as r/   zBest z is z0.4fz at Ú )r%   rH   rD   r>   ÚgetÚinit_value_mapr-   r<   r*   ÚospÚisfileÚosÚremover   Úsave_checkpointÚwork_dirÚjoinÚloggerÚinfo)r,   rB   rV   rY   Úcur_typeÚcur_timer]   Úbest_ckpt_namer   r   r   rT   Ë   s<   
ÿÿ
ÿÿÿÿñzEvalHook._save_ckptc                 C   s€   | j jj|fd|ji| j¤Ž}| ¡ D ]
\}}||jj|< qd|j_| j	dur>| j
dkr9|  | jt| ¡ ƒd ¡ || j
 S dS )z¢Evaluate the results.

        Args:
            runner (:obj:`mmcv.Runner`): The underlined training runner.
            results (list): Output results.
        rl   TNr   r   )r"   ÚdatasetrS   rl   r(   ÚitemsÚ
log_bufferÚoutputÚreadyr'   r   r+   r-   ÚlistÚkeys)r,   rB   rU   Úeval_resÚnameÚvalr   r   r   rS   è   s   ÿÿÿ


zEvalHook.evaluate)Nr   Tr   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r7   r   rd   r:   r;   r.   r+   rC   rF   rJ   rE   rI   rL   rP   rT   rS   r   r   r   r   r      s,    (
ú+

r   c                       s:   e Zd ZdZ								d‡ fdd„	Zd	d
„ Z‡  ZS )ÚDistEvalHookaS	  Distributed evaluation hook.

    This hook will regularly perform evaluation in a given interval when
    performing in distributed environment.

    Args:
        dataloader (DataLoader): A PyTorch dataloader.
        start (int | None, optional): Evaluation starting epoch. It enables
            evaluation before the training starts if ``start`` <= the resuming
            epoch. If None, whether to evaluate is merely decided by
            ``interval``. Default: None.
        interval (int): Evaluation interval. Default: 1.
        by_epoch (bool): Determine perform evaluation by epoch or by iteration.
            If set to True, it will perform by epoch. Otherwise, by iteration.
            default: True.
        save_best (str | None, optional): If a metric is specified, it would
            measure the best checkpoint during evaluation. The information
            about best checkpoint would be save in best.json.
            Options are the evaluation metrics to the test dataset. e.g.,
             ``top1_acc``, ``top5_acc``, ``mean_class_accuracy``,
            ``mean_average_precision``, ``mmit_mean_average_precision``
            for action recognition dataset (RawframeDataset and VideoDataset).
            ``AR@AN``, ``auc`` for action localization dataset
            (ActivityNetDataset). ``mAP@0.5IOU`` for spatio-temporal action
            detection dataset (AVADataset). If ``save_best`` is ``auto``, the
            first key of the returned ``OrderedDict`` result will be used.
            Default: 'auto'.
        rule (str | None, optional): Comparison rule for best score. If set to
            None, it will infer a reasonable rule. Keys such as 'acc', 'top'
            .etc will be inferred by 'greater' rule. Keys contain 'loss' will
            be inferred by 'less' rule. Options are 'greater', 'less', None.
            Default: None.
        tmpdir (str | None): Temporary directory to save the results of all
            processes. Default: None.
        gpu_collect (bool): Whether to use gpu or cpu to collect results.
            Default: False.
        broadcast_bn_buffer (bool): Whether to broadcast the
            buffer(running_mean and running_var) of rank 0 to other rank
            before evaluation. Default: True.
        **eval_kwargs: Evaluation arguments fed into the evaluate function of
            the dataset.
    Nr   Tr   Fc
                    s6   t ƒ j|f|||||dœ|
¤Ž || _|| _|	| _d S )N)r$   r#   r%   r'   r-   )Úsuperr.   Úbroadcast_bn_bufferÚtmpdirÚgpu_collect)r,   r"   r$   r#   r%   r'   r-   r�   r‚   rƒ   r(   ©Ú	__class__r   r   r.   )  s   ÿúù
zDistEvalHook.__init__c           	      C   sÒ   | j r%|j}| ¡ D ]\}}t|tƒr$|jr$t |jd¡ t |j	d¡ q
|  
|¡s,dS ddlm} | j}|du r@t |jd¡}||j| j|| jd�}|jdkretdƒ |  ||¡}| jrg|  ||¡ dS dS dS )rN   r   N)Úmulti_gpu_testz
.eval_hook)r‚   rƒ   Ú
)r�   rR   Únamed_modulesr   r   Útrack_running_statsÚdistÚ	broadcastÚrunning_varÚrunning_meanrP   rQ   r†   r‚   re   rk   rj   r"   rƒ   ÚrankÚprintrS   r'   rT   )	r,   rB   rR   ry   Úmoduler†   r‚   rU   rV   r   r   r   rL   @  s<   ÿÿ€
ü
ûzDistEvalHook._do_evaluate)Nr   Tr   NTNF)r{   r|   r}   r~   r.   rL   Ú__classcell__r   r   r„   r   r   ý   s    -÷r   c                       ó    e Zd ZdZ‡ fdd„Z‡  ZS )ÚEpochEvalHookz"Deprecated class for ``EvalHook``.c                    ó    t  d¡ tƒ j|i |¤Ž d S )Nz}"EpochEvalHook" is deprecated, please switch to"EvalHook". See https://github.com/open-mmlab/mmaction2/pull/395 for more info©r   r    r€   r.   ©r,   ÚargsÚkwargsr„   r   r   r.   g  ó   ÿzEpochEvalHook.__init__©r{   r|   r}   r~   r.   r‘   r   r   r„   r   r“   d  ó    r“   c                       r’   )ÚDistEpochEvalHookz&Deprecated class for ``DistEvalHook``.c                    r”   )Nz…"DistEpochEvalHook" is deprecated, please switch to"DistEvalHook". See https://github.com/open-mmlab/mmaction2/pull/395 for more infor•   r–   r„   r   r   r.   r  r™   zDistEpochEvalHook.__init__rš   r   r   r„   r   rœ   o  r›   rœ   )rg   Úos.pathÚpathre   r   Úmathr   Útorch.distributedÚdistributedrŠ   Úmmcv.runnerr   Útorch.nn.modules.batchnormr   Útorch.utils.datar   r   r   r“   rœ   r   r   r   r   Ú<module>   s     rg