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
| import torch import torch.nn as nn
class HuLiGenModel(nn.Module): """ HuLiGen: 从SMPL参数生成LiDAR点云 核心架构: 1. 输入:SMPL参数 (pose, shape, translation) 2. 生成:Point Transformer + Flow Matching 3. 输出:人体LiDAR点云 (N, 3) + 强度 与raycasting的区别: - Raycasting:从传感器射光线,与3D网格求交 → 理想化,缺少噪声/遮挡 - HuLiGen:学习真实LiDAR的分布特征 → 生成更真实的点云 """ def __init__(self, n_points=4096, hidden_dim=256, n_layers=6, n_heads=4): super().__init__() self.n_points = n_points self.smpl_encoder = SMPLParameterEncoder( pose_dim=72, shape_dim=10, trans_dim=3, hidden_dim=hidden_dim ) self.point_transformer = PointTransformerGenerator( n_points=n_points, hidden_dim=hidden_dim, n_layers=n_layers, n_heads=n_heads ) self.flow_matching = FlowMatchingObjective() def forward(self, smpl_params: dict) -> dict: """ Args: smpl_params: { 'pose': (B, 72), 'shape': (B, 10), 'translation': (B, 3) } Returns: {'points': (B, N, 3), 'intensity': (B, N, 1)} """ smpl_feat = self.smpl_encoder(smpl_params) generated = self.point_transformer(smpl_feat) return generated def generate_lidar(self, smpl_params: dict, n_steps: int = 50) -> torch.Tensor: """ Flow Matching生成过程 从噪声逐步流向目标分布 """ B = smpl_params['pose'].shape[0] x_t = torch.randn(B, self.n_points, 3) smpl_feat = self.smpl_encoder(smpl_params) for t in range(n_steps, 0, -1): t_tensor = torch.full((B,), t/n_steps, device=x_t.device) v = self.point_transformer.predict_velocity( x_t, smpl_feat, t_tensor ) x_t = x_t - v / n_steps return x_t
class SMPLParameterEncoder(nn.Module): """SMPL参数编码器""" def __init__(self, pose_dim=72, shape_dim=10, trans_dim=3, hidden_dim=256): super().__init__() self.pose_encoder = nn.Sequential( nn.Linear(pose_dim, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, hidden_dim) ) self.shape_encoder = nn.Sequential( nn.Linear(shape_dim, hidden_dim // 2), nn.ReLU(), nn.Linear(hidden_dim // 2, hidden_dim) ) self.trans_encoder = nn.Sequential( nn.Linear(trans_dim, hidden_dim // 4), nn.ReLU(), nn.Linear(hidden_dim // 4, hidden_dim) ) self.fusion = nn.Sequential( nn.Linear(hidden_dim * 3, hidden_dim), nn.LayerNorm(hidden_dim), nn.GELU() ) def forward(self, params): pose_feat = self.pose_encoder(params['pose']) shape_feat = self.shape_encoder(params['shape']) trans_feat = self.trans_encoder(params['translation']) concat = torch.cat([pose_feat, shape_feat, trans_feat], dim=-1) return self.fusion(concat)
class PointTransformerGenerator(nn.Module): """Point Transformer生成器""" def __init__(self, n_points=4096, hidden_dim=256, n_layers=6, n_heads=4): super().__init__() self.n_points = n_points self.hidden_dim = hidden_dim self.input_proj = nn.Linear(3, hidden_dim) self.layers = nn.ModuleList([ PointTransformerLayer(hidden_dim, n_heads) for _ in range(n_layers) ]) self.output_proj = nn.Linear(hidden_dim, 3) self.intensity_proj = nn.Linear(hidden_dim, 1) self.velocity_proj = nn.Linear(hidden_dim, 3) def forward(self, condition: torch.Tensor) -> dict: """ Args: condition: (B, hidden_dim) SMPL条件特征 """ B = condition.shape[0] x = torch.randn(B, self.n_points, 3, device=condition.device) h = self.input_proj(x) h = h + condition.unsqueeze(1) for layer in self.layers: h = layer(h) points = self.output_proj(h) + x intensity = torch.sigmoid(self.intensity_proj(h)) return {'points': points, 'intensity': intensity} def predict_velocity(self, x, condition, t): """预测Flow Matching速度""" h = self.input_proj(x) + condition.unsqueeze(1) h = h + self.time_embed(t).unsqueeze(1) for layer in self.layers: h = layer(h) return self.velocity_proj(h)
class PointTransformerLayer(nn.Module): """Point Transformer层""" def __init__(self, dim, num_heads): super().__init__() self.attn = nn.MultiheadAttention( dim, num_heads, batch_first=True ) self.norm1 = nn.LayerNorm(dim) self.norm2 = nn.LayerNorm(dim) self.ffn = nn.Sequential( nn.Linear(dim, dim * 2), nn.GELU(), nn.Linear(dim * 2, dim) ) def forward(self, x): attn_out, _ = self.attn(x, x, x) x = self.norm1(x + attn_out) x = self.norm2(x + self.ffn(x)) return x
class FlowMatchingObjective: """ Flow Matching训练目标 与扩散模型的区别: - 扩散:反向SDE去噪 - Flow Matching:学习从先验到数据的向量场 优势:训练更稳定,采样更快 """ def compute_loss(self, model, x_0, condition): """ Args: x_0: 真实LiDAR点云 (B, N, 3) condition: SMPL条件 """ B = x_0.shape[0] t = torch.rand(B, device=x_0.device) noise = torch.randn_like(x_0) x_t = (1 - t.view(-1, 1, 1)) * x_0 + t.view(-1, 1, 1) * noise target_v = noise - x_0 pred_v = model.predict_velocity(x_t, condition, t) loss = F.mse_loss(pred_v, target_v) return loss
import torch.nn.functional as F
if __name__ == "__main__": model = HuLiGenModel(n_points=2048, hidden_dim=128, n_layers=4) smpl_params = { 'pose': torch.randn(2, 72), 'shape': torch.randn(2, 10), 'translation': torch.randn(2, 3), } output = model(smpl_params) print("=== HuLiGen 生成 ===") print(f"输入: pose={smpl_params['pose'].shape}, shape={smpl_params['shape'].shape}") print(f"生成点云: {output['points'].shape}") print(f"强度: {output['intensity'].shape}") total_params = sum(p.numel() for p in model.parameters()) print(f"参数量: {total_params:,}")
|