-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
75 lines (62 loc) · 2.82 KB
/
model.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
from temporal_convolutional_layers import TemporalConvNetPro, TemporalConvNetProM
import torch
from torch import nn
import numpy as np
from torch.nn import Module
class Flatten(Module):
def forward(self, input):
return input.view(input.size(0), -1)
class SA_TCN(nn.Module):
def __init__(self, cnn1d_channels=[128, 128, 128], cnn1d_kernel_size=5,
cnn1d_dropout_rate=0.1, num_eeg_chan=32, freq=6, output_dim=1, early_fusion=True, model_type='reg'):
super().__init__()
self.output_dim = output_dim
self.mode = model_type
if self.mode == 'cls':
assert output_dim > 1, "This model support at least binary classification. output_dim should > 1."
self.temporal = TemporalConvNetPro(num_channels=cnn1d_channels, num_eeg_chan=num_eeg_chan, freq=freq,
kernel_size=cnn1d_kernel_size, dropout=cnn1d_dropout_rate,
early_fusion=early_fusion)
self.regressor = nn.Linear(cnn1d_channels[-1], output_dim)
def forward(self, x):
# x: batch, 1, hidden, seq
x = self.temporal(x).transpose(1, 3).contiguous()
x = x.squeeze(-2)
x = self.regressor(x).contiguous()
if self.mode == 'cls':
x = torch.mean(x, dim=1)
return x
class MASA_TCN(nn.Module):
def __init__(self, cnn1d_channels=[128, 128, 128], cnn1d_kernel_size=[3, 5, 15],
cnn1d_dropout_rate=0.1, num_eeg_chan=32, freq=6,
output_dim=1, early_fusion=True, model_type='reg'):
super().__init__()
self.output_dim = output_dim
self.mode = model_type
if self.mode == 'cls':
assert output_dim > 1, "This model support at least binary classification. output_dim should > 1."
self.temporal = TemporalConvNetProM(num_channels=cnn1d_channels, num_eeg_chan=num_eeg_chan, freq=freq,
kernel_size=cnn1d_kernel_size, dropout=cnn1d_dropout_rate,
early_fusion=early_fusion)
self.regressor = nn.Linear(cnn1d_channels[-1], output_dim)
def forward(self, x):
# x: batch, 1, hidden, seq
x = self.temporal(x).transpose(1, 3).contiguous()
x = x.squeeze(-2)
x = self.regressor(x).contiguous()
if self.mode == 'cls':
x = torch.mean(x, dim=1)
return x
if __name__ == "__main__":
data = torch.randn(1, 1, 192, 96)
model = MASA_TCN(
cnn1d_channels=[128, 128, 128],
cnn1d_kernel_size=[3, 5, 15],
cnn1d_dropout_rate=0.1,
num_eeg_chan=32,
freq=6,
output_dim=1,
early_fusion=True,
model_type='reg')
output = model(data)
print("Done")