a
    ¡²"eÄ  ã                
   @   s   d Z ddlZddd„ZdS )z%2 mismatched segments dilation model.é    Né@   é   é   é   é   ÚreluTé   c
                    s~  t jjjˆ|gd�}
‡‡fdd„t|	d ƒD ƒ}|
g}| |¡ dd„ |D ƒ}t|tƒrd|g| }n|}tdƒ t jjj	|dd�|
ƒ‰ t|ƒD ]Z}t jjj	|||| d|| d�ˆ ƒ‰ t jjj	|||| d|| d�‰‡fd	d„|D ƒ}qŽ‡ fd
d„|D ƒ}t jjj
ddd�‰‡fdd„|D ƒ}t jj t jj ¡ |ƒ¡}t jj||gd�}|�rz|jt jj ¡ dgdgd� t| ¡ ƒ |S )u  Convolutional dilation model.

    Code was taken and adapted from
    https://github.com/exporl/eeg-matching-eusipco2020

    Parameters
    ----------
    time_window : int or None
        Segment length. If None, the model will accept every time window input
        length.
    eeg_input_dimension : int
        number of channels of the EEG
    env_input_dimension : int
        dimemsion of the stimulus representation.
        if stimulus == envelope, env_input_dimension =1
        if stimulus == mel, env_input_dimension =28
    layers : int
        Depth of the network/Number of layers
    kernel_size : int
        Size of the kernel for the dilation convolutions
    spatial_filters : int
        Number of parallel filters to use in the spatial layer
    dilation_filters : int
        Number of parallel filters to use in the dilation layers
    activation : str or list or tuple
        Name of the non-linearity to apply after the dilation layers
        or list/tuple of different non-linearities
    compile : bool
        If model should be compiled
    inputs : tuple
        Alternative inputs

    Returns
    -------
    tf.Model
        The dilation model


    References
    ----------
    Accou, B., Jalilpour Monesi, M., Montoya, J., Van hamme, H. & Francart, T.
    Modeling the relationship between acoustic stimulus and EEG with a dilated
    convolutional neural network. In 2020 28th European Signal Processing
    Conference (EUSIPCO), 1175â€“1179, DOI: 10.23919/Eusipco47968.2020.9287417
    (2021). ISSN: 2076-1465.

    Accou, B., Monesi, M. J., hamme, H. V. & Francart, T.
    Predicting speech intelligibility from EEG in a non-linear classification
    paradigm. J. Neural Eng. 18, 066008, DOI: 10.1088/1741-2552/ac33e9 (2021).
    Publisher: IOP Publishing
    ©Úshapec                    s    g | ]}t jjjˆˆ gd �‘qS )r	   )ÚtfÚkerasÚlayersÚInput)Ú.0Ú_)Úenv_input_dimensionÚtime_window© úh/Users/qiuzelin/Desktop/project_20/ICASSP2024/task1_match_mismatch/models/dilated_convolutional_model.pyÚ
<listcomp>F   ó    z"dilation_model.<locals>.<listcomp>r   c                 S   s   g | ]}|‘qS r   r   )r   Úxr   r   r   r   L   r   z########################)Úkernel_size)r   Zdilation_rateÚstridesÚ
activationc                    s   g | ]}ˆ |ƒ‘qS r   r   ©r   Zstimulus_proj)Úenv_proj_layerr   r   r   l   r   c                    s&   g | ]}t jjjd dd�ˆ |gƒ‘qS )r   T)Ú	normalize)r   r   r   ÚDotr   )Ú
eeg_proj_1r   r   r   p   r   Úlinear)r   c                    s    g | ]}ˆ t jj ¡ |ƒƒ‘qS r   )r   r   r   ÚFlatten)r   Zcos_i)Úlinear_proj_simr   r   r   u   r   )ÚinputsÚoutputsÚaccuracyZcategorical_crossentropy)Ú	optimizerÚmetricsÚloss)r   r   r   r   ÚrangeÚextendÚ
isinstanceÚstrÚprintZConv1DÚDenseÚactivationsÚsoftmaxÚConcatenateZModelÚcompileZ
optimizersÚAdamÚsummary)r   Zeeg_input_dimensionr   r   r   Zspatial_filtersZdilation_filtersr   r2   Únum_mismatched_segmentsZeegZstimuli_inputÚ
all_inputsZstimuli_projr/   Úlayer_indexÚcosZcos_projÚoutÚmodelr   )r   r   r   r"   r   r   Údilation_model   sR    @

ûú	û
ýr;   )
Nr   r   r   r   r   r   r   Tr   )Ú__doc__Ú
tensorflowr   r;   r   r   r   r   Ú<module>   s             ö