-
Notifications
You must be signed in to change notification settings - Fork 15
/
realnvp.py
941 lines (809 loc) · 36.3 KB
/
realnvp.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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
"""Utility classes for real NVP.
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class WeightNormConv2d(nn.Module):
def __init__(self, in_dim, out_dim, kernel_size, stride=1, padding=0,
bias=True, weight_norm=True, scale=False):
"""Intializes a Conv2d augmented with weight normalization.
(See torch.nn.utils.weight_norm for detail.)
Args:
in_dim: number of input channels.
out_dim: number of output channels.
kernel_size: size of convolving kernel.
stride: stride of convolution.
padding: zero-padding added to both sides of input.
bias: True if include learnable bias parameters, False otherwise.
weight_norm: True if apply weight normalization, False otherwise.
scale: True if include magnitude parameters, False otherwise.
"""
super(WeightNormConv2d, self).__init__()
if weight_norm:
self.conv = nn.utils.weight_norm(
nn.Conv2d(in_dim, out_dim, kernel_size,
stride=stride, padding=padding, bias=bias))
if not scale:
self.conv.weight_g.data = torch.ones_like(self.conv.weight_g.data)
self.conv.weight_g.requires_grad = False # freeze scaling
else:
self.conv = nn.Conv2d(in_dim, out_dim, kernel_size,
stride=stride, padding=padding, bias=bias)
def forward(self, x):
"""Forward pass.
Args:
x: input tensor.
Returns:
transformed tensor.
"""
return self.conv(x)
class ResidualBlock(nn.Module):
def __init__(self, dim, bottleneck, weight_norm):
"""Initializes a ResidualBlock.
Args:
dim: number of input and output features.
bottleneck: True if use bottleneck, False otherwise.
weight_norm: True if apply weight normalization, False otherwise.
"""
super(ResidualBlock, self).__init__()
self.in_block = nn.Sequential(
nn.BatchNorm2d(dim),
nn.ReLU())
if bottleneck:
self.res_block = nn.Sequential(
WeightNormConv2d(dim, dim, (1, 1), stride=1, padding=0,
bias=False, weight_norm=weight_norm, scale=False),
nn.BatchNorm2d(dim),
nn.ReLU(),
WeightNormConv2d(dim, dim, (3, 3), stride=1, padding=1,
bias=False, weight_norm=weight_norm, scale=False),
nn.BatchNorm2d(dim),
nn.ReLU(),
WeightNormConv2d(dim, dim, (1, 1), stride=1, padding=0,
bias=True, weight_norm=weight_norm, scale=True))
else:
self.res_block = nn.Sequential(
WeightNormConv2d(dim, dim, (3, 3), stride=1, padding=1,
bias=False, weight_norm=weight_norm, scale=False),
nn.BatchNorm2d(dim),
nn.ReLU(),
WeightNormConv2d(dim, dim, (3, 3), stride=1, padding=1,
bias=True, weight_norm=weight_norm, scale=True))
def forward(self, x):
"""Forward pass.
Args:
x: input tensor.
Returns:
transformed tensor.
"""
return x + self.res_block(self.in_block(x))
class ResidualModule(nn.Module):
def __init__(self, in_dim, dim, out_dim,
res_blocks, bottleneck, skip, weight_norm):
"""Initializes a ResidualModule.
Args:
in_dim: number of input features.
dim: number of features in residual blocks.
out_dim: number of output features.
res_blocks: number of residual blocks to use.
bottleneck: True if use bottleneck, False otherwise.
skip: True if use skip architecture, False otherwise.
weight_norm: True if apply weight normalization, False otherwise.
"""
super(ResidualModule, self).__init__()
self.res_blocks = res_blocks
self.skip = skip
if res_blocks > 0:
self.in_block = WeightNormConv2d(in_dim, dim, (3, 3), stride=1,
padding=1, bias=True, weight_norm=weight_norm, scale=False)
self.core_block = nn.ModuleList(
[ResidualBlock(dim, bottleneck, weight_norm)
for _ in range(res_blocks)])
self.out_block = nn.Sequential(
nn.BatchNorm2d(dim),
nn.ReLU(),
WeightNormConv2d(dim, out_dim, (1, 1), stride=1, padding=0,
bias=True, weight_norm=weight_norm, scale=True))
if skip:
self.in_skip = WeightNormConv2d(dim, dim, (1, 1), stride=1,
padding=0, bias=True, weight_norm=weight_norm, scale=True)
self.core_skips = nn.ModuleList(
[WeightNormConv2d(
dim, dim, (1, 1), stride=1, padding=0, bias=True,
weight_norm=weight_norm, scale=True)
for _ in range(res_blocks)])
else:
if bottleneck:
self.block = nn.Sequential(
WeightNormConv2d(in_dim, dim, (1, 1), stride=1, padding=0,
bias=False, weight_norm=weight_norm, scale=False),
nn.BatchNorm2d(dim),
nn.ReLU(),
WeightNormConv2d(dim, dim, (3, 3), stride=1, padding=1,
bias=False, weight_norm=weight_norm, scale=False),
nn.BatchNorm2d(dim),
nn.ReLU(),
WeightNormConv2d(dim, out_dim, (1, 1), stride=1, padding=0,
bias=True, weight_norm=weight_norm, scale=True))
else:
self.block = nn.Sequential(
WeightNormConv2d(in_dim, dim, (3, 3), stride=1, padding=1,
bias=False, weight_norm=weight_norm, scale=False),
nn.BatchNorm2d(dim),
nn.ReLU(),
WeightNormConv2d(dim, out_dim, (3, 3), stride=1, padding=1,
bias=True, weight_norm=weight_norm, scale=True))
def forward(self, x):
"""Forward pass.
Args:
x: input tensor.
Returns:
transformed tensor.
"""
if self.res_blocks > 0:
x = self.in_block(x)
if self.skip:
out = self.in_skip(x)
for i in range(len(self.core_block)):
x = self.core_block[i](x)
if self.skip:
out = out + self.core_skips[i](x)
if self.skip:
x = out
return self.out_block(x)
else:
return self.block(x)
class AbstractCoupling(nn.Module):
def __init__(self, mask_config, hps):
"""Initializes an AbstractCoupling.
Args:
mask_config: mask configuration (see build_mask() for more detail).
hps: the set of hyperparameters.
"""
super(AbstractCoupling, self).__init__()
self.mask_config = mask_config
self.res_blocks = hps.res_blocks
self.bottleneck = hps.bottleneck
self.skip = hps.skip
self.weight_norm = hps.weight_norm
self.coupling_bn = hps.coupling_bn
def build_mask(self, size, config=1.):
"""Builds a binary checkerboard mask.
(Only for constructing masks for checkerboard coupling layers.)
Args:
size: height/width of features.
config: mask configuration that determines which pixels to mask up.
if 1: if 0:
1 0 0 1
0 1 1 0
Returns:
a binary mask (1: pixel on, 0: pixel off).
"""
mask = np.arange(size).reshape(-1, 1) + np.arange(size)
mask = np.mod(config + mask, 2)
mask = mask.reshape(-1, 1, size, size)
return torch.tensor(mask.astype('float32'))
def batch_stat(self, x):
"""Compute (spatial) batch statistics.
Args:
x: input minibatch.
Returns:
batch mean and variance.
"""
mean = torch.mean(x, dim=(0, 2, 3), keepdim=True)
var = torch.mean((x - mean)**2, dim=(0, 2, 3), keepdim=True)
return mean, var
class CheckerboardAdditiveCoupling(AbstractCoupling):
def __init__(self, in_out_dim, mid_dim, size, mask_config, hps):
"""Initializes a CheckerboardAdditiveCoupling.
Args:
in_out_dim: number of input and output features.
mid_dim: number of features in residual blocks.
size: height/width of features.
mask_config: mask configuration (see build_mask() for more detail).
hps: the set of hyperparameters.
"""
super(CheckerboardAdditiveCoupling, self).__init__(mask_config, hps)
self.mask = self.build_mask(size, config=mask_config).cuda()
self.in_bn = nn.BatchNorm2d(in_out_dim)
self.block = nn.Sequential(
nn.ReLU(),
ResidualModule(2*in_out_dim+1, mid_dim, in_out_dim,
self.res_blocks, self.bottleneck, self.skip, self.weight_norm))
self.out_bn = nn.BatchNorm2d(in_out_dim, affine=False)
def forward(self, x, reverse=False):
"""Forward pass.
Args:
x: input tensor.
reverse: True in inference mode, False in sampling mode.
Returns:
transformed tensor and log of diagonal elements of Jacobian.
"""
[B, _, _, _] = list(x,size())
mask = self.mask.repeat(B, 1, 1, 1)
x_ = self.in_bn(x * mask)
x_ = torch.cat((x_, -x_), dim=1)
x_ = torch.cat((x_, mask), dim=1) # 2C+1 channels
shift = self.block(x_) * (1. - mask)
log_diag_J = torch.zeros_like(x) # unit Jacobian determinant
# See Eq(3) and Eq(4) in NICE and Section 3.7 in real NVP
if reverse:
if self.coupling_bn:
mean, var = self.out_bn.running_mean, self.out_bn.running_var
mean = mean.reshape(-1, 1, 1, 1).transpose(0, 1)
var = var.reshape(-1, 1, 1, 1).transpose(0, 1)
x = x * torch.exp(0.5 * torch.log(var + 1e-5) * (1. - mask)) \
+ mean * (1. - mask)
x = x - shift
else:
x = x + shift
if self.coupling_bn:
if self.training:
_, var = self.batch_stat(x)
else:
var = self.out_bn.running_var
var = var.reshape(-1, 1, 1, 1).transpose(0, 1)
x = self.out_bn(x) * (1. - mask) + x * mask
log_diag_J = log_diag_J - 0.5 * torch.log(var + 1e-5) * (1. - mask)
return x, log_diag_J
class CheckerboardAffineCoupling(AbstractCoupling):
def __init__(self, in_out_dim, mid_dim, size, mask_config, hps):
"""Initializes a CheckerboardAffineCoupling.
Args:
in_out_dim: number of input and output features.
mid_dim: number of features in residual blocks.
size: height/width of features.
mask_config: mask configuration (see build_mask() for more detail).
hps: the set of hyperparameters.
"""
super(CheckerboardAffineCoupling, self).__init__(mask_config, hps)
self.mask = self.build_mask(size, config=mask_config).cuda()
self.scale = nn.Parameter(torch.zeros(1), requires_grad=True)
self.scale_shift = nn.Parameter(torch.zeros(1), requires_grad=True)
self.in_bn = nn.BatchNorm2d(in_out_dim)
self.block = nn.Sequential( # 1st half of resnet: shift
nn.ReLU(), # 2nd half of resnet: log_rescale
ResidualModule(2*in_out_dim+1, mid_dim, 2*in_out_dim,
self.res_blocks, self.bottleneck, self.skip, self.weight_norm))
self.out_bn = nn.BatchNorm2d(in_out_dim, affine=False)
def forward(self, x, reverse=False):
"""Forward pass.
Args:
x: input tensor.
reverse: True in inference mode, False in sampling mode.
Returns:
transformed tensor and log of diagonal elements of Jacobian.
"""
[B, C, _, _] = list(x.size())
mask = self.mask.repeat(B, 1, 1, 1)
x_ = self.in_bn(x * mask)
x_ = torch.cat((x_, -x_), dim=1)
x_ = torch.cat((x_, mask), dim=1) # 2C+1 channels
(shift, log_rescale) = self.block(x_).split(C, dim=1)
log_rescale = self.scale * torch.tanh(log_rescale) + self.scale_shift
shift = shift * (1. - mask)
log_rescale = log_rescale * (1. - mask)
log_diag_J = log_rescale # See Eq(6) in real NVP
# See Eq(7) and Eq(8) and Section 3.7 in real NVP
if reverse:
if self.coupling_bn:
mean, var = self.out_bn.running_mean, self.out_bn.running_var
mean = mean.reshape(-1, 1, 1, 1).transpose(0, 1)
var = var.reshape(-1, 1, 1, 1).transpose(0, 1)
x = x * torch.exp(0.5 * torch.log(var + 1e-5) * (1. - mask)) \
+ mean * (1. - mask)
x = (x - shift) * torch.exp(-log_rescale)
else:
x = x * torch.exp(log_rescale) + shift
if self.coupling_bn:
if self.training:
_, var = self.batch_stat(x)
else:
var = self.out_bn.running_var
var = var.reshape(-1, 1, 1, 1).transpose(0, 1)
x = self.out_bn(x) * (1. - mask) + x * mask
log_diag_J = log_diag_J - 0.5 * torch.log(var + 1e-5) * (1. - mask)
return x, log_diag_J
class CheckerboardCoupling(nn.Module):
def __init__(self, in_out_dim, mid_dim, size, mask_config, hps):
"""Initializes a CheckerboardCoupling.
Args:
in_out_dim: number of input and output features.
mid_dim: number of features in residual blocks.
size: height/width of features.
mask_config: mask configuration (see build_mask() for more detail).
hps: the set of hyperparameters.
"""
super(CheckerboardCoupling, self).__init__()
if hps.affine:
self.coupling = CheckerboardAffineCoupling(
in_out_dim, mid_dim, size, mask_config, hps)
else:
self.coupling = CheckerboardAdditiveCoupling(
in_out_dim, mid_dim, size, mask_config, hps)
def forward(self, x, reverse=False):
"""Forward pass.
Args:
x: input tensor.
reverse: True in inference mode, False in sampling mode.
Returns:
transformed tensor and log of diagonal elements of Jacobian.
"""
return self.coupling(x, reverse)
class ChannelwiseAdditiveCoupling(AbstractCoupling):
def __init__(self, in_out_dim, mid_dim, mask_config, hps):
"""Initializes a ChannelwiseAdditiveCoupling.
Args:
in_out_dim: number of input and output features.
mid_dim: number of features in residual blocks.
mask_config: 1 if change the top half, 0 if change the bottom half.
hps: the set of hyperparameters.
"""
super(ChannelwiseAdditiveCoupling, self).__init__(mask_config, hps)
self.in_bn = nn.BatchNorm2d(in_out_dim//2)
self.block = nn.Sequential(
nn.ReLU(),
ResidualModule(in_out_dim, mid_dim, in_out_dim//2,
self.res_blocks, self.bottleneck, self.skip, self.weight_norm))
self.out_bn = nn.BatchNorm2d(in_out_dim//2, affine=False)
def forward(self, x, reverse=False):
"""Forward pass.
Args:
x: input tensor.
reverse: True in inference mode, False in sampling mode.
Returns:
transformed tensor and log of diagonal elements of Jacobian.
"""
[_, C, _, _] = list(x.size())
if self.mask_config:
(on, off) = x.split(C//2, dim=1)
else:
(off, on) = x.split(C//2, dim=1)
off_ = self.in_bn(off)
off_ = torch.cat((off_, -off_), dim=1) # C channels
shift = self.block(off_)
log_diag_J = torch.zeros_like(x) # unit Jacobian determinant
# See Eq(3) and Eq(4) in NICE and Section 3.7 in real NVP
if reverse:
if self.coupling_bn:
mean, var = self.out_bn.running_mean, self.out_bn.running_var
mean = mean.reshape(-1, 1, 1, 1).transpose(0, 1)
var = var.reshape(-1, 1, 1, 1).transpose(0, 1)
on = on * torch.exp(0.5 * torch.log(var + 1e-5)) + mean
on = on - shift
else:
on = on + shift
if self.coupling_bn:
if self.training:
_, var = self.batch_stat(on)
else:
var = self.out_bn.running_var
var = var.reshape(-1, 1, 1, 1).transpose(0, 1)
on = self.out_bn(on)
log_diag_J = log_diag_J - 0.5 * torch.log(var + 1e-5)
if self.mask_config:
x = torch.cat((on, off), dim=1)
else:
x = torch.cat((off, on), dim=1)
return x, log_diag_J
class ChannelwiseAffineCoupling(AbstractCoupling):
def __init__(self, in_out_dim, mid_dim, mask_config, hps):
"""Initializes a ChannelwiseAffineCoupling.
Args:
in_out_dim: number of input and output features.
mid_dim: number of features in residual blocks.
mask_config: 1 if change the top half, 0 if change the bottom half.
hps: the set of hyperparameters.
"""
super(ChannelwiseAffineCoupling, self).__init__(mask_config, hps)
self.scale = nn.Parameter(torch.zeros(1), requires_grad=True)
self.scale_shift = nn.Parameter(torch.zeros(1), requires_grad=True)
self.in_bn = nn.BatchNorm2d(in_out_dim//2)
self.block = nn.Sequential( # 1st half of resnet: shift
nn.ReLU(), # 2nd half of resnet: log_rescale
ResidualModule(in_out_dim, mid_dim, in_out_dim,
self.res_blocks, self.bottleneck, self.skip, self.weight_norm))
self.out_bn = nn.BatchNorm2d(in_out_dim//2, affine=False)
def forward(self, x, reverse=False):
"""Forward pass.
Args:
x: input tensor.
reverse: True in inference mode, False in sampling mode.
Returns:
transformed tensor and log of diagonal elements of Jacobian.
"""
[_, C, _, _] = list(x.size())
if self.mask_config:
(on, off) = x.split(C//2, dim=1)
else:
(off, on) = x.split(C//2, dim=1)
off_ = self.in_bn(off)
off_ = torch.cat((off_, -off_), dim=1) # C channels
out = self.block(off_)
(shift, log_rescale) = out.split(C//2, dim=1)
log_rescale = self.scale * torch.tanh(log_rescale) + self.scale_shift
log_diag_J = log_rescale # See Eq(6) in real NVP
# See Eq(7) and Eq(8) and Section 3.7 in real NVP
if reverse:
if self.coupling_bn:
mean, var = self.out_bn.running_mean, self.out_bn.running_var
mean = mean.reshape(-1, 1, 1, 1).transpose(0, 1)
var = var.reshape(-1, 1, 1, 1).transpose(0, 1)
on = on * torch.exp(0.5 * torch.log(var + 1e-5)) + mean
on = (on - shift) * torch.exp(-log_rescale)
else:
on = on * torch.exp(log_rescale) + shift
if self.coupling_bn:
if self.training:
_, var = self.batch_stat(on)
else:
var = self.out_bn.running_var
var = var.reshape(-1, 1, 1, 1).transpose(0, 1)
on = self.out_bn(on)
log_diag_J = log_diag_J - 0.5 * torch.log(var + 1e-5)
if self.mask_config:
x = torch.cat((on, off), dim=1)
log_diag_J = torch.cat((log_diag_J, torch.zeros_like(log_diag_J)),
dim=1)
else:
x = torch.cat((off, on), dim=1)
log_diag_J = torch.cat((torch.zeros_like(log_diag_J), log_diag_J),
dim=1)
return x, log_diag_J
class ChannelwiseCoupling(nn.Module):
def __init__(self, in_out_dim, mid_dim, mask_config, hps):
"""Initializes a ChannelwiseCoupling.
Args:
in_out_dim: number of input and output features.
mid_dim: number of features in residual blocks.
mask_config: 1 if change the top half, 0 if change the bottom half.
hps: the set of hyperparameters.
"""
super(ChannelwiseCoupling, self).__init__()
if hps.affine:
self.coupling = ChannelwiseAffineCoupling(
in_out_dim, mid_dim, mask_config, hps)
else:
self.coupling = ChannelwiseAdditiveCoupling(
in_out_dim, mid_dim, mask_config, hps)
def forward(self, x, reverse=False):
"""Forward pass.
Args:
x: input tensor.
reverse: True in inference mode, False in sampling mode.
Returns:
transformed tensor and log of diagonal elements of Jacobian.
"""
return self.coupling(x, reverse)
class RealNVP(nn.Module):
def __init__(self, datainfo, prior, hps):
"""Initializes a RealNVP.
Args:
datainfo: information of dataset to be modeled.
prior: prior distribution over latent space Z.
hps: the set of hyperparameters.
"""
super(RealNVP, self).__init__()
self.datainfo = datainfo
self.prior = prior
self.hps = hps
chan = datainfo.channel
size = datainfo.size
dim = hps.base_dim
if datainfo.name == 'cifar10':
# architecture for CIFAR-10 (down to 16 x 16 x C)
# SCALE 1: 3 x 32 x 32
self.s1_ckbd = self.checkerboard_combo(chan, dim, size, hps)
self.s1_chan = self.channelwise_combo(chan*4, dim, hps)
self.order_matrix_1 = self.order_matrix(chan).cuda()
chan *= 2
size //= 2
# SCALE 2: 6 x 16 x 16
self.s2_ckbd = self.checkerboard_combo(chan, dim, size, hps, final=True)
else: # NOTE: can construct with loop (for future edit)
# architecture for ImageNet and CelebA (down to 4 x 4 x C)
# SCALE 1: 3 x 32(64) x 32(64)
self.s1_ckbd = self.checkerboard_combo(chan, dim, size, hps)
self.s1_chan = self.channelwise_combo(chan*4, dim*2, hps)
self.order_matrix_1 = self.order_matrix(chan).cuda()
chan *= 2
size //= 2
dim *= 2
# SCALE 2: 6 x 16(32) x 16(32)
self.s2_ckbd = self.checkerboard_combo(chan, dim, size, hps)
self.s2_chan = self.channelwise_combo(chan*4, dim*2, hps)
self.order_matrix_2 = self.order_matrix(chan).cuda()
chan *= 2
size //= 2
dim *= 2
# SCALE 3: 12 x 8(16) x 8(16)
self.s3_ckbd = self.checkerboard_combo(chan, dim, size, hps)
self.s3_chan = self.channelwise_combo(chan*4, dim*2, hps)
self.order_matrix_3 = self.order_matrix(chan).cuda()
chan *= 2
size //= 2
dim *= 2
if datainfo.name == 'imnet32':
# SCALE 4: 24 x 4 x 4
self.s4_ckbd = self.checkerboard_combo(chan, dim, size, hps, final=True)
elif datainfo.name in ['imnet64', 'celeba']:
# SCALE 4: 24 x 8 x 8
self.s4_ckbd = self.checkerboard_combo(chan, dim, size, hps)
self.s4_chan = self.channelwise_combo(chan*4, dim*2, hps)
self.order_matrix_4 = self.order_matrix(chan).cuda()
chan *= 2
size //= 2
dim *= 2
# SCALE 5: 48 x 4 x 4
self.s5_ckbd = self.checkerboard_combo(chan, dim, size, hps, final=True)
def checkerboard_combo(self, in_out_dim, mid_dim, size, hps, final=False):
"""Construct a combination of checkerboard coupling layers.
Args:
in_out_dim: number of input and output features.
mid_dim: number of features in residual blocks.
size: height/width of features.
hps: the set of hyperparameters.
final: True if at final scale, False otherwise.
Returns:
A combination of checkerboard coupling layers.
"""
if final:
return nn.ModuleList([
CheckerboardCoupling(in_out_dim, mid_dim, size, 1., hps),
CheckerboardCoupling(in_out_dim, mid_dim, size, 0., hps),
CheckerboardCoupling(in_out_dim, mid_dim, size, 1., hps),
CheckerboardCoupling(in_out_dim, mid_dim, size, 0., hps)])
else:
return nn.ModuleList([
CheckerboardCoupling(in_out_dim, mid_dim, size, 1., hps),
CheckerboardCoupling(in_out_dim, mid_dim, size, 0., hps),
CheckerboardCoupling(in_out_dim, mid_dim, size, 1., hps)])
def channelwise_combo(self, in_out_dim, mid_dim, hps):
"""Construct a combination of channelwise coupling layers.
Args:
in_out_dim: number of input and output features.
mid_dim: number of features in residual blocks.
hps: the set of hyperparameters.
Returns:
A combination of channelwise coupling layers.
"""
return nn.ModuleList([
ChannelwiseCoupling(in_out_dim, mid_dim, 0., hps),
ChannelwiseCoupling(in_out_dim, mid_dim, 1., hps),
ChannelwiseCoupling(in_out_dim, mid_dim, 0., hps)])
def squeeze(self, x):
"""Squeezes a C x H x W tensor into a 4C x H/2 x W/2 tensor.
(See Fig 3 in the real NVP paper.)
Args:
x: input tensor (B x C x H x W).
Returns:
the squeezed tensor (B x 4C x H/2 x W/2).
"""
[B, C, H, W] = list(x.size())
x = x.reshape(B, C, H//2, 2, W//2, 2)
x = x.permute(0, 1, 3, 5, 2, 4)
x = x.reshape(B, C*4, H//2, W//2)
return x
def undo_squeeze(self, x):
"""unsqueezes a C x H x W tensor into a C/4 x 2H x 2W tensor.
(See Fig 3 in the real NVP paper.)
Args:
x: input tensor (B x C x H x W).
Returns:
the squeezed tensor (B x C/4 x 2H x 2W).
"""
[B, C, H, W] = list(x.size())
x = x.reshape(B, C//4, 2, 2, H, W)
x = x.permute(0, 1, 4, 2, 5, 3)
x = x.reshape(B, C//4, H*2, W*2)
return x
def order_matrix(self, channel):
"""Constructs a matrix that defines the ordering of variables
when downscaling/upscaling is performed.
Args:
channel: number of features.
Returns:
a kernel for rearrange the variables.
"""
weights = np.zeros((channel*4, channel, 2, 2))
ordering = np.array([[[[1., 0.],
[0., 0.]]],
[[[0., 0.],
[0., 1.]]],
[[[0., 1.],
[0., 0.]]],
[[[0., 0.],
[1., 0.]]]])
for i in range(channel):
s1 = slice(i, i+1)
s2 = slice(4*i, 4*(i+1))
weights[s2, s1, :, :] = ordering
shuffle = np.array([4*i for i in range(channel)]
+ [4*i+1 for i in range(channel)]
+ [4*i+2 for i in range(channel)]
+ [4*i+3 for i in range(channel)])
weights = weights[shuffle, :, :, :].astype('float32')
return torch.tensor(weights)
def factor_out(self, x, order_matrix):
"""Downscales and factors out the bottom half of the tensor.
(See Fig 4(b) in the real NVP paper.)
Args:
x: input tensor (B x C x H x W).
order_matrix: a kernel that defines the ordering of variables.
Returns:
the top half for further transformation (B x 2C x H/2 x W/2)
and the Gaussianized bottom half (B x 2C x H/2 x W/2).
"""
x = F.conv2d(x, order_matrix, stride=2, padding=0)
[_, C, _, _] = list(x.size())
(on, off) = x.split(C//2, dim=1)
return on, off
def restore(self, on, off, order_matrix):
"""Merges variables and restores their ordering.
(See Fig 4(b) in the real NVP paper.)
Args:
on: the active (transformed) variables (B x C x H x W).
off: the inactive variables (B x C x H x W).
order_matrix: a kernel that defines the ordering of variables.
Returns:
combined variables (B x 2C x H x W).
"""
x = torch.cat((on, off), dim=1)
return F.conv_transpose2d(x, order_matrix, stride=2, padding=0)
def g(self, z):
"""Transformation g: Z -> X (inverse of f).
Args:
z: tensor in latent space Z.
Returns:
transformed tensor in data space X.
"""
x, x_off_1 = self.factor_out(z, self.order_matrix_1)
if self.datainfo.name in ['imnet32', 'imnet64', 'celeba']:
x, x_off_2 = self.factor_out(x, self.order_matrix_2)
x, x_off_3 = self.factor_out(x, self.order_matrix_3)
if self.datainfo.name in ['imnet64', 'celeba']:
x, x_off_4 = self.factor_out(x, self.order_matrix_4)
# SCALE 5: 4 x 4
for i in reversed(range(len(self.s5_ckbd))):
x, _ = self.s5_ckbd[i](x, reverse=True)
x = self.restore(x, x_off_4, self.order_matrix_4)
# SCALE 4: 8 x 8
x = self.squeeze(x)
for i in reversed(range(len(self.s4_chan))):
x, _ = self.s4_chan[i](x, reverse=True)
x = self.undo_squeeze(x)
for i in reversed(range(len(self.s4_ckbd))):
x, _ = self.s4_ckbd[i](x, reverse=True)
x = self.restore(x, x_off_3, self.order_matrix_3)
# SCALE 3: 8(16) x 8(16)
x = self.squeeze(x)
for i in reversed(range(len(self.s3_chan))):
x, _ = self.s3_chan[i](x, reverse=True)
x = self.undo_squeeze(x)
for i in reversed(range(len(self.s3_ckbd))):
x, _ = self.s3_ckbd[i](x, reverse=True)
x = self.restore(x, x_off_2, self.order_matrix_2)
# SCALE 2: 16(32) x 16(32)
x = self.squeeze(x)
for i in reversed(range(len(self.s2_chan))):
x, _ = self.s2_chan[i](x, reverse=True)
x = self.undo_squeeze(x)
for i in reversed(range(len(self.s2_ckbd))):
x, _ = self.s2_ckbd[i](x, reverse=True)
x = self.restore(x, x_off_1, self.order_matrix_1)
# SCALE 1: 32(64) x 32(64)
x = self.squeeze(x)
for i in reversed(range(len(self.s1_chan))):
x, _ = self.s1_chan[i](x, reverse=True)
x = self.undo_squeeze(x)
for i in reversed(range(len(self.s1_ckbd))):
x, _ = self.s1_ckbd[i](x, reverse=True)
return x
def f(self, x):
"""Transformation f: X -> Z (inverse of g).
Args:
x: tensor in data space X.
Returns:
transformed tensor and log of diagonal elements of Jacobian.
"""
z, log_diag_J = x, torch.zeros_like(x)
# SCALE 1: 32(64) x 32(64)
for i in range(len(self.s1_ckbd)):
z, inc = self.s1_ckbd[i](z)
log_diag_J = log_diag_J + inc
z, log_diag_J = self.squeeze(z), self.squeeze(log_diag_J)
for i in range(len(self.s1_chan)):
z, inc = self.s1_chan[i](z)
log_diag_J = log_diag_J + inc
z, log_diag_J = self.undo_squeeze(z), self.undo_squeeze(log_diag_J)
z, z_off_1 = self.factor_out(z, self.order_matrix_1)
log_diag_J, log_diag_J_off_1 = self.factor_out(log_diag_J, self.order_matrix_1)
# SCALE 2: 16(32) x 16(32)
for i in range(len(self.s2_ckbd)):
z, inc = self.s2_ckbd[i](z)
log_diag_J = log_diag_J + inc
if self.datainfo.name in ['imnet32', 'imnet64', 'celeba']:
z, log_diag_J = self.squeeze(z), self.squeeze(log_diag_J)
for i in range(len(self.s2_chan)):
z, inc = self.s2_chan[i](z)
log_diag_J = log_diag_J + inc
z, log_diag_J = self.undo_squeeze(z), self.undo_squeeze(log_diag_J)
z, z_off_2 = self.factor_out(z, self.order_matrix_2)
log_diag_J, log_diag_J_off_2 = self.factor_out(log_diag_J, self.order_matrix_2)
# SCALE 3: 8(16) x 8(16)
for i in range(len(self.s3_ckbd)):
z, inc = self.s3_ckbd[i](z)
log_diag_J = log_diag_J + inc
z, log_diag_J = self.squeeze(z), self.squeeze(log_diag_J)
for i in range(len(self.s3_chan)):
z, inc = self.s3_chan[i](z)
log_diag_J = log_diag_J + inc
z, log_diag_J = self.undo_squeeze(z), self.undo_squeeze(log_diag_J)
z, z_off_3 = self.factor_out(z, self.order_matrix_3)
log_diag_J, log_diag_J_off_3 = self.factor_out(log_diag_J, self.order_matrix_3)
# SCALE 4: 4(8) x 4(8)
for i in range(len(self.s4_ckbd)):
z, inc = self.s4_ckbd[i](z)
log_diag_J = log_diag_J + inc
if self.datainfo.name in ['imnet64', 'celeba']:
z, log_diag_J = self.squeeze(z), self.squeeze(log_diag_J)
for i in range(len(self.s4_chan)):
z, inc = self.s4_chan[i](z)
log_diag_J = log_diag_J + inc
z, log_diag_J = self.undo_squeeze(z), self.undo_squeeze(log_diag_J)
z, z_off_4 = self.factor_out(z, self.order_matrix_4)
log_diag_J, log_diag_J_off_4 = self.factor_out(log_diag_J, self.order_matrix_4)
# SCALE 5: 4 x 4
for i in range(len(self.s5_ckbd)):
z, inc = self.s5_ckbd[i](z)
log_diag_J = log_diag_J + inc
z = self.restore(z, z_off_4, self.order_matrix_4)
log_diag_J = self.restore(log_diag_J, log_diag_J_off_4, self.order_matrix_4)
z = self.restore(z, z_off_3, self.order_matrix_3)
z = self.restore(z, z_off_2, self.order_matrix_2)
log_diag_J = self.restore(log_diag_J, log_diag_J_off_3, self.order_matrix_3)
log_diag_J = self.restore(log_diag_J, log_diag_J_off_2, self.order_matrix_2)
z = self.restore(z, z_off_1, self.order_matrix_1)
log_diag_J = self.restore(log_diag_J, log_diag_J_off_1, self.order_matrix_1)
return z, log_diag_J
def log_prob(self, x):
"""Computes data log-likelihood.
(See Eq(2) and Eq(3) in the real NVP paper.)
Args:
x: input minibatch.
Returns:
log-likelihood of input.
"""
z, log_diag_J = self.f(x)
log_det_J = torch.sum(log_diag_J, dim=(1, 2, 3))
log_prior_prob = torch.sum(self.prior.log_prob(z), dim=(1, 2, 3))
return log_prior_prob + log_det_J
def sample(self, size):
"""Generates samples.
Args:
size: number of samples to generate.
Returns:
samples from the data space X.
"""
C = self.datainfo.channel
H = W = self.datainfo.size
z = self.prior.sample((size, C, H, W))
return self.g(z)
def forward(self, x):
"""Forward pass.
Args:
x: input minibatch.
Returns:
log-likelihood of input and sum of squares of scaling factors.
(the latter is used in L2 regularization.)
"""
weight_scale = None
for name, param in self.named_parameters():
param_name = name.split('.')[-1]
if param_name in ['weight_g', 'scale'] and param.requires_grad:
if weight_scale is None:
weight_scale = torch.pow(param, 2).sum()
else:
weight_scale = weight_scale + torch.pow(param, 2).sum()
return self.log_prob(x), weight_scale