[367703]: / __pycache__ / Models.cpython-37.pyc

Download this file

151 lines (141 with data), 11.8 kB

B

Mm^Š4ã@s”dZddlZddlmZddlmZddlmmZGdd„dej	ƒZ
Gdd„dej	ƒZGdd„dej	ƒZGd	d
„d
ej	ƒZ
Gdd„dej	ƒZdS)
a
Created by Victor Delvigne
ISIA Lab, Faculty of Engineering University of Mons, Mons (Belgium)
victor.delvigne@umons.ac.be

Source: Bashivan, et al."Learning Representations from EEG with Deep Recurrent-Convolutional Neural Networks." International conference on learning representations (2016).

Copyright (C) 2019 - UMons

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
éNcsBeZdZdZe dddd¡dddddf‡fdd	„	Zd
d„Z‡ZS)ÚBasicCNNaì
    Build the  Mean Basic model performing a classification with CNN 

    param input_image: list of EEG image [batch_size, n_window, n_channel, h, w]
    param kernel: kernel size used for the convolutional layers
    param stride: stride apply during the convolutions
    param padding: padding used during the convolutions
    param max_kernel: kernel used for the maxpooling steps
    param n_classes: number of classes
    return x: output of the last layers after the log softmax
    ééé )rr)érécstt|ƒ ¡|jd}tj|d|||d|_tjdd|||d|_tjdd|||d|_tjdd|||d|_	t 
|¡|_tjdd|||d|_tjdd|||d|_
tjdd|||d|_t 
d¡|_tjdd|_t d	d
¡|_t d
|¡|_t ¡|_dS)Nrr)ÚstrideÚpaddingé@é€)rrgà?)Úpii)ÚsuperrÚ__init__ÚshapeÚnnÚConv2dÚconv1Úconv2Úconv3Úconv4Ú	MaxPool2dÚpool1Úconv5Úconv6Úconv7ÚpoolÚDropoutÚdropÚLinearÚfc1Úfc2Ú
LogSoftmaxÚmax)ÚselfÚinput_imageÚkernelrr	Ú
max_kernelÚ	n_classesÚ	n_channel)Ú	__class__©ú6/home/vdelv/PycharmProjects/EEGLearn_Pytorch/Models.pyr,s
zBasicCNN.__init__cCsð|jd}t | |¡¡}t | |¡¡}t | |¡¡}t | |¡¡}| |¡}t | |¡¡}t | 	|¡¡}| |¡}t | 
|¡¡}| |¡}| |jd|jdd¡}| |¡}| |jdd¡}| 
|¡}| |¡}| |¡}|S)Nrréÿÿÿÿ)rÚFÚrelurrrrrrrrÚreshaperrr r")r#ÚxÚ
batch_sizer*r*r+ÚforwardAs$







zBasicCNN.forward)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚtorchÚzerosrr2Ú
__classcell__r*r*)r)r+r s&rcsDeZdZdZe ddddd¡dddddf‡fd	d
„	Zdd„Z‡ZS)
ÚMaxCNNaø
    Build the Max-pooling model performing a maxpool over the 7 parallel convnets

    param input_image: list of EEG image [batch_size, n_window, n_channel, h, w]
    param kernel: kernel size used for the convolutional layers
    param stride: stride apply during the convolutions
    param padding: padding used during the convolutions
    param max_kernel: kernel used for the maxpooling steps
    param n_classes: number of classes
    return x: output of the last layers after the log softmax
    rérr)rr)rrrc	stt|ƒ ¡|jd}|jd}tj|d|||d|_tjdd|||d|_tjdd|||d|_tjdd|||d|_	t 
|¡|_tjdd|||d|_tjdd|||d|_
tjdd|||d|_t 
|df¡|_tjdd|_t |td	|ƒd
¡|_t d
|¡|_t ¡|_dS)Nrrr)rr	r
rgà?)rii)r
r:rrrrrrrrrrrrrrrrrÚintÚfcr r!r")	r#r$r%rr	r&r'Ún_windowr()r)r*r+rbs 

zMaxCNN.__init__c&Cs<| ¡dkr0t |jd|jdddd¡ ¡}n"t |jd|jdddd¡ ¡}x’tdƒD]†}| t 	| 
| t 	| t 	| | t 	| 
t 	| t 	| t 	| |dd…|f¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡|dd…|f<q\W| |jd|jddd¡}| |¡}| |jdd¡}| | |¡¡}| |¡}|S)Nrrrrr;ir,)Ú
get_devicer7r8rÚcudaÚcpuÚrangerr-r.rrrrrrrr/rÚviewr r=r")r#r0ÚtmpÚir*r*r+r2xs$"†

zMaxCNN.forward)	r3r4r5r6r7r8rr2r9r*r*)r)r+r:Vs(r:csDeZdZdZe ddddd¡dddddf‡fd	d
„	Zdd„Z‡ZS)
ÚTempCNNaù
    Build the Conv1D model performing a convolution1D over the 7 parallel convnets

    param input_image: list of EEG image [batch_size, n_window, n_channel, h, w]
    param kernel: kernel size used for the convolutional layers
    param stride: stride apply during the convolutions
    param padding: padding used during the convolutions
    param max_kernel: kernel used for the maxpooling steps
    param n_classes: number of classes
    return x: output of the last layers after the log softmax
    rr;rr)rr)rrrc	stt|ƒ ¡|jd}|jd}tj|d|||d|_tjdd|||d|_tjdd|||d|_tjdd|||d|_	t 
|¡|_tjdd|||d|_tjdd|||d|_
tjdd|||d|_tj|dd||d|_t 
|df¡|_tjdd	|_t d
|¡|_t ¡|_dS)Nrrr)rr	r
r)irgà?)réÀ)r
rFrrrrrrrrrrrrrÚConv1dÚconv8rrrrr=r!r")	r#r$r%rr	r&r'r>r()r)r*r+r“s 

zTempCNN.__init__c&Cs<| ¡dkr0t |jd|jdddd¡ ¡}n"t |jd|jdddd¡ ¡}x’tdƒD]†}| t 	| 
| t 	| t 	| | t 	| 
t 	| t 	| t 	| |dd…|f¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡|dd…|f<q\W| |jd|jddd¡}t 	| |¡¡}| |jdd¡}| |¡}| |¡}|S)Nrrrrr;ir,)r?r7r8rr@rArBrr-r.rrrrrrrr/rIrCr=r")r#r0rDrEr*r*r+r2ªs$"†

zTempCNN.forward)	r3r4r5r6r7r8rr2r9r*r*)r)r+rF‡s(rFcsFeZdZdZe ddddd¡dddddd	f‡fd
d„	Zdd
„Z‡ZS)ÚLSTMa
    Build the LSTM model applying a RNN over the 7 parallel convnets outputs

    param input_image: list of EEG image [batch_size, n_window, n_channel, h, w]
    param kernel: kernel size used for the convolutional layers
    param stride: stride apply during the convolutions
    param padding: padding used during the convolutions
    param max_kernel: kernel used for the maxpooling steps
    param n_classes: number of classes
    param n_units: number of units
    return x: output of the last layers after the log softmax
    rr;rr)rr)rrrrc
s"tt|ƒ ¡|jd}|jd}	tj|	d|||d|_tjdd|||d|_tjdd|||d|_tjdd|||d|_	t 
|¡|_tjdd|||d|_tjdd|||d|_
tjdd|||d|_t d||¡|_t ddd¡|_t 
|df¡|_tjd	d
|_t d|¡|_t ¡|_dS)Nrrr)rr	r
rir;gà?)ri€)r
rJrrrrrrrrrrrrrÚRNNÚrnnr7r8Úrnn_outrrrrr=r!r")
r#r$r%rr	r&r'Ún_unitsr>r()r)r*r+rÆs"

z
LSTM.__init__cCsh| ¡dkr0t |jd|jdddd¡ ¡}n"t |jd|jdddd¡ ¡}x¼tdƒD]°}|dd…|f}t | 	|¡¡}t | 
|¡¡}t | |¡¡}t | |¡¡}| 
|¡}t | |¡¡}t | |¡¡}| 
|¡}t | |¡¡}| 
|¡|dd…|f<~q\W| |jd|jdd¡}~| |¡\|_}|j |jdd¡}| |¡}| |¡}|S)Nrrrrr;ir,)r?r7r8rr@rArBr-r.rrrrrrrrr/rLrMrCr=r")r#r0rDrEÚimgÚ_r*r*r+r2Þs.$"



zLSTM.forward)	r3r4r5r6r7r8rr2r9r*r*)r)r+rJ¹s*rJcsFeZdZdZe ddddd¡dddddd	f‡fd
d„	Zdd
„Z‡ZS)ÚMixaH
        Build the LSTM model applying a RNN and a CNN over the 7 parallel convnets outputs

        param input_image: list of EEG image [batch_size, n_window, n_channel, h, w]
        param kernel: kernel size used for the convolutional layers
        param stride: stride apply during the convolutions
        param padding: padding used during the convolutions
        param max_kernel: kernel used for the maxpooling steps
        param n_classes: number of classes
        param n_units: number of units
        return x: output of the last layers after the log softmax
        rr;rr)rr)rrrrc
sFtt|ƒ ¡|jd}|jd}	tj|	d|||d|_tjdd|||d|_tjdd|||d|_tjdd|||d|_	t 
|¡|_tjdd|||d|_tjdd|||d|_
tjdd|||d|_t d||¡|_t ddd¡|_tj|dd	||d|_t 
|df¡|_tjd
d|_t dd
¡|_t d
|¡|_t ¡|_dS)Nrrr)rr	r
rir;)irgà?)ri@i)r
rQrrrrrrrrrrrrrrKrLr7r8rMrHrIrrrrrr r!r")
r#r$r%rr	r&r'rNr>r()r)r*r+rs&

zMix.__init__c
Cs¸| ¡dkr0t |jd|jdddd¡ ¡}n"t |jd|jdddd¡ ¡}x¼tdƒD]°}|dd…|f}t | 	|¡¡}t | 
|¡¡}t | |¡¡}t | |¡¡}| 
|¡}t | |¡¡}t | |¡¡}| 
|¡}t | |¡¡}| 
|¡|dd…|f<~q\Wt | | |jd|jddd¡¡¡}| |jdd¡}| | |jd|jdd¡¡\|_}~|j |jdd¡}t ||fd¡}| |¡}| |¡}| |¡}|S)Nrrrrr;ir,)r?r7r8rr@rArBr-r.rrrrrrrrrIr/rLZlstm_outrCÚcatrr r")r#r0rDrErOZ	temp_convrPÚlstmr*r*r+r2#s4$"

(&


zMix.forward)	r3r4r5r6r7r8rr2r9r*r*)r)r+rQùs*rQ)r6r7Útorch.optimÚoptimÚtorch.nnrÚtorch.nn.functionalÚ
functionalr-ÚModulerr:rFrJrQr*r*r*r+Ú<module>s612@