-
Notifications
You must be signed in to change notification settings - Fork 24
/
Models.py
198 lines (176 loc) · 8.01 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
'''
This is the models of TSception and its variant
To use the models, please manage the data into
the dimension of(mini-batch, 1, EEG-channels,data point)
before feed the data into forward()
For more details about the models, please refer to our paper:
Yi Ding, Neethu Robinson, Qiuhao Zeng, Dou Chen, Aung Aung Phyo Wai, Tih-Shih Lee, Cuntai Guan,
"TSception: A Deep Learning Framework for Emotion Detection Useing EEG"(IJCNN 2020)
'''
################################################## TSception ######################################################
class TSception(nn.Module):
def conv_block(self, in_chan, out_chan, kernel, step, pool):
return nn.Sequential(
nn.Conv2d(in_channels=in_chan, out_channels=out_chan,
kernel_size=kernel, stride=step, padding=0),
nn.LeakyReLU(),
nn.AvgPool2d(kernel_size=(1, pool), stride=(1, pool)))
def __init__(self, num_classes, input_size, sampling_rate, num_T, num_S, hidden, dropout_rate):
# input_size: EEG channel x datapoint
super(TSception, self).__init__()
self.inception_window = [0.5, 0.25, 0.125]
self.pool = 8
# by setting the convolutional kernel being (1,lenght) and the strids being 1 we can use conv2d to
# achieve the 1d convolution operation
self.Tception1 = self.conv_block(1, num_T, (1, int(self.inception_window[0] * sampling_rate)), 1, self.pool)
self.Tception2 = self.conv_block(1, num_T, (1, int(self.inception_window[1] * sampling_rate)), 1, self.pool)
self.Tception3 = self.conv_block(1, num_T, (1, int(self.inception_window[2] * sampling_rate)), 1, self.pool)
self.Sception1 = self.conv_block(num_T, num_S, (int(input_size[-2]), 1), 1, int(self.pool*0.25))
self.Sception2 = self.conv_block(num_T, num_S, (int(input_size[-2] * 0.5), 1), (int(input_size[-2] * 0.5), 1),
int(self.pool*0.25))
self.BN_t = nn.BatchNorm2d(num_T)
self.BN_s = nn.BatchNorm2d(num_S)
size = self.get_size(input_size)
self.fc = nn.Sequential(
nn.Linear(size[1], hidden),
nn.ReLU(),
nn.Dropout(dropout_rate),
nn.Linear(hidden, num_classes)
)
def forward(self, x):
y = self.Tception1(x)
out = y
y = self.Tception2(x)
out = torch.cat((out, y), dim=-1)
y = self.Tception3(x)
out = torch.cat((out, y), dim=-1)
out = self.BN_t(out)
z = self.Sception1(out)
out_ = z
z = self.Sception2(out)
out_ = torch.cat((out_, z), dim=2)
out = self.BN_s(out_)
out = out.view(out.size()[0], -1)
out = self.fc(out)
return out
def get_size(self, input_size):
# here we use an array with the shape being
# (1(mini-batch),1(convolutional channel),EEG channel,time data point)
# to simulate the input data and get the output size
data = torch.ones((1, 1, input_size[-2], int(input_size[-1])))
y = self.Tception1(data)
out = y
y = self.Tception2(data)
out = torch.cat((out, y), dim=-1)
y = self.Tception3(data)
out = torch.cat((out, y), dim=-1)
out = self.BN_t(out)
z = self.Sception1(out)
out_final = z
z = self.Sception2(out)
out_final = torch.cat((out_final, z), dim=2)
out = self.BN_s(out_final)
out = out.view(out.size()[0], -1)
return out.size()
######################################### Temporal ########################################
class Tception(nn.Module):
def __init__(self, num_classes, input_size, sampling_rate, num_T, hidden, dropout_rate):
# input_size: channel x datapoint
super(Tception, self).__init__()
self.inception_window = [0.5, 0.25, 0.125, 0.0625, 0.03125]
# by setting the convolutional kernel being (1,lenght) and the strids being 1 we can use conv2d to
# achieve the 1d convolution operation
self.Tception1 = nn.Sequential(
nn.Conv2d(1, num_T, kernel_size=(1,int(self.inception_window[0]*sampling_rate)), stride=1, padding=0),
nn.ReLU(),
nn.AvgPool2d(kernel_size=(1,16), stride=(1,16)))
self.Tception2 = nn.Sequential(
nn.Conv2d(1, num_T, kernel_size=(1,int(self.inception_window[1]*sampling_rate)), stride=1, padding=0),
nn.ReLU(),
nn.AvgPool2d(kernel_size=(1,16), stride=(1,16)))
self.Tception3 = nn.Sequential(
nn.Conv2d(1, num_T, kernel_size=(1,int(self.inception_window[2]*sampling_rate)), stride=1, padding=0),
nn.ReLU(),
nn.AvgPool2d(kernel_size=(1,16), stride=(1,16)))
self.BN_t = nn.BatchNorm2d(num_T)
size = self.get_size(input_size,sampling_rate,num_T)
self.fc1 = nn.Sequential(
nn.Linear(size[1], hidden),
nn.ReLU(),
nn.Dropout(dropout_rate))
self.fc2 = nn.Sequential(
nn.Linear(hidden, num_classes))
def forward(self, x):
y = self.Tception1(x)
out = y
y = self.Tception2(x)
out = torch.cat((out,y),dim = -1)
y = self.Tception3(x)
out = torch.cat((out,y),dim = -1)
out = self.BN_t(out)
out = out.view(out.size()[0], -1)
out = self.fc1(out)
out = self.fc2(out)
return out
def get_size(self,input_size,sampling_rate,num_T):
data = torch.ones((1,1,input_size[0],input_size[1]))
y = self.Tception1(data)
out = y
y = self.Tception2(data)
out = torch.cat((out,y),dim = -1)
y = self.Tception3(data)
out = torch.cat((out,y),dim = -1)
out = self.BN_t(out)
out = out.view(out.size()[0], -1)
return out.size()
############################################ Spacial ########################################
class Sception(nn.Module):
def __init__(self, num_classes, input_size, sampling_rate, num_S, hidden, dropout_rate):
# input_size: channel x datapoint
super(Sception, self).__init__()
self.Sception1 = nn.Sequential(
nn.Conv2d(1, num_S, kernel_size=(int(input_size[0]),1), stride=1, padding=0),
nn.ReLU(),
nn.AvgPool2d(kernel_size=(1,16), stride=(1,16)))
self.Sception2 = nn.Sequential(
nn.Conv2d(1, num_S, kernel_size=(int(input_size[0]*0.5),1), stride=(int(input_size[0]*0.5),1), padding=0),
nn.ReLU(),
nn.AvgPool2d(kernel_size=(1,16), stride=(1,16)))
self.BN_s = nn.BatchNorm2d(num_S)
size = self.get_size(input_size)
self.fc1 = nn.Sequential(
nn.Linear(size[1], hidden),
nn.ReLU(),
nn.Dropout(dropout_rate))
self.fc2 = nn.Sequential(
nn.Linear(hidden, num_classes))
def forward(self, x):
y = self.Sception1(x)
out = y
y = self.Sception2(x)
out = torch.cat((out,y),dim = 2)
out = self.BN_s(out)
out = out.view(out.size()[0], -1)
out = self.fc1(out)
out = self.fc2(out)
return out
def get_size(self, input_size):
data = torch.ones((1,1,input_size[0],input_size[1]))
y = self.Sception1(data)
out = y
y = self.Sception2(data)
out = torch.cat((out,y),dim = 2)
out = self.BN_s(out)
out = out.view(out.size()[0], -1)
return out.size()
if __name__ == "__main__":
model = TSception(2,(4,1024),256,9,6,128,0.2)
#model = Sception(2,(4,1024),256,6,128,0.2)
#model = Tception(2,(4,1024),256,9,128,0.2)
print(model)
pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(pytorch_total_params)