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
| """ DeltaGateNet: 双向时间动力学EEG疲劳识别 论文: arXiv 2602.14071v3 代码: https://github.com/yip-tin-po-alex/DeltaGateNet
核心: 将EEG一阶差分分解为正(激活)/负(抑制)分量 依赖: torch>=2.0, numpy """
import torch import torch.nn as nn import torch.nn.functional as F from typing import Tuple
class BidirectionalDelta(nn.Module): """ 双向Delta模块 将一阶时间差分 Δx_t = x_t - x_{t-1} 分解为: - 正分量 (激活): Δx⁺ = max(Δx, 0) — 神经激活增强 - 负分量 (抑制): Δx⁻ = min(Δx, 0) — 神经抑制加深 论文Section 3.2: "fatigue presents as slow and irregular temporal variations instead of sudden state transitions" """ def __init__(self, channels: int): super().__init__() self.pos_conv = nn.Conv1d(channels, channels, kernel_size=3, padding=1, groups=channels) self.pos_bn = nn.BatchNorm1d(channels) self.neg_conv = nn.Conv1d(channels, channels, kernel_size=3, padding=1, groups=channels) self.neg_bn = nn.BatchNorm1d(channels) self.fuse = nn.Conv1d(channels * 2, channels, kernel_size=1) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x: (B, C, T) - EEG信号, C通道×T时间点 Returns: out: (B, C, T) - 双向Delta特征 """ delta = torch.diff(x, dim=-1, prepend=x[:, :, :1]) delta_pos = torch.clamp(delta, min=0) delta_neg = torch.clamp(delta, max=0) pos_feat = self.pos_bn(F.relu(self.pos_conv(delta_pos))) neg_feat = self.neg_bn(F.relu(self.neg_conv(delta_neg))) fused = self.fuse(torch.cat([pos_feat, neg_feat], dim=1)) return fused + x
class GatedTemporalConvolution(nn.Module): """ 门控时间卷积模块 使用深度可分离时间卷积捕获每个EEG通道的长期时序依赖 同时通过门控机制保留通道特异性 """ def __init__(self, channels: int, kernel_size: int = 3, dropout: float = 0.2): super().__init__() padding = kernel_size // 2 self.depth_conv = nn.Conv1d( channels, channels, kernel_size, padding=padding, groups=channels ) self.point_conv = nn.Conv1d(channels, channels * 2, kernel_size=1) self.gate = nn.Sigmoid() self.out_conv = nn.Conv1d(channels, channels, kernel_size=1) self.bn = nn.BatchNorm1d(channels) self.dropout = nn.Dropout(dropout) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x: (B, C, T) Returns: out: (B, C, T) """ temp_feat = self.depth_conv(x) point_out = self.point_conv(temp_feat) feat, gate = torch.chunk(point_out, 2, dim=1) gated = feat * self.gate(gate) out = self.out_conv(gated) out = self.bn(out) out = self.dropout(out) return out + x
class DeltaGateNet(nn.Module): """ DeltaGateNet完整模型 输入: 原始EEG时间序列 (无需多阶段滤波) 输出: 疲劳等级分类 架构: Bidirectional Delta → Gated TC → 分类头 论文亮点: "directly ingests segmented time-domain EEG epochs without requiring multi-stage runtime filtering or artifact removal" """ def __init__(self, num_channels: int = 32, num_classes: int = 3, hidden_dim: int = 128, num_blocks: int = 3, dropout: float = 0.2): super().__init__() self.input_proj = nn.Conv1d(num_channels, hidden_dim, kernel_size=1) self.blocks = nn.ModuleList([ nn.Sequential( BidirectionalDelta(hidden_dim), GatedTemporalConvolution(hidden_dim, dropout=dropout) ) for _ in range(num_blocks) ]) self.gap = nn.AdaptiveAvgPool1d(1) self.classifier = nn.Sequential( nn.Linear(hidden_dim, 64), nn.ReLU(), nn.Dropout(dropout), nn.Linear(64, num_classes) ) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x: (B, C, T) - 原始EEG分段 B: batch size C: 通道数 (如32导联) T: 时间点数 (如30秒×200Hz=6000) Returns: logits: (B, num_classes) """ x = self.input_proj(x) for block in self.blocks: x = block(x) x = self.gap(x).squeeze(-1) return self.classifier(x)
if __name__ == "__main__": B, C, T = 16, 32, 6000 model_3class = DeltaGateNet(num_channels=C, num_classes=3) x = torch.randn(B, C, T) logits = model_3class(x) print(f"输入: {x.shape}") print(f"输出: {logits.shape}") print(f"预测: {['清醒', '轻度疲劳', '严重疲劳'][logits.argmax(1)[0].item()]}") total = sum(p.numel() for p in model_3class.parameters()) print(f"参数量: {total:,} ({total/1e3:.1f}K)") model_2class = DeltaGateNet(num_channels=C, num_classes=2) total_2 = sum(p.numel() for p in model_2class.parameters()) print(f"\n2分类模型参数量: {total_2:,}") import time model_3class.eval() with torch.no_grad(): times = [] for _ in range(100): t0 = time.perf_counter() _ = model_3class(x) times.append((time.perf_counter() - t0) * 1000) print(f"\n推理速度:") print(f" 平均: {np.mean(times):.2f}ms") print(f" P95: {np.percentile(times, 95):.2f}ms") print(f" FPS: {1000/np.mean(times):.1f}")
|