Spaces:
Running on Zero
Running on Zero
Skip DiffDMC (CUDA arch mismatch on A10G), use marching cubes with adaptive iso-level and bg_fill
Browse files
patches/triposg/triposg/inference_utils.py
CHANGED
|
@@ -486,36 +486,26 @@ def flash_extract_geometry(
|
|
| 486 |
del grid_logits
|
| 487 |
torch.cuda.empty_cache()
|
| 488 |
mesh_v_f = []
|
|
|
|
| 489 |
try:
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
raise RuntimeError("DiffDMC unavailable — using marching cubes")
|
| 507 |
except Exception as e:
|
| 508 |
-
print(f"[TripoSG]
|
| 509 |
-
|
| 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)
|
| 520 |
|
| 521 |
return [mesh_v_f]
|
|
|
|
| 486 |
del grid_logits
|
| 487 |
torch.cuda.empty_cache()
|
| 488 |
mesh_v_f = []
|
| 489 |
+
# Marching cubes on CPU — skip DiffDMC (CUDA arch mismatch on ZeroGPU A10G)
|
| 490 |
try:
|
| 491 |
+
# Replace NaN/background with min_real - 1 so they're clearly "outside"
|
| 492 |
+
real_vals = grid_logits_cpu[~np.isnan(grid_logits_cpu)]
|
| 493 |
+
if len(real_vals) == 0:
|
| 494 |
+
raise RuntimeError("grid contains no real values (all NaN)")
|
| 495 |
+
min_real = float(real_vals.min())
|
| 496 |
+
max_real = float(real_vals.max())
|
| 497 |
+
print(f"[TripoSG] grid value range: [{min_real:.4f}, {max_real:.4f}], real cells: {len(real_vals)}")
|
| 498 |
+
bg_fill = min_real - 1.0
|
| 499 |
+
gl_mc = np.where(np.isnan(grid_logits_cpu), bg_fill, grid_logits_cpu)
|
| 500 |
+
# Find a valid iso-level — prefer 0.0 but fall back to midpoint
|
| 501 |
+
iso_level = 0.0 if (min_real < 0.0 < max_real) else (min_real + max_real) * 0.5
|
| 502 |
+
print(f"[TripoSG] using iso_level={iso_level:.4f}")
|
| 503 |
+
vertices, faces, normals, _ = measure.marching_cubes(gl_mc, iso_level, method="lewiner")
|
| 504 |
+
vertices = vertices / (2 ** octree_depth) * bbox_size + bbox_min
|
| 505 |
+
mesh_v_f = (vertices.astype(np.float32), np.ascontiguousarray(faces))
|
| 506 |
+
print(f"[TripoSG] marching cubes: {len(vertices)} vertices, {len(faces)} faces")
|
|
|
|
| 507 |
except Exception as e:
|
| 508 |
+
print(f"[TripoSG] marching cubes failed: {e}")
|
| 509 |
+
mesh_v_f = (None, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
|
| 511 |
return [mesh_v_f]
|