(path: str)
| 315 | PlyData([el]).write(path) |
| 316 | |
| 317 | def load_ply(path: str) -> torch.nn.ParameterDict: |
| 318 | # Read PLY file |
| 319 | plydata = PlyData.read(path) |
| 320 | vertices = plydata['vertex'] |
| 321 | |
| 322 | # Get total number of vertices |
| 323 | n_vertices = vertices.count |
| 324 | |
| 325 | # Extract basic attributes (positions) |
| 326 | means = np.stack((vertices['x'], vertices['y'], vertices['z']), axis=1) |
| 327 | |
| 328 | # Calculate dimensions for sh0 and shN |
| 329 | sh0_size = len([prop for prop in vertices.properties if prop.name.startswith('f_dc_')]) |
| 330 | shN_size = len([prop for prop in vertices.properties if prop.name.startswith('f_rest_')]) |
| 331 | |
| 332 | # Extract sh0 data |
| 333 | sh0_data = np.zeros((n_vertices, sh0_size)) |
| 334 | for i in range(sh0_size): |
| 335 | sh0_data[:, i] = vertices[f'f_dc_{i}'] |
| 336 | |
| 337 | # Extract shN data |
| 338 | shN_data = np.zeros((n_vertices, shN_size)) |
| 339 | for i in range(shN_size): |
| 340 | shN_data[:, i] = vertices[f'f_rest_{i}'] |
| 341 | |
| 342 | # Extract opacity data |
| 343 | opacities = vertices['opacity'].reshape(-1, 1) |
| 344 | |
| 345 | # Extract scales data |
| 346 | scale_size = len([prop for prop in vertices.properties if prop.name.startswith('scale_')]) |
| 347 | scales = np.zeros((n_vertices, scale_size)) |
| 348 | for i in range(scale_size): |
| 349 | scales[:, i] = vertices[f'scale_{i}'] |
| 350 | |
| 351 | # Extract quaternion data |
| 352 | quat_size = len([prop for prop in vertices.properties if prop.name.startswith('rot_')]) |
| 353 | quats = np.zeros((n_vertices, quat_size)) |
| 354 | for i in range(quat_size): |
| 355 | quats[:, i] = vertices[f'rot_{i}'] |
| 356 | |
| 357 | # Reshape sh0 and shN to original dimensions |
| 358 | sh0_dim2 = 3 # Assume 3, adjust based on actual data |
| 359 | sh0_dim1 = sh0_size // sh0_dim2 |
| 360 | shN_dim2 = 3 # Assume 3, adjust based on actual data |
| 361 | shN_dim1 = shN_size // shN_dim2 |
| 362 | |
| 363 | sh0_data = sh0_data.reshape(-1, sh0_dim2, sh0_dim1).transpose(0, 2, 1) |
| 364 | shN_data = shN_data.reshape(-1, shN_dim2, shN_dim1).transpose(0, 2, 1) |
| 365 | |
| 366 | # Convert to torch tensors and create ParameterDict |
| 367 | splats = torch.nn.ParameterDict({ |
| 368 | "means": torch.nn.Parameter(torch.from_numpy(means.astype(np.float32))), |
| 369 | "sh0": torch.nn.Parameter(torch.from_numpy(sh0_data.astype(np.float32))), |
| 370 | "shN": torch.nn.Parameter(torch.from_numpy(shN_data.astype(np.float32))), |
| 371 | "opacities": torch.nn.Parameter(torch.from_numpy(opacities.astype(np.float32)).squeeze(1)), |
| 372 | "scales": torch.nn.Parameter(torch.from_numpy(scales.astype(np.float32))), |
| 373 | "quats": torch.nn.Parameter(torch.from_numpy(quats.astype(np.float32))) |
| 374 | }) |
no outgoing calls