-
Notifications
You must be signed in to change notification settings - Fork 37
/
extract_mesh.py
40 lines (31 loc) · 1.21 KB
/
extract_mesh.py
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
import argparse
from pathlib import Path
import torch
import trimesh
from network.field import extract_geometry
from network.renderer import name2renderer
from utils.base_utils import load_cfg
def main():
cfg = load_cfg(flags.cfg)
network = name2renderer[cfg['network']](cfg, training=False)
ckpt = torch.load(f'data/model/{cfg["name"]}/model.pth')
step = ckpt['step']
network.load_state_dict(ckpt['network_state_dict'])
network.eval().cuda()
torch.set_default_tensor_type('torch.cuda.FloatTensor')
print(f'successfully load {cfg["name"]} step {step}!')
bbox_min = -torch.ones(3)
bbox_max = torch.ones(3)
with torch.no_grad():
vertices, triangles = extract_geometry(bbox_min, bbox_max, flags.resolution, 0, lambda x: network.sdf_network.sdf(x))
# output geometry
mesh = trimesh.Trimesh(vertices, triangles)
output_dir = Path('data/meshes')
output_dir.mkdir(exist_ok=True)
mesh.export(str(output_dir/f'{cfg["name"]}-{step}.ply'))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', type=str, required=True)
parser.add_argument('--resolution', type=int, default=512)
flags = parser.parse_args()
main()