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
| """ PressurePose 合成数据生成管道
两阶段物理仿真: 1. 刚体仿真: 人体模型 + 软床/压力垫 → 稳定姿态 2. 软体仿真: 姿态 → 真实压力分布图像 """
import numpy as np from dataclasses import dataclass from typing import Tuple, List import torch
@dataclass class PressurePoseConfig: """PressurePose 数据集配置""" mat_width: int = 60 mat_height: int = 30 mat_resolution: float = 0.026 n_joints: int = 24 n_vertices: int = 6890 n_synthetic: int = 206_000 n_real: int = 1_051 n_real_subjects: int = 20 posture_types: List[str] = None
def generate_resting_pose() -> dict: """ 生成静止姿态参数 模拟人体在床/座椅上的静止姿态 返回 SMPL 姿态参数 """ np.random.seed() pose = np.zeros(72) posture_type = np.random.choice(['supine', 'lateral', 'fetal', 'prone']) if posture_type == 'supine': pose[16:19] = np.random.uniform(-0.3, 0.3) pose[19:22] = np.random.uniform(-0.2, 0.2) elif posture_type == 'lateral': pose[0:3] = [0, 0, np.pi/2 + np.random.uniform(-0.2, 0.2)] elif posture_type == 'fetal': pose[6:9] = [0, 0, -1.2] pose[9:12] = [0, 0, 1.2] elif posture_type == 'prone': pose[0:3] = [0, 0, np.pi] pose += np.random.normal(0, 0.05, 72) return {'pose': pose, 'type': posture_type}
def simulate_pressure_image(pose_params: dict, mat_shape: Tuple[int, int] = (30, 60)) -> np.ndarray: """ 模拟压力垫图像 Args: pose_params: SMPL 姿态参数 mat_shape: 压力垫尺寸 (H, W) Returns: pressure_map: 压力分布, shape=(H, W) """ H, W = mat_shape pressure = np.zeros((H, W)) pose_type = pose_params['type'] if pose_type == 'supine': contact_points = [ (5, 30, 0.8), (12, 30, 0.6), (15, 30, 0.9), (20, 30, 1.0), (25, 28, 0.4), (25, 32, 0.4), ] elif pose_type == 'lateral': contact_points = [ (5, 25, 0.7), (10, 25, 0.9), (18, 25, 1.0), (23, 25, 0.7), (27, 25, 0.5), ] elif pose_type == 'fetal': contact_points = [ (8, 30, 0.6), (15, 28, 0.9), (20, 30, 1.0), (24, 32, 0.7), ] else: contact_points = [(15, 30, 0.8)] for y, x, intensity in contact_points: y_grid, x_grid = np.meshgrid( np.arange(H), np.arange(W), indexing='ij' ) sigma = 3 pressure += intensity * np.exp( -((y_grid - y)**2 + (x_grid - x)**2) / (2 * sigma**2) ) pressure += np.random.normal(0, 0.01, (H, W)) pressure = np.clip(pressure, 0, None) return pressure
class PressureNet(nn.Module): """ PressureNet: 压力图像 → 3D 人体网格 双阶段架构: Mod1: 粗估计压力图像 → 3D 网格 Mod2: 精细化 (输入: 压力图 + Mod1 重建压力图) 关键组件: PMR (Pressure Map Reconstruction) """ def __init__(self, n_vertices: int = 6890, latent_dim: int = 256): super().__init__() self.encoder1 = nn.Sequential( nn.Conv2d(1, 32, 3, padding=1), nn.ReLU(), nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(128, latent_dim) ) self.regressor1 = nn.Sequential( nn.Linear(latent_dim, latent_dim), nn.ReLU(), nn.Linear(latent_dim, n_vertices * 3) ) self.pmr = PressureMapReconstruction(n_vertices=n_vertices) self.encoder2 = nn.Sequential( nn.Conv2d(2, 32, 3, padding=1), nn.ReLU(), nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(128, latent_dim) ) self.regressor2 = nn.Sequential( nn.Linear(latent_dim * 2, latent_dim), nn.ReLU(), nn.Linear(latent_dim, n_vertices * 3) ) def forward(self, pressure_img: torch.Tensor, gender: torch.Tensor = None) -> dict: """ Args: pressure_img: 压力图, shape=(B, 1, H, W) gender: 性别 (0=female, 1=male) Returns: { 'mesh1': Mod1 粗估计, (B, V, 3) 'mesh2': Mod2 精细化, (B, V, 3) 'recon_pressure': PMR 重建压力图 } """ B = pressure_img.shape[0] feat1 = self.encoder1(pressure_img) mesh1 = self.regressor1(feat1).reshape(B, -1, 3) recon_pressure = self.pmr(mesh1, pressure_img.shape[-2:]) mod2_input = torch.cat([ pressure_img, recon_pressure.unsqueeze(1) ], dim=1) feat2 = self.encoder2(mod2_input) feat_combined = torch.cat([feat1, feat2], dim=1) mesh_residual = self.regressor2(feat_combined).reshape(B, -1, 3) mesh2 = mesh1 + mesh_residual return { 'mesh1': mesh1, 'mesh2': mesh2, 'recon_pressure': recon_pressure }
class PressureMapReconstruction(nn.Module): """ PMR: 从 3D 网格重建压力图 约束: 预测网格生成的压力图应与输入一致 作用: 防止错误关节定位 """ def __init__(self, n_vertices: int = 6890): super().__init__() self.projector = nn.Linear(n_vertices * 3, 60 * 30) def forward(self, mesh: torch.Tensor, target_shape: tuple) -> torch.Tensor: """ Args: mesh: 3D 网格, shape=(B, V, 3) target_shape: (H, W) Returns: recon_pressure: 重建压力图, shape=(B, H, W) """ B, V, C = mesh.shape flat = mesh.reshape(B, V * C) pressure = self.projector(flat) H, W = target_shape return pressure.reshape(B, H, W)
if __name__ == "__main__": config = PressurePoseConfig() config.posture_types = ['Supine', 'Lateral', 'Fetal', 'Prone', 'Reclined'] print("=== PressurePose 数据集 ===") print(f"合成图像: {config.n_synthetic:,}") print(f"真实图像: {config.n_real}") print(f"真实受试者: {config.n_real_subjects}") print(f"压力垫: {config.mat_width}×{config.mat_height} = {config.mat_width*config.mat_height} 传感器") pose = generate_resting_pose() pressure = simulate_pressure_image(pose) print(f"\n姿态类型: {pose['type']}") print(f"压力图 shape: {pressure.shape}") print(f"压力范围: [{pressure.min():.3f}, {pressure.max():.3f}]") model = PressureNet(n_vertices=6890, latent_dim=256) pressure_tensor = torch.randn(2, 1, 30, 60) output = model(pressure_tensor) print(f"\n=== PressureNet 测试 ===") print(f"输入: 压力图 {pressure_tensor.shape}") print(f"Mod1 网格: {output['mesh1'].shape}") print(f"Mod2 网格: {output['mesh2'].shape}") print(f"PMR 重建: {output['recon_pressure'].shape}") print(f"\n=== 论文性能 ===") print(f"{'指标':<25} {'合成数据':<15} {'真实数据':<15}") print(f"{'MPJPE (cm)':<25} {'11.18':<15} {'—':<15}") print(f"{'3DVPE (cm)':<25} {'3.94':<15} {'4.99':<15}") print(f"{'PMR 移除 MPJPE':<25} {'+1.1':<15} {'—':<15}")
|