Comments (5)
Hi @vincenthesiyuan,
You can try adding the following app in sdf-net/app
. This is untested with the current branch but should work, as long as you call it with the same options you used to train.
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import numpy as np
import torch
import torch.nn as nn
import trimesh
import mcubes
from lib.models import *
from lib.options import parse_options
def extract_mesh(args):
# Prepare directory
ins_dir = os.path.join(args.mesh_dir, name)
if not os.path.exists(ins_dir):
os.makedirs(ins_dir)
# Get SDFs
with torch.no_grad():
xx = torch.linspace(-1, 1, args.mc_resolution, device=device)
pts = torch.stack(torch.meshgrid(xx, xx, xx), dim=-1).reshape(-1,3)
chunks = torch.split(pts, args.batch_size)
dists = []
for chunk_pts in chunks:
dists.append(net(chunk_pts).detach())
# Convert to occupancy
dists = torch.cat(dists, dim=0)
grid = dists.reshape(args.mc_resolution, args.mc_resolution, args.mc_resolution)
occupancy = torch.where(grid <= 0, 1, 0)
# Meshify
print('Fraction occupied: {:.5f}'.format((occupancy == 1).float().mean().item()))
# vertices, triangles = mcubes.marching_cubes(occupancy.cpu().numpy(), 0.5) # Original post, small bug
vertices, triangles = mcubes.marching_cubes(occupancy.cpu().numpy(), 0)
# Resize + recenter
b_min_np = np.array([-1., -1., -1.])
b_max_np = np.array([ 1., 1., 1.])
vertices = vertices / (args.mc_resolution - 1.0) * (b_max_np - b_min_np) + b_min_np
# Save mesh
mesh = trimesh.Trimesh(vertices, triangles)
mesh_fname = os.path.join(ins_dir, f'mc_res{args.mc_resolution}.obj')
print(f'Saving mesh to {mesh_fname}')
mesh.export(mesh_fname)
if __name__ == '__main__':
# Parse
parser = parse_options(return_parser=True)
app_group = parser.add_argument_group('app')
app_group.add_argument('--mesh-dir', type=str, default='_results/render_app/meshes',
help='Directory to save the mesh')
app_group.add_argument('--mc-resolution', type=int, default=256,
help='Marching cube grid resolution.')
args = parser.parse_args()
# Pick device
use_cuda = torch.cuda.is_available()
device = torch.device('cuda' if use_cuda else 'cpu')
# Get model
if args.pretrained is not None:
name = args.pretrained.split('/')[-1].split('.')[0]
else:
raise ValueError('No network weights specified!')
net = globals()[args.net](args)
net.load_state_dict(torch.load(args.pretrained), strict=False)
net.to(device)
net.eval()
# Run Marching Cubes
extract_mesh(args)
from nglod.
I met the same problem exactly using the code above. I found the keypoint here is that it uses occupancy values as marching cubes input instead of SDF values, which causes the striped mesh.
Therefore, changing the codes of marching cubes works. Specifically, replace the original code
vertices, triangles = mcubes.marching_cubes(occupancy.cpu().numpy(), 0.5)
with
vertices, triangles = mcubes.marching_cubes(grid.cpu().numpy(), 0)
The results of 256 resolution will be as follows:
Hope the above helps!
from nglod.
Hi @joeylitalien ,
Thank you for providing the exporter! I used the script and export a mesh, which has layered effect (like a voxel instead of smooth mesh) and is probably because the resolution of marching cube is too low.
However, I've used 256 and even 512 so res shouldn't be the cause. I noticed that the fraction occupied is only 0.04. Is it fraction too low? Is it possible that there are too many empty points (points that are not inside the mesh) so even a 512 res doesn't help smooth the mesh?
As a comparison, the img rendered from sdf using sdf_renderer.py
looks pretty smooth.
Thank you!
from nglod.
Hi @YuanxunLu, thank you so much for the reply! I haven't noticed this 😂
from nglod.
Welp, that teaches me to put untested code snippets! Thanks for the quick fix @YuanxunLu.
from nglod.
Related Issues (20)
- mesh2sdf errors HOT 6
- Crash using Kaolin SPC HOT 2
- Building sol-rendere: CMAKE_CUDA_COMPILER not set, after EnableLanguage
- Render OBJ files HOT 2
- Question about generate parents in create_trinkets.
- Installation Help HOT 2
- Rendering on Mobile
- The accuracy of predicted sdf function
- ModuleNotFoundError: No module named 'sol_nglod'
- Mismatched model size for different LODs HOT 1
- Args to Export .npz File HOT 4
- Can't export .npz files HOT 1
- abbreviation problem HOT 1
- Can you share chamfer distance evaluation code for SPC part?
- Training with surface normal / sdf gradient supervision HOT 1
- Storage problem of your paper HOT 2
- About rendering 3D models HOT 1
- Building CUDA extensions failed HOT 1
- ShapeNet150 data HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nglod.