-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
183 lines (162 loc) · 6.86 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
import torch
from torch import nn
import numpy as np
import random
import os
from deform_conv import DeformConv3d, DeformableConv2d
SEED =0
torch.manual_seed(SEED)
torch.cuda.manual_seed_all(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(SEED)
random.seed(SEED)
os.environ['PYTHONHASHSEED'] = str(SEED)
class EEG3DAutoencoder(nn.Module):
def __init__(self, hidden_layers):
super(EEG3DAutoencoder, self).__init__()
self.hidden_layers = hidden_layers
self.encoder = nn.Sequential(
nn.Conv3d(1, self.hidden_layers[0], 3, stride=1, padding=1),
nn.BatchNorm3d(self.hidden_layers[0]),
nn.ELU(True),
nn.MaxPool3d(2, stride=2),
nn.Conv3d(self.hidden_layers[0], self.hidden_layers[1], 3, stride=1, padding=1),
nn.BatchNorm3d(self.hidden_layers[1]),
nn.ELU(True),
nn.MaxPool3d(2, stride=2)
)
self.decoder = nn.Sequential(
nn.ConvTranspose3d(self.hidden_layers[1], self.hidden_layers[0], 2, stride=2),
nn.BatchNorm3d(self.hidden_layers[0]),
nn.ELU(True),
nn.ConvTranspose3d(self.hidden_layers[0], 1, 2, stride=2),
nn.BatchNorm3d(1),
nn.ELU(True)
)
self.pool = nn.AdaptiveAvgPool3d((3, 6, 8))
self.fc = nn.Linear(3 * 6 * 8 * self.hidden_layers[1], 2)
def forward(self, x):
original_shapes = x.shape
codes = self.encoder(x)
classes = self.pool(codes)
classes = self.fc(classes.view(-1, 3 * 6 * 8 * self.hidden_layers[1]))
x = self.decoder(codes)
x = nn.functional.interpolate(x, size=original_shapes[2:])
return codes, x, classes
class EEGAutoencoder(nn.Module):
def __init__(self, hidden_layers):
super(EEGAutoencoder, self).__init__()
self.hidden_layers = hidden_layers
self.encoder = nn.Sequential(
nn.Conv2d(15, self.hidden_layers[0], 3, stride=1, padding=1),
nn.BatchNorm2d(self.hidden_layers[0]),
nn.ELU(True),
nn.MaxPool2d(2, stride=2),
nn.Conv2d(self.hidden_layers[0], self.hidden_layers[1], 3, stride=1, padding=1),
nn.BatchNorm2d(self.hidden_layers[1]),
nn.ELU(True),
nn.MaxPool2d(2, stride=2),
nn.Conv2d(self.hidden_layers[1], self.hidden_layers[2], 3, stride=1, padding=1),
nn.BatchNorm2d(self.hidden_layers[2]),
nn.ELU(True),
nn.MaxPool2d(2, stride=2),
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(self.hidden_layers[2], self.hidden_layers[1], 2, stride=2),
nn.ELU(True),
nn.BatchNorm2d(self.hidden_layers[1]),
nn.ConvTranspose2d(self.hidden_layers[1], self.hidden_layers[0], 2, stride=2),
nn.ELU(True),
nn.BatchNorm2d(self.hidden_layers[0]),
nn.ConvTranspose2d(self.hidden_layers[0], 15, 2, stride=2),
nn.BatchNorm2d(15),
nn.ELU(True)
)
self.pool = nn.AdaptiveAvgPool2d((8, 8))
self.fc = nn.Linear(8 * 8 * self.hidden_layers[2], 2)
def forward(self, x):
original_shapes = x.shape
codes = self.encoder(x)
classes = self.pool(codes)
classes = self.fc(classes.view(-1, 8 * 8 * self.hidden_layers[2]))
x = self.decoder(codes)
x = nn.functional.interpolate(x, size=original_shapes[2:])
return codes, x, classes
class EEGDeformAutoencoder(nn.Module):
def __init__(self, hidden_layers):
super(EEGDeformAutoencoder, self).__init__()
self.hidden_layers = hidden_layers
self.encoder = nn.Sequential(
DeformableConv2d(15, self.hidden_layers[0], 3, stride=1, padding=1),
nn.BatchNorm2d(self.hidden_layers[0]),
nn.ELU(True),
nn.MaxPool2d(2, stride=2),
DeformableConv2d(self.hidden_layers[0], self.hidden_layers[1], 3, stride=1, padding=1),
nn.BatchNorm2d(self.hidden_layers[1]),
nn.ELU(True),
nn.MaxPool2d(2, stride=2),
# DeformableConv2d(self.hidden_layers[1], self.hidden_layers[2], 3, stride=1, padding=1),
# nn.BatchNorm2d(self.hidden_layers[2]),
# nn.ELU(True),
# nn.MaxPool2d(2, stride=2),
# DeformableConv2d(self.hidden_layers[2], self.hidden_layers[3], 3, stride=1, padding=1),
# nn.BatchNorm2d(self.hidden_layers[3]),
# nn.ELU(True),
# nn.MaxPool2d(2, stride=2),
)
self.decoder = nn.Sequential(
# nn.ConvTranspose2d(self.hidden_layers[3], self.hidden_layers[2], 2, stride=2),
# nn.ELU(True),
# nn.BatchNorm2d(self.hidden_layers[2]),
# nn.ConvTranspose2d(self.hidden_layers[2], self.hidden_layers[1], 2, stride=2),
# nn.ELU(True),
# nn.BatchNorm2d(self.hidden_layers[1]),
nn.ConvTranspose2d(self.hidden_layers[1], self.hidden_layers[0], 2, stride=2),
nn.ELU(True),
nn.BatchNorm2d(self.hidden_layers[0]),
nn.ConvTranspose2d(self.hidden_layers[0], 15, 2, stride=2),
nn.BatchNorm2d(15),
nn.ELU(True)
)
self.pool = nn.AdaptiveAvgPool2d((8, 8))
self.fc = nn.Linear(8 * 8 * self.hidden_layers[1], 2)
def forward(self, x):
original_shapes = x.shape
codes = self.encoder(x)
classes = self.pool(codes)
classes = self.fc(classes.view(-1, 8 * 8 * self.hidden_layers[1]))
x = self.decoder(codes)
x = nn.functional.interpolate(x, size=original_shapes[2:])
return codes, x, classes
class EEG3DDeformAutoencoder(nn.Module):
def __init__(self):
super(EEG3DDeformAutoencoder, self).__init__()
self.encoder = nn.Sequential(
DeformConv3d(1, 32, 3, stride=1, padding=1),
nn.BatchNorm3d(32),
nn.ELU(True),
nn.MaxPool3d(2, stride=2), # b, 16, 5, 5
DeformConv3d(32, 64, 3, stride=1, padding=1), # b, 8, 3, 3
nn.BatchNorm3d(64),
nn.ELU(True),
nn.MaxPool3d(2, stride=2)
)
self.decoder = nn.Sequential(
nn.ConvTranspose3d(64, 32, 2, stride=2), # b, 16, 5, 5
nn.BatchNorm3d(32),
nn.ELU(True),
nn.ConvTranspose3d(32, 1, 2, stride=2), # b, 8, 15, 15
nn.BatchNorm3d(1),
nn.ELU(True)
)
self.pool = nn.AdaptiveAvgPool3d((3, 6, 8))
self.fc = nn.Linear(3 * 6 * 8 * 64, 2)
def forward(self, x):
original_shapes = x.shape
codes = self.encoder(x)
classes = self.pool(codes)
classes = self.fc(classes.view(-1, 3 * 6 * 8 * 64))
x = self.decoder(codes)
x = nn.functional.interpolate(x, size=original_shapes[2:])
return codes, x, classes