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
| """ Modified TSception: EEG脑电信号驾驶员疲劳与认知负荷分析 基于: arXiv 2512.21747 (2025)
TSception 核心思想: - 时间维度: 捕捉EEG信号的时序动态 - 空间维度: 捕捉大脑不同区域的空间关联 - 双流网络: 分别处理后融合 """
import torch import torch.nn as nn import torch.nn.functional as F import numpy as np from typing import Tuple
class TemporalBlock(nn.Module): """时间特征提取块""" def __init__( self, in_channels: int, num_filters: int = 32, kernel_sizes: Tuple[int, ...] = (15, 31, 63) ): super().__init__() self.branches = nn.ModuleList([ nn.Sequential( nn.Conv2d(in_channels, num_filters, (1, k), padding=(0, k//2)), nn.BatchNorm2d(num_filters), nn.ELU(inplace=True), ) for k in kernel_sizes ]) self.fusion = nn.Sequential( nn.Conv2d(num_filters * len(kernel_sizes), num_filters, 1), nn.BatchNorm2d(num_filters), nn.ELU(inplace=True), ) def forward(self, x): outs = [branch(x) for branch in self.branches] out = torch.cat(outs, dim=1) return self.fusion(out)
class SpatialBlock(nn.Module): """空间特征提取块""" def __init__( self, in_channels: int, num_electrodes: int = 32, num_filters: int = 32 ): super().__init__() self.spatial_conv = nn.Sequential( nn.Conv2d(in_channels, num_filters, (num_electrodes, 1)), nn.BatchNorm2d(num_filters), nn.ELU(inplace=True), ) def forward(self, x): return self.spatial_conv(x)
class ModifiedTSception(nn.Module): """ 改进版 TSception 模型 输入: EEG信号 (B, num_electrodes, T) 输出: - 疲劳等级 (0: 清醒, 1: 轻度, 2: 中度, 3: 重度) - 认知负荷 (0: 低, 1: 中, 2: 高) 改进点: 1. 多尺度时间卷积 (15/31/63) 2. 空间注意力机制 3. 双任务输出 (疲劳+认知负荷) 4. 域对抗训练 (个体差异消除) """ def __init__( self, num_electrodes: int = 32, sampling_rate: int = 250, num_classes_drowsy: int = 4, num_classes_workload: int = 3 ): super().__init__() self.num_electrodes = num_electrodes self.temporal = TemporalBlock( in_channels=num_electrodes, num_filters=32, kernel_sizes=(15, 31, 63) ) self.spatial = SpatialBlock( in_channels=32, num_electrodes=1, num_filters=64 ) self.spatial_attn = nn.Sequential( nn.Linear(64, 32), nn.ReLU(inplace=True), nn.Linear(32, 64), nn.Sigmoid() ) self.temporal_attn = nn.Sequential( nn.Linear(64, 32), nn.ReLU(inplace=True), nn.Linear(32, 64), nn.Sigmoid() ) self.drowsy_head = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(64, 128), nn.ELU(inplace=True), nn.Dropout(0.5), nn.Linear(128, num_classes_drowsy) ) self.workload_head = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(64, 128), nn.ELU(inplace=True), nn.Dropout(0.5), nn.Linear(128, num_classes_workload) ) self.domain_head = nn.Sequential( nn.Linear(64, 32), nn.ReLU(inplace=True), nn.Linear(32, 10), nn.LogSoftmax(dim=1) ) def forward(self, x): """ Args: x: EEG信号 (B, num_electrodes, T) B=batch, T=时间步 """ x = x.unsqueeze(-1) t_feat = self.temporal(x) s_feat = self.spatial(t_feat) s_flat = s_feat.view(s_feat.size(0), -1) s_attn = self.spatial_attn(s_flat) s_feat = s_feat * s_attn.view(s_feat.size(0), -1, 1, 1) drowsy_out = self.drowsy_head(s_feat) workload_out = self.workload_head(s_feat) domain_out = self.domain_head(s_feat.view(s_feat.size(0), -1)) return { 'drowsiness': drowsy_out, 'workload': workload_out, 'domain': domain_out }
class TSceptionLoss(nn.Module): """多任务损失 (疲劳+认知负荷+域对抗)""" def __init__(self, alpha=1.0, beta=0.5, gamma=0.1): super().__init__() self.alpha = alpha self.beta = beta self.gamma = gamma def forward(self, outputs, targets): drowsy_loss = F.cross_entropy( outputs['drowsiness'], targets['drowsiness'] ) workload_loss = F.cross_entropy( outputs['workload'], targets['workload'] ) domain_loss = -F.nll_loss( outputs['domain'], targets['domain'] ) total = ( self.alpha * drowsy_loss + self.beta * workload_loss + self.gamma * domain_loss ) return total, { 'drowsy': drowsy_loss.item(), 'workload': workload_loss.item(), 'domain': domain_loss.item() }
if __name__ == "__main__": print("=" * 60) print("Modified TSception EEG 分析") print("=" * 60) model = ModifiedTSception( num_electrodes=32, sampling_rate=250 ) eeg = torch.randn(4, 32, 500) outputs = model(eeg) print(f"输入: {eeg.shape}") print(f"疲劳: {outputs['drowsiness'].shape}") print(f"认知负荷: {outputs['workload'].shape}") print(f"域: {outputs['domain'].shape}") params = sum(p.numel() for p in model.parameters()) print(f"\n参数量: {params:,}") import time times = [] model.eval() with torch.no_grad(): for _ in range(50): t0 = time.time() _ = model(eeg) times.append((time.time() - t0) * 1000) print(f"推理延迟: {np.median(times):.2f}ms") print(f"吞吐率: {1000/np.median(times):.0f} samples/s")
|