-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterfaces.py
839 lines (661 loc) · 32.7 KB
/
interfaces.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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
"""Encapsulation of model training and testing for learning light path manifold.
"""
# Python
import sys
import matplotlib.pyplot as plt
from abc import ABCMeta, abstractmethod
# NumPy and PyTorch
import torch
import numpy as np
import torch.nn as nn
# Gharbi et al. dependency
#
# Cho et al. dependency
from support.utils import crop_like
class BaseInterface(metaclass=ABCMeta):
def __init__(self, models, optims, loss_funcs, args, visual=False, use_llpm_buf=False, manif_learn=False, w_manif=0.1):
self.models = models
self.optims = optims
self.loss_funcs = loss_funcs
self.args = args
self.visual = visual
self.use_llpm_buf = use_llpm_buf
self.manif_learn = manif_learn
self.w_manif = w_manif
self.iters = 0
self.m_losses = {}
self.best_err = 1e10
self.fixed_batch = None
@abstractmethod
def to_train_mode(self):
pass
@abstractmethod
def preprocess(self, batch=None):
pass
@abstractmethod
def train_batch(self, batch):
pass
@abstractmethod
def _manifold_forward(self, batch):
return {}
@abstractmethod
def _regress_forward(self, batch):
return {}
@abstractmethod
def _backward(self, batch, out, p_buffers):
return {}
@abstractmethod
def _logging(self, loss_dict):
pass
@abstractmethod
def _optimization(self):
pass
@abstractmethod
def to_eval_mode(self):
pass
@abstractmethod
def validate_batch(self, batch):
pass
@abstractmethod
def get_epoch_summary(self, mode, norm):
return 0.0
class KPCNInterface(BaseInterface):
def __init__(self, models, optims, loss_funcs, args, visual=False, use_llpm_buf=False, manif_learn=False, w_manif=0.1, train_branches=True, disentanglement_option="m11r11"):
if manif_learn:
assert 'backbone_diffuse' in models, "argument `models` dictionary should contain `'backbone_diffuse'` key."
assert 'backbone_specular' in models, "argument `models` dictionary should contain `'backbone_specular'` key."
assert 'dncnn' in models, "argument `models` dictionary should contain `'dncnn'` key."
if train_branches:
assert 'l_diffuse' in loss_funcs
assert 'l_specular' in loss_funcs
if manif_learn:
assert 'l_manif' in loss_funcs
assert 'l_recon' in loss_funcs
assert 'l_test' in loss_funcs
assert disentanglement_option in ['m11r11', 'm10r01', 'm11r01', 'm10r11']
super(KPCNInterface, self).__init__(models, optims, loss_funcs, args, visual, use_llpm_buf, manif_learn, w_manif)
self.train_branches = train_branches
self.disentanglement_option = disentanglement_option
def __str__(self):
return 'KPCNInterface'
def to_train_mode(self):
for model_name in self.models:
self.models[model_name].train()
assert 'optim_' + model_name in self.optims, '`optim_%s`: an optimization algorithm is not defined.'%(model_name)
def preprocess(self, batch=None):
assert 'target_total' in batch
assert 'target_diffuse' in batch
assert 'target_specular' in batch
assert 'kpcn_diffuse_in' in batch
assert 'kpcn_specular_in' in batch
assert 'kpcn_diffuse_buffer' in batch
assert 'kpcn_specular_buffer' in batch
assert 'kpcn_albedo' in batch
if self.use_llpm_buf:
assert 'paths' in batch
self.iters += 1
def train_batch(self, batch, grad_hook_mode=False):
out_manif = None
if self.use_llpm_buf:
self.models['backbone_diffuse'].zero_grad()
self.models['backbone_specular'].zero_grad()
p_buffers = self._manifold_forward(batch)
if self.iters % 1000 == 1:
pimg = np.mean(np.transpose(p_buffers['diffuse'].detach().cpu().numpy()[0,:,:3,...], (2, 3, 0, 1)), 2)
pimg = np.clip(pimg, 0.0, 1.0)
plt.imsave('../LLPM_results/pbuf_%s_diffuse.png'%(self.args.model_name), pimg)
pimg = np.mean(np.transpose(p_buffers['specular'].detach().cpu().numpy()[0,:,:3,...], (2, 3, 0, 1)), 2)
pimg = np.clip(pimg, 0.0, 1.0)
plt.imsave('../LLPM_results/pbuf_%s_specular.png'%(self.args.model_name), pimg)
""" Feature disentanglement """
_, _, c, _, _ = p_buffers['diffuse'].shape
assert c >= 2
if self.disentanglement_option == 'm11r11':
out_manif = p_buffers
elif self.disentanglement_option == 'm10r01':
out_manif = {
'diffuse': p_buffers['diffuse'][:,:,c//2:,...],
'specular': p_buffers['specular'][:,:,c//2:,...]
}
p_buffers = {
'diffuse': p_buffers['diffuse'][:,:,:c//2,...],
'specular': p_buffers['specular'][:,:,:c//2,...]
}
elif self.disentanglement_option == 'm11r01':
out_manif = p_buffers
p_buffers = {
'diffuse': p_buffers['diffuse'][:,:,:c//2,...],
'specular': p_buffers['specular'][:,:,:c//2,...]
}
elif self.disentanglement_option == 'm10r11':
out_manif = {
'diffuse': p_buffers['diffuse'][:,:,c//2:,...],
'specular': p_buffers['specular'][:,:,c//2:,...]
}
p_var_diffuse = p_buffers['diffuse'].var(1).mean(1, keepdims=True).detach()
p_var_diffuse /= p_buffers['diffuse'].shape[1] # spp
p_var_specular = p_buffers['specular'].var(1).mean(1, keepdims=True).detach()
p_var_specular /= p_buffers['specular'].shape[1]
# make a new batch
batch = {
'target_total': batch['target_total'],
'target_diffuse': batch['target_diffuse'],
'target_specular': batch['target_specular'],
'kpcn_diffuse_in': torch.cat([batch['kpcn_diffuse_in'], p_buffers['diffuse'].mean(1), p_var_diffuse], 1),
'kpcn_specular_in': torch.cat([batch['kpcn_specular_in'], p_buffers['specular'].mean(1), p_var_specular], 1),
'kpcn_diffuse_buffer': batch['kpcn_diffuse_buffer'],
'kpcn_specular_buffer': batch['kpcn_specular_buffer'],
'kpcn_albedo': batch['kpcn_albedo'],
}
self.models['dncnn'].zero_grad()
out = self._regress_forward(batch)
loss_dict = self._backward(batch, out, out_manif)
if grad_hook_mode: # do not update this model
return
self._logging(loss_dict)
self._optimization()
def _manifold_forward(self, batch):
p_buffer_diffuse = self.models['backbone_diffuse'](batch)
p_buffer_specular = self.models['backbone_specular'](batch)
p_buffers = {
'diffuse': p_buffer_diffuse,
'specular': p_buffer_specular
}
return p_buffers
def _regress_forward(self, batch):
return self.models['dncnn'](batch)
def _backward(self, batch, out, p_buffers):
assert 'radiance' in out
assert 'diffuse' in out
assert 'specular' in out
total, diffuse, specular = out['radiance'], out['diffuse'], out['specular']
loss_dict = {}
tgt_total = crop_like(batch['target_total'], total)
if self.train_branches: # training diffuse and specular branches
tgt_diffuse = crop_like(batch['target_diffuse'], diffuse)
L_diffuse = self.loss_funcs['l_diffuse'](diffuse, tgt_diffuse)
tgt_specular = crop_like(batch['target_specular'], specular)
L_specular = self.loss_funcs['l_specular'](specular, tgt_specular)
loss_dict['l_diffuse'] = L_diffuse.detach()
loss_dict['l_specular'] = L_specular.detach()
if self.manif_learn:
p_buffer_diffuse = crop_like(p_buffers['diffuse'], diffuse)
L_manif_diffuse = self.loss_funcs['l_manif'](p_buffer_diffuse, tgt_diffuse)
L_diffuse += L_manif_diffuse * self.w_manif
p_buffer_specular = crop_like(p_buffers['specular'], specular)
L_manif_specular = self.loss_funcs['l_manif'](p_buffer_specular, tgt_specular)
L_specular += L_manif_specular * self.w_manif
loss_dict['l_manif_diffuse'] = L_manif_diffuse.detach()
loss_dict['l_manif_specular'] = L_manif_specular.detach()
L_diffuse.backward()
L_specular.backward()
with torch.no_grad():
L_total = self.loss_funcs['l_recon'](total, tgt_total)
loss_dict['l_total'] = L_total.detach()
else: # post-training the entire system
L_total = self.loss_funcs['l_recon'](total, tgt_total)
loss_dict['l_total'] = L_total.detach()
L_total.backward()
with torch.no_grad():
loss_dict['rmse'] = self.loss_funcs['l_test'](total, tgt_total).detach()
return loss_dict
def _logging(self, loss_dict):
""" error handling """
for key in loss_dict:
if not torch.isfinite(loss_dict[key]).all():
raise RuntimeError("%s: Non-finite loss at train time."%(key))
# (NOTE: modified for each model)
for model_name in self.models:
nn.utils.clip_grad_value_(self.models[model_name].parameters(), clip_value=1.0)
""" logging """
for key in loss_dict:
if 'm_' + key not in self.m_losses:
self.m_losses['m_' + key] = torch.tensor(0.0, device=loss_dict[key].device)
self.m_losses['m_' + key] += loss_dict[key]
def _optimization(self):
for model_name in self.models:
self.optims['optim_' + model_name].step()
def to_eval_mode(self):
for model_name in self.models:
self.models[model_name].eval()
self.m_losses['m_val'] = torch.tensor(0.0)
def validate_batch(self, batch):
p_buffers = None
if self.use_llpm_buf:
p_buffers = self._manifold_forward(batch)
""" Feature disentanglement """
_, _, c, _, _ = p_buffers['diffuse'].shape
assert c >= 2
if self.disentanglement_option == 'm10r01' or self.disentanglement_option == 'm11r01':
p_buffers = {
'diffuse': p_buffers['diffuse'][:,:,:c//2,...],
'specular': p_buffers['specular'][:,:,:c//2,...]
}
p_var_diffuse = p_buffers['diffuse'].var(1).mean(1, keepdims=True).detach()
p_var_diffuse /= p_buffers['diffuse'].shape[1] # spp
p_var_specular = p_buffers['specular'].var(1).mean(1, keepdims=True).detach()
p_var_specular /= p_buffers['specular'].shape[1]
# make a new batch
batch = {
'target_total': batch['target_total'],
'target_diffuse': batch['target_diffuse'],
'target_specular': batch['target_specular'],
'kpcn_diffuse_in': torch.cat([batch['kpcn_diffuse_in'], p_buffers['diffuse'].mean(1), p_var_diffuse], 1),
'kpcn_specular_in': torch.cat([batch['kpcn_specular_in'], p_buffers['specular'].mean(1), p_var_specular], 1),
'kpcn_diffuse_buffer': batch['kpcn_diffuse_buffer'],
'kpcn_specular_buffer': batch['kpcn_specular_buffer'],
'kpcn_albedo': batch['kpcn_albedo'],
}
out = self._regress_forward(batch)
tgt_total = crop_like(batch['target_total'], out['radiance'])
L_total = self.loss_funcs['l_test'](out['radiance'], tgt_total)
if self.m_losses['m_val'] == 0.0 and self.m_losses['m_val'].device != L_total.device:
self.m_losses['m_val'] = torch.tensor(0.0, device=L_total.device)
self.m_losses['m_val'] += L_total.detach()
return out['radiance'], p_buffers
def get_epoch_summary(self, mode, norm):
if mode == 'train':
print('[][][]', end=' ')
for key in self.m_losses:
if key == 'm_val':
continue
tr_l_tmp = self.m_losses[key] / (norm * 2)
tr_l_tmp *= 1000
print('%s: %.3fE-3'%(key, tr_l_tmp), end='\t')
self.m_losses[key] = torch.tensor(0.0, device=self.m_losses[key].device)
print('')
return -1.0
else:
return self.m_losses['m_val'].item() / (norm * 2)
class SBMCInterface(BaseInterface):
def __init__(self, models, optims, loss_funcs, args, visual=False, use_llpm_buf=False, manif_learn=False, w_manif=0.1, use_sbmc_buf=True, disentangle="m11r11"):
if manif_learn:
assert 'backbone' in models, "argument `models` dictionary should contain `'backbone'` key."
assert 'dncnn' in models, "argument `models` dictionary should contain `'dncnn'` key."
if manif_learn:
assert 'l_manif' in loss_funcs
assert 'l_recon' in loss_funcs
assert 'l_test' in loss_funcs
assert disentangle in ['m11r11', 'm10r01', 'm11r01', 'm10r11']
super(SBMCInterface, self).__init__(models, optims, loss_funcs, args, visual, use_llpm_buf, manif_learn, w_manif)
self.disentangle = disentangle
self.use_sbmc_buf = use_sbmc_buf
def __str__(self):
return 'SBMCInterface'
def to_train_mode(self):
for model_name in self.models:
self.models[model_name].train()
assert 'optim_' + model_name in self.optims, '`optim_%s`: an optimization algorithm is not defined.'%(model_name)
def preprocess(self, batch=None):
assert 'target_image' in batch
assert 'radiance' in batch
assert 'features' in batch
if self.use_llpm_buf:
assert 'paths' in batch
self.iters += 1
def train_batch(self, batch, grad_hook_mode=False):
out_manif = None
if self.use_llpm_buf:
self.models['backbone'].zero_grad()
p_buffer = self._manifold_forward(batch)
if self.iters % 1000 == 1:
pimg = np.mean(np.transpose(p_buffer.detach().cpu().numpy()[0,:,:3,...], (2, 3, 0, 1)), 2)
pimg = np.clip(pimg, 0.0, 1.0)
plt.imsave('../LLPM_results/pbuf_%s.png'%(self.args.model_name), pimg)
""" Feature disentanglement """
_, s, c, _, _ = p_buffer.shape
assert c >= 2
if self.disentangle == 'm11r11':
out_manif = p_buffer
elif self.disentangle == 'm10r01':
out_manif = p_buffer[:,:,c//2:,...]
p_buffer = p_buffer[:,:,:c//2,...]
elif self.disentangle == 'm11r01':
out_manif = p_buffer
p_buffer = p_buffer[:,:,:c//2,...]
elif self.disentangle == 'm10r11':
out_manif = p_buffer[:,:,c//2:,...]
p_var = p_buffer.var(1).mean(1, keepdims=True)
p_var /= s # spp
p_var = torch.stack([p_var,]*s, axis=1).detach()
# make a new batch
batch = {
'target_image': batch['target_image'],
'radiance': batch['radiance'],
'features': torch.cat([batch['features'], p_buffer, p_var], 2),
}
self.models['dncnn'].zero_grad()
out = self._regress_forward(batch)
loss_dict = self._backward(batch, out, out_manif)
if grad_hook_mode: # do not update this model
return
self._logging(loss_dict)
self._optimization()
def _manifold_forward(self, batch):
return self.models['backbone'](batch)
def _regress_forward(self, batch):
return self.models['dncnn'](batch)
def _backward(self, batch, out, p_buffer):
loss_dict = {}
tgt_total = crop_like(batch['target_image'], out)
L_total = self.loss_funcs['l_recon'](out, tgt_total)
if self.manif_learn:
p_buffer = crop_like(p_buffer, out)
L_manif = self.loss_funcs['l_manif'](p_buffer, tgt_total)
loss_dict['l_manif'] = L_manif.detach()
loss_dict['l_recon'] = L_total.detach()
L_total += L_manif * self.w_manif
loss_dict['l_total'] = L_total.detach()
L_total.backward()
with torch.no_grad():
loss_dict['rmse'] = self.loss_funcs['l_test'](out, tgt_total).detach()
return loss_dict
def _logging(self, loss_dict):
""" error handling """
for key in loss_dict:
if not torch.isfinite(loss_dict[key]).all():
raise RuntimeError("%s: Non-finite loss at train time."%(key))
# (NOTE: modified for each model)
clip = 1000
for model_name in self.models:
actual = nn.utils.clip_grad_norm_(self.models[model_name].parameters(), max_norm=clip)
if actual > clip:
print("Clipped %s gradients %f -> %f"%(model_name, clip, actual))
""" logging """
for key in loss_dict:
if 'm_' + key not in self.m_losses:
self.m_losses['m_' + key] = torch.tensor(0.0, device=loss_dict[key].device)
self.m_losses['m_' + key] += loss_dict[key]
def _optimization(self):
for model_name in self.models:
self.optims['optim_' + model_name].step()
def to_eval_mode(self):
for model_name in self.models:
self.models[model_name].eval()
self.m_losses['m_val'] = torch.tensor(0.0)
def validate_batch(self, batch):
p_buffer = None
if self.use_llpm_buf:
p_buffer = self._manifold_forward(batch)
""" Feature disentanglement """
_, s, c, _, _ = p_buffer.shape
assert c >= 2
if self.disentangle == 'm10r01':
p_buffer = p_buffer[:,:,:c//2,...]
elif self.disentangle == 'm11r01':
p_buffer = p_buffer[:,:,:c//2,...]
p_var = p_buffer.var(1).mean(1, keepdims=True)
p_var /= s # spp
p_var = torch.stack([p_var,]*s, axis=1).detach()
# make a new batch
batch = {
'target_image': batch['target_image'],
'radiance': batch['radiance'],
'features': torch.cat([batch['features'], p_buffer, p_var], 2),
}
out = self._regress_forward(batch)
tgt_total = crop_like(batch['target_image'], out)
L_total = self.loss_funcs['l_test'](out, tgt_total)
if self.m_losses['m_val'] == 0.0 and self.m_losses['m_val'].device != L_total.device:
self.m_losses['m_val'] = torch.tensor(0.0, device=L_total.device)
self.m_losses['m_val'] += L_total.detach()
return out, p_buffer
def get_epoch_summary(self, mode, norm):
if mode == 'train':
print('[][][]', end=' ')
for key in self.m_losses:
if key == 'm_val':
continue
tr_l_tmp = self.m_losses[key] / (norm * 2)
tr_l_tmp *= 1000
print('%s: %.3fE-3'%(key, tr_l_tmp), end='\t')
self.m_losses[key] = torch.tensor(0.0, device=self.m_losses[key].device)
print('')
return -1.0
else:
return self.m_losses['m_val'].item() / (norm * 2)
class KPCNRefInterface(KPCNInterface):
def __init__(self, models, optims, loss_funcs, args, visual=False, use_llpm_buf=False, manif_learn=False, w_manif=0.1, train_branches=True):
assert not use_llpm_buf
assert not manif_learn
if train_branches:
assert 'l_diffuse' in loss_funcs
assert 'l_specular' in loss_funcs
assert 'l_recon' in loss_funcs
assert 'l_test' in loss_funcs
super(KPCNRefInterface, self).__init__(models, optims, loss_funcs, args, visual, use_llpm_buf, manif_learn, w_manif, train_branches)
self.train_branches = train_branches
def train_batch(self, batch):
out_manif = None
batch = {
'target_total': batch['target_total'],
'target_diffuse': batch['target_diffuse'],
'target_specular': batch['target_specular'],
'kpcn_diffuse_in': torch.cat([batch['kpcn_diffuse_in'], batch['target_diffuse']], 1),
'kpcn_specular_in': torch.cat([batch['kpcn_specular_in'], batch['target_specular']], 1),
'kpcn_diffuse_buffer': batch['kpcn_diffuse_buffer'],
'kpcn_specular_buffer': batch['kpcn_specular_buffer'],
'kpcn_albedo': batch['kpcn_albedo'],
}
self.models['dncnn'].zero_grad()
out = self._regress_forward(batch)
loss_dict = self._backward(batch, out, out_manif)
self._logging(loss_dict)
self._optimization()
def validate_batch(self, batch):
p_buffers = None
batch = {
'target_total': batch['target_total'],
'target_diffuse': batch['target_diffuse'],
'target_specular': batch['target_specular'],
'kpcn_diffuse_in': torch.cat([batch['kpcn_diffuse_in'], batch['target_diffuse']], 1),
'kpcn_specular_in': torch.cat([batch['kpcn_specular_in'], batch['target_specular']], 1),
'kpcn_diffuse_buffer': batch['kpcn_diffuse_buffer'],
'kpcn_specular_buffer': batch['kpcn_specular_buffer'],
'kpcn_albedo': batch['kpcn_albedo'],
}
out = self._regress_forward(batch)
tgt_total = crop_like(batch['target_total'], out['radiance'])
L_total = self.loss_funcs['l_test'](out['radiance'], tgt_total)
if self.m_losses['m_val'] == 0.0 and self.m_losses['m_val'].device != L_total.device:
self.m_losses['m_val'] = torch.tensor(0.0, device=L_total.device)
self.m_losses['m_val'] += L_total.detach()
return out['radiance'], p_buffers
class KPCNPreInterface(KPCNInterface):
def __init__(self, models, optims, loss_funcs, args, visual=False, manif_learn=False, w_manif=0.1, train_branches=True):
# if manif_learn is True, pre-train the manifold feature extractor.
# else, train KPCN using the freezed & pre-trained feature extractor.
if train_branches:
assert 'l_diffuse' in loss_funcs
assert 'l_specular' in loss_funcs
assert 'l_recon' in loss_funcs
assert 'l_test' in loss_funcs
use_llpm_buf = True
super(KPCNPreInterface, self).__init__(models, optims, loss_funcs, args, visual, use_llpm_buf, manif_learn, w_manif, train_branches)
def to_train_mode(self):
for model_name in self.models:
if self.manif_learn:
if 'dncnn' in model_name: # KPCN
self.models[model_name].eval()
if 'backbone' in model_name: # manifold feature extractor
self.models[model_name].train()
else:
if 'dncnn' in model_name: # KPCN
self.models[model_name].train()
if 'backbone' in model_name: # manifold feature extractor
self.models[model_name].eval()
assert 'optim_' + model_name in self.optims, '`optim_%s`: an optimization algorithm is not defined.'%(model_name)
def train_batch(self, batch):
out_manif = None
if self.manif_learn:
self.models['backbone_diffuse'].zero_grad()
self.models['backbone_specular'].zero_grad()
p_buffers = self._manifold_forward(batch)
if self.iters % 1000 == 1:
pimg = np.mean(np.transpose(p_buffers['diffuse'].detach().cpu().numpy()[0,:,:3,...], (2, 3, 0, 1)), 2)
pimg = np.clip(pimg, 0.0, 1.0)
plt.imsave('../LLPM_results/pbuf_%s_diffuse.png'%(self.args.model_name), pimg)
pimg = np.mean(np.transpose(p_buffers['specular'].detach().cpu().numpy()[0,:,:3,...], (2, 3, 0, 1)), 2)
pimg = np.clip(pimg, 0.0, 1.0)
plt.imsave('../LLPM_results/pbuf_%s_specular.png'%(self.args.model_name), pimg)
out_manif = p_buffers
loss_dict = self._backward(batch, None, out_manif)
self._logging(loss_dict)
self._optimization()
else:
self.models['backbone_diffuse'].zero_grad()
self.models['backbone_specular'].zero_grad()
self.models['dncnn'].zero_grad()
p_buffers = self._manifold_forward(batch)
out_manif = p_buffers
p_var_diffuse = p_buffers['diffuse'].var(1).mean(1, keepdims=True).detach()
p_var_diffuse /= p_buffers['diffuse'].shape[1] # spp
p_var_specular = p_buffers['specular'].var(1).mean(1, keepdims=True).detach()
p_var_specular /= p_buffers['specular'].shape[1]
# make a new batch
batch = {
'target_total': batch['target_total'],
'target_diffuse': batch['target_diffuse'],
'target_specular': batch['target_specular'],
'kpcn_diffuse_in': torch.cat([batch['kpcn_diffuse_in'], p_buffers['diffuse'].mean(1), p_var_diffuse], 1),
'kpcn_specular_in': torch.cat([batch['kpcn_specular_in'], p_buffers['specular'].mean(1), p_var_specular], 1),
'kpcn_diffuse_buffer': batch['kpcn_diffuse_buffer'],
'kpcn_specular_buffer': batch['kpcn_specular_buffer'],
'kpcn_albedo': batch['kpcn_albedo'],
}
out = self._regress_forward(batch)
loss_dict = self._backward(batch, out, None)
self._logging(loss_dict)
self._optimization()
def _backward(self, batch, out, p_buffers):
assert not out or 'radiance' in out
assert not out or 'diffuse' in out
assert not out or 'specular' in out
if out:
total, diffuse, specular = out['radiance'], out['diffuse'], out['specular']
tgt_total = crop_like(batch['target_total'], total)
loss_dict = {}
if self.manif_learn:
tgt_diffuse = batch['target_diffuse'] #crop_like(batch['target_diffuse'], diffuse)
tgt_specular = batch['target_specular'] #crop_like(batch['target_specular'], specular)
p_buffer_diffuse = p_buffers['diffuse'] #crop_like(p_buffers['diffuse'], diffuse)
L_manif_diffuse = self.loss_funcs['l_manif'](p_buffer_diffuse, tgt_diffuse) * self.w_manif
p_buffer_specular = p_buffers['specular'] #crop_like(p_buffers['specular'], specular)
L_manif_specular = self.loss_funcs['l_manif'](p_buffer_specular, tgt_specular) * self.w_manif
loss_dict['l_manif_diffuse'] = L_manif_diffuse.detach() / self.w_manif
loss_dict['l_manif_specular'] = L_manif_specular.detach() / self.w_manif
L_manif_diffuse.backward()
L_manif_specular.backward()
elif self.train_branches:
tgt_diffuse = crop_like(batch['target_diffuse'], diffuse)
L_diffuse = self.loss_funcs['l_diffuse'](diffuse, tgt_diffuse)
tgt_specular = crop_like(batch['target_specular'], specular)
L_specular = self.loss_funcs['l_specular'](specular, tgt_specular)
loss_dict['l_diffuse'] = L_diffuse.detach()
loss_dict['l_specular'] = L_specular.detach()
L_diffuse.backward()
L_specular.backward()
with torch.no_grad():
L_total = self.loss_funcs['l_recon'](total, tgt_total)
loss_dict['l_total'] = L_total.detach()
else:
L_total = self.loss_funcs['l_recon'](total, tgt_total)
loss_dict['l_total'] = L_total.detach()
L_total.backward()
return loss_dict
def _logging(self, loss_dict):
""" error handling """
for key in loss_dict:
if not torch.isfinite(loss_dict[key]).all():
raise RuntimeError("%s: Non-finite loss at train time."%(key))
# (NOTE: modified for each model)
for model_name in self.models:
if self.manif_learn:
if 'backbone' in model_name: # manifold feature extractor
nn.utils.clip_grad_value_(self.models[model_name].parameters(), clip_value=1.0)
else:
if 'dncnn' in model_name: # KPCN
nn.utils.clip_grad_value_(self.models[model_name].parameters(), clip_value=1.0)
""" logging """
for key in loss_dict:
if 'm_' + key not in self.m_losses:
self.m_losses['m_' + key] = torch.tensor(0.0, device=loss_dict[key].device)
self.m_losses['m_' + key] += loss_dict[key]
def _optimization(self):
for model_name in self.models:
if self.manif_learn:
if 'backbone' in model_name:
self.optims['optim_' + model_name].step()
else:
if 'dncnn' in model_name:
self.optims['optim_' + model_name].step()
class LBMCInterface(SBMCInterface):
def __init__(self, models, optims, loss_funcs, args, use_llpm_buf=False, manif_learn=False, w_manif=0.1, disentangle='m11r11'):
if manif_learn:
assert 'backbone' in models, "argument `models` dictionary should contain `'backbone'` key."
assert 'dncnn' in models, "argument `models` dictionary should contain `'dncnn'` key."
if manif_learn:
assert 'l_manif' in loss_funcs
assert 'l_recon' in loss_funcs
assert 'l_test' in loss_funcs
assert disentangle in ['m11r11', 'm10r01', 'm11r01', 'm10r11']
super(LBMCInterface, self).__init__(models, optims, loss_funcs, args, False, use_llpm_buf, manif_learn, w_manif, False, disentangle)
def __str__(self):
return 'LBMCInterface'
def train_batch(self, batch, grad_hook_mode=False):
out_manif = None
if self.use_llpm_buf:
self.models['backbone'].zero_grad()
p_buffer = self._manifold_forward(batch)
if self.iters % 1000 == 1:
pimg = np.mean(np.transpose(p_buffer.detach().cpu().numpy()[0,:,:3,...], (2, 3, 0, 1)), 2)
pimg = np.clip(pimg, 0.0, 1.0)
plt.imsave('../LLPM_results/pbuf_%s.png'%(self.args.model_name), pimg)
""" Feature disentanglement """
_, s, c, _, _ = p_buffer.shape
assert c >= 2
if self.disentangle == 'm11r11':
out_manif = p_buffer
elif self.disentangle == 'm10r01':
out_manif = p_buffer[:,:,c//2:,...]
p_buffer = p_buffer[:,:,:c//2,...]
elif self.disentangle == 'm11r01':
out_manif = p_buffer
p_buffer = p_buffer[:,:,:c//2,...]
elif self.disentangle == 'm10r11':
out_manif = p_buffer[:,:,c//2:,...]
p_var = p_buffer.var(1).mean(1, keepdims=True)
p_var /= s # spp
p_var = torch.stack([p_var,]*s, axis=1).detach()
# make a new batch
batch = {
'target_image': batch['target_image'],
'radiance': batch['radiance'],
'features': torch.cat([batch['features'], p_buffer, p_var], 2),
}
self.models['dncnn'].zero_grad()
out = self._regress_forward(batch)
loss_dict = self._backward(batch, out, out_manif)
if grad_hook_mode: # do not update this model
return
self._logging(loss_dict)
self._optimization()
def _logging(self, loss_dict):
""" error handling """
for key in loss_dict:
if not torch.isfinite(loss_dict[key]).all():
raise RuntimeError("%s: Non-finite loss at train time."%(key))
# (NOTE: modified for each model)
GRADIENT_CLAMP_N = 0.25 * 1000
#GRADIENT_CLAMP = 0.001 * 1000
for model_name in self.models:
actual = nn.utils.clip_grad_norm_(self.models[model_name].parameters(), GRADIENT_CLAMP_N)
#nn.utils.clip_grad_value_(self.models[model_name].parameters(), GRADIENT_CLAMP)
if actual > GRADIENT_CLAMP_N:
print("Clipped %s gradients %f -> %f"%(model_name, GRADIENT_CLAMP_N, actual))
#self.models[model_name].zero_grad()
""" logging """
for key in loss_dict:
if 'm_' + key not in self.m_losses:
self.m_losses['m_' + key] = torch.tensor(0.0, device=loss_dict[key].device)
self.m_losses['m_' + key] += loss_dict[key]