o
    ϯi                     @   sH   d dl Zd dlm  mZ d dlmZ ddlmZ G dd dej	Z
dS )    N)nn   )	MLPLayersc                       s(   e Zd Zd fdd	ZdddZ  ZS )LinearProbeNc                    s   t    d}|| _d| j_|| _|rt||d |gd| _nt||| _| jr4| j	 D ]}d|_
q.|dkr=d| _dS |dkrHt | _dS |dkrSt | _dS |d	kr`tj|d
| _dS |dkrmtjdd| _dS |dkrxt | _dS dS )a  
        Args:
            model: nn.Module
            mlp: bool, if True, then use the MLP layer as the linear probe module
            freeze: bool, if Ture, then freeze all the CLAP model's layers when training the linear probe
            in_ch: int, the output channel from CLAP model
            out_ch: int, the output channel from linear probe (class_num)
            act: torch.nn.functional, the activation function before the loss function
        i   N   )unitsFNonerelueluprelu)num_parameterssoftmax)dimsigmoid)super__init__
clap_modeltext_branchfreezer   lp_layerr   Linear
parametersrequires_gradactReLUELUPReLUSoftmaxSigmoid)selfmodelmlpr   in_chout_chr   param	__class__ W/home/ubuntu/.local/lib/python3.10/site-packages/laion_clap/clap_module/linear_probe.pyr      s0   


zLinearProbe.__init__c                 C   sP   | j r| j  | j| jj|||dd }| |}| jdur&| |}|S )z
        Args:
            x: waveform, torch.tensor [batch, t_samples] / batch of mel_spec and longer list
            mix_lambda: torch.tensor [batch], the mixup lambda
        Returns:
            class_prob: torch.tensor [batch, class_num]

        )mixup_lambdadevice	embeddingN)r   r   evalaudio_projectionaudio_branchr   r   )r    x
mix_lambdar+   outr(   r(   r)   forward-   s   




zLinearProbe.forward)N)NN)__name__
__module____qualname__r   r3   __classcell__r(   r(   r&   r)   r      s    %r   )numpynptorch.nn.functionalr   
functionalFtorchr!   r   Moduler   r(   r(   r(   r)   <module>   s
    