o
    _{j1  ã                   @   s4   d dl mZ ddlmZ e ¡ G dd„ deƒƒZdS )é   )ÚRECOGNIZERSé   )ÚBaseRecognizerc                   @   sB   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 )ÚAudioRecognizerz!Audio recognizer model framework.NTc                 C   s*   |r|du r
t dƒ‚|  ||¡S |  |¡S )z/Define the computation performed at every call.NzLabel should not be None.)Ú
ValueErrorÚforward_trainÚforward_test)ÚselfÚaudiosÚlabelÚreturn_loss© r   úB/root/disk_3tb/HAC/mmaction/models/recognizers/audio_recognizer.pyÚforward	   s
   
zAudioRecognizer.forwardc                 C   sF   |  d|jdd…  ¡}|  |¡}|  |¡}| ¡ }| j ||¡}|S )z>Defines the computation performed at every call when training.©éÿÿÿÿr   N)ÚreshapeÚshapeÚextract_featÚcls_headÚsqueezeÚloss)r	   r
   ÚlabelsÚxÚ	cls_scoreÚ	gt_labelsr   r   r   r   r      s   

zAudioRecognizer.forward_trainc                 C   sN   |j d }| d|j dd…  ¡}|  |¡}|  |¡}|  ||¡}| ¡  ¡ S )zTDefines the computation performed at every call when evaluation and
        testing.r   r   r   N)r   r   r   r   Zaverage_clipÚcpuÚnumpy)r	   r
   Únum_segsr   r   r   r   r   r      s   


zAudioRecognizer.forward_testc                 C   s   t ‚)N)ÚNotImplementedError)r	   r
   r   r   r   Úforward_gradcam'   s   zAudioRecognizer.forward_gradcamc           
      K   óJ   |d }|d }| ||ƒ}|   |¡\}}t||ttt| ¡ ƒƒƒd�}	|	S )a¥  The iteration step during training.

        This method defines an iteration step during training, except for the
        back propagation and optimizer updating, which are done in an optimizer
        hook. Note that in some complicated cases or models, the whole process
        including back propagation and optimizer updating is also defined in
        this method, such as GAN.

        Args:
            data_batch (dict): The output of dataloader.
            optimizer (:obj:`torch.optim.Optimizer` | dict): The optimizer of
                runner is passed to ``train_step()``. This argument is unused
                and reserved.

        Returns:
            dict: It should contain at least 3 keys: ``loss``, ``log_vars``,
                ``num_samples``.
                ``loss`` is a tensor for back propagation, which can be a
                weighted sum of multiple losses.
                ``log_vars`` contains all the variables to be sent to the
                logger.
                ``num_samples`` indicates the batch size (when the model is
                DDP, it means the batch size on each GPU), which is used for
                averaging the logs.
        r
   r   ©r   Úlog_varsÚnum_samples©Ú_parse_lossesÚdictÚlenÚnextÚiterÚvalues©
r	   Ú
data_batchÚ	optimizerÚkwargsr
   r   Úlossesr   r#   Úoutputsr   r   r   Ú
train_step*   s   
ýzAudioRecognizer.train_stepc           
      K   r!   )a  The iteration step during validation.

        This method shares the same signature as :func:`train_step`, but used
        during val epochs. Note that the evaluation after training epochs is
        not implemented with this method, but an evaluation hook.
        r
   r   r"   r%   r,   r   r   r   Úval_stepR   s   
ýzAudioRecognizer.val_step)NT)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r    r2   r3   r   r   r   r   r      s    
	
(r   N)Úregistryr   Úbaser   Úregister_moduler   r   r   r   r   Ú<module>   s    