forked from ycq091044/BIOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiot.py
217 lines (185 loc) · 6.88 KB
/
biot.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
import time
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from linear_attention_transformer import LinearAttentionTransformer
class PatchFrequencyEmbedding(nn.Module):
def __init__(self, emb_size=256, n_freq=101):
super().__init__()
self.projection = nn.Linear(n_freq, emb_size)
def forward(self, x):
"""
x: (batch, 1, freq, time)
out: (batch, time, emb_size)
"""
b, _, _, _ = x.shape
x = x.squeeze(1).permute(0, 2, 1)
x = self.projection(x)
return x
class ClassificationHead(nn.Sequential):
def __init__(self, emb_size, n_classes):
super().__init__()
self.clshead = nn.Sequential(
nn.ELU(),
nn.Linear(emb_size, n_classes),
)
def forward(self, x):
out = self.clshead(x)
return out
class PositionalEncoding(nn.Module):
def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 1000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
# Compute the positional encodings once in log space.
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len).unsqueeze(1).float()
div_term = torch.exp(
torch.arange(0, d_model, 2).float() * -(math.log(10000.0) / d_model)
)
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0)
self.register_buffer("pe", pe)
def forward(self, x: torch.FloatTensor) -> torch.FloatTensor:
"""
Args:
x: `embeddings`, shape (batch, max_len, d_model)
Returns:
`encoder input`, shape (batch, max_len, d_model)
"""
x = x + self.pe[:, : x.size(1)]
return self.dropout(x)
class BIOTEncoder(nn.Module):
def __init__(
self,
emb_size=256,
heads=8,
depth=4,
n_channels=16,
n_fft=200,
hop_length=100,
**kwargs
):
super().__init__()
self.n_fft = n_fft
self.hop_length = hop_length
self.patch_embedding = PatchFrequencyEmbedding(
emb_size=emb_size, n_freq=self.n_fft // 2 + 1
)
self.transformer = LinearAttentionTransformer(
dim=emb_size,
heads=heads,
depth=depth,
max_seq_len=1024,
attn_layer_dropout=0.2, # dropout right after self-attention layer
attn_dropout=0.2, # dropout post-attention
)
self.positional_encoding = PositionalEncoding(emb_size)
# channel token, N_channels >= your actual channels
self.channel_tokens = nn.Embedding(n_channels, 256)
self.index = nn.Parameter(
torch.LongTensor(range(n_channels)), requires_grad=False
)
def stft(self, sample):
signal = []
for s in range(sample.shape[1]):
spectral = torch.stft(
sample[:, s, :],
n_fft=self.n_fft,
hop_length=self.hop_length,
normalized=False,
center=False,
onesided=True,
return_complex=True,
)
signal.append(spectral)
stacked = torch.stack(signal).permute(1, 0, 2, 3)
return torch.abs(stacked)
def forward(self, x, n_channel_offset=0, perturb=False):
"""
x: [batch_size, channel, ts]
output: [batch_size, emb_size]
"""
emb_seq = []
for i in range(x.shape[1]):
channel_spec_emb = self.stft(x[:, i : i + 1, :])
channel_spec_emb = self.patch_embedding(channel_spec_emb)
batch_size, ts, _ = channel_spec_emb.shape
# (batch_size, ts, emb)
channel_token_emb = (
self.channel_tokens(self.index[i + n_channel_offset])
.unsqueeze(0)
.unsqueeze(0)
.repeat(batch_size, ts, 1)
)
# (batch_size, ts, emb)
channel_emb = self.positional_encoding(channel_spec_emb + channel_token_emb)
# perturb
if perturb:
ts = channel_emb.shape[1]
ts_new = np.random.randint(ts // 2, ts)
selected_ts = np.random.choice(range(ts), ts_new, replace=False)
channel_emb = channel_emb[:, selected_ts]
emb_seq.append(channel_emb)
# (batch_size, 16 * ts, emb)
emb = torch.cat(emb_seq, dim=1)
# (batch_size, emb)
emb = self.transformer(emb).mean(dim=1)
return emb
# supervised classifier module
class BIOTClassifier(nn.Module):
def __init__(self, emb_size=256, heads=8, depth=4, n_classes=6, **kwargs):
super().__init__()
self.biot = BIOTEncoder(emb_size=emb_size, heads=heads, depth=depth, **kwargs)
self.classifier = ClassificationHead(emb_size, n_classes)
def forward(self, x):
x = self.biot(x)
x = self.classifier(x)
return x
# unsupervised pre-train module
class UnsupervisedPretrain(nn.Module):
def __init__(self, emb_size=256, heads=8, depth=4, n_channels=18, **kwargs):
super(UnsupervisedPretrain, self).__init__()
self.biot = BIOTEncoder(emb_size, heads, depth, n_channels, **kwargs)
self.prediction = nn.Sequential(
nn.Linear(256, 256),
nn.GELU(),
nn.Linear(256, 256),
)
def forward(self, x, n_channel_offset=0):
emb = self.biot(x, n_channel_offset, perturb=True)
emb = self.prediction(emb)
pred_emb = self.biot(x, n_channel_offset)
return emb, pred_emb
# supervised pre-train module
class SupervisedPretrain(nn.Module):
def __init__(self, emb_size=256, heads=8, depth=4, **kwargs):
super().__init__()
self.biot = BIOTEncoder(emb_size=emb_size, heads=heads, depth=depth)
self.classifier_chb_mit = ClassificationHead(emb_size, 1)
self.classifier_iiic_seizure = ClassificationHead(emb_size, 6)
self.classifier_tuab = ClassificationHead(emb_size, 1)
self.classifier_tuev = ClassificationHead(emb_size, 6)
def forward(self, x, task="chb-mit"):
x = self.biot(x)
if task == "chb-mit":
x = self.classifier_chb_mit(x)
elif task == "iiic-seizure":
x = self.classifier_iiic_seizure(x)
elif task == "tuab":
x = self.classifier_tuab(x)
elif task == "tuev":
x = self.classifier_tuev(x)
else:
raise NotImplementedError
return x
if __name__ == "__main__":
x = torch.randn(16, 2, 2000)
model = BIOTClassifier(n_fft=200, hop_length=200, depth=4, heads=8)
out = model(x)
print(out.shape)
model = UnsupervisedPretrain(n_fft=200, hop_length=200, depth=4, heads=8)
out1, out2 = model(x)
print(out1.shape, out2.shape)