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 306 307
| """ SoundMHPE: 声学多人 3D 姿态估计 论文复现: arXiv 2609.04902 (ECCV 2026)
核心架构: 1. Acoustic Multi-scale Encoder: 多尺度时频特征提取 2. Temporal Pose Decoder: 时序注意力解耦多人姿态
数据集: AMP Dataset - 432,000 同步帧 - 6小时多人生标 + 声学数据 - 2-3人/场景 - 17个身体关节点 × 3D坐标 """
import torch import torch.nn as nn import torch.nn.functional as F import numpy as np from typing import List, Tuple
class AcousticMultiScaleEncoder(nn.Module): """ 多尺度声学编码器 论文 Section 3.2: 短时窗: 捕获细粒度频率特征(单个运动) 中时窗: 捕获时序模式 长时窗: 捕获跨帧运动关系 通过多尺度并行提取 → 分离重叠信号 """ def __init__(self, input_dim: int = 257, hidden_dim: int = 256, num_scales: int = 3): super().__init__() self.num_scales = num_scales self.scale_convs = nn.ModuleList([ nn.Sequential( nn.Conv1d(input_dim, hidden_dim, kernel_size=2**i + 1, padding=2**(i-1), stride=1), nn.BatchNorm1d(hidden_dim), nn.GELU(), nn.Conv1d(hidden_dim, hidden_dim, kernel_size=3, padding=1), nn.BatchNorm1d(hidden_dim), nn.GELU() ) for i in range(num_scales) ]) self.fusion = nn.Sequential( nn.Linear(hidden_dim * num_scales, hidden_dim * 2), nn.GELU(), nn.Linear(hidden_dim * 2, hidden_dim) ) def forward(self, acoustic_signal: torch.Tensor) -> torch.Tensor: """ Args: acoustic_signal: (B, T, F) STFT频谱 Returns: features: (B, T, hidden_dim) 多尺度特征 """ x = acoustic_signal.transpose(1, 2) scale_features = [] for conv in self.scale_convs: feat = conv(x) scale_features.append(feat.transpose(1, 2)) multi_scale = torch.cat(scale_features, dim=-1) output = self.fusion(multi_scale) return output
class TemporalPoseDecoder(nn.Module): """ 时序姿态解码器 论文 Section 3.3: 跨帧注意力: 捕获时序动态 跨人注意力: 解耦多人信息 通过注意力机制将混合信号分配到各人 """ def __init__(self, hidden_dim: int = 256, num_persons: int = 3, num_joints: int = 17, coord_dim: int = 3): super().__init__() self.num_persons = num_persons self.num_joints = num_joints self.coord_dim = coord_dim self.temporal_attn = nn.MultiheadAttention( embed_dim=hidden_dim, num_heads=8, batch_first=True ) self.temporal_norm = nn.LayerNorm(hidden_dim) self.person_queries = nn.Parameter( torch.randn(num_persons, hidden_dim) ) self.cross_person_attn = nn.MultiheadAttention( embed_dim=hidden_dim, num_heads=8, batch_first=True ) self.person_norm = nn.LayerNorm(hidden_dim) self.pose_head = nn.Sequential( nn.Linear(hidden_dim, hidden_dim // 2), nn.GELU(), nn.Linear(hidden_dim // 2, num_joints * coord_dim) ) self.presence_head = nn.Sequential( nn.Linear(hidden_dim, hidden_dim // 4), nn.GELU(), nn.Linear(hidden_dim // 4, 1), nn.Sigmoid() ) def forward(self, features: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """ Args: features: (B, T, hidden_dim) 编码器输出 Returns: poses: (B, num_persons, T, num_joints, 3) 3D姿态 presence: (B, num_persons, T) 存在概率 """ B, T, D = features.shape temporal_out, _ = self.temporal_attn( features, features, features ) temporal_out = self.temporal_norm(features + temporal_out) queries = self.person_queries.unsqueeze(0).expand(B, -1, -1) person_features = temporal_out person_out, _ = self.cross_person_attn( query=queries, key=person_features, value=person_features ) person_out = self.person_norm(person_out + queries) poses = self.pose_head(person_out) poses = poses.view(B, self.num_persons, self.num_joints, self.coord_dim) presence = self.presence_head(person_out).squeeze(-1) return poses, presence
class SoundMHPE(nn.Module): """ 完整模型: Sound-based Multi-person Human Pose Estimator 输入: 声学STFT频谱 输出: 多人3D姿态 + 存在性 """ def __init__(self, config: dict = None): super().__init__() self.config = config or { 'input_freq_bins': 257, 'hidden_dim': 256, 'num_scales': 3, 'max_persons': 3, 'num_joints': 17, 'coord_dim': 3, } self.encoder = AcousticMultiScaleEncoder( input_dim=self.config['input_freq_bins'], hidden_dim=self.config['hidden_dim'], num_scales=self.config['num_scales'] ) self.decoder = TemporalPoseDecoder( hidden_dim=self.config['hidden_dim'], num_persons=self.config['max_persons'], num_joints=self.config['num_joints'], coord_dim=self.config['coord_dim'] ) def forward(self, acoustic_signal: torch.Tensor) -> dict: """ Args: acoustic_signal: (B, T, F) STFT频谱 Returns: poses: (B, P, T, J, 3) presence: (B, P, T) """ features = self.encoder(acoustic_signal) poses, presence = self.decoder(features) return { 'poses': poses, 'presence': presence, 'features': features } def loss(self, pred_poses, target_poses, pred_presence, target_presence): """ 组合损失: MPJPE + 存在性BCE MPJPE (Mean Per Joint Position Error): 论文 Table 1 的评估指标 """ presence_loss = F.binary_cross_entropy( pred_presence, target_presence.float() ) mask = target_presence.unsqueeze(-1).unsqueeze(-1) mpjpe = torch.norm( pred_poses - target_poses, dim=-1 ).mean() pose_loss = (mpjpe * mask.squeeze(-1)).sum() / ( mask.sum() + 1e-8 ) return { 'total_loss': pose_loss + 0.1 * presence_loss, 'pose_loss': pose_loss.item(), 'presence_loss': presence_loss.item(), 'mpjpe_mm': mpjpe.item() * 1000 }
if __name__ == "__main__": model = SoundMHPE() batch_size = 2 T = 16 F = 257 acoustic_input = torch.randn(batch_size, T, F) with torch.no_grad(): output = model(acoustic_input) print("=== SoundMHPE 输出 ===") print(f"输入: {acoustic_input.shape} (B, T, F)") print(f"姿态输出: {output['poses'].shape} (B, P, J, 3)") print(f"存在性: {output['presence'].shape} (B, P)") print(f"特征维度: {output['features'].shape} (B, T, D)") print(f"\n模型参数量: {sum(p.numel() for p in model.parameters()):,}") print(f"约 {sum(p.numel() for p in model.parameters())/1e6:.1f}M 参数") target_poses = torch.randn(batch_size, 3, 17, 3) target_presence = torch.tensor([[1.0, 1.0, 0.0], [1.0, 0.0, 0.0]]) losses = model.loss( output['poses'], target_poses, output['presence'], target_presence ) print(f"\n=== 损失 ===") print(f"姿态损失: {losses['pose_loss']:.4f}") print(f"存在性损失: {losses['presence_loss']:.4f}") print(f"MPJPE: {losses['mpjpe_mm']:.1f} mm") print(f"总损失: {losses['total_loss'].item():.4f}")
|