Daankular commited on
Commit
2bb9311
·
1 Parent(s): b7c4667

Fix NaN handling in flash_extract_geometry: preserve NaN for DiffDMC, use -1.0 for MC fallback

Browse files
patches/triposg/triposg/inference_utils.py CHANGED
@@ -479,15 +479,19 @@ def flash_extract_geometry(
479
  mesh_v_f = []
480
  grid_logits = grid_logits[0]
481
  print("final grids shape = ", grid_logits.shape)
482
- # Pre-convert to CPU float32 before any CUDA-compiled ops (DiffDMC may corrupt CUDA ctx)
483
- grid_logits_cpu = np.nan_to_num(grid_logits.float().cpu().numpy(), nan=0.0)
 
 
 
484
  torch.cuda.empty_cache()
485
  mesh_v_f = []
486
  try:
487
  if DiffDMC is not None:
488
  print("[TripoSG] Attempting DiffDMC extraction...")
489
- gl_gpu = torch.from_numpy(grid_logits_cpu).to(grid_logits.device)
490
- dmc = DiffDMC(dtype=torch.float32).to(gl_gpu.device)
 
491
  sdf = -gl_gpu / octree_resolution
492
  sdf = sdf.contiguous()
493
  vertices, faces = dmc(sdf, deform=None, return_quads=False, normalize=False)
@@ -495,17 +499,21 @@ def flash_extract_geometry(
495
  faces = faces.detach().cpu().numpy()[:, ::-1]
496
  vertices = vertices / (2 ** octree_depth) * bbox_size + bbox_min
497
  mesh_v_f = (vertices.astype(np.float32), np.ascontiguousarray(faces))
498
- print(f"[TripoSG] DiffDMC succeeded: {len(vertices)} vertices, {len(faces)} faces")
 
 
499
  else:
500
  raise RuntimeError("DiffDMC unavailable — using marching cubes")
501
  except Exception as e:
502
- print(f"[TripoSG] DiffDMC failed ({e}), falling back to marching cubes")
503
  torch.cuda.empty_cache()
504
  try:
505
- vertices, faces, normals, _ = measure.marching_cubes(grid_logits_cpu, 0, method="lewiner")
 
 
506
  vertices = vertices / (2 ** octree_depth) * bbox_size + bbox_min
507
  mesh_v_f = (vertices.astype(np.float32), np.ascontiguousarray(faces))
508
- print(f"[TripoSG] marching cubes succeeded: {len(vertices)} vertices")
509
  except Exception as e2:
510
  print(f"[TripoSG] marching cubes also failed: {e2}")
511
  mesh_v_f = (None, None)
 
479
  mesh_v_f = []
480
  grid_logits = grid_logits[0]
481
  print("final grids shape = ", grid_logits.shape)
482
+ # Pre-move to CPU float32 while CUDA context is still clean.
483
+ # Preserve NaN — DiffDMC handles NaN; marching cubes uses a separate nan-filled copy.
484
+ grid_logits_cpu = grid_logits.float().cpu().numpy() # preserves NaN
485
+ device = grid_logits.device
486
+ del grid_logits
487
  torch.cuda.empty_cache()
488
  mesh_v_f = []
489
  try:
490
  if DiffDMC is not None:
491
  print("[TripoSG] Attempting DiffDMC extraction...")
492
+ # Re-upload with NaN intact so DiffDMC ignores masked cells
493
+ gl_gpu = torch.from_numpy(grid_logits_cpu).to(device)
494
+ dmc = DiffDMC(dtype=torch.float32).to(device)
495
  sdf = -gl_gpu / octree_resolution
496
  sdf = sdf.contiguous()
497
  vertices, faces = dmc(sdf, deform=None, return_quads=False, normalize=False)
 
499
  faces = faces.detach().cpu().numpy()[:, ::-1]
500
  vertices = vertices / (2 ** octree_depth) * bbox_size + bbox_min
501
  mesh_v_f = (vertices.astype(np.float32), np.ascontiguousarray(faces))
502
+ print(f"[TripoSG] DiffDMC: {len(vertices)} vertices, {len(faces)} faces")
503
+ if len(vertices) == 0:
504
+ raise RuntimeError("DiffDMC returned empty mesh — falling back to marching cubes")
505
  else:
506
  raise RuntimeError("DiffDMC unavailable — using marching cubes")
507
  except Exception as e:
508
+ print(f"[TripoSG] DiffDMC path failed ({e}), falling back to marching cubes")
509
  torch.cuda.empty_cache()
510
  try:
511
+ # NaN -1 makes masked/unqueried cells "outside" the isosurface
512
+ gl_mc = np.nan_to_num(grid_logits_cpu, nan=-1.0)
513
+ vertices, faces, normals, _ = measure.marching_cubes(gl_mc, 0, method="lewiner")
514
  vertices = vertices / (2 ** octree_depth) * bbox_size + bbox_min
515
  mesh_v_f = (vertices.astype(np.float32), np.ascontiguousarray(faces))
516
+ print(f"[TripoSG] marching cubes: {len(vertices)} vertices")
517
  except Exception as e2:
518
  print(f"[TripoSG] marching cubes also failed: {e2}")
519
  mesh_v_f = (None, None)