forked from yenchenlin/nerf-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_mesh.py
143 lines (108 loc) · 3.78 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os, sys
# os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# import tensorflow as tf
# tf.compat.v1.enable_eager_execution()
import torch
import numpy as np
import imageio
import pprint
import matplotlib.pyplot as plt
import run_nerf
import run_nerf_helpers
torch.set_default_tensor_type('torch.cuda.FloatTensor')
basedir = './logs'
expname = 'blender_paper_lego'
config = os.path.join(basedir, expname, 'config.txt')
print('Args:')
print(open(config, 'r').read())
parser = run_nerf.config_parser()
ft_str = ''
ft_str = '--ft_path {}'.format(os.path.join(basedir, expname, '200000.tar'))
args = parser.parse_args('--config {} '.format(config) + ft_str)
print(args)
# Create nerf model
_, render_kwargs_test, start, grad_vars, models = run_nerf.create_nerf(args)
bds_dict = {
'near' : torch.tensor(2., dtype=torch.float32),
'far' : torch.tensor(6., dtype=torch.float32),
}
render_kwargs_test.update(bds_dict)
print('Render kwargs:')
pprint.pprint(render_kwargs_test)
net_fn = render_kwargs_test['network_query_fn']
print(net_fn)
# Render an overhead view to check model was loaded correctly
c2w = np.eye(4)[:3,:4].astype(np.float32) # identity pose matrix
c2w[2,-1] = 4.
H, W, focal = 800, 1200, 1200.
down = 8
H, W, focal = 400, 400, 555.5555155968841
K = np.array([
[focal, 0, 0.5*W],
[0, focal, 0.5*H],
[0, 0, 1]
])
test = run_nerf.render(H, W, K, c2w=torch.tensor(c2w, dtype=torch.float32), **render_kwargs_test)
img = np.clip(test[0].cpu(),0,1)
plt.imshow(img)
plt.show()
N = 256
t = np.linspace(-1.2, 1.2, N+1)
query_pts = np.stack(np.meshgrid(t, t, t), -1).astype(np.float32)
print(query_pts.shape)
sh = query_pts.shape
flat = torch.tensor(query_pts.reshape([-1,3]))
def batchify(fn, chunk):
if chunk is None:
return fn
def ret(inputs):
return tf.concat([fn(inputs[i:i+chunk]) for i in range(0, inputs.shape[0], chunk)], 0)
return ret
fn = lambda i0, i1 : net_fn(flat[i0:i1,None,:], viewdirs=torch.zeros_like(flat[i0:i1]), network_fn=render_kwargs_test['network_fine'])
chunk = 1024*64
raw = np.concatenate([np.array(fn(i, i+chunk).cpu()) for i in range(0, flat.shape[0], chunk)], 0)
raw = np.reshape(raw, list(sh[:-1]) + [-1])
sigma = np.maximum(raw[...,-1], 0.)
print(raw.shape)
plt.hist(np.maximum(0,sigma.ravel()), log=True)
plt.show()
import mcubes
threshold = 50.
print('fraction occupied', np.mean(sigma > threshold))
vertices, triangles = mcubes.marching_cubes(sigma, threshold)
print('done', vertices.shape, triangles.shape)
### Uncomment to save out the mesh
mcubes.export_mesh(vertices, triangles, "logs/blender_paper_lego/lego_{}.dae".format(N), "lego")
import trimesh
mesh = trimesh.Trimesh(vertices / N - .5, triangles)
mesh.show()
os.environ["PYOPENGL_PLATFORM"] = "egl"
import pyrender
from load_blender import pose_spherical
scene = pyrender.Scene()
scene.add(pyrender.Mesh.from_trimesh(mesh, smooth=False))
# Set up the camera -- z-axis away from the scene, x-axis right, y-axis up
camera = pyrender.PerspectiveCamera(yfov=np.pi / 3.0)
camera_pose = pose_spherical(-20., -40., 1.).numpy()
nc = pyrender.Node(camera=camera, matrix=camera_pose)
scene.add_node(nc)
# Set up the light -- a point light in the same spot as the camera
light = pyrender.PointLight(color=np.ones(3), intensity=4.0)
nl = pyrender.Node(light=light, matrix=camera_pose)
scene.add_node(nl)
# Render the scene
r = pyrender.OffscreenRenderer(640, 480)
color, depth = r.render(scene)
plt.imshow(color)
plt.show()
plt.imshow(depth)
plt.show()
imgs = []
for th in np.linspace(0, 360., 120+1)[:-1]:
camera_pose = pose_spherical(th, -40., 1.).numpy()
scene.set_pose(nc, pose=camera_pose)
imgs.append(r.render(scene)[0])
f = 'logs/blender_paper_lego/lego_mesh_turntable.mp4'
imageio.mimwrite(f, imgs, fps=30)
print('done')