-
Notifications
You must be signed in to change notification settings - Fork 0
/
RealNVP.py
716 lines (577 loc) · 26.9 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
############################################################
### Code based on https://github.com/chrischute/real-nvp ###
############################################################
import numpy as np
import torch.nn.utils as utils
import functools
import torch
import torch.nn as nn
import torch.nn.functional as F
from enum import IntEnum
from tqdm import trange, tqdm
from torchvision.utils import make_grid
import matplotlib.pyplot as plt
import wandb
import os
from config import models_dir
from sklearn.metrics import roc_auc_score
def squeeze_2x2(x, reverse=False, alt_order=False):
"""For each spatial position, a sub-volume of shape `1x1x(N^2 * C)`,
reshape into a sub-volume of shape `NxNxC`, where `N = block_size`.
Adapted from:
https://github.com/tensorflow/models/blob/master/research/real_nvp/real_nvp_utils.py
See Also:
- TensorFlow nn.depth_to_space: https://www.tensorflow.org/api_docs/python/tf/nn/depth_to_space
- Figure 3 of RealNVP paper: https://arxiv.org/abs/1605.08803
Args:
x (torch.Tensor): Input tensor of shape (B, C, H, W).
reverse (bool): Whether to do a reverse squeeze (unsqueeze).
alt_order (bool): Whether to use alternate ordering.
"""
block_size = 2
if alt_order:
n, c, h, w = x.size()
if reverse:
if c % 4 != 0:
raise ValueError('Number of channels must be divisible by 4, got {}.'.format(c))
c //= 4
else:
if h % 2 != 0:
raise ValueError('Height must be divisible by 2, got {}.'.format(h))
if w % 2 != 0:
raise ValueError('Width must be divisible by 4, got {}.'.format(w))
# Defines permutation of input channels (shape is (4, 1, 2, 2)).
squeeze_matrix = torch.tensor([[[[1., 0.], [0., 0.]]],
[[[0., 0.], [0., 1.]]],
[[[0., 1.], [0., 0.]]],
[[[0., 0.], [1., 0.]]]],
dtype=x.dtype,
device=x.device)
perm_weight = torch.zeros((4 * c, c, 2, 2), dtype=x.dtype, device=x.device)
for c_idx in range(c):
slice_0 = slice(c_idx * 4, (c_idx + 1) * 4)
slice_1 = slice(c_idx, c_idx + 1)
perm_weight[slice_0, slice_1, :, :] = squeeze_matrix
shuffle_channels = torch.tensor([c_idx * 4 for c_idx in range(c)]
+ [c_idx * 4 + 1 for c_idx in range(c)]
+ [c_idx * 4 + 2 for c_idx in range(c)]
+ [c_idx * 4 + 3 for c_idx in range(c)])
perm_weight = perm_weight[shuffle_channels, :, :, :]
if reverse:
x = F.conv_transpose2d(x, perm_weight, stride=2)
else:
x = F.conv2d(x, perm_weight, stride=2)
else:
b, c, h, w = x.size()
x = x.permute(0, 2, 3, 1)
if reverse:
if c % 4 != 0:
raise ValueError('Number of channels {} is not divisible by 4'.format(c))
x = x.view(b, h, w, c // 4, 2, 2)
x = x.permute(0, 1, 4, 2, 5, 3)
x = x.contiguous().view(b, 2 * h, 2 * w, c // 4)
else:
if h % 2 != 0 or w % 2 != 0:
raise ValueError('Expected even spatial dims HxW, got {}x{}'.format(h, w))
x = x.view(b, h // 2, 2, w // 2, 2, c)
x = x.permute(0, 1, 3, 5, 2, 4)
x = x.contiguous().view(b, h // 2, w // 2, c * 4)
x = x.permute(0, 3, 1, 2)
return x
def checkerboard_mask(height, width, reverse=False, dtype=torch.float32,
device=None, requires_grad=False):
"""Get a checkerboard mask, such that no two entries adjacent entries
have the same value. In non-reversed mask, top-left entry is 0.
Args:
height (int): Number of rows in the mask.
width (int): Number of columns in the mask.
reverse (bool): If True, reverse the mask (i.e., make top-left entry 1).
Useful for alternating masks in RealNVP.
dtype (torch.dtype): Data type of the tensor.
device (torch.device): Device on which to construct the tensor.
requires_grad (bool): Whether the tensor requires gradient.
Returns:
mask (torch.tensor): Checkerboard mask of shape (1, 1, height, width).
"""
checkerboard = [[((i % 2) + j) % 2 for j in range(width)] for i in range(height)]
mask = torch.tensor(checkerboard, dtype=dtype, device=device, requires_grad=requires_grad)
if reverse:
mask = 1 - mask
# Reshape to (1, 1, height, width) for broadcasting with tensors of shape (B, C, H, W)
mask = mask.view(1, 1, height, width)
return mask
def get_norm_layer(norm_type='instance'):
if norm_type == 'batch':
return functools.partial(nn.BatchNorm2d, affine=True)
elif norm_type == 'instance':
return functools.partial(nn.InstanceNorm2d, affine=False)
else:
raise NotImplementedError('Invalid normalization type: {}'.format(norm_type))
def get_param_groups(net, weight_decay, norm_suffix='weight_g', verbose=False):
"""Get two parameter groups from `net`: One named "normalized" which will
override the optimizer with `weight_decay`, and one named "unnormalized"
which will inherit all hyperparameters from the optimizer.
Args:
net (torch.nn.Module): Network to get parameters from
weight_decay (float): Weight decay to apply to normalized weights.
norm_suffix (str): Suffix to select weights that should be normalized.
For WeightNorm, using 'weight_g' normalizes the scale variables.
verbose (bool): Print out number of normalized and unnormalized parameters.
"""
norm_params = []
unnorm_params = []
for n, p in net.named_parameters():
if n.endswith(norm_suffix):
norm_params.append(p)
else:
unnorm_params.append(p)
param_groups = [{'name': 'normalized', 'params': norm_params, 'weight_decay': weight_decay},
{'name': 'unnormalized', 'params': unnorm_params}]
if verbose:
print('{} normalized parameters'.format(len(norm_params)))
print('{} unnormalized parameters'.format(len(unnorm_params)))
return param_groups
class WNConv2d(nn.Module):
"""Weight-normalized 2d convolution.
Args:
in_channels (int): Number of channels in the input.
out_channels (int): Number of channels in the output.
kernel_size (int): Side length of each convolutional kernel.
padding (int): Padding to add on edges of input.
bias (bool): Use bias in the convolution operation.
"""
def __init__(self, in_channels, out_channels, kernel_size, padding, bias=True):
super(WNConv2d, self).__init__()
self.conv = nn.utils.parametrizations.weight_norm(
nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding, bias=bias))
def forward(self, x):
x = self.conv(x)
return x
class BatchNormStats2d(nn.Module):
"""Compute BatchNorm2d normalization statistics: `mean` and `var`.
Useful for keeping track of sum of log-determinant of Jacobians in flow models.
Args:
num_features (int): Number of features in the input (i.e., `C` in `(N, C, H, W)`).
eps (float): Added to the denominator for numerical stability.
decay (float): The value used for the running_mean and running_var computation.
Different from conventional momentum, see `nn.BatchNorm2d` for more.
"""
def __init__(self, num_features, eps=1e-5, decay=0.1):
super(BatchNormStats2d, self).__init__()
self.eps = eps
self.register_buffer('running_mean', torch.zeros(num_features))
self.register_buffer('running_var', torch.ones(num_features))
self.decay = decay
def forward(self, x, training):
# Get mean and variance per channel
if training:
channels = x.transpose(0, 1).contiguous().view(x.size(1), -1)
used_mean, used_var = channels.mean(-1), channels.var(-1)
curr_mean, curr_var = used_mean, used_var
# Update variables
self.running_mean = self.running_mean - self.decay * (self.running_mean - curr_mean)
self.running_var = self.running_var - self.decay * (self.running_var - curr_var)
else:
used_mean = self.running_mean
used_var = self.running_var
used_var += self.eps
# Reshape to (N, C, H, W)
used_mean = used_mean.view(1, x.size(1), 1, 1).expand_as(x)
used_var = used_var.view(1, x.size(1), 1, 1).expand_as(x)
return used_mean, used_var
def bits_per_dim(x, nll):
"""Get the bits per dimension implied by using model with `loss`
for compressing `x`, assuming each entry can take on `k` discrete values.
Args:
x (torch.Tensor): Input to the model. Just used for dimensions.
nll (torch.Tensor): Scalar negative log-likelihood loss tensor.
Returns:
bpd (torch.Tensor): Bits per dimension implied if compressing `x`.
"""
dim = np.prod(x.size()[1:])
bpd = nll / (np.log(2) * dim)
return bpd
def clip_grad_norm(optimizer, max_norm, norm_type=2):
"""Clip the norm of the gradients for all parameters under `optimizer`.
Args:
optimizer (torch.optim.Optimizer):
max_norm (float): The maximum allowable norm of gradients.
norm_type (int): The type of norm to use in computing gradient norms.
"""
for group in optimizer.param_groups:
utils.clip_grad_norm_(group['params'], max_norm, norm_type)
class ResidualBlock(nn.Module):
"""ResNet basic block with weight norm."""
def __init__(self, in_channels, out_channels):
super(ResidualBlock, self).__init__()
self.in_norm = nn.BatchNorm2d(in_channels)
self.in_conv = WNConv2d(in_channels, out_channels, kernel_size=3, padding=1, bias=False)
self.out_norm = nn.BatchNorm2d(out_channels)
self.out_conv = WNConv2d(out_channels, out_channels, kernel_size=3, padding=1, bias=True)
def forward(self, x):
skip = x
x = self.in_norm(x)
x = F.relu(x)
x = self.in_conv(x)
x = self.out_norm(x)
x = F.relu(x)
x = self.out_conv(x)
x = x + skip
return x
class ResNet(nn.Module):
"""ResNet for scale and translate factors in Real NVP.
Args:
in_channels (int): Number of channels in the input.
mid_channels (int): Number of channels in the intermediate layers.
out_channels (int): Number of channels in the output.
num_blocks (int): Number of residual blocks in the network.
kernel_size (int): Side length of each filter in convolutional layers.
padding (int): Padding for convolutional layers.
double_after_norm (bool): Double input after input BatchNorm.
"""
def __init__(self, in_channels, mid_channels, out_channels,
num_blocks, kernel_size, padding, double_after_norm):
super(ResNet, self).__init__()
self.in_norm = nn.BatchNorm2d(in_channels)
self.double_after_norm = double_after_norm
self.in_conv = WNConv2d(2 * in_channels, mid_channels, kernel_size, padding, bias=True)
self.in_skip = WNConv2d(mid_channels, mid_channels, kernel_size=1, padding=0, bias=True)
self.blocks = nn.ModuleList([ResidualBlock(mid_channels, mid_channels)
for _ in range(num_blocks)])
self.skips = nn.ModuleList([WNConv2d(mid_channels, mid_channels, kernel_size=1, padding=0, bias=True)
for _ in range(num_blocks)])
self.out_norm = nn.BatchNorm2d(mid_channels)
self.out_conv = WNConv2d(mid_channels, out_channels, kernel_size=1, padding=0, bias=True)
def forward(self, x):
x = self.in_norm(x)
if self.double_after_norm:
x *= 2.
x = torch.cat((x, -x), dim=1)
x = F.relu(x)
x = self.in_conv(x)
x_skip = self.in_skip(x)
for block, skip in zip(self.blocks, self.skips):
x = block(x)
x_skip += skip(x)
x = self.out_norm(x_skip)
x = F.relu(x)
x = self.out_conv(x)
return x
class RealNVPLoss(nn.Module):
"""Get the NLL loss for a RealNVP model.
Args:
k (int or float): Number of discrete values in each input dimension.
E.g., `k` is 256 for natural images.
See Also:
Equation (3) in the RealNVP paper: https://arxiv.org/abs/1605.08803
"""
def __init__(self, k=256):
super(RealNVPLoss, self).__init__()
self.k = k
def forward(self, z, sldj):
prior_ll = -0.5 * (z ** 2 + np.log(2 * np.pi))
prior_ll = prior_ll.view(z.size(0), -1).sum(-1) \
- np.log(self.k) * np.prod(z.size()[1:])
ll = prior_ll + sldj
nll = -ll.mean()
return nll
class MaskType(IntEnum):
CHECKERBOARD = 0
CHANNEL_WISE = 1
class CouplingLayer(nn.Module):
"""Coupling layer in RealNVP.
Args:
in_channels (int): Number of channels in the input.
mid_channels (int): Number of channels in the `s` and `t` network.
num_blocks (int): Number of residual blocks in the `s` and `t` network.
mask_type (MaskType): One of `MaskType.CHECKERBOARD` or `MaskType.CHANNEL_WISE`.
reverse_mask (bool): Whether to reverse the mask. Useful for alternating masks.
"""
def __init__(self, in_channels, mid_channels, num_blocks, mask_type, reverse_mask):
super(CouplingLayer, self).__init__()
# Save mask info
self.mask_type = mask_type
self.reverse_mask = reverse_mask
# Build scale and translate network
if self.mask_type == MaskType.CHANNEL_WISE:
in_channels //= 2
self.st_net = ResNet(in_channels, mid_channels, 2 * in_channels,
num_blocks=num_blocks, kernel_size=3, padding=1,
double_after_norm=(self.mask_type == MaskType.CHECKERBOARD))
# Learnable scale for s
self.rescale = nn.utils.parametrizations.weight_norm(Rescale(in_channels))
def forward(self, x, sldj=None, reverse=True):
if self.mask_type == MaskType.CHECKERBOARD:
# Checkerboard mask
b = checkerboard_mask(x.size(2), x.size(3), self.reverse_mask, device=x.device)
x_b = x * b
st = self.st_net(x_b)
s, t = st.chunk(2, dim=1)
s = self.rescale(torch.tanh(s))
s = s * (1 - b)
t = t * (1 - b)
# Scale and translate
if reverse:
inv_exp_s = s.mul(-1).exp()
if torch.isnan(inv_exp_s).any():
raise RuntimeError('Scale factor has NaN entries')
x = x * inv_exp_s - t
else:
exp_s = s.exp()
if torch.isnan(exp_s).any():
raise RuntimeError('Scale factor has NaN entries')
x = (x + t) * exp_s
# Add log-determinant of the Jacobian
sldj += s.reshape(s.size(0), -1).sum(-1)
else:
# Channel-wise mask
if self.reverse_mask:
x_id, x_change = x.chunk(2, dim=1)
else:
x_change, x_id = x.chunk(2, dim=1)
st = self.st_net(x_id)
s, t = st.chunk(2, dim=1)
s = self.rescale(torch.tanh(s))
# Scale and translate
if reverse:
inv_exp_s = s.mul(-1).exp()
if torch.isnan(inv_exp_s).any():
raise RuntimeError('Scale factor has NaN entries')
x_change = x_change * inv_exp_s - t
else:
exp_s = s.exp()
if torch.isnan(exp_s).any():
raise RuntimeError('Scale factor has NaN entries')
x_change = (x_change + t) * exp_s
# Add log-determinant of the Jacobian
sldj += s.reshape(s.size(0), -1).sum(-1)
if self.reverse_mask:
x = torch.cat((x_id, x_change), dim=1)
else:
x = torch.cat((x_change, x_id), dim=1)
return x, sldj
class Rescale(nn.Module):
"""Per-channel rescaling. Need a proper `nn.Module` so we can wrap it
with `torch.nn.utils.parametrizations.weight_norm`.
Args:
num_channels (int): Number of channels in the input.
"""
def __init__(self, num_channels):
super(Rescale, self).__init__()
self.weight = nn.Parameter(torch.ones(num_channels, 1, 1))
def forward(self, x):
x = self.weight * x
return x
def create_checkpoint_dir():
"""Create a directory to save model checkpoints."""
if not os.path.exists(models_dir):
os.makedirs(models_dir)
if not os.path.exists(os.path.join(models_dir, 'RealNVP')):
os.makedirs(os.path.join(models_dir, 'RealNVP'))
class RealNVP(nn.Module):
"""RealNVP Model
Based on the paper:
"Density estimation using Real NVP"
by Laurent Dinh, Jascha Sohl-Dickstein, and Samy Bengio
(https://arxiv.org/abs/1605.08803).
Args:
num_scales (int): Number of scales in the RealNVP model.
in_channels (int): Number of channels in the input.
mid_channels (int): Number of channels in the intermediate layers.
num_blocks (int): Number of residual blocks in the s and t network of
`Coupling` layers.
"""
def __init__(self, img_size, in_channels, args):
super(RealNVP, self).__init__()
# Register data_constraint to pre-process images, not learnable
self.register_buffer('data_constraint', torch.tensor([0.9], dtype=torch.float32))
self.flows = _RealNVP(0, args.num_scales, in_channels, args.mid_channels, args.num_blocks)
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.flows.to(self.device)
self.img_size = img_size
self.channels = in_channels
self.no_wandb = args.no_wandb
def forward(self, x, reverse=False):
sldj = None
if not reverse:
# Expect inputs in [0, 1]
if x.min() < 0 or x.max() > 1:
raise ValueError('Expected x in [0, 1], got x with min/max {}/{}'
.format(x.min(), x.max()))
# De-quantize and convert to logits
x, sldj = self._pre_process(x)
x, sldj = self.flows(x, sldj, reverse)
return x, sldj
def _pre_process(self, x):
"""Dequantize the input image `x` and convert to logits.
Args:
x (torch.Tensor): Input image.
Returns:
y (torch.Tensor): Dequantized logits of `x`.
See Also:
- Dequantization: https://arxiv.org/abs/1511.01844, Section 3.1
- Modeling logits: https://arxiv.org/abs/1605.08803, Section 4.1
"""
y = (x * 255. + torch.rand_like(x)) / 256.
y = (2 * y - 1) * self.data_constraint.to(self.device)
y = (y + 1) / 2
y = y.log() - (1. - y).log()
# Save log-determinant of Jacobian of initial transform
ldj = F.softplus(y) + F.softplus(-y) \
- F.softplus((1. - self.data_constraint.to(self.device)).log() - self.data_constraint.to(self.device).log())
sldj = ldj.view(ldj.size(0), -1).sum(-1)
return y, sldj
def train_model(self, dataloader, args, verbose=True):
"""Train the RealNVP model.
Args:
dataloader (DataLoader): DataLoader for the training set.
args (argparse.Namespace): Command-line arguments.
"""
create_checkpoint_dir()
loss = RealNVPLoss()
optimizer = torch.optim.Adam(self.parameters(), lr=args.lr)
epoch_bar = trange(args.n_epochs, desc='Epochs', leave=True)
best_loss = np.inf
for epoch in epoch_bar:
self.train()
loss_epoch = 0.
for x,_ in tqdm(dataloader, desc='Batches', leave=False, disable=not verbose):
x = x.to(self.device)
optimizer.zero_grad()
z, sldj = self.forward(x, reverse=False)
nll = loss(z, sldj)
nll.backward()
clip_grad_norm(optimizer, args.max_grad_norm)
optimizer.step()
loss_epoch += nll.item()*x.size(0)
loss_epoch /= len(dataloader.dataset)
epoch_bar.set_postfix(loss=loss_epoch)
if not self.no_wandb:
wandb.log({"train_loss": loss_epoch})
if loss_epoch < best_loss:
best_loss = loss_epoch
torch.save(self.state_dict(), os.path.join(models_dir, 'RealNVP', f'RealNVP_{args.dataset}.pt'))
if (epoch + 1) % args.sample_and_save_freq == 0 or epoch == 0:
self.sample(16, train=True)
def sample(self, n_samples, train=False):
"""Sample from RealNVP model.
Args:
net (torch.nn.DataParallel): The RealNVP model wrapped in DataParallel.
batch_size (int): Number of samples to generate.
train (bool): Whether to log samples as training samples.
"""
self.eval()
z = torch.randn((n_samples, self.channels, self.img_size, self.img_size), dtype=torch.float32, device=self.device)
x, _ = self.forward(z, reverse=True)
x = torch.sigmoid(x)
x = x.clamp(0, 1).cpu().detach()
grid = make_grid(x, nrow=int(n_samples ** 0.5), padding=0)
fig = plt.figure(figsize=(10, 10))
plt.imshow(grid.permute(1, 2, 0))
plt.axis('off')
if train:
if not self.no_wandb:
wandb.log({"train_samples": fig})
else:
plt.show()
plt.close(fig)
def outlier_detection(self, in_loader, out_loader):
"""Detect outliers using RealNVP model.
Args:
net (torch.nn.DataParallel): The RealNVP model wrapped in DataParallel.
in_loader (DataLoader): DataLoader for the in-distribution dataset.
out_loader (DataLoader): DataLoader for the out-of-distribution dataset.
"""
self.eval()
in_scores = []
out_scores = []
for x, _ in tqdm(in_loader, desc='In-distribution', leave=False):
x = x.to(self.device)
z, sldj = self.forward(x, reverse=False)
nll = self.nll_score(z, sldj)
in_scores.append(nll.cpu().detach().numpy())
for x, _ in tqdm(out_loader, desc='Out-of-distribution', leave=False):
x = x.to(self.device)
z, sldj = self.forward(x, reverse=False)
nll = self.nll_score(z, sldj)
out_scores.append(nll.cpu().detach().numpy())
in_scores = np.concatenate(in_scores)
out_scores = np.concatenate(out_scores)
plt.figure(figsize=(10, 5))
plt.hist(in_scores, bins=50, alpha=0.5, label='In-distribution')
plt.hist(out_scores, bins=50, alpha=0.5, label='Out-of-distribution')
plt.legend()
plt.title('RealNVP Outlier Detection')
plt.xlabel('NLL Score')
plt.ylabel('Counts')
plt.show()
def nll_score(self, z, sldj):
k = 256
prior_ll = -0.5 * (z ** 2 + np.log(2 * np.pi))
prior_ll = prior_ll.view(z.size(0), -1).sum(-1) \
- np.log(k) * np.prod(z.size()[1:])
ll = prior_ll + sldj
nll = -ll
return nll
class _RealNVP(nn.Module):
"""Recursive builder for a `RealNVP` model.
Each `_RealNVPBuilder` corresponds to a single scale in `RealNVP`,
and the constructor is recursively called to build a full `RealNVP` model.
Args:
scale_idx (int): Index of current scale.
num_scales (int): Number of scales in the RealNVP model.
in_channels (int): Number of channels in the input.
mid_channels (int): Number of channels in the intermediate layers.
num_blocks (int): Number of residual blocks in the s and t network of
`Coupling` layers.
"""
def __init__(self, scale_idx, num_scales, in_channels, mid_channels, num_blocks, img_size=32):
super(_RealNVP, self).__init__()
self.is_last_block = scale_idx == num_scales - 1
self.in_couplings = nn.ModuleList([
CouplingLayer(in_channels, mid_channels, num_blocks, MaskType.CHECKERBOARD, reverse_mask=False),
CouplingLayer(in_channels, mid_channels, num_blocks, MaskType.CHECKERBOARD, reverse_mask=True),
CouplingLayer(in_channels, mid_channels, num_blocks, MaskType.CHECKERBOARD, reverse_mask=False)
])
if self.is_last_block:
self.in_couplings.append(
CouplingLayer(in_channels, mid_channels, num_blocks, MaskType.CHECKERBOARD, reverse_mask=True))
else:
self.out_couplings = nn.ModuleList([
CouplingLayer(4 * in_channels, 2 * mid_channels, num_blocks, MaskType.CHANNEL_WISE, reverse_mask=False),
CouplingLayer(4 * in_channels, 2 * mid_channels, num_blocks, MaskType.CHANNEL_WISE, reverse_mask=True),
CouplingLayer(4 * in_channels, 2 * mid_channels, num_blocks, MaskType.CHANNEL_WISE, reverse_mask=False)
])
self.next_block = _RealNVP(scale_idx + 1, num_scales, 2 * in_channels, 2 * mid_channels, num_blocks)
def forward(self, x, sldj, reverse=False):
if reverse:
if not self.is_last_block:
# Re-squeeze -> split -> next block
x = squeeze_2x2(x, reverse=False, alt_order=True)
x, x_split = x.chunk(2, dim=1)
x, sldj = self.next_block(x, sldj, reverse)
x = torch.cat((x, x_split), dim=1)
x = squeeze_2x2(x, reverse=True, alt_order=True)
# Squeeze -> 3x coupling (channel-wise)
x = squeeze_2x2(x, reverse=False)
for coupling in reversed(self.out_couplings):
x, sldj = coupling(x, sldj, reverse)
x = squeeze_2x2(x, reverse=True)
for coupling in reversed(self.in_couplings):
x, sldj = coupling(x, sldj, reverse)
else:
for coupling in self.in_couplings:
x, sldj = coupling(x, sldj, reverse)
if not self.is_last_block:
# Squeeze -> 3x coupling (channel-wise)
x = squeeze_2x2(x, reverse=False)
for coupling in self.out_couplings:
x, sldj = coupling(x, sldj, reverse)
x = squeeze_2x2(x, reverse=True)
# Re-squeeze -> split -> next block
x = squeeze_2x2(x, reverse=False, alt_order=True)
x, x_split = x.chunk(2, dim=1)
x, sldj = self.next_block(x, sldj, reverse)
x = torch.cat((x, x_split), dim=1)
x = squeeze_2x2(x, reverse=True, alt_order=True)
return x, sldj