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
| """ PressureMesh (MDP-Net) 架构复现
论文: arXiv 2608.09550 核心: Multi-Device Pressure Network 创新: MoE 融合多压力设备数据 → 3D 人体网格
关键模块: 1. 单设备特征提取器(Temporal CNN) 2. MoE 跨设备融合模块 3. 网格回归头 + 关节回归头 """
import torch import torch.nn as nn import torch.nn.functional as F from typing import List, Dict, Tuple
class SingleDeviceEncoder(nn.Module): """ 单设备压力数据编码器 输入: (B, T, H, W) 单个压力垫的时序压力数据 输出: (B, D) 设备特征向量 结构: 2D CNN 提取空间特征 → 1D Conv 提取时序特征 """ def __init__(self, input_channels: int = 1, feat_dim: int = 256): super().__init__() self.spatial_encoder = nn.Sequential( nn.Conv2d(input_channels, 32, kernel_size=3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, kernel_size=3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d((4, 4)), ) self.temporal_encoder = nn.Sequential( nn.Conv1d(128 * 16, 256, kernel_size=3, padding=1), nn.ReLU(), nn.Conv1d(256, feat_dim, kernel_size=3, padding=1), nn.ReLU(), nn.AdaptiveAvgPool1d(1), ) def forward(self, pressure_seq: torch.Tensor) -> torch.Tensor: """ Args: pressure_seq: (B, T, H, W) 时序压力数据 Returns: feat: (B, feat_dim) 设备特征 """ B, T, H, W = pressure_seq.shape frames = pressure_seq.unsqueeze(2) frames = frames.view(B * T, 1, H, W) spatial_feat = self.spatial_encoder(frames) spatial_feat = spatial_feat.view(B, T, -1) temporal_feat = self.temporal_encoder(spatial_feat.transpose(1, 2)) temporal_feat = temporal_feat.squeeze(-1) return temporal_feat
class MoEFusionModule(nn.Module): """ Mixture of Experts 融合模块 论文描述:受 MoE 启发的多模态融合机制, 实现跨设备压力信息的有效互补和增强。 核心思想: - 每个设备是一个 "Expert" - 门控网络根据输入动态分配权重 - 不同坐姿/姿态下不同设备贡献度不同 """ def __init__(self, num_devices: int, feat_dim: int = 256): super().__init__() self.num_devices = num_devices self.feat_dim = feat_dim self.gate = nn.Sequential( nn.Linear(feat_dim * num_devices, 512), nn.ReLU(), nn.Linear(512, num_devices), nn.Softmax(dim=1) ) self.fusion = nn.Sequential( nn.Linear(feat_dim, 512), nn.ReLU(), nn.Dropout(0.2), nn.Linear(512, feat_dim), nn.ReLU() ) def forward(self, device_features: List[torch.Tensor]) -> torch.Tensor: """ Args: device_features: 每个设备的特征 (B, feat_dim) Returns: fused: 融合后的特征 (B, feat_dim) """ B = device_features[0].shape[0] concat_feat = torch.cat(device_features, dim=1) gate_weights = self.gate(concat_feat) stacked = torch.stack(device_features, dim=1) weighted = stacked * gate_weights.unsqueeze(-1) aggregated = weighted.sum(dim=1) fused = self.fusion(aggregated) return fused
class MeshRegressionHead(nn.Module): """ 3D 网格回归头 从融合特征回归: - 3D 人体网格顶点 (n_verts, 3) - 3D 关节位置 (n_joints, 3) - 2D 关节投影 (n_joints, 2) """ def __init__(self, feat_dim: int = 256, n_verts: int = 6890, n_joints: int = 24): super().__init__() self.mesh_head = nn.Sequential( nn.Linear(feat_dim, 1024), nn.ReLU(), nn.Dropout(0.3), nn.Linear(1024, 2048), nn.ReLU(), nn.Linear(2048, n_verts * 3) ) self.joint_3d_head = nn.Sequential( nn.Linear(feat_dim, 512), nn.ReLU(), nn.Linear(512, n_joints * 3) ) self.joint_2d_head = nn.Sequential( nn.Linear(feat_dim, 256), nn.ReLU(), nn.Linear(256, n_joints * 2) ) def forward(self, feat: torch.Tensor) -> Dict[str, torch.Tensor]: return { 'mesh': self.mesh_head(feat).view(-1, 6890, 3), 'joints_3d': self.joint_3d_head(feat).view(-1, 24, 3), 'joints_2d': self.joint_2d_head(feat).view(-1, 24, 2), }
class MDPNet(nn.Module): """ 完整的 MDP-Net (Multi-Device Pressure Network) 输入: 多个压力设备的时序压力数据 输出: 3D 人体网格 + 关节位置 性能: 关节位置误差 12.6cm (MDP 数据集) 应用场景: - 车辆座椅 OOP 检测 - 康复评估 - 人机交互 """ def __init__(self, num_devices: int = 3, pressure_h: int = 32, pressure_w: int = 32): super().__init__() self.num_devices = num_devices self.encoders = nn.ModuleList([ SingleDeviceEncoder() for _ in range(num_devices) ]) self.fusion = MoEFusionModule(num_devices=num_devices) self.regression = MeshRegressionHead() def forward(self, pressure_inputs: List[torch.Tensor]) -> Dict[str, torch.Tensor]: """ Args: pressure_inputs: 每个设备的 (B, T, H, W) 压力序列 Returns: Dict with 'mesh', 'joints_3d', 'joints_2d' """ device_feats = [enc(inp) for enc, inp in zip(self.encoders, pressure_inputs)] fused_feat = self.fusion(device_feats) outputs = self.regression(fused_feat) return outputs
if __name__ == "__main__": import numpy as np import time num_devices = 3 batch_size = 1 time_steps = 30 pressure_h, pressure_w = 32, 32 model = MDPNet(num_devices=num_devices) model.eval() inputs = [torch.randn(batch_size, time_steps, pressure_h, pressure_w) for _ in range(num_devices)] start = time.time() with torch.no_grad(): outputs = model(inputs) infer_time = (time.time() - start) * 1000 print("=" * 60) print("MDP-Net (PressureMesh) 测试结果") print("=" * 60) print(f"输入设备数: {num_devices}") print(f"每设备输入: ({batch_size}, {time_steps}, {pressure_h}, {pressure_w})") print(f"输出网格顶点数: {outputs['mesh'].shape[1]}") print(f"输出 3D 关节数: {outputs['joints_3d'].shape[1]}") print(f"输出 2D 关节数: {outputs['joints_2d'].shape[1]}") print(f"\n推理时间: {infer_time:.1f} ms") total_params = sum(p.numel() for p in model.parameters()) print(f"模型参数量: {total_params:,}") print("\n" + "=" * 60) print("OOP 检测模拟") print("=" * 60) normal_joints = outputs['joints_3d'][0] hip_y = normal_joints[1, 1].item() shoulder_y = normal_joints[15, 1].item() torso_angle = np.degrees(np.arctan2( normal_joints[15, 0].item() - normal_joints[1, 0].item(), abs(shoulder_y - hip_y) )) print(f"躯干前倾角度: {torso_angle:.1f}°") if torso_angle > 25: print("⚠️ OOP 警告:驾驶员前倾异常") else: print("✅ 正常坐姿")
|