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
| """ DriveEmo-FL: mmWave雷达座舱情绪感知 + 联邦学习 论文: Scientific Reports 16, 13600 (2026)
硬件: TI IWR-1443BOOST (77GHz FMCW雷达) 架构: EmoNet双流网络 + 联邦学习
依赖: torch, numpy, scipy """
import torch import torch.nn as nn import numpy as np from typing import Dict, List, Tuple
class EmoNetDualStream(nn.Module): """ EmoNet: 双流情绪分类网络 Stream 1: 微多普勒谱图 → CNN Stream 2: 速度时间特征(VTP) → BiLSTM 融合: 两流特征拼接 → 分类器 输出: 4类情绪 (高兴/悲伤/愤怒/中性) """ def __init__(self, n_classes=4): super().__init__() self.doppler_cnn = nn.Sequential( nn.Conv2d(1, 32, 3, stride=1, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, stride=1, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, stride=1, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d((1, 1)), ) self.vtp_lstm = nn.LSTM( input_size=3, hidden_size=128, num_layers=2, batch_first=True, bidirectional=True, ) self.classifier = nn.Sequential( nn.Linear(128 + 256, 128), nn.ReLU(), nn.Dropout(0.5), nn.Linear(128, n_classes) ) def forward(self, doppler_spec, vtp_features): """ Args: doppler_spec: (B, 1, H, W) 微多普勒谱图 vtp_features: (B, T, 3) 速度时间特征 Returns: logits: (B, 4) 情绪分类 """ doppler_feat = self.doppler_cnn(doppler_spec) doppler_feat = doppler_feat.flatten(1) vtp_feat, _ = self.vtp_lstm(vtp_features) vtp_feat = vtp_feat[:, -1, :] fused = torch.cat([doppler_feat, vtp_feat], dim=1) logits = self.classifier(fused) return logits
class MicroDopplerExtractor: """ 从FMCW雷达原始数据提取微多普勒特征 处理流程: 1. Range-FFT: 分离不同距离的目标 2. 选择目标距离bin (乘员位置) 3. 时间维FFT: 多普勒谱图 4. STFT: 微多普勒时频图 """ def __init__(self, n_range_bins=64, n_doppler_bins=64, stft_window=64, hop_length=16): self.n_range_bins = n_range_bins self.n_doppler_bins = n_doppler_bins self.stft_window = stft_window self.hop_length = hop_length def extract(self, raw_adc_data, target_range_bin=None): """ 从ADC原始数据提取微多普勒谱图 Args: raw_adc_data: (n_chirps, n_samples) 复数ADC数据 target_range_bin: 目标距离bin (如已知) Returns: doppler_spec: (H, W) 微多普勒谱图 """ range_profile = np.fft.fft(raw_adc_data, axis=1) if target_range_bin is None: target_range_bin = np.argmax( np.mean(np.abs(range_profile), axis=0) ) target_signal = range_profile[:, target_range_bin] from scipy.signal import stft f, t, Zxx = stft( target_signal, nperseg=self.stft_window, noverlap=self.stft_window - self.hop_length ) doppler_spec = np.abs(Zxx) doppler_spec = (doppler_spec - doppler_spec.min()) / \ (doppler_spec.max() - doppler_spec.min() + 1e-8) return doppler_spec
class VelocityTimeProfiler: """ 速度时间特征(VTP)提取器 从雷达数据提取: - 瞬时速度 (多普勒频移) - 加速度 (速度变化率) - 敏捷度 (加速度变化率) """ def __init__(self, fs=100): self.fs = fs def extract(self, range_profile, target_bin): """ 从距离bin提取速度时间特征 Returns: vtp: (T, 3) [velocity, acceleration, agility] """ phase = np.unwrap(np.angle(range_profile[:, target_bin])) wavelength = 3e8 / 77e9 velocity = np.diff(phase) * wavelength / (4 * np.pi) * self.fs acceleration = np.diff(velocity) * self.fs agility = np.diff(acceleration) * self.fs min_len = min(len(velocity), len(acceleration), len(agility)) vtp = np.stack([ velocity[:min_len], acceleration[:min_len], agility[:min_len] ], axis=1) return vtp
class FederatedEmotionLearning: """ DriveEmo-FL 联邦学习框架 每辆车作为独立客户端: 1. 本地收集雷达数据 + 情绪标注 2. 本地训练EmoNet 3. 仅上传模型梯度到服务器 4. 服务器聚合梯度 → 更新全局模型 5. 下发新全局模型到所有车辆 优势: - 隐私: 原始数据不离开车辆 - 可扩展: 车队规模可扩展 - 个性化: 每车可微调 """ def __init__(self, n_clients=10, n_rounds=50): self.n_clients = n_clients self.n_rounds = n_rounds self.global_model = EmoNetDualStream() def federated_averaging(self, client_models, client_weights): """ FedAvg: 加权平均客户端模型参数 Args: client_models: list of state_dicts client_weights: list of float (数据量权重) """ global_state = self.global_model.state_dict() for key in global_state: weighted_sum = sum( w * model[key] for w, model in zip(client_weights, client_models) ) global_state[key] = weighted_sum / sum(client_weights) self.global_model.load_state_dict(global_state) def simulate_federated_round(self): """模拟一轮联邦学习""" client_accuracies = [] for client_id in range(self.n_clients): n_samples = np.random.randint(50, 200) local_acc = 0.75 + 0.15 * np.random.rand() client_accuracies.append(local_acc) weights = np.random.dirichlet(np.ones(self.n_clients)) self.federated_averaging( [self.global_model.state_dict()] * self.n_clients, weights ) return np.mean(client_accuracies)
if __name__ == "__main__": print("=" * 60) print("DriveEmo-FL: mmWave雷达座舱情绪感知") print("=" * 60) model = EmoNetDualStream(n_classes=4) doppler = torch.randn(2, 1, 64, 64) vtp = torch.randn(2, 100, 3) logits = model(doppler, vtp) print(f"\nEmoNet输出:") print(f" 输入: doppler={doppler.shape}, vtp={vtp.shape}") print(f" 输出: {logits.shape} (4类情绪)") emotions = ['高兴', '悲伤', '愤怒', '中性'] for i, (s, l) in enumerate(zip(logits, emotions)): pred = emotions[s.argmax()] print(f" 样本{i}: 预测={pred}") params = sum(p.numel() for p in model.parameters()) print(f"\n模型参数量: {params/1e6:.2f}M") extractor = MicroDopplerExtractor() raw_adc = np.random.randn(200, 64) + 1j * np.random.randn(200, 64) spec = extractor.extract(raw_adc) print(f"\n微多普勒谱图: {spec.shape}") fl = FederatedEmotionLearning(n_clients=10, n_rounds=50) print(f"\n联邦学习模拟 (10车辆, 50轮):") for round_idx in [1, 10, 25, 50]: acc = fl.simulate_federated_round() print(f" Round {round_idx:2d}: 平均准确率 {acc:.1%}") print(f"\n情绪→AV自适应响应映射:") av_responses = { '高兴': '舒适驾驶模式, 播放喜欢的音乐', '悲伤': '安静驾驶模式, 暖色调灯光, 减速', '愤怒': '平稳驾驶模式, 冷色调灯光, 避免急加速', '中性': '标准驾驶模式', } for emotion, response in av_responses.items(): print(f" {emotion} → {response}")
|