-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsdf.py
More file actions
304 lines (255 loc) · 11.7 KB
/
sdf.py
File metadata and controls
304 lines (255 loc) · 11.7 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from torch import nn
import torch
import matplotlib.pyplot as plt
import mesh_to_sdf
import os
import numpy as np
os.environ['PYOPENGL_PLATFORM'] = 'egl'
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class FourierFeatureTransform(torch.nn.Module):
"""
An implementation of Gaussian Fourier feature mapping.
"Fourier Features Let Networks Learn High Frequency Functions in Low Dimensional Domains":
https://arxiv.org/abs/2006.10739
https://people.eecs.berkeley.edu/~bmild/fourfeat/index.html
Given an input of size [batches, N, num_input_channels],
returns a tensor of size [batches, N, num_input_channels + mapping_size*2].
"""
def __init__(self, num_input_channels, mapping_size=256, scale=10):
super().__init__()
self._num_input_channels = num_input_channels
self._mapping_size = mapping_size
B = torch.randn((num_input_channels, mapping_size)) * scale
# NOTE: row-wise sorting (per-channel)
B_sort = sorted(B, key=lambda x: torch.norm(x, p=2))
self._B = torch.stack(B_sort) # for sape
def forward(self, x):
# Expected input: B x N x C
# B shape: C x C'
channels = x.shape[-1]
assert channels == self._num_input_channels, \
"Expected input to have {} channels (got {} channels)".format(self._num_input_channels, channels)
res = x @ self._B.to(x.device).unsqueeze(0) # B x N x C'
res = 2 * np.pi * res
return torch.cat([x, torch.sin(res), torch.cos(res)], dim=-1)
# Define the neural SDF model
class SDF(nn.Module):
def __init__(self, positional=True, positional_dim=256):
super(SDF, self).__init__()
self.positional = positional
self.input_dim = 3
self.hidden_dim = 256
# Positional encoding
if self.positional:
self.fourier = FourierFeatureTransform(3, mapping_size=positional_dim, scale=5)
self.input_dim = 3 + positional_dim * 2
self.hidden_dim = 3 + positional_dim * 2
self.mlp = nn.Sequential(
nn.Linear(self.input_dim, self.hidden_dim),
nn.ReLU(inplace=True),
nn.Linear(self.hidden_dim, self.hidden_dim),
nn.ReLU(inplace=True),
nn.Linear(self.hidden_dim, self.hidden_dim),
nn.ReLU(inplace=True),
nn.Linear(self.hidden_dim, 1),
)
def forward(self, x):
if self.positional:
x = self.fourier(x.unsqueeze(0)).squeeze()
return self.mlp(x)
## ===== TRAINING STUFF =====
def to_numpy(tensor):
return tensor.detach().cpu().numpy()
def mesh_coordinates(mesh_size):
"""
A helper function which takes in the mesh size and returns a
tensor of coordinates for each voxel of the mesh. The coordinates
are scaled to fit into [-1.0, 1.0]^3, meaning that the top-left corner
has the coordinate (-1.0, -1.0, -1.0), and the bottom-right corner has the
coordinate (1.0, 1.0, 1.0).
Args:
mesh_size: the size of the mesh of which we want the voxel
coordinates
Returns:
A torch.Tensor of size [n_voxels, 3] where each row represents
the *scaled* 3D coordinates of that voxel.
"""
tensors = tuple([torch.linspace(-1, 1, steps=mesh_size)] * 3)
mgrid = torch.stack(torch.meshgrid(*tensors, indexing="ij"), dim=-1)
return mgrid.reshape(-1, 3)
# input a mesh and 3d coordinate [x,y,z]
# output an sdf value
from torch import nn
from torch.utils.data import IterableDataset
import trimesh
class GeometryDataset(IterableDataset):
def __init__(self, mesh, res=256, batch_size=256):
super().__init__()
self.batch_size = batch_size
self.res = res
self.coords = mesh_coordinates(res).to(device)
self.values = torch.from_numpy(mesh_to_sdf.mesh_to_sdf(mesh, self.coords.detach().cpu().numpy(),
surface_point_method='scan',
scan_count=200, scan_resolution=800)).to(device)
# self.values = torch.from_numpy(mesh_to_voxels(mesh, res, surface_point_method="sample", pad=False, check_result=True)).to(device)
self.values = self.values.reshape(-1,1)
def __iter__(self):
while True:
# Randomly sample a subset of the image coordinates:
n_coords = self.coords.shape[0]
coords_idcs = torch.randint(low=0, high=n_coords, size=(self.batch_size,))
# Return the randomly sampled coordinates and corresponding values:
yield self.coords[coords_idcs, :], self.values[coords_idcs, :]
def plot_pointcloud(coords, values=None, ax=None, vmin=0, vmax=1):
"""
Shows a scatter plot of image coordinates (optionally with associated values).
Note that we have to do some coordinate shuffling to have the plots displayed
in the same coordinate system as the one matplotlib uses for images.
"""
# NOTE: In MPL z and y axis are FLIPPED
x = coords[:, 0]
z = coords[:, 1]
y = coords[:, 2]
if ax is None:
_, axes = plt.subplots(1, 1, figsize=(8, 8), squeeze=False, subplot_kw=dict(projection='3d'))
ax = axes[0, 0]
xmin, xmax = -1, 1
ymin, ymax = -1, 1
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
ax.scatter(x, y, z, c=values, vmin=vmin, vmax=vmax)
def train_geometry_representation(data, mlp, logdir, total_steps=3000, steps_til_summary=500):
optim = torch.optim.Adam(lr=1e-3, params=mlp.parameters())
# Learning rate scheduler
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optim, 'min', min_lr=1e-6, patience=100, factor=0.5, cooldown=100)
# We use vmin and vmax are necessary to keep the visualizations in the
# same color range, because otherwise they would not be comparable.
vmin, vmax = data.values.min(), data.values.max()
data_iter = iter(data)
for step in range(total_steps):
model_input, ground_truth = next(data_iter)
model_output = mlp(model_input)
# L1 loss
loss = torch.nn.functional.l1_loss(model_output, ground_truth)
# L2 loss
# loss = ((model_output - ground_truth) ** 2).mean()
optim.zero_grad()
loss.backward()
optim.step()
scheduler.step(loss)
# NOTE: Matplotlib doesn't work in cluster
if (step % steps_til_summary) == 0:
with torch.no_grad():
current_model = []
for i in range(0, len(data.coords), data.batch_size):
current_model.append(mlp(data.coords[i:i+data.batch_size]).detach().cpu().numpy())
current_model = np.concatenate(current_model, axis=0)
print(f"Step {step}: loss = {float(loss.detach().cpu()):.2f}")
fig, axes = plt.subplots(1, 4, figsize=(18, 4), squeeze=False, subplot_kw=dict(projection='3d'))
for ax in axes[0]:
ax.view_init(elev=20, azim=45, vertical_axis='z')
plot_pointcloud(
model_input.detach().cpu().numpy(),
values=model_output.detach().cpu().numpy(),
ax=axes[0, 0],
vmin=vmin,
vmax=vmax,
)
axes[0, 0].set_title("Trained MLP (Training Input)")
plot_pointcloud(
model_input.detach().cpu().numpy(),
values=ground_truth.detach().cpu().numpy(),
ax=axes[0, 1],
vmin=vmin,
vmax=vmax,
)
axes[0, 1].set_title("Ground Truth (Training Input)")
plot_pointcloud(
(data.coords[abs(current_model[:, 0]) <= 0.01]).detach().cpu().numpy(),
values=current_model[abs(current_model[:, 0]) <= 0.01],
ax=axes[0, 2],
vmin=vmin,
vmax=vmax
)
axes[0, 2].set_title("Trained MLP (Zero Level Set)")
plot_pointcloud(
data.coords[abs(data.values[:, 0]) <= 0.01].detach().cpu().numpy(),
values=data.values[abs(data.values[:, 0]) <= 0.01].detach().cpu().numpy(),
ax=axes[0, 3],
vmin=vmin,
vmax=vmax
)
axes[0, 3].set_title("Ground Truth (Zero Level Set)")
plt.savefig(os.path.join(logdir, f"step_{step:05}.png"))
plt.close()
return mlp
if __name__ == "__main__":
import argparse
from optimize_utils import clear_directory
import trimesh
import os
parser = argparse.ArgumentParser()
parser.add_argument("--objdir", type=str, required=True)
parser.add_argument("--nsteps", type=int, default=3000)
parser.add_argument("--summarysteps", type=int, default=500)
parser.add_argument("--res", type=int, default=128)
parser.add_argument("--scale", type=float, default=1.)
parser.add_argument("--batch_size", type=int, default=2048)
parser.add_argument("--positional", action="store_true")
parser.add_argument("--overwrite", action="store_true")
parser.add_argument("--eval", action="store_true")
args = parser.parse_args()
from pathlib import Path
logdir = os.path.join(os.path.dirname(args.objdir), "sdf")
Path(logdir).mkdir(parents=True, exist_ok=True)
if args.overwrite:
clear_directory(logdir)
mesh = trimesh.load(args.objdir, force='mesh')
# Normalize using bounding box
from igl import bounding_box
import numpy as np
bb_vs, bf = bounding_box(mesh.vertices)
mesh.vertices -= bb_vs.mean(axis=0)
scale = np.max(np.linalg.norm(mesh.vertices, axis=1)) / args.scale
mesh.vertices /= scale
# Cache this dataset -- preprocessing takes long!!
datasetdir = os.path.join(logdir, "sdfdata.pt")
if not os.path.exists(datasetdir):
print(f"Preprocessing the SDF training data ...")
data = GeometryDataset(mesh, res=args.res, batch_size=args.batch_size)
torch.save(data, datasetdir)
print(f"Saved sdf data to cache: {datasetdir}")
else:
data = torch.load(datasetdir, weights_only=False)
print("Loaded sdf data from cache")
# If batch size is different, then change that
if args.batch_size != data.batch_size:
data.batch_size = args.batch_size
objdir = os.path.dirname(args.objdir)
if not args.eval:
sdf = SDF(positional=args.positional)
sdf.to(device)
trained_sdf = train_geometry_representation(data, sdf, logdir=logdir, total_steps=args.nsteps,
steps_til_summary=args.summarysteps)
torch.save(trained_sdf.state_dict(), os.path.join(objdir, "sdf.pt"))
print(f"Saved trained SDF model to {os.path.join(objdir, 'sdf.pt')}")
# Save zero level set prediction
trained_sdf.eval()
with torch.no_grad():
model_input = data.coords.to(device)
model_output = []
for i in range(0, len(data.coords), data.batch_size):
model_output.append(trained_sdf(data.coords[i:i+data.batch_size]))
model_output = torch.cat(model_output, dim=0)
level_set = (data.coords[abs(model_output[:, 0]) <= 0.01]).detach().cpu().numpy()
np.save(os.path.join(objdir, "sdf_level_set.npy"), level_set)
if args.eval:
trained_sdf = SDF()
trained_sdf.load_state_dict(torch.load(os.path.join(objdir, "sdf.pt"), weights_only=True))
trained_sdf.to(device)
trained_sdf.eval()
with torch.no_grad():
model_input = data.coords.to(device)
model_output = trained_sdf(model_input)
level_set = (data.coords[abs(model_output[:, 0]) <= 0.01]).detach().cpu().numpy()
np.save(os.path.join(objdir, "sdf_level_set.npy"), level_set)