-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtraining.py
540 lines (438 loc) · 22.2 KB
/
training.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
'''Implements a generic training loop.
'''
from pathlib import Path
import time
import os
import torch
from torch import nn
from torch.utils.data import dataloader
from torch.utils.tensorboard import SummaryWriter
from tqdm.autonotebook import tqdm
import numpy as np
from modules_sdf import SDFIBRNet
from torchmeta.modules.utils import get_subdict
import utils.common_utils as common_utils
import utils.diff_operators as diff_operators
from scheduler import Scheduler
def train(model, train_dataloader, epochs, lr, steps_til_summary, epochs_til_checkpoint, model_dir, loss_fn,
summary_fn, val_dataloader=None, clip_grad=False, loss_schedules=None, optim=None, verbose_record_file: Path = None,
teacher: SDFIBRNet = None, meta_spec_log=False, ibr_log=False):
no_optimization = 0
if optim is None:
# Dummy optimizer.
optim = torch.optim.Adam(lr=lr, params=model.parameters())
no_optimization = 1
os.makedirs(model_dir, 0o777, True)
summaries_dir = os.path.join(model_dir, 'summaries')
common_utils.cond_mkdir(summaries_dir)
checkpoints_dir = os.path.join(model_dir, 'checkpoints')
common_utils.cond_mkdir(checkpoints_dir)
verbose_dir = None
loss_log = []
if model.opt.verbose_logging:
verbose_dir = Path(model_dir) / 'verbose'
verbose_dir.mkdir(0o777, True, True)
writer = SummaryWriter(summaries_dir)
device = model.device
# Scheduler.
scheduler = Scheduler(model.opt, model, optim, writer)
# Restore verbose record.
epoch_0 = 0
total_steps = 0
verbose_record = None
if verbose_record_file is not None:
verbose_record = torch.load(verbose_record_file, map_location=model.device)
optim.load_state_dict(verbose_record['optimizer_state_dict'])
model.load_state_dict(verbose_record['model_state_dict'], strict=False)
scheduler.update(verbose_record['epoch'], verbose_record['step'] - 1, verbose_record['loss_log'][-2])
epoch_0 = verbose_record['epoch']
total_steps = verbose_record['step']
print(f'Will train for {epochs} epochs with {len(train_dataloader)} iterations per epoch.')
train_loss = torch.Tensor([0])
with tqdm(total=len(train_dataloader) * epochs) as pbar:
train_losses = []
for epoch in range(epoch_0, epochs):
if is_event_step_log(epoch, epochs_til_checkpoint, orig_log=True):
# Regular checkpoint
save_checkpoint(checkpoints_dir, model, optim, epoch, total_steps, train_loss.item(), True, False, orig_log=True)
for step, (model_input, gt) in enumerate(train_dataloader):
start_time = time.time()
# Override inputs for verbose record iter 0
if verbose_record is not None and total_steps == verbose_record['step']:
model_input = verbose_record['inputs']
gt = verbose_record['gt']
# To GPU.
for k, v in model_input.items():
v = v.to(device)
if v.dtype == torch.float:
v = v.requires_grad_(True)
model_input[k] = v
gt = {key: value.to(device) for key, value in gt.items()}
# If teacher exists, override GT.
if teacher is not None:
gt = query_teacher(teacher, model_input, gt)
# Renormalize params.
model.renormalize()
if ibr_log:
if total_steps <= 5000:
model.opt.occ_threshold = 1e-3
elif 5000 < total_steps <= 10000:
model.opt.occ_threshold = 1e-4
else:
model.opt.occ_threshold = 1e-5
elif meta_spec_log:
if total_steps <= 3000:
model.opt.occ_threshold = 1e-3
elif 3000 < total_steps <= 8000:
model.opt.occ_threshold = 1e-4
else:
model.opt.occ_threshold = 1e-5
if model.opt.dataset_name == 'shapenet':
OPTSHAPE_FIRST = 2000
OPTSHAPE_EVERY = 5
elif model.opt.dataset_name == 'dtu':
OPTSHAPE_FIRST = 50
OPTSHAPE_EVERY = 7
elif model.opt.dataset_name == 'nlr':
OPTSHAPE_FIRST = 100
OPTSHAPE_EVERY = 3
# Forward.
t1iter = time.time()
if total_steps < OPTSHAPE_FIRST:
model_input['train_shape'] = 1
elif total_steps % OPTSHAPE_EVERY == 0:
model_input['train_shape'] = 1
else:
model_input['train_shape'] = 0
scheduler.set_lr('sdf', 0, total_steps)
model_output = model(model_input)
model_output['weights_sdf'] = {k: v for k, v in model.decoder_sdf.named_parameters()}
liter = time.time() - t1iter
# print(f'Iteration Length: {liter}')
t1loss = time.time()
# Compute losses.
losses = loss_fn(model_output, gt)
lloss = time.time() - t1loss
# print(f'Loss Length {lloss}')
# Sum only active losses for optimization.
train_loss = 0.
for loss_name, (loss, loss_enabled) in losses.items():
single_loss = loss.mean()
if torch.isnan(single_loss).any().item():
print('We have NAN in loss!!!!')
import pdb
pdb.set_trace()
raise Exception('NaN in loss!')
if loss_schedules is not None and loss_name in loss_schedules:
# Optionally apply schedule.
schedule = loss_schedules[loss_name](total_steps)
writer.add_scalar(loss_name + "_weight", schedule, total_steps)
single_loss *= schedule
writer.add_scalar(loss_name, single_loss, total_steps)
if loss_enabled:
# Sum only active losses.
train_loss += single_loss
# Log summary loss.
train_losses.append(train_loss.item())
writer.add_scalar("total_train_loss", train_loss, total_steps)
writer.add_scalar("swish_beta", np.mean(
[m.beta.mean().item() for m in model.modules() if type(m).__name__ in ['Swish']]), total_steps)
# Verify rerun
if verbose_record is not None and total_steps == verbose_record['step']:
assert abs(train_loss.item() - verbose_record['loss'].item()) < 1e-5
# Summarize?
if is_event_step_log(total_steps, steps_til_summary, meta_spec_log=meta_spec_log, ibr_log=ibr_log,
orig_log=(not (meta_spec_log or ibr_log))):
# Current checkpoint.
save_checkpoint(checkpoints_dir, model, optim, epoch, total_steps, train_loss.item(), False, False, meta_spec_log=meta_spec_log,
ibr_log=ibr_log, orig_log=(not (meta_spec_log or ibr_log)))
summary_fn(model, model_input, gt, model_output, writer, total_steps)
elif verbose_record:
# Render
summary_fn(model, model_input, gt, model_output, writer, total_steps)
pass
# Detailed verbose logging.
if model.opt.verbose_logging:
loss_log += [train_loss.item()]
torch.save({
'epoch': epoch,
'step': total_steps,
'loss': train_loss,
'losses': losses,
'loss_log': torch.from_numpy(np.array(loss_log)),
'optimizer_state_dict': optim.state_dict(),
'model_state_dict': model.state_dict(),
'git_revision': common_utils.get_git_revision(),
'inputs': model_input,
'gt': gt,
}, verbose_dir / f'{total_steps:08d}.pth')
# Backward.
t1backward = time.time()
optim.zero_grad()
if not no_optimization:
train_loss.backward()
lbackward = time.time() - t1backward
# print(f'Backward Length: {lbackward}')
grads_isnan = {k: torch.isnan(x.grad).any().item()
for k, x in model.named_parameters() if x.grad is not None}
if np.any(list(grads_isnan.values())):
print('We have NAN in gradients!!!!')
import pdb
pdb.set_trace()
torch.save(model.state_dict(), Path(checkpoints_dir) / 'model_broken.pth')
torch.save(model_input, Path(checkpoints_dir) / 'model_inputs.pth')
torch.save(gt, Path(checkpoints_dir) / 'model_gt.pth')
raise Exception('NaN in gradients!')
t1optim = time.time()
if clip_grad:
if isinstance(clip_grad, bool):
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.)
else:
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=clip_grad)
optim.step()
loptim = time.time() - t1optim
# print(f'Optimizer Length: {loptim}')
total_time = liter + lloss + lbackward + loptim
writer.add_scalar("iteration_total_time", total_time, total_steps)
if not no_optimization:
scheduler.update(epoch, total_steps, train_loss)
params_isnan = [torch.isnan(x).any().item() for x in model.parameters()]
if np.any(params_isnan):
print('We have NAN in parameters!!!!')
import pdb
pdb.set_trace()
raise Exception('NaN in parameters!')
pbar.update(1)
if is_event_step_log(total_steps, steps_til_summary, meta_spec_log=meta_spec_log, ibr_log=ibr_log,
orig_log=(not (meta_spec_log or ibr_log))):
tqdm.write("Epoch %d/%d, Total loss %0.6f, iteration time %0.6f" %
(epoch, epochs, train_loss.item(), time.time() - start_time))
total_steps += 1
# Final checkpoint.
save_checkpoint(checkpoints_dir, model, optim, epoch, total_steps, train_loss.item(), True, True, orig_log=True)
def train_meta(meta_model, meta_dataloader, epochs, lr, steps_til_summary, epochs_til_checkpoint, model_dir, loss_fn,
summary_fn, val_dataloader=None, clip_grad=False, loss_schedules=None, optim=None, verbose_record_file: Path = None,
teacher: SDFIBRNet = None):
if optim is None:
optim = torch.optim.Adam(lr=lr, params=meta_model.parameters())
os.makedirs(model_dir, 0o777, True)
summaries_dir = os.path.join(model_dir, 'summaries')
common_utils.cond_mkdir(summaries_dir)
checkpoints_dir = os.path.join(model_dir, 'checkpoints')
common_utils.cond_mkdir(checkpoints_dir)
verbose_dir = None
loss_log = []
if meta_model.opt.verbose_logging:
verbose_dir = Path(model_dir) / 'verbose'
verbose_dir.mkdir(0o777, True, True)
writer = SummaryWriter(summaries_dir)
device = meta_model.device
# Scheduler.
scheduler = Scheduler(meta_model.opt, meta_model, optim, writer)
# Restore verbose record.
epoch_0 = 0
total_steps = 0
verbose_record = None
if verbose_record_file is not None:
verbose_record = torch.load(verbose_record_file, map_location=meta_model.device)
optim.load_state_dict(verbose_record['optimizer_state_dict'])
meta_model.load_state_dict(verbose_record['model_state_dict'], strict=False)
scheduler.update(verbose_record['epoch'], verbose_record['step'] - 1, verbose_record['loss_log'][-2])
epoch_0 = verbose_record['epoch']
total_steps = verbose_record['step']
print(f'Will train for {epochs} epochs with {len(meta_dataloader)} iterations per epoch.')
meta_loss = torch.Tensor([0])
with tqdm(total=len(meta_dataloader) * epochs) as pbar:
meta_losses = []
for epoch in range(epoch_0, epochs):
if is_event_step_log(epoch, epochs_til_checkpoint, orig_log=True):
# Regular checkpoint
save_checkpoint(checkpoints_dir, meta_model, optim, epoch, total_steps, meta_loss.item(), True, False, orig_log=True)
for step, meta_batch in enumerate(meta_dataloader):
start_time = time.time()
# Override inputs for verbose record iter 0
if verbose_record is not None and total_steps == verbose_record['step']:
meta_batch = verbose_record['meta_batch']
# To GPU.
def dict_to_gpu(d):
for k, v in d.items():
v = v.to(device)
if v.dtype == torch.float:
v = v.requires_grad_(True)
d[k] = v
return d
for k, v in meta_batch['context'].items():
v = [dict_to_gpu(item) for item in v]
meta_batch['context'][k] = v
for k, v in meta_batch['query'].items():
v = dict_to_gpu(v)
meta_batch['query'][k] = v
# Renormalize params.
meta_model.renormalize()
# Forward.
meta_model_output = meta_model(meta_batch)
# Add model parameters for regularization losses.
meta_model_output['model_out']['weights_sdf'] = {k: v for k, v in get_subdict(meta_model_output['fast_params'], 'decoder_sdf').items()}
# Currently meta_loss is a placeholder, since optimization is done via REPTILE for weight updates
meta_loss = torch.tensor([0])
# Log summary loss.
meta_losses.append(meta_loss.item())
writer.add_scalar("total_train_loss", meta_loss, total_steps)
# Verify rerun
if verbose_record is not None and total_steps == verbose_record['step']:
assert abs(meta_loss.item() - verbose_record['loss'].item()) < 1e-5
# Summarize?
if is_event_step_log(total_steps, steps_til_summary):
# Current checkpoint.
save_checkpoint(checkpoints_dir, meta_model, optim, epoch, total_steps, meta_loss.item(), False, False, meta_learning_log=True)
summary_fn(meta_model, meta_batch, meta_model_output, writer, total_steps)
elif verbose_record:
# Render
summary_fn(meta_model, meta_batch, meta_model_output, writer, total_steps)
pass
# Detailed verbose logging.
if meta_model.opt.verbose_logging:
loss_log += [meta_loss.item()]
torch.save({
'epoch': epoch,
'step': total_steps,
'loss': meta_loss,
'loss_log': torch.from_numpy(np.array(loss_log)),
'optimizer_state_dict': optim.state_dict(),
'model_state_dict': meta_model.state_dict(),
'git_revision': common_utils.get_git_revision(),
'meta_batch': meta_batch,
}, verbose_dir / f'{total_steps:08d}.pth')
# Backward.
optim.zero_grad()
if meta_model.opt.meta_algorithm == 'maml':
meta_loss.backward()
grads_isnan = {k: torch.isnan(x.grad).any().item()
for k, x in meta_model.named_parameters() if x.grad is not None}
if np.any(list(grads_isnan.values())):
print('We have NAN in gradients!!!!')
import pdb
pdb.set_trace()
torch.save(meta_model.state_dict(), Path(checkpoints_dir) / 'model_broken.pth')
torch.save(meta_batch, Path(checkpoints_dir) / 'meta_batch.pth')
raise Exception('NaN in gradients!')
if clip_grad:
if isinstance(clip_grad, bool):
torch.nn.utils.clip_grad_norm_(meta_model.parameters(), max_norm=1.)
else:
torch.nn.utils.clip_grad_norm_(meta_model.parameters(), max_norm=clip_grad)
optim.step()
elif meta_model.opt.meta_algorithm == 'reptile':
meta_model._update_meta_params(meta_model_output['fast_params'], meta_model.opt.meta_lr)
else:
print('Not a valid meta-learning algorithm!')
raise Exception('Not a valid meta-learning algorithm!')
scheduler.update(epoch, total_steps, meta_loss)
update_meta_lr(meta_model, total_steps)
params_isnan = [torch.isnan(x).any().item() for x in meta_model.parameters()]
if np.any(params_isnan):
print('We have NAN in parameters!!!!')
import pdb
pdb.set_trace()
raise Exception('NaN in parameters!')
pbar.update(1)
if is_event_step_log(total_steps, steps_til_summary):
tqdm.write("Epoch %d/%d, Total loss %0.6f, iteration time %0.6f" %
(epoch, epochs, meta_loss.item(), time.time() - start_time))
total_steps += 1
# Final checkpoint.
save_checkpoint(checkpoints_dir, meta_model, optim, epoch, total_steps, meta_loss.item(), True, True, orig_log=True)
def save_checkpoint(checkpoints_dir, model, optim, epoch, steps, loss, is_regular=False, is_final=False, meta_spec_log=False, orig_log=False,
meta_learning_log=False, ibr_log=False):
"""
Saves full training state.
"""
suffix = 'final' if is_final else 'current'
checkpoints_dir = Path(checkpoints_dir)
# Model.
torch.save(model.state_dict(), checkpoints_dir / f'model_{suffix}.pth')
if is_regular and orig_log:
torch.save(model.state_dict(), checkpoints_dir / f'model_epoch_{epoch:04d}.pth')
if meta_spec_log or meta_learning_log or ibr_log or orig_log:
torch.save(model.state_dict(), checkpoints_dir / f'model_iter_{steps:08d}.pth')
# Optimizer.
train_state = {
'epoch': epoch,
'step': steps,
'loss': loss,
'optimizer_state_dict': optim.state_dict(),
'git_revision': common_utils.get_git_revision()
}
torch.save(train_state, checkpoints_dir / f'optim_{suffix}.pth')
if is_regular and orig_log:
torch.save(train_state, checkpoints_dir / f'optim_epoch_{epoch:04d}.pth')
if meta_spec_log or meta_learning_log or ibr_log or orig_log:
torch.save(train_state, checkpoints_dir / f'optim_iter_{steps:08d}.pth')
@torch.enable_grad()
def query_teacher(teacher: SDFIBRNet, model_input, gt):
"""
Computes updated GT based on teacher.
"""
input_pcd = {
'coords': model_input['coords'],
'time': model_input['time'],
}
teacher_out = teacher.decoder_sdf(input_pcd)
sdf = teacher_out['model_out'][..., :1]
normals = diff_operators.gradient(sdf, teacher_out['model_in'])[0, ...]
# Override SDF ground-truth.
gt['sdf'] = sdf.detach()
gt['normals'] = normals[None, ...].detach()
gt['is_sdf_explicit'] = True
return gt
class LinearDecaySchedule():
def __init__(self, start_val, final_val, num_steps):
self.start_val = start_val
self.final_val = final_val
self.num_steps = num_steps
def __call__(self, iter):
return self.start_val + (self.final_val - self.start_val) * min(iter / self.num_steps, 1.)
def is_event_step_log(step: int, event_freq_log: int = 1, meta_spec_log=False, orig_log=False, ibr_log=False):
if meta_spec_log:
# return is_event_step_log_meta_spec(step, event_freq_log)
if step in [0, 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100, 125, 150, 200]:
return 1
return step % 100 == 0
if orig_log:
# return is_event_step_log_orig(step, event_freq_log)
if step in [0, 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100, 125, 200]:
return 1
return step % 100 == 0
if ibr_log:
if step in [0, 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100, 125, 200]:
return 1
return step % 100 == 0
return step % 2 == 0
# return True
def update_meta_lr(meta_model, total_steps: int):
if (total_steps + 1) % 400 == 0:
meta_model.update_lr(lr_sdf_new=meta_model.lr_sdf * 0.5)
if (total_steps + 1) % 60 == 0:
meta_model.update_lr(lr_new=meta_model.lr * 0.5)
if (total_steps + 1) % 100000000 == 0:
meta_model.update_lr(meta_lr=meta_model.opt.meta_lr * 0.5)
if (total_steps + 1) % 100000000 == 0 and meta_model.num_meta_steps >= 32:
meta_model.update_lr(meta_steps=int(meta_model.num_meta_steps/2))
def is_event_step_log_orig(step: int, event_freq_log: int = 1):
"""
Checks if event should trigger using log rule.
"""
step_log = step
event_freq = event_freq_log
while step_log // 10 > 0:
step_log //= 10
event_freq *= 10
if event_freq >= 1e4:
break # Max period = 10000
return step % event_freq == 0
def is_event_step_log_meta_spec(step: int, event_freq_log: int = 1):
if step in [1, 4, 8, 16, 32, 64, 128, 256, 512, 1024]:
return True
if step == 1025:
exit()
return False