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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381 | def train(config):
"""Train the model.
Args:
config: The training and model config.
"""
batch_size = wandb.config.batch_size = config["batch_size"]
iterations = wandb.config.iterations = config["iterations"]
learning_rate = wandb.config.learning_rate = config["learning_rate"]
latent_size = wandb.config.latent_size = config["latent_size"]
l2_large_weight = wandb.config.l2_large_weight = config["l2_large_weight"]
l2_small_weight = wandb.config.l2_small_weight = config["l2_small_weight"]
l1_large_weight = wandb.config.l1_large_weight = config["l1_large_weight"]
l1_small_weight = wandb.config.l1_small_weight = config["l1_small_weight"]
kld_weight = wandb.config.kld_weight = config["kld_weight"]
pc_weight = wandb.config.pc_weight = config["pc_weight"]
encoder_dict = wandb.config.encoder_dict = config["encoder"]
decoder_dict = wandb.config.decoder_dict = config["decoder"]
dataset_path = wandb.config.dataset_path = config["dataset_path"]
tsdf = wandb.config.tsdf = config["tsdf"]
# init dataset, dataloader, model and optimizer
dataset = SDFDataset(dataset_path)
data_loader = torch.utils.data.DataLoader(
dataset=dataset, batch_size=batch_size, shuffle=True, drop_last=True
)
camera = Camera(640, 480, 320, 320, 320, 240, pixel_center=0.5)
if "device" not in config or config["device"] is None:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
else:
device = torch.device(config["device"])
sdfvae = SDFVAE(
sdf_size=64,
latent_size=latent_size,
encoder_dict=encoder_dict,
decoder_dict=decoder_dict,
device=device,
tsdf=tsdf,
).to(device)
print(str(sdfvae))
summary(sdfvae, (1, 1, 64, 64, 64), device=device)
optimizer = torch.optim.Adam(sdfvae.parameters(), lr=learning_rate)
# load checkpoint if provided
if "checkpoint" in config and config["checkpoint"] is not None:
# TODO: checkpoint should always go together with model config!
model, optimizer, current_iteration, run_name, epoch = utils.load_checkpoint(
config["checkpoint"], sdfvae, optimizer
)
else:
current_iteration = 0
current_epoch = 0
run_name = f"sdfvae_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S-%f')}"
# create SummaryWriter for logging and intermediate output
writer = torch.utils.tensorboard.SummaryWriter(f"runs/" + run_name)
model_base_path = os.path.join(os.getcwd(), "models", run_name)
program_starts = time.time()
warm_up_iterations = 1000
stop = False
while current_iteration <= iterations:
current_epoch += 1
for sdf_volumes in data_loader:
sdf_volumes = sdf_volumes.to(device)
# train first N iters with SDF instead of TSDF to stabilize early training
if current_iteration > warm_up_iterations:
sdfvae.prepare_input(sdf_volumes)
recon_sdf_volumes, mean, log_var, z = sdfvae(
sdf_volumes, enforce_tsdf=False
)
if tsdf is not False and current_iteration > warm_up_iterations:
# during training, clamp the reconstructed SDF only where both target
# and output are outside the TSDF range
mask = torch.logical_and(
torch.abs(sdf_volumes) >= tsdf, torch.abs(recon_sdf_volumes) >= tsdf
)
recon_sdf_volumes_temp = recon_sdf_volumes
recon_sdf_volumes = recon_sdf_volumes_temp.clone()
recon_sdf_volumes[mask] = recon_sdf_volumes_temp[mask].clamp(
-tsdf, tsdf
)
# Compute losses, use negative log-likelihood here
# Note: for average negative log-likelihood all of these have to be divided
# by the batch size. Probably would be better to keep losses comparable for
# varying batch size.
l1_error = torch.abs(recon_sdf_volumes - sdf_volumes)
l2_error = l1_error**2
loss_l2_small = torch.sum(l2_error[torch.abs(sdf_volumes) < 0.1])
loss_l2_large = torch.sum(l2_error[torch.abs(sdf_volumes) >= 0.1])
loss_l1_small = torch.sum(l1_error[torch.abs(sdf_volumes) < 0.1])
loss_l1_large = torch.sum(l1_error[torch.abs(sdf_volumes) >= 0.1])
loss_pc = 0
depth_images = torch.empty((batch_size, 480, 640), device=device)
pointclouds = []
# compute point clouds from sdf volumes at zero crossings
for recon_sdf_volume, sdf_volume, depth_image in zip(
recon_sdf_volumes, sdf_volumes, depth_images
):
with torch.no_grad():
import random
import math
u1, u2, u3 = random.random(), random.random(), random.random()
q = (
torch.tensor(
[
math.sqrt(1 - u1) * math.sin(2 * math.pi * u2),
math.sqrt(1 - u1) * math.cos(2 * math.pi * u2),
math.sqrt(u1) * math.sin(2 * math.pi * u3),
math.sqrt(u1) * math.cos(2 * math.pi * u3),
]
)
.unsqueeze(0)
.to(device)
)
s = torch.Tensor([1.0]).to(device)
p = torch.Tensor([0.0, 0.0, -5.0]).to(device)
depth_image = render_depth_gpu(
sdf_volume[0],
p,
q,
s,
threshold=0.01,
camera=camera,
)
pointcloud = pointset_utils.depth_to_pointcloud(depth_image, camera)
loss_pc = loss_pc + torch.sum(
pc_loss(pointcloud, p, q[0], s, recon_sdf_volume[0]) ** 2
)
loss_kld = -0.5 * torch.sum(1 + log_var - mean.pow(2) - log_var.exp())
loss = (
l2_small_weight * loss_l2_small
+ l2_large_weight * loss_l2_large
+ l1_small_weight * loss_l1_small
+ l1_large_weight * loss_l1_large
+ pc_weight * loss_pc
+ loss_kld
* (kld_weight if current_iteration > warm_up_iterations else 0)
)
print(f"Iteration {current_iteration}, epoch {current_epoch}, loss {loss}")
optimizer.zero_grad()
loss.backward()
optimizer.step()
writer.add_scalar("loss", loss.item(), current_iteration)
writer.add_scalar("loss l2 small", loss_l2_small.item(), current_iteration)
writer.add_scalar("loss l2 large", loss_l2_large.item(), current_iteration)
writer.add_scalar("loss l1 small", loss_l1_small.item(), current_iteration)
writer.add_scalar("loss l1 large", loss_l1_large.item(), current_iteration)
writer.add_scalar("loss pc", loss_pc.item(), current_iteration)
writer.add_scalar("loss kl", loss_kld, current_iteration)
wandb.log(
{
"total loss": loss.item(),
"loss l2 small": loss_l2_small.item(),
"loss l2 large": loss_l2_large.item(),
"loss l1 small": loss_l1_small.item(),
"loss l1 large": loss_l1_large.item(),
"loss kl": loss_kld.item(),
"loss pc": loss_pc.item(),
},
step=current_iteration,
)
current_iteration += 1
with torch.no_grad():
if current_iteration % 1000 == 0:
# show one reconstruction
mean = mean[0].cpu()
figure = sdf_utils.visualize_sdf_reconstruction(
sdf_volumes[0, 0].cpu().numpy(),
recon_sdf_volumes[0, 0].cpu().numpy(),
)
if figure.gca().has_data():
writer.add_figure(
tag="reconstruction",
figure=figure,
global_step=current_iteration,
)
wandb.log({"reconstruction": figure}, step=current_iteration)
# generate 8 samples from the prior
output, _ = sdfvae.inference(n=8, enforce_tsdf=True)
figure = sdf_utils.visualize_sdf_batch(
output.squeeze().cpu().numpy()
)
if figure.gca().has_data():
writer.add_figure(
tag="samples from prior",
figure=figure,
global_step=current_iteration,
)
wandb.log(
{"samples from prior": figure}, step=current_iteration
)
# generate 4 samples from prior
output, _ = sdfvae.inference(n=4, enforce_tsdf=True)
figure = sdf_utils.visualize_sdf_batch_columns(
output.squeeze().cpu().numpy()
)
if figure.gca().has_data():
writer.add_figure(
tag="samples from prior, columns",
figure=figure,
global_step=current_iteration,
)
wandb.log(
{"samples from prior, columns": figure},
step=current_iteration,
)
# if current_iteration % 10000 == 0:
# os.makedirs(model_base_path, exist_ok=True)
# checkpoint_path = os.path.join(model_base_path, F"{current_iteration}.ckp")
# utils.save_checkpoint(
# path=checkpoint_path,
# model=sdfvae,
# optimizer=optimizer,
# iteration=current_iteration,
# run_name=run_name)
if current_iteration > iterations:
break
if current_iteration > iterations:
break
now = time.time()
print("It has been {0} seconds since the loop started".format(now - program_starts))
# save the final model
torch.save(sdfvae.state_dict(), os.path.join(wandb.run.dir, f"{wandb.run.name}.pt"))
config_path = os.path.join(wandb.run.dir, f"{wandb.run.name}.yaml")
config["model"] = os.path.join(".", f"{wandb.run.name}.pt")
yoco.save_config_to_file(config_path, config)
|