-
Notifications
You must be signed in to change notification settings - Fork 1
/
temporal_convolutional_layers.py
175 lines (146 loc) · 7.1 KB
/
temporal_convolutional_layers.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
import math
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from torch.nn.utils import weight_norm
from torch.autograd import Variable
class Chomp1d(nn.Module):
def __init__(self, chomp_size):
super(Chomp1d, self).__init__()
self.chomp_size = chomp_size
def forward(self, x):
return x[:, :, :-self.chomp_size].contiguous()
class Chomp2d(nn.Module):
def __init__(self, chomp_size):
super(Chomp2d, self).__init__()
self.chomp_size = chomp_size
def forward(self, x):
return x[:, :, :, :-self.chomp_size].contiguous()
class TemporalBlockPro(nn.Module):
def __init__(self, n_inputs, n_outputs, kernel_size, stride, dilation, padding, dropout=0.2):
super(TemporalBlockPro, self).__init__()
self.conv1 = weight_norm(nn.Conv2d(n_inputs, n_outputs, (1, kernel_size),
stride=stride, padding=(0, padding), dilation=(1, dilation)))
self.chomp1 = Chomp2d(padding)
self.relu1 = nn.PReLU()
self.dropout1 = nn.Dropout(dropout)
self.conv2 = weight_norm(nn.Conv2d(n_outputs, n_outputs, (1, kernel_size),
stride=stride, padding=(0, padding), dilation=(1, dilation)))
self.chomp2 = Chomp2d(padding)
self.relu2 = nn.PReLU()
self.dropout2 = nn.Dropout(dropout)
self.net = nn.Sequential(self.conv1, self.chomp1, self.relu1, self.dropout1,
self.conv2, self.chomp2, self.relu2, self.dropout2)
self.downsample = nn.Conv2d(n_inputs, n_outputs, 1) if n_inputs != n_outputs else None
self.relu = nn.PReLU()
self.init_weights()
def init_weights(self):
self.conv1.weight.data.normal_(0, 0.01)
self.conv2.weight.data.normal_(0, 0.01)
if self.downsample is not None:
self.downsample.weight.data.normal_(0, 0.01)
def forward(self, x):
net = self.net(x)
res = x if self.downsample is None else self.downsample(x)
return self.relu(net + res)
class TemporalConvNetPro(nn.Module):
def __init__(self, num_channels, num_eeg_chan=32, freq=6, kernel_size=2, dropout=0.2, early_fusion=True):
super(TemporalConvNetPro, self).__init__()
self.early_fusion = early_fusion
if early_fusion:
self.fusion_layer = weight_norm(nn.Conv2d(
in_channels=num_channels[0], out_channels=num_channels[0],
kernel_size=(num_eeg_chan, 1), stride=(1, 1)
))
else:
self.fusion_layer = nn.Identity()
self.space_aware_temporal_layer = nn.Sequential(
weight_norm(nn.Conv2d(
in_channels=1, out_channels=num_channels[0],
kernel_size=(freq, kernel_size), stride=(freq, 1),
dilation=(1, 2), padding=(0, ((kernel_size - 1) * 2)))),
Chomp2d((kernel_size - 1) * 2),
nn.PReLU(),
nn.Dropout(dropout),
self.fusion_layer
)
layers = []
num_levels = len(num_channels) - 1
for i in range(num_levels):
dilation_size = 2 ** (i+2)
in_channels = num_channels[i] if i == 0 else num_channels[i]
out_channels = num_channels[i+1]
layers += [TemporalBlockPro(in_channels, out_channels, kernel_size, stride=1, dilation=dilation_size,
padding=int((kernel_size - 1) * dilation_size), dropout=dropout)]
self.network = nn.Sequential(*layers)
self.init_weights()
def init_weights(self):
self.space_aware_temporal_layer[0].weight.data.normal_(0, 0.01)
if self.early_fusion:
self.fusion_layer.weight.data.normal_(0, 0.01)
def forward(self, x):
x = self.space_aware_temporal_layer(x)
return self.network(x)
class SpaceAwareTemporalBlock(nn.Module):
def __init__(self, in_channels=1, out_channels=32, num_eeg_chan=32, freq=6, kernel_size=2, dropout=0.2, early_fusion=True):
super(SpaceAwareTemporalBlock, self).__init__()
self.early_fusion = early_fusion
if early_fusion:
self.fusion_layer = weight_norm(nn.Conv2d(
in_channels=out_channels, out_channels=out_channels,
kernel_size=(num_eeg_chan, 1), stride=(1, 1)
))
else:
self.fusion_layer = nn.Identity()
self.space_aware_temporal_layer = nn.Sequential(
weight_norm(nn.Conv2d(
in_channels=in_channels, out_channels=out_channels,
kernel_size=(freq, kernel_size), stride=(freq, 1),
dilation=(1, 2), padding=(0, ((kernel_size - 1) * 2)))),
Chomp2d((kernel_size - 1) * 2),
nn.PReLU(),
nn.Dropout(dropout),
self.fusion_layer
)
self.init_weights()
def forward(self, x):
return self.space_aware_temporal_layer(x)
def init_weights(self):
self.space_aware_temporal_layer[0].weight.data.normal_(0, 0.01)
if self.early_fusion:
self.fusion_layer.weight.data.normal_(0, 0.01)
class TemporalConvNetProM(nn.Module):
def __init__(self, num_channels, num_eeg_chan=32, freq=6, kernel_size=[2, 4, 6], dropout=0.2, early_fusion=True):
super(TemporalConvNetProM, self).__init__()
self.early_fusion = early_fusion
self.sa_tcn_1 = SpaceAwareTemporalBlock(
out_channels=num_channels[0], num_eeg_chan=num_eeg_chan,
freq=freq, kernel_size=kernel_size[0], dropout=dropout, early_fusion=early_fusion)
self.sa_tcn_2 = SpaceAwareTemporalBlock(
out_channels=num_channels[0], num_eeg_chan=num_eeg_chan,
freq=freq, kernel_size=kernel_size[1], dropout=dropout, early_fusion=early_fusion)
self.sa_tcn_3 = SpaceAwareTemporalBlock(
out_channels=num_channels[0], num_eeg_chan=num_eeg_chan,
freq=freq, kernel_size=kernel_size[2], dropout=dropout, early_fusion=early_fusion)
layers = []
num_levels = len(num_channels) - 1
for i in range(num_levels):
dilation_size = 2 ** (i+2)
in_channels = num_channels[i]
out_channels = num_channels[i+1]
layers += [TemporalBlockPro(in_channels, out_channels, kernel_size[1], stride=1, dilation=dilation_size,
padding=int((kernel_size[1] - 1) * dilation_size), dropout=dropout)]
self.OneByOneConv = weight_norm(nn.Conv2d(
in_channels=3*num_channels[0], out_channels=num_channels[0],
kernel_size=(1, 1), stride=(1, 1)
))
self.OneByOneConv.weight.data.normal_(0, 0.01)
self.pure_temporal_layers = nn.Sequential(*layers)
def forward(self, x):
x1 = self.sa_tcn_1(x)
x2 = self.sa_tcn_2(x)
x3 = self.sa_tcn_3(x)
x = torch.cat((x1, x2, x3), dim=1)
x = self.OneByOneConv(x)
return self.pure_temporal_layers(x)