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
| """ 基于深度图像的3D人体姿态估计
论文: In-vehicle 3D vision for perceiving dangerous driving behaviors 期刊: Scientific Reports (Nature), 2026
核心: 从 ToF 深度图像直接回归 16 个 3D 关节点 关键创新: 模型比基准小 70%,准确率保持 96.02% """
import torch import torch.nn as nn import torch.nn.functional as F
class DepthPoseNet(nn.Module): """ 轻量化 3D 姿态估计网络 输入: 深度图像 (B, 1, H, W) 输出: 16 个 3D 关节点坐标 (B, 16, 3) 相比基准模型(如 PoseFormer/SimpleBaseline)体积减小 70% """ JOINT_NAMES = [ 'head_top', 'neck', 'right_shoulder', 'right_elbow', 'right_wrist', 'left_shoulder', 'left_elbow', 'left_wrist', 'right_hip', 'right_knee', 'right_ankle', 'left_hip', 'left_knee', 'left_ankle', 'pelvis', 'spine' ] def __init__(self, num_joints=16, depth_channels=1): super().__init__() self.backbone = nn.Sequential( self._conv_block(depth_channels, 32, 3, 2, 1), self._conv_block(32, 64, 3, 2, 1), self._conv_block(64, 96, 3, 2, 1), self._conv_block(96, 128, 3, 2, 1), self._conv_block(128, 256, 3, 2, 1), ) self.heatmap_head = nn.Conv2d(256, num_joints, 1) self.regression_head = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(256, 512), nn.ReLU(inplace=True), nn.Dropout(0.2), nn.Linear(512, num_joints * 3) ) self.joint_attention = JointRelationModule( num_joints=num_joints, embed_dim=128, num_heads=4 ) def _conv_block(self, in_ch, out_ch, kernel, stride, padding): return nn.Sequential( nn.Conv2d(in_ch, out_ch, kernel, stride, padding, bias=False), nn.BatchNorm2d(out_ch), nn.ReLU(inplace=True) ) def forward(self, depth_image): """ Args: depth_image: (B, 1, 224, 224) 归一化深度图 Returns: joints_3d: (B, 16, 3) 3D 关节坐标(相机坐标系,毫米) heatmaps: (B, 16, 56, 56) 2D 热图(辅助监督) """ features = self.backbone(depth_image) heatmaps = self.heatmap_head(features) heatmaps = F.interpolate( heatmaps, scale_factor=8, mode='bilinear', align_corners=False ) joints_3d = self.regression_head(features) joints_3d = joints_3d.view(-1, 16, 3) joints_3d = self.joint_attention(joints_3d) return joints_3d, heatmaps
class JointRelationModule(nn.Module): """ 关节关系注意力模块 建模关节间的运动学约束关系 例如: 肘部应在肩部和手腕之间 """ def __init__(self, num_joints=16, embed_dim=128, num_heads=4): super().__init__() self.proj = nn.Linear(3, embed_dim) self.attention = nn.MultiheadAttention( embed_dim=embed_dim, num_heads=num_heads, batch_first=True ) self.norm = nn.LayerNorm(embed_dim) self.ffn = nn.Sequential( nn.Linear(embed_dim, embed_dim * 2), nn.GELU(), nn.Linear(embed_dim * 2, embed_dim), nn.Dropout(0.1) ) self.norm2 = nn.LayerNorm(embed_dim) self.out_proj = nn.Linear(embed_dim, 3) def forward(self, joints_3d): """ Args: joints_3d: (B, 16, 3) 初始 3D 关节坐标 Returns: refined_joints: (B, 16, 3) 优化后的 3D 关节坐标 """ x = self.proj(joints_3d) attn_out, _ = self.attention(x, x, x) x = x + attn_out x = self.norm(x) ffn_out = self.ffn(x) x = x + ffn_out x = self.norm2(x) refined = self.out_proj(x) return joints_3d + refined
class PoseLoss(nn.Module): """ 组合损失: 3D 坐标 MPJPE + 2D 热图 MSE + 关节关系约束 """ def __init__(self, lambda_3d=1.0, lambda_2d=0.5, lambda_bone=0.3): super().__init__() self.lambda_3d = lambda_3d self.lambda_2d = lambda_2d self.lambda_bone = lambda_bone def forward(self, pred_3d, gt_3d, pred_2d, gt_2d): """ Args: pred_3d: (B, 16, 3) 预测 3D 关节 gt_3d: (B, 16, 3) 真值 3D 关节 pred_2d: (B, 16, 56, 56) 预测 2D 热图 gt_2d: (B, 16, 56, 56) 真值 2D 热图 """ mpjpe = torch.mean( torch.norm(pred_3d - gt_3d, dim=-1) ) loss_2d = F.mse_loss(pred_2d, gt_2d) bones = [ (0, 1), (1, 2), (1, 5), (2, 3), (3, 4), (5, 6), (6, 7), (1, 14), (14, 15), (15, 8), (15, 11), ] bone_loss = 0 for j1, j2 in bones: pred_len = torch.norm( pred_3d[:, j1] - pred_3d[:, j2], dim=-1 ) gt_len = torch.norm( gt_3d[:, j1] - gt_3d[:, j2], dim=-1 ) bone_loss += torch.mean((pred_len - gt_len) ** 2) bone_loss /= len(bones) total = ( self.lambda_3d * mpjpe + self.lambda_2d * loss_2d + self.lambda_bone * bone_loss ) return total, { 'mpjpe': mpjpe.item(), 'loss_2d': loss_2d.item(), 'bone_loss': bone_loss.item() }
|