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
| import torch import torch.nn as nn import torch.nn.functional as F
class PressureNet(nn.Module): """ PressureNet: 从压力图像估计3D人体姿态和体型 两阶段架构: - Mod1: 粗略估计 - Mod2: 精细优化 关键创新:Pressure Map Reconstruction (PMR) - 从估计的3D mesh重建压力图 - 与输入压力图对比,确保一致性 Args: pressure_size: 压力垫分辨率 (H, W) n_mesh_vertices: SMPL mesh 顶点数 gender_dim: 性别编码维度 """ def __init__(self, pressure_h=64, pressure_w=32, n_mesh_vertices=6890, gender_dim=2): super().__init__() self.mod1 = PressureEstimator( pressure_h, pressure_w, n_mesh_vertices, gender_dim, use_reconstruction=True ) self.mod2 = PressureEstimator( pressure_h, pressure_w, n_mesh_vertices, gender_dim, use_reconstruction=True, input_channels=2 ) def forward(self, pressure_image, gender): """ Args: pressure_image: (B, 1, H, W) 压力分布图 gender: (B, 2) one-hot性别编码 Returns: mesh_vertices: (B, V, 3) 3D mesh顶点坐标 reconstructed_pressure: (B, 1, H, W) 重建压力图 """ mesh1, recon1 = self.mod1(pressure_image, gender) mod2_input = torch.cat([pressure_image, recon1], dim=1) mesh2, recon2 = self.mod2(mod2_input, gender) return mesh2, recon2
class PressureEstimator(nn.Module): """单阶段压力→3D mesh估计器""" def __init__(self, pressure_h, pressure_w, n_vertices, gender_dim, use_reconstruction=True, input_channels=1): super().__init__() self.use_reconstruction = use_reconstruction self.encoder = nn.Sequential( nn.Conv2d(input_channels, 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.MaxPool2d(2), nn.Conv2d(128, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(256, 512, 3, padding=1), nn.BatchNorm2d(512), nn.ReLU(), nn.AdaptiveAvgPool2d(1), ) self.gender_encoder = nn.Linear(gender_dim, 64) self.pose_head = nn.Sequential( nn.Linear(512 + 64, 256), nn.ReLU(), nn.Linear(256, 72) ) self.shape_head = nn.Sequential( nn.Linear(512 + 64, 256), nn.ReLU(), nn.Linear(256, 10) ) self.mesh_regressor = SMPLMeshRegressor(n_vertices) if use_reconstruction: self.pmr = PressureMapReconstructor( n_vertices, pressure_h, pressure_w ) def forward(self, pressure_image, gender): feat = self.encoder(pressure_image).flatten(1) gender_feat = self.gender_encoder(gender) combined = torch.cat([feat, gender_feat], dim=1) pose = self.pose_head(combined) shape = self.shape_head(combined) mesh = self.mesh_regressor(pose, shape) recon_pressure = None if self.use_reconstruction: recon_pressure = self.pmr(mesh, pressure_image.shape[-2:]) return mesh, recon_pressure
class PressureMapReconstructor(nn.Module): """PMR: 从3D mesh重建压力图""" def __init__(self, n_vertices, out_h, out_w): super().__init__() self.out_h = out_h self.out_w = out_w self.proj = nn.Sequential( nn.Linear(n_vertices * 3, 1024), nn.ReLU(), nn.Linear(1024, out_h * out_w) ) def forward(self, mesh, target_size): B, V, _ = mesh.shape flat = mesh.view(B, -1) pressure = self.proj(flat) pressure = pressure.view(B, 1, self.out_h, self.out_w) return F.relu(pressure)
class SMPLMeshRegressor(nn.Module): """简化的SMPL mesh回归器""" def __init__(self, n_vertices): super().__init__() self.regressor = nn.Sequential( nn.Linear(72 + 10, 512), nn.ReLU(), nn.Linear(512, 1024), nn.ReLU(), nn.Linear(1024, n_vertices * 3) ) def forward(self, pose, shape): x = torch.cat([pose, shape], dim=1) mesh = self.regressor(x) return mesh.view(-1, 6890, 3)
class SeatPressureOOPDetector: """座椅压力传感器OOP检测""" OOP_CATEGORIES = { 0: 'normal', 1: 'forward_lean', 2: 'reclined', 3: 'side_lean', 4: 'child_seat', 5: 'empty', 6: 'feet_on_dash', } def __init__(self, sensor_h=32, sensor_w=16): self.sensor_h = sensor_h self.sensor_w = sensor_w self.model = PressureNet( pressure_h=sensor_h, pressure_w=sensor_w ) def detect_oop(self, pressure_image): """OOP检测""" mesh, recon = self.model(pressure_image, torch.tensor([[1, 0]])) posture_features = self._extract_posture_features(mesh) oop_class = self._classify_oop(posture_features, pressure_image) return { 'oop_category': self.OOP_CATEGORIES[oop_class], 'confidence': 0.85, 'mesh': mesh, 'posture_features': posture_features } def _extract_posture_features(self, mesh): """从3D mesh提取姿态特征""" return {'torso_angle': 15, 'head_height': 0.8} def _classify_oop(self, features, pressure): """OOP分类""" total_pressure = pressure.sum() if total_pressure < 0.1: return 5 return 0
if __name__ == "__main__": B = 4 pressure = torch.randn(B, 1, 64, 32) gender = torch.tensor([[1, 0]] * B, dtype=torch.float32) model = PressureNet(64, 32, 6890) mesh, recon = model(pressure, gender) print(f"压力输入: {pressure.shape}") print(f"3D Mesh: {mesh.shape}") print(f"重建压力: {recon.shape}") print(f"参数量: {sum(p.numel() for p in model.parameters())/1e6:.2f}M") detector = SeatPressureOOPDetector(32, 16) result = detector.detect_oop(pressure[:1]) print(f"\nOOP检测结果: {result['oop_category']}")
|