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
| import numpy as np import torch import torch.nn as nn import torch.nn.functional as F from typing import Tuple
class DepthIRFusionNet(nn.Module): """ 深度+红外双模态3D姿态估计网络 论文: Tambwekar et al., Sensors 2024 核心: 轻量级双分支特征提取 + 深度引导的3D坐标恢复 """ def __init__( self, num_joints: int = 14, depth_channels: int = 1, ir_channels: int = 1, hidden_dim: int = 128 ): super().__init__() self.num_joints = num_joints self.depth_encoder = nn.Sequential( nn.Conv2d(depth_channels, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d((16, 16)) ) self.ir_encoder = nn.Sequential( nn.Conv2d(ir_channels, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d((16, 16)) ) self.fusion = nn.Sequential( nn.Conv2d(256, 256, 1), nn.BatchNorm2d(256), nn.ReLU(), nn.Conv2d(256, 128, 1), nn.BatchNorm2d(128), nn.ReLU() ) self.heatmap_head = nn.Sequential( nn.ConvTranspose2d(128, 64, 4, stride=2, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.ConvTranspose2d(64, 32, 4, stride=2, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.Conv2d(32, num_joints, 1) ) self.regression_head = nn.Sequential( nn.Linear(128 * 16 * 16, 512), nn.ReLU(), nn.Dropout(0.3), nn.Linear(512, 256), nn.ReLU(), nn.Linear(256, num_joints * 3) ) def forward( self, depth: torch.Tensor, ir: torch.Tensor ) -> Tuple[torch.Tensor, torch.Tensor]: """ Args: depth: 深度图, shape=(B, 1, H, W), 单位mm ir: 红外图, shape=(B, 1, H, W), 0-255 Returns: heatmaps: 2D关键点热图, shape=(B, num_joints, H/4, W/4) joints_3d: 3D关节坐标, shape=(B, num_joints, 3), 单位mm """ depth_feat = self.depth_encoder(depth) ir_feat = self.ir_encoder(ir) fused = torch.cat([depth_feat, ir_feat], dim=1) fused = self.fusion(fused) heatmaps = self.heatmap_head(fused) flat = fused.flatten(1) joints_3d = self.regression_head(flat) joints_3d = joints_3d.reshape(-1, self.num_joints, 3) return heatmaps, joints_3d
def depth_to_3d_joints( heatmap: np.ndarray, depth_map: np.ndarray, camera_intrinsics: dict ) -> np.ndarray: """ 从2D热图+深度图恢复3D关节坐标 Args: heatmap: 关键点热图, shape=(num_joints, H, W) depth_map: 深度图, shape=(H, W), 单位mm camera_intrinsics: 相机内参 fx, fy: 焦距 cx, cy: 主点 Returns: joints_3d: 3D关节坐标, shape=(num_joints, 3), 单位mm """ fx = camera_intrinsics['fx'] fy = camera_intrinsics['fy'] cx = camera_intrinsics['cx'] cy = camera_intrinsics['cy'] num_joints = heatmap.shape[0] joints_3d = np.zeros((num_joints, 3)) for j in range(num_joints): v, u = np.unravel_index(np.argmax(heatmap[j]), heatmap[j].shape) scale_h = depth_map.shape[0] / heatmap[j].shape[0] scale_w = depth_map.shape[1] / heatmap[j].shape[1] u_full = int(u * scale_w) v_full = int(v * scale_h) z = depth_map[v_full, u_full] x = (u_full - cx) * z / fx y = (v_full - cy) * z / fy joints_3d[j] = [x, y, z] return joints_3d
if __name__ == "__main__": intrinsics = { 'fx': 615.0, 'fy': 615.0, 'cx': 320.0, 'cy': 240.0 } model = DepthIRFusionNet(num_joints=14) depth = torch.randn(2, 1, 480, 640) * 1000 ir = torch.rand(2, 1, 480, 640) * 255 heatmaps, joints_3d = model(depth, ir) print(f"热图: {heatmaps.shape}") print(f"3D关节: {joints_3d.shape}") print(f"参数量: {sum(p.numel() for p in model.parameters()):,}") heatmaps_np = heatmaps[0].detach().numpy() depth_np = depth[0, 0].numpy() joints = depth_to_3d_joints(heatmaps_np, depth_np, intrinsics) print(f"恢复的3D坐标 (mm): {joints[0]}")
|