-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathModels.py
323 lines (269 loc) · 13.1 KB
/
Models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'''
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
'''
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
class BasicCNN(nn.Module):
'''
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
'''
def __init__(self, input_image=torch.zeros(1, 3, 32, 32), kernel=(3,3), stride=1, padding=1,max_kernel=(2,2), n_classes=4):
super(BasicCNN, self).__init__()
n_channel = input_image.shape[1]
self.conv1 = nn.Conv2d(n_channel,32,kernel,stride=stride, padding=padding)
self.conv2 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.conv3 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.conv4 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.pool1 = nn.MaxPool2d(max_kernel)
self.conv5 = nn.Conv2d(32,64,kernel,stride=stride,padding=padding)
self.conv6 = nn.Conv2d(64,64,kernel,stride=stride,padding=padding)
self.conv7 = nn.Conv2d(64,128,kernel,stride=stride,padding=padding)
self.pool = nn.MaxPool2d((1,1))
self.drop = nn.Dropout(p=0.5)
self.fc1 = nn.Linear(2048,512)
self.fc2 = nn.Linear(512,n_classes)
self.max = nn.LogSoftmax()
def forward(self, x):
batch_size = x.shape[0]
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = self.pool1(x)
x = F.relu(self.conv5(x))
x = F.relu(self.conv6(x))
x = self.pool1(x)
x = F.relu(self.conv7(x))
x = self.pool1(x)
x = x.reshape(x.shape[0],x.shape[1], -1)
x = self.pool(x)
x = x.reshape(x.shape[0],-1)
x = self.fc1(x)
x = self.fc2(x)
x = self.max(x)
return x
class MaxCNN(nn.Module):
'''
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
'''
def __init__(self, input_image=torch.zeros(1, 7, 3, 32, 32), kernel=(3,3), stride=1, padding=1,max_kernel=(2,2), n_classes=4):
super(MaxCNN, self).__init__()
n_window = input_image.shape[1]
n_channel = input_image.shape[2]
self.conv1 = nn.Conv2d(n_channel,32,kernel,stride=stride, padding=padding)
self.conv2 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.conv3 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.conv4 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.pool1 = nn.MaxPool2d(max_kernel)
self.conv5 = nn.Conv2d(32,64,kernel,stride=stride,padding=padding)
self.conv6 = nn.Conv2d(64,64,kernel,stride=stride,padding=padding)
self.conv7 = nn.Conv2d(64,128,kernel,stride=stride,padding=padding)
self.pool = nn.MaxPool2d((n_window,1))
self.drop = nn.Dropout(p=0.5)
self.fc = nn.Linear(n_window*int(4*4*128/n_window),512)
self.fc2 = nn.Linear(512,n_classes)
self.max = nn.LogSoftmax()
def forward(self, x):
if x.get_device() == 0:
tmp = torch.zeros(x.shape[0],x.shape[1],128,4,4).cuda()
else:
tmp = torch.zeros(x.shape[0],x.shape[1],128,4,4).cpu()
for i in range(7):
tmp[:,i] = self.pool1( F.relu(self.conv7(self.pool1(F.relu(self.conv6(F.relu(self.conv5(self.pool1( F.relu(self.conv4(F.relu(self.conv3( F.relu(self.conv2(F.relu(self.conv1(x[:,i])))))))))))))))))
x = tmp.reshape(x.shape[0], x.shape[1],4*128*4,1)
x = self.pool(x)
x = x.view(x.shape[0],-1)
x = self.fc2(self.fc(x))
x = self.max(x)
return x
class TempCNN(nn.Module):
'''
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
'''
def __init__(self, input_image=torch.zeros(1, 7, 3, 32, 32), kernel=(3,3), stride=1, padding=1,max_kernel=(2,2), n_classes=4):
super(TempCNN, self).__init__()
n_window = input_image.shape[1]
n_channel = input_image.shape[2]
self.conv1 = nn.Conv2d(n_channel,32,kernel,stride=stride, padding=padding)
self.conv2 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.conv3 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.conv4 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.pool1 = nn.MaxPool2d(max_kernel)
self.conv5 = nn.Conv2d(32,64,kernel,stride=stride,padding=padding)
self.conv6 = nn.Conv2d(64,64,kernel,stride=stride,padding=padding)
self.conv7 = nn.Conv2d(64,128,kernel,stride=stride,padding=padding)
#Temporal CNN Layer
self.conv8 = nn.Conv1d(n_window,64,(4*4*128,3),stride=stride,padding=padding)
self.pool = nn.MaxPool2d((n_window,1))
self.drop = nn.Dropout(p=0.5)
self.fc = nn.Linear(64*3,n_classes)
self.max = nn.LogSoftmax()
def forward(self, x):
if x.get_device() == 0:
tmp = torch.zeros(x.shape[0],x.shape[1],128,4,4).cuda()
else:
tmp = torch.zeros(x.shape[0],x.shape[1],128,4,4).cpu()
for i in range(7):
tmp[:,i] = self.pool1( F.relu(self.conv7(self.pool1(F.relu(self.conv6(F.relu(self.conv5(self.pool1( F.relu(self.conv4(F.relu(self.conv3( F.relu(self.conv2(F.relu(self.conv1(x[:,i])))))))))))))))))
x = tmp.reshape(x.shape[0], x.shape[1],4*128*4,1)
x = F.relu(self.conv8(x))
x = x.view(x.shape[0],-1)
x = self.fc(x)
x = self.max(x)
return x
class LSTM(nn.Module):
'''
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
'''
def __init__(self, input_image=torch.zeros(1, 7, 3, 32, 32), kernel=(3,3), stride=1, padding=1,max_kernel=(2,2), n_classes=4, n_units=128):
super(LSTM, self).__init__()
n_window = input_image.shape[1]
n_channel = input_image.shape[2]
self.conv1 = nn.Conv2d(n_channel,32,kernel,stride=stride, padding=padding)
self.conv2 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.conv3 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.conv4 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.pool1 = nn.MaxPool2d(max_kernel)
self.conv5 = nn.Conv2d(32,64,kernel,stride=stride,padding=padding)
self.conv6 = nn.Conv2d(64,64,kernel,stride=stride,padding=padding)
self.conv7 = nn.Conv2d(64,128,kernel,stride=stride,padding=padding)
# LSTM Layer
self.rnn = nn.RNN(4*4*128, n_units, n_window)
self.rnn_out = torch.zeros(2, 7, 128)
self.pool = nn.MaxPool2d((n_window,1))
self.drop = nn.Dropout(p=0.5)
self.fc = nn.Linear(896, n_classes)
self.max = nn.LogSoftmax()
def forward(self, x):
if x.get_device() == 0:
tmp = torch.zeros(x.shape[0], x.shape[1], 128, 4, 4).cuda()
else:
tmp = torch.zeros(x.shape[0], x.shape[1], 128, 4, 4).cpu()
for i in range(7):
img = x[:, i]
img = F.relu(self.conv1(img))
img = F.relu(self.conv2(img))
img = F.relu(self.conv3(img))
img = F.relu(self.conv4(img))
img = self.pool1(img)
img = F.relu(self.conv5(img))
img = F.relu(self.conv6(img))
img = self.pool1(img)
img = F.relu(self.conv7(img))
tmp[:, i] = self.pool1(img)
del img
x = tmp.reshape(x.shape[0], x.shape[1], 4 * 128 * 4)
del tmp
self.rnn_out, _ = self.rnn(x)
x = self.rnn_out.view(x.shape[0], -1)
x = self.fc(x)
x = self.max(x)
return x
class Mix(nn.Module):
'''
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
'''
def __init__(self, input_image=torch.zeros(1, 7, 3, 32, 32), kernel=(3,3), stride=1, padding=1,max_kernel=(2,2), n_classes=4, n_units=128):
super(Mix, self).__init__()
n_window = input_image.shape[1]
n_channel = input_image.shape[2]
self.conv1 = nn.Conv2d(n_channel,32,kernel,stride=stride, padding=padding)
self.conv2 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.conv3 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.conv4 = nn.Conv2d(32,32,kernel,stride=stride, padding=padding)
self.pool1 = nn.MaxPool2d(max_kernel)
self.conv5 = nn.Conv2d(32,64,kernel,stride=stride,padding=padding)
self.conv6 = nn.Conv2d(64,64,kernel,stride=stride,padding=padding)
self.conv7 = nn.Conv2d(64,128,kernel,stride=stride,padding=padding)
# LSTM Layer
self.rnn = nn.RNN(4*4*128, n_units, n_window)
self.rnn_out = torch.zeros(2, 7, 128)
# Temporal CNN Layer
self.conv8 = nn.Conv1d(n_window, 64, (4 * 4 * 128, 3), stride=stride, padding=padding)
self.pool = nn.MaxPool2d((n_window, 1))
self.drop = nn.Dropout(p=0.5)
self.fc1 = nn.Linear(1088,512)
self.fc2 = nn.Linear(512, n_classes)
self.max = nn.LogSoftmax()
def forward(self, x):
if x.get_device() == 0:
tmp = torch.zeros(x.shape[0], x.shape[1], 128, 4, 4).cuda()
else:
tmp = torch.zeros(x.shape[0], x.shape[1], 128, 4, 4).cpu()
for i in range(7):
img = x[:, i]
img = F.relu(self.conv1(img))
img = F.relu(self.conv2(img))
img = F.relu(self.conv3(img))
img = F.relu(self.conv4(img))
img = self.pool1(img)
img = F.relu(self.conv5(img))
img = F.relu(self.conv6(img))
img = self.pool1(img)
img = F.relu(self.conv7(img))
tmp[:, i] = self.pool1(img)
del img
temp_conv = F.relu(self.conv8(tmp.reshape(x.shape[0], x.shape[1], 4 * 128 * 4, 1)))
temp_conv = temp_conv.reshape(temp_conv.shape[0], -1)
self.lstm_out, _ = self.rnn(tmp.reshape(x.shape[0], x.shape[1], 4 * 128 * 4))
del tmp
lstm = self.lstm_out.view(x.shape[0], -1)
x = torch.cat((temp_conv, lstm), 1)
x = self.fc1(x)
x = self.fc2(x)
x = self.max(x)
return x