forked from 1Konny/FactorVAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
614 lines (512 loc) · 19.3 KB
/
model.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
"""model.py"""
from torch.autograd import Variable
from models import flows
# TODO: change models
import torch.cuda as cuda
import torch.nn as nn
import torch.nn.init as init
import torch
class Discriminator(nn.Module):
def __init__(self, z_dim):
super(Discriminator, self).__init__()
self.z_dim = z_dim
self.net = nn.Sequential(
nn.Linear(z_dim, 1000),
nn.LeakyReLU(0.2, True),
nn.Linear(1000, 1000),
nn.LeakyReLU(0.2, True),
nn.Linear(1000, 1000),
nn.LeakyReLU(0.2, True),
nn.Linear(1000, 1000),
nn.LeakyReLU(0.2, True),
nn.Linear(1000, 1000),
nn.LeakyReLU(0.2, True),
nn.Linear(1000, 2),
)
self.weight_init()
def weight_init(self, mode='normal'):
if mode == 'kaiming':
initializer = kaiming_init
elif mode == 'normal':
initializer = normal_init
for block in self._modules:
for m in self._modules[block]:
initializer(m)
def forward(self, z):
return self.net(z).squeeze()
class SylvesterableVAE1(nn.Module):
"""
64x64 variant of the VAE class in Sylvester flows.
command-line args replaced with kwargs.
z_size
[input_size: c x w x h]
[input_type: 'binary']
[last_kernel_size: 7]
"""
def __init__(self, z_size=10):
super(SylvesterableVAE1, self).__init__()
self.z_size = z_size
# self.input_size = [1, 64, 64]
# self.last_kernel_size = 7
self.q_z_nn, self.q_z_mean, self.q_z_var = self.create_encoder()
self.p_x_nn, self.p_x_mean = self.create_decoder()
self.q_z_nn_output_dim = 128
if cuda.is_available():
self.FloatTensor = cuda.FloatTensor
else:
raise NotImplementedError()
#self.FloatTensor = torch.FloatTensor
# log-det-jacobian = 0 without flows
self.log_det_j = Variable(self.FloatTensor(1).zero_())
self.weight_init()
def create_encoder(self):
h_dim = 128
q_z_nn = nn.Sequential(
nn.Conv2d(1, 32, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(32, 32, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(32, 64, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(64, 64, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(64, 128, 4, 1),
nn.ReLU(True),
nn.Conv2d(128, h_dim, 1)
)
'''
q_z_nn = nn.Sequential(
GatedConv2d(self.input_size[0], 32, 5, 1, 2),
GatedConv2d(32, 32, 5, 2, 2),
GatedConv2d(32, 64, 5, 1, 2),
GatedConv2d(64, 64, 5, 2, 2),
GatedConv2d(64, 64, 5, 1, 2),
GatedConv2d(64, 256, self.last_kernel_size, 1, 0),
)
'''
q_z_mean = nn.Sequential(
nn.Linear(h_dim, self.z_size),
)
q_z_var = nn.Sequential(
nn.Linear(h_dim, self.z_size),
nn.Softplus(),
)
return q_z_nn, q_z_mean, q_z_var
def create_decoder(self):
'''
self.decode = nn.Sequential(
nn.Conv2d(z_dim, 128, 1),
nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 4),
nn.ReLU(True),
nn.ConvTranspose2d(64, 64, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(64, 32, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(32, 32, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(32, 1, 4, 2, 1),
)
p_x_nn = nn.Sequential(
GatedConvTranspose2d(self.z_size, 64, self.last_kernel_size, 1, 0),
GatedConvTranspose2d(64, 64, 5, 1, 2),
GatedConvTranspose2d(64, 32, 5, 2, 2, 1),
GatedConvTranspose2d(32, 32, 5, 1, 2),
GatedConvTranspose2d(32, 32, 5, 2, 2, 1),
GatedConvTranspose2d(32, 32, 5, 1, 2)
)
p_x_mean = nn.Sequential(
nn.Conv2d(32, self.input_size[0], 1, 1, 0),
nn.Sigmoid()
)
'''
p_x_nn = nn.Sequential(
nn.Conv2d(self.z_size, 128, 1),
nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 4),
nn.ReLU(True),
nn.ConvTranspose2d(64, 64, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(64, 32, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(32, 32, 4, 2, 1),
nn.ReLU(True),
)
# no final convolution layer
p_x_mean = nn.Sequential(
nn.ConvTranspose2d(32, 1, 4, 2, 1),
# nn.Sigmoid()
)
return p_x_nn, p_x_mean
def reparameterize(self, mu, var):
"""
Samples z from a multivariate Gaussian with diagonal covariance matrix using the
reparameterization trick.
"""
std = var.sqrt()
eps = self.FloatTensor(std.size()).normal_()
eps = Variable(eps)
z = eps.mul(std).add_(mu)
return z
def __encode__(self, x):
"""
Encoder expects following data shapes as input: shape = (batch_size, num_channels, width, height)
"""
h = self.q_z_nn(x)
h = h.view(h.size(0), -1)
mean = self.q_z_mean(h)
var = self.q_z_var(h)
return mean, var
def encode(self, x):
mean, var = self.__encode__(x)
return torch.cat([mean, var], dim=1)
def decode(self, z):
"""
Decoder outputs reconstructed image in the following shapes:
x_mean.shape = (batch_size, num_channels, width, height)
"""
z = z.view(z.size(0), self.z_size, 1, 1)
h = self.p_x_nn(z)
x_mean = self.p_x_mean(h)
return x_mean
def forward(self, x, no_dec=False):
"""
Evaluates the model as a whole, encodes and decodes. Note that the log det jacobian is zero
for a plain VAE (without flows), and z_0 = z_k.
"""
# mean and variance of z
z_mu, z_var = self.__encode__(x)
# sample z
z = self.reparameterize(z_mu, z_var)
x_mean = self.decode(z)
# x_recon, mu, logvar, z
if no_dec:
return z.squeeze()
else:
return x_mean, z_mu, z_var, z.squeeze()
# return x_mean, z_mu, z_var, self.log_det_j, z, z
# return x_mean, z_mu, z_var, self.log_det_j, z, z
# from FVAE
def weight_init(self, mode='normal'):
if mode == 'kaiming':
initializer = kaiming_init
elif mode == 'normal':
initializer = normal_init
for block in self._modules:
for m in self._modules[block]:
initializer(m)
class OrthogonalSylvesterVAE1(SylvesterableVAE1):
"""
Variational auto-encoder with orthogonal flows in the encoder.
"""
def __init__(self, num_flows=4, num_ortho_vecs=8, z_size=10):
super(OrthogonalSylvesterVAE1, self).__init__(z_size=z_size)
# Initialize log-det-jacobian to zero
self.log_det_j = 0.
# Flow parameters
flow = flows.Sylvester
self.num_flows = num_flows
self.num_ortho_vecs = num_ortho_vecs
assert (self.num_ortho_vecs <= self.z_size) and (self.num_ortho_vecs > 0)
# Orthogonalization parameters
if self.num_ortho_vecs == self.z_size:
self.cond = 1.e-5
else:
self.cond = 1.e-6
self.steps = 100
identity = torch.eye(self.num_ortho_vecs, self.num_ortho_vecs)
# Add batch dimension
identity = identity.unsqueeze(0)
# Put identity in buffer so that it will be moved to GPU if needed by any call of .cuda
self.register_buffer('_eye', Variable(identity))
self._eye.requires_grad = False
# Masks needed for triangular R1 and R2.
triu_mask = torch.triu(torch.ones(self.num_ortho_vecs, self.num_ortho_vecs), diagonal=1)
triu_mask = triu_mask.unsqueeze(0).unsqueeze(3)
diag_idx = torch.arange(0, self.num_ortho_vecs).long()
self.register_buffer('triu_mask', Variable(triu_mask))
self.triu_mask.requires_grad = False
self.register_buffer('diag_idx', diag_idx)
# Amortized flow parameters
# Diagonal elements of R1 * R2 have to satisfy -1 < R1 * R2 for flow to be invertible
self.diag_activation = nn.Tanh()
self.amor_d = nn.Linear(self.q_z_nn_output_dim, self.num_flows * self.num_ortho_vecs * self.num_ortho_vecs)
self.amor_diag1 = nn.Sequential(
nn.Linear(self.q_z_nn_output_dim, self.num_flows * self.num_ortho_vecs),
self.diag_activation
)
self.amor_diag2 = nn.Sequential(
nn.Linear(self.q_z_nn_output_dim, self.num_flows * self.num_ortho_vecs),
self.diag_activation
)
self.amor_q = nn.Linear(self.q_z_nn_output_dim, self.num_flows * self.z_size * self.num_ortho_vecs)
self.amor_b = nn.Linear(self.q_z_nn_output_dim, self.num_flows * self.num_ortho_vecs)
# Normalizing flow layers
for k in range(self.num_flows):
flow_k = flow(self.num_ortho_vecs)
self.add_module('flow_' + str(k), flow_k)
def batch_construct_orthogonal(self, q):
"""
Batch orthogonal matrix construction.
:param q: q contains batches of matrices, shape : (batch_size * num_flows, z_size * num_ortho_vecs)
:return: batches of orthogonalized matrices, shape: (batch_size * num_flows, z_size, num_ortho_vecs)
"""
# Reshape to shape (num_flows * batch_size, z_size * num_ortho_vecs)
q = q.view(-1, self.z_size * self.num_ortho_vecs)
norm = torch.norm(q, p=2, dim=1, keepdim=True)
amat = torch.div(q, norm)
dim0 = amat.size(0)
amat = amat.resize(dim0, self.z_size, self.num_ortho_vecs)
max_norm = 0.
# Iterative orthogonalization
for s in range(self.steps):
tmp = torch.bmm(amat.transpose(2, 1), amat)
tmp = self._eye - tmp
tmp = self._eye + 0.5 * tmp
amat = torch.bmm(amat, tmp)
# Testing for convergence
test = torch.bmm(amat.transpose(2, 1), amat) - self._eye
norms2 = torch.sum(torch.norm(test, p=2, dim=2) ** 2, dim=1)
norms = torch.sqrt(norms2)
max_norm = torch.max(norms).item()
if max_norm <= self.cond:
break
if max_norm > self.cond:
print('\nWARNING WARNING WARNING: orthogonalization not complete')
print('\t Final max norm =', max_norm)
print()
# Reshaping: first dimension is batch_size
amat = amat.view(-1, self.num_flows, self.z_size, self.num_ortho_vecs)
amat = amat.transpose(0, 1)
return amat
def __encode__(self, x):
"""
Encoder that ouputs parameters for base distribution of z and flow parameters.
"""
batch_size = x.size(0)
h = self.q_z_nn(x)
h = h.view(-1, self.q_z_nn_output_dim)
mean_z = self.q_z_mean(h)
var_z = self.q_z_var(h)
# Amortized r1, r2, q, b for all flows
full_d = self.amor_d(h)
diag1 = self.amor_diag1(h)
diag2 = self.amor_diag2(h)
full_d = full_d.resize(batch_size, self.num_ortho_vecs, self.num_ortho_vecs, self.num_flows)
diag1 = diag1.resize(batch_size, self.num_ortho_vecs, self.num_flows)
diag2 = diag2.resize(batch_size, self.num_ortho_vecs, self.num_flows)
r1 = full_d * self.triu_mask
r2 = full_d.transpose(2, 1) * self.triu_mask
r1[:, self.diag_idx, self.diag_idx, :] = diag1
r2[:, self.diag_idx, self.diag_idx, :] = diag2
q = self.amor_q(h)
b = self.amor_b(h)
# Resize flow parameters to divide over K flows
b = b.resize(batch_size, 1, self.num_ortho_vecs, self.num_flows)
return mean_z, var_z, r1, r2, q, b
def encode(self, x):
mean_z, var_z, r1, r2, q, b = self.__encode__(x)
return torch.cat([mean_z, var_z], 1)
def forward(self, x, no_dec=False):
"""
Forward pass with orthogonal sylvester flows for the transformation z_0 -> z_1 -> ... -> z_k.
Log determinant is computed as log_det_j = N E_q_z0[\sum_k log |det dz_k/dz_k-1| ].
"""
self.log_det_j = 0.
z_mu, z_var, r1, r2, q, b = self.__encode__(x)
# Orthogonalize all q matrices
q_ortho = self.batch_construct_orthogonal(q)
# Sample z_0
z = [self.reparameterize(z_mu, z_var)]
# Normalizing flows
for k in range(self.num_flows):
flow_k = getattr(self, 'flow_' + str(k))
z_k, log_det_jacobian = flow_k(z[k], r1[:, :, :, k], r2[:, :, :, k], q_ortho[k, :, :, :], b[:, :, :, k])
z.append(z_k)
self.log_det_j += log_det_jacobian
x_mean = self.decode(z[-1])
# return x_mean, z_mu, z_var, self.log_det_j, z[0], z[-1]
if no_dec:
return z[0]
else:
return x_mean, z_mu, z_var, z[0], self.log_det_j
class FactorVAE1(nn.Module):
"""Encoder and Decoder architecture for 2D Shapes data."""
def __init__(self, z_dim=10):
super(FactorVAE1, self).__init__()
self.z_dim = z_dim
self.encode = nn.Sequential(
nn.Conv2d(1, 32, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(32, 32, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(32, 64, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(64, 64, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(64, 128, 4, 1),
nn.ReLU(True),
nn.Conv2d(128, 2*z_dim, 1)
)
self.decode = nn.Sequential(
nn.Conv2d(z_dim, 128, 1),
nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 4),
nn.ReLU(True),
nn.ConvTranspose2d(64, 64, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(64, 32, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(32, 32, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(32, 1, 4, 2, 1),
)
self.weight_init()
def weight_init(self, mode='normal'):
if mode == 'kaiming':
initializer = kaiming_init
elif mode == 'normal':
initializer = normal_init
for block in self._modules:
for m in self._modules[block]:
initializer(m)
def reparametrize(self, mu, logvar):
std = logvar.mul(0.5).exp_()
eps = std.data.new(std.size()).normal_()
return eps.mul(std).add_(mu)
def forward(self, x, no_dec=False):
stats = self.encode(x)
mu = stats[:, :self.z_dim]
logvar = stats[:, self.z_dim:]
z = self.reparametrize(mu, logvar)
if no_dec:
return z.squeeze()
else:
x_recon = self.decode(z).view(x.size())
return x_recon, mu, logvar, z.squeeze()
class FactorVAE2(nn.Module):
"""Encoder and Decoder architecture for 3D Shapes, Celeba, Chairs data."""
def __init__(self, z_dim=10):
super(FactorVAE2, self).__init__()
self.z_dim = z_dim
self.encode = nn.Sequential(
nn.Conv2d(3, 32, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(32, 32, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(32, 64, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(64, 64, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(64, 256, 4, 1),
nn.ReLU(True),
nn.Conv2d(256, 2*z_dim, 1)
)
self.decode = nn.Sequential(
nn.Conv2d(z_dim, 256, 1),
nn.ReLU(True),
nn.ConvTranspose2d(256, 64, 4),
nn.ReLU(True),
nn.ConvTranspose2d(64, 64, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(64, 32, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(32, 32, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(32, 3, 4, 2, 1),
)
self.weight_init()
def weight_init(self, mode='normal'):
if mode == 'kaiming':
initializer = kaiming_init
elif mode == 'normal':
initializer = normal_init
for block in self._modules:
for m in self._modules[block]:
initializer(m)
def reparametrize(self, mu, logvar):
std = logvar.mul(0.5).exp_()
eps = std.data.new(std.size()).normal_()
return eps.mul(std).add_(mu)
def forward(self, x, no_dec=False):
stats = self.encode(x)
mu = stats[:, :self.z_dim]
logvar = stats[:, self.z_dim:]
z = self.reparametrize(mu, logvar)
if no_dec:
return z.squeeze()
else:
x_recon = self.decode(z)
return x_recon, mu, logvar, z.squeeze()
class FactorVAE3(nn.Module):
"""Encoder and Decoder architecture for 3D Faces data."""
def __init__(self, z_dim=10):
super(FactorVAE3, self).__init__()
self.z_dim = z_dim
self.encode = nn.Sequential(
nn.Conv2d(1, 32, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(32, 32, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(32, 64, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(64, 64, 4, 2, 1),
nn.ReLU(True),
nn.Conv2d(64, 256, 4, 1),
nn.ReLU(True),
nn.Conv2d(256, 2*z_dim, 1)
)
self.decode = nn.Sequential(
nn.Conv2d(z_dim, 256, 1),
nn.ReLU(True),
nn.ConvTranspose2d(256, 64, 4),
nn.ReLU(True),
nn.ConvTranspose2d(64, 64, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(64, 32, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(32, 32, 4, 2, 1),
nn.ReLU(True),
nn.ConvTranspose2d(32, 1, 4, 2, 1),
)
self.weight_init()
def weight_init(self, mode='normal'):
if mode == 'kaiming':
initializer = kaiming_init
elif mode == 'normal':
initializer = normal_init
for block in self._modules:
for m in self._modules[block]:
initializer(m)
def reparametrize(self, mu, logvar):
std = logvar.mul(0.5).exp_()
eps = std.data.new(std.size()).normal_()
return eps.mul(std).add_(mu)
def forward(self, x, no_dec=False):
stats = self.encode(x)
mu = stats[:, :self.z_dim]
logvar = stats[:, self.z_dim:]
z = self.reparametrize(mu, logvar)
if no_dec:
return z.squeeze()
else:
x_recon = self.decode(z)
return x_recon, mu, logvar, z.squeeze()
def kaiming_init(m):
if isinstance(m, (nn.Linear, nn.Conv2d, nn.ConvTranspose2d)):
init.kaiming_normal_(m.weight)
if m.bias is not None:
m.bias.data.fill_(0)
elif isinstance(m, (nn.BatchNorm1d, nn.BatchNorm2d)):
m.weight.data.fill_(1)
if m.bias is not None:
m.bias.data.fill_(0)
def normal_init(m):
if isinstance(m, (nn.Linear, nn.Conv2d, nn.ConvTranspose2d)):
init.normal_(m.weight, 0, 0.02)
if m.bias is not None:
m.bias.data.fill_(0)
elif isinstance(m, (nn.BatchNorm1d, nn.BatchNorm2d)):
m.weight.data.fill_(1)
if m.bias is not None:
m.bias.data.fill_(0)