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
| import torch import torch.nn as nn from typing import Tuple, Dict import numpy as np
class TextToMmWavePipeline: """ mmExpert 文本→mmWave 合成管线 流程: 1. LLM 生成场景描述(动作、环境、上下文) 2. 文本→3D动作序列(预训练运动模型) 3. SMPL 人体模型拟合 4. 电磁仿真生成雷达信号 5. 域随机化增强泛化 """ def __init__(self): self.scenario_generator = LLMScenarioGenerator() self.motion_synthesizer = MotionSynthesizer() self.smpl_model = SMPLModel() self.rf_simulator = RFSignalSimulator() self.domain_randomizer = DomainRandomizer() def generate( self, prompt: str, num_samples: int = 100, ) -> Dict: """ 从文本提示生成 mmWave 合成数据 Args: prompt: "一个孩子在后座睡觉" num_samples: 生成样本数 Returns: data: { 'mmwave_signals': (N, T, F) 多普勒频谱 'text_labels': [str] 文本标签 'motion_params': SMPL参数 } Example: >>> pipe = TextToMmWavePipeline() >>> data = pipe.generate("child sleeping in rear seat", 50) >>> print(f"生成 {len(data['mmwave_signals'])} 个样本") """ descriptions = self.scenario_generator.generate(prompt, num_samples) motions = self.motion_synthesizer.synthesize(descriptions) smpl_params = self.smpl_model.fit(motions) raw_signals = self.rf_simulator.simulate(smpl_params) augmented = self.domain_randomizer.augment(raw_signals) return { 'mmwave_signals': augmented, 'text_labels': descriptions, 'motion_params': smpl_params, }
class LLMScenarioGenerator(nn.Module): """使用 LLM 生成多样化场景描述""" def __init__(self): super().__init__() self.prompt_template = """ Generate {n} diverse descriptions of: "{base_prompt}" Include variations in: - Body posture (sitting, lying, leaning) - Movement speed (slow, normal, fast) - Environment (vehicle type, seat position) - Clothing (winter coat, summer clothes) Each description should be unique and realistic. """ def generate(self, base_prompt: str, n: int) -> list: """生成 n 个多样化描述""" descriptions = [ f"{base_prompt} - variant {i}: {self._add_variation(base_prompt, i)}" for i in range(n) ] return descriptions def _add_variation(self, base: str, idx: int) -> str: variations = [ "slow breathing, relaxed posture", "occasional limb movement", "covered with blanket", "sitting upright with head tilted", "lying across rear seat", "curled fetal position", ] return variations[idx % len(variations)]
class RFSignalSimulator: """ RF 电磁仿真器 基于论文描述的物理模型: - 路径损耗 (path loss) - 天线损耗 (antenna loss) - 散射损耗 (scattering loss) """ def __init__(self): self.c = 3e8 self.fc = 60e9 def simulate( self, smpl_params: list, ) -> np.ndarray: """ 仿真 SMPL 人体模型的雷达回波 Args: smpl_params: SMPL 参数列表 [(vertices, faces), ...] Returns: signals: (N, T, F) 多普勒频谱 N: 样本数 T: 时间帧 F: 频率bin """ N = len(smpl_params) T = 120 F = 64 signals = np.zeros((N, T, F), dtype=np.float32) for i, (vertices, _) in enumerate(smpl_params): rcs = self._compute_rcs(vertices) for t in range(T): velocities = self._estimate_vertex_velocity(vertices, t) doppler_shifts = 2 * velocities * self.fc / self.c spectrum = np.zeros(F, dtype=np.float32) for v_idx, shift in enumerate(doppler_shifts): bin_idx = int((shift + F/2) % F) spectrum[bin_idx] += rcs[v_idx] signals[i, t] = spectrum return signals def _compute_rcs(self, vertices: np.ndarray) -> np.ndarray: """计算每个顶点的雷达截面(RCS)""" n_verts = vertices.shape[0] rcs = np.random.uniform(0.001, 0.01, n_verts) return rcs def _estimate_velocity(self, vertices: np.ndarray, t: int) -> np.ndarray: """估计顶点速度""" return np.random.normal(0, 0.1, vertices.shape[0])
class DomainRandomizer: """ Sim-to-Real 域随机化 论文关键组件:使合成数据训练的模型能零样本迁移到真实数据 """ def __init__(self): self.randomize_params = { 'radar_view': True, 'body_segment': True, 'antenna_pattern': True, 'background_noise': True, 'nonlinear_scaling': True, } def augment(self, signals: np.ndarray) -> np.ndarray: """ 域随机化增强 Args: signals: (N, T, F) 原始信号 Returns: augmented: (N, T, F) 增强后信号 """ N, T, F = signals.shape augmented = signals.copy() if self.randomize_params['radar_view']: view_shift = np.random.randint(-3, 4, N) for i in range(N): augmented[i] = np.roll(augmented[i], view_shift[i], axis=1) if self.randomize_params['antenna_pattern']: antenna_gain = np.random.uniform(0.7, 1.3, (N, 1, 1)) augmented *= antenna_gain if self.randomize_params['background_noise']: noise = np.random.normal(0, 0.01, augmented.shape) augmented += noise if self.randomize_params['nonlinear_scaling']: scale = np.random.uniform(0.8, 1.2, (N, 1, 1)) augmented = augmented * scale + np.tanh(augmented) * (1 - scale) if self.randomize_params['background_noise']: multipath = np.random.uniform(0, 0.05, augmented.shape) augmented = augmented + multipath * np.roll(augmented, 1, axis=1) return augmented.astype(np.float32)
if __name__ == "__main__": pipe = TextToMmWavePipeline() data = pipe.generate("child sleeping in rear seat", 50) print(f"生成样本数: {len(data['mmwave_signals'])}") print(f"信号维度: {data['mmwave_signals'].shape}") print(f"文本标签示例: {data['text_labels'][0]}") print(f"\n域随机化后范围: [{data['mmwave_signals'].min():.3f}, {data['mmwave_signals'].max():.3f}]")
|