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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
| import torch import torch.nn as nn import torch.nn.functional as F import numpy as np
class NeurophysiologyGuidedGrouping: """ 神经生理引导的EEG通道分组 基于脑科学先验将17通道分为3组: 1. 前额认知组(Fp1, Fp2, F3, F4, Fz)→ 执行控制/注意力 2. 中央运动组(C3, Cz, C4, T7, T8)→ 运动准备/体感 3. 后部感知组(Pz, O1, O2, P3, P4, Oz, P7)→ 视觉处理/疲劳 论文Section 2.1: Channel Grouping Strategy """ GROUPS = { 'prefrontal': ['Fp1', 'Fp2', 'F3', 'F4', 'Fz'], 'central': ['C3', 'Cz', 'C4', 'T7', 'T8'], 'posterior': ['Pz', 'O1', 'O2', 'P3', 'P4', 'Oz', 'P7'] } def __init__(self): self.channel_indices = self._build_indices() def _build_indices(self): """构建通道索引映射""" all_channels = ['Fp1', 'Fp2', 'F3', 'F4', 'Fz', 'C3', 'Cz', 'C4', 'T7', 'T8', 'Pz', 'O1', 'O2', 'P3', 'P4', 'Oz', 'P7'] indices = {} for group_name, channels in self.GROUPS.items(): indices[group_name] = [all_channels.index(ch) for ch in channels if ch in all_channels] return indices def group(self, eeg: torch.Tensor) -> dict: """ Args: eeg: (B, 17, T) 17通道EEG信号 Returns: dict: { 'prefrontal': (B, 5, T), 'central': (B, 5, T), 'posterior': (B, 7, T) } """ return { name: eeg[:, idx, :] for name, idx in self.channel_indices.items() }
class SelectiveSSM(nn.Module): """ 选择性状态空间模型(Mamba架构) 核心改进:输入自适应的选择机制 - 传统SSM: 固定参数A, B, C, D - Mamba SSM: A(x), B(x), C(x), D(x) 随输入动态变化 论文Section 2.2: Tri-Stream Selective State Space Args: d_model: 模型维度 d_state: 状态空间维度 d_conv: 卷积核大小 expand: 扩展因子 """ def __init__(self, d_model: int = 64, d_state: int = 16, d_conv: int = 4, expand: int = 2): super().__init__() self.d_model = d_model self.d_state = d_state self.d_conv = d_conv self.expand = expand self.d_inner = int(d_model * expand) self.in_proj = nn.Linear(d_model, self.d_inner * 2, bias=False) self.conv1d = nn.Conv1d( self.d_inner, self.d_inner, kernel_size=d_conv, padding=d_conv - 1, groups=self.d_inner, bias=True ) self.x_proj = nn.Linear( self.d_inner, d_state * 2 + self.d_inner, bias=False ) self.dt_proj = nn.Linear(d_state, self.d_inner, bias=True) self.A_log = nn.Parameter( torch.randn(self.d_inner, d_state) * 0.01 ) self.D = nn.Parameter(torch.ones(self.d_inner)) self.out_proj = nn.Linear(self.d_inner, d_model, bias=False) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x: (B, L, d_model) 输入序列 Returns: out: (B, L, d_model) 输出序列 """ B, L, _ = x.shape x_proj = self.in_proj(x) x1, x2 = x_proj.chunk(2, dim=-1) x1 = x1.transpose(1, 2) x1 = self.conv1d(x1)[:, :, :L] x1 = F.silu(x1) x1 = x1.transpose(1, 2) x_ssm = self.x_proj(x1) B_param = x_ssm[:, :, :self.d_state] C_param = x_ssm[:, :, self.d_state:2*self.d_state] delta = F.softplus(self.dt_proj( x_ssm[:, :, 2*self.d_state:] )) A = -torch.exp(self.A_log) A_discrete = torch.exp(delta.unsqueeze(-1) * A) B_discrete = delta.unsqueeze(-1) * B_param.unsqueeze(2) h = torch.zeros(B, self.d_inner, self.d_state, device=x.device) outputs = [] for t in range(L): h = A_discrete[:, t] * h + B_discrete[:, t] * x1[:, t].unsqueeze(-1) y = (h * C_param[:, t].unsqueeze(1)).sum(-1) outputs.append(y) y = torch.stack(outputs, dim=1) y = y + x1 * self.D y = y * F.silu(x2) out = self.out_proj(y) return out
class TriStreamNTSM(nn.Module): """ NTSM: 神经生理引导的三流选择性状态空间模型 三流: Stream 1: EEG时域(通道分组 → 各组Mamba → 融合) Stream 2: EOG时域(眼动特征 → Mamba) Stream 3: 频域特征(δ/θ/α/β/γ → Mamba) 论文Section 2: Full Architecture Args: eeg_channels: EEG通道数 (SEED-VIG: 17) eog_channels: EOG通道数 (2: 水平+垂直) d_model: 模型维度 d_state: SSM状态维度 num_classes: 分类数 (3: 清醒/轻度/重度) """ def __init__(self, eeg_channels: int = 17, eog_channels: int = 2, d_model: int = 64, d_state: int = 16, num_classes: int = 3): super().__init__() self.grouper = NeurophysiologyGuidedGrouping() self.eeg_embeds = nn.ModuleDict({ name: nn.Linear(len(idx), d_model) for name, idx in self.grouper.channel_indices.items() }) self.eeg_ssms = nn.ModuleDict({ name: SelectiveSSM(d_model, d_state) for name in self.grouper.channel_indices }) self.eog_embed = nn.Linear(eog_channels, d_model) self.eog_ssm = SelectiveSSM(d_model, d_state) self.freq_embed = nn.Linear(5 * eeg_channels, d_model) self.freq_ssm = SelectiveSSM(d_model, d_state) self.cross_attn = nn.MultiheadAttention( d_model, num_heads=4, batch_first=True ) self.classifier = nn.Sequential( nn.LayerNorm(d_model), nn.Linear(d_model, 32), nn.GELU(), nn.Dropout(0.2), nn.Linear(32, num_classes) ) def forward(self, eeg: torch.Tensor, eog: torch.Tensor, freq: torch.Tensor) -> torch.Tensor: """ Args: eeg: (B, 17, L) EEG时域信号 eog: (B, 2, L) EOG时域信号 freq: (B, 17*5, L) 频域特征(5频段×17通道) Returns: logits: (B, num_classes) """ groups = self.grouper.group(eeg) eeg_features = [] for name, g in groups.items(): g = g.transpose(1, 2) g = self.eeg_embeds[name](g) g = self.eeg_ssms[name](g) eeg_features.append(g.mean(dim=1)) eeg_feat = torch.stack(eeg_features, dim=1) eog_in = eog.transpose(1, 2) eog_in = self.eog_embed(eog_in) eog_out = self.eog_ssm(eog_in) eog_feat = eog_out.mean(dim=1).unsqueeze(1) freq_in = freq.transpose(1, 2) freq_in = self.freq_embed(freq_in) freq_out = self.freq_ssm(freq_in) freq_feat = freq_out.mean(dim=1).unsqueeze(1) all_features = torch.cat([eeg_feat, eog_feat, freq_feat], dim=1) attn_out, _ = self.cross_attn(all_features, all_features, all_features) fused = attn_out.mean(dim=1) return self.classifier(fused)
if __name__ == "__main__": import time from sklearn.metrics import accuracy_score EEG_CHANNELS = 17 EOG_CHANNELS = 2 SEQ_LEN = 1200 NUM_CLASSES = 3 model = TriStreamNTSM( eeg_channels=EEG_CHANNELS, eog_channels=EOG_CHANNELS, d_model=64, d_state=16, num_classes=NUM_CLASSES ) total_params = sum(p.numel() for p in model.parameters()) print(f"NTSM参数量: {total_params:,}") np.random.seed(42) def generate_eeg_eog(state: str, batch=1): """根据疲劳状态生成模拟EEG+EOG""" t = np.linspace(0, 300, SEQ_LEN) if state == 'alert': eeg = np.random.randn(batch, EEG_CHANNELS, SEQ_LEN) * 5 eeg += 3 * np.sin(2*np.pi*25*t + np.random.rand()) eog = np.random.randn(batch, EOG_CHANNELS, SEQ_LEN) * 20 eog += 50 * (np.random.rand(batch, EOG_CHANNELS, SEQ_LEN) > 0.95) elif state == 'mild': eeg = np.random.randn(batch, EEG_CHANNELS, SEQ_LEN) * 5 eeg += 8 * np.sin(2*np.pi*10*t) eog = np.random.randn(batch, EOG_CHANNELS, SEQ_LEN) * 10 eog += 30 * (np.random.rand(batch, EOG_CHANNELS, SEQ_LEN) > 0.98) else: eeg = np.random.randn(batch, EEG_CHANNELS, SEQ_LEN) * 3 eeg += 15 * np.sin(2*np.pi*4*t) eeg += 10 * np.sin(2*np.pi*2*t) eog = np.random.randn(batch, EOG_CHANNELS, SEQ_LEN) * 5 eog += 20 * np.sin(2*np.pi*0.5*t) freq = np.random.rand(batch, EEG_CHANNELS * 5, SEQ_LEN).astype(np.float32) if state == 'alert': freq[:, ::5, :] *= 2.0 elif state == 'mild': freq[:, 1::5, :] *= 2.0 else: freq[:, 3::5, :] *= 2.0 return (torch.tensor(eeg, dtype=torch.float32), torch.tensor(eog, dtype=torch.float32), torch.tensor(freq, dtype=torch.float32)) print("\n--- 生成模拟数据 ---") states = ['alert', 'mild', 'severe'] all_eeg, all_eog, all_freq, all_labels = [], [], [], [] for i, state in enumerate(states): eeg, eog, freq = generate_eeg_eog(state, batch=10) all_eeg.append(eeg) all_eog.append(eog) all_freq.append(freq) all_labels.extend([i] * 10) eeg_batch = torch.cat(all_eeg) eog_batch = torch.cat(all_eog) freq_batch = torch.cat(all_freq) labels = torch.tensor(all_labels) print(f"EEG: {eeg_batch.shape}") print(f"EOG: {eog_batch.shape}") print(f"Freq: {freq_batch.shape}") print("\n--- 训练流程 ---") criterion = nn.CrossEntropyLoss() optimizer = torch.optim.AdamW(model.parameters(), lr=2e-4, weight_decay=0.01) scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=50) model.train() for epoch in range(5): optimizer.zero_grad() logits = model(eeg_batch, eog_batch, freq_batch) loss = criterion(logits, labels) loss.backward() optimizer.step() scheduler.step() acc = accuracy_score(labels.numpy(), torch.argmax(logits, dim=1).detach().numpy()) print(f"Epoch {epoch+1}: Loss={loss.item():.4f}, Acc={acc:.2%}, LR={scheduler.get_last_lr()[0]:.6f}") print("\n--- 评估 ---") model.eval() with torch.no_grad(): logits = model(eeg_batch, eog_batch, freq_batch) preds = torch.argmax(logits, dim=1) for i, state in enumerate(states): mask = (labels == i) acc = (preds[mask] == i).float().mean() print(f" {state:8s}: Acc={acc:.2%} ({mask.sum().item()} samples)") with torch.no_grad(): start = time.time() for _ in range(20): _ = model(eeg_batch[:1], eog_batch[:1], freq_batch[:1]) elapsed = (time.time() - start) / 20 print(f"\n单样本推理延迟: {elapsed*1000:.1f}ms") print(f"理论帧率: {1/elapsed:.0f} FPS") print(f"复杂度: O(n) 线性序列建模")
|