-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
554 lines (417 loc) · 17.2 KB
/
models.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
from abc import abstractmethod
import torch
import torch.nn as nn
import numpy as np
from torch.distributions import Bernoulli
from torch.nn import modules
from torch.nn.modules import module
from torch.nn.modules.activation import ReLU
from torch.nn import init
from torchvision import models
def kaiming_init(m):
if isinstance(m, (nn.Linear, nn.Conv2d)):
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 fc_block(in_features, out_features, use_bn=True, use_relu=True):
moduels = [nn.Linear(in_features, out_features)]
if use_bn:
moduels.append(nn.BatchNorm1d(out_features))
if use_relu:
moduels.append(nn.ReLU(True))
return nn.ModuleList(moduels)
def conv_block(in_channels, out_channels, kernel_size,
stride=1, padding=0,
use_bn=True, use_relu=True):
modules = [nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)]
if use_bn:
modules.append(nn.BatchNorm2d(out_channels))
if use_relu:
modules.append(nn.ReLU(True))
return nn.ModuleList(modules)
def deconv_block(in_channels, out_channels, kernel_size,
stride=1, padding=0, output_padding=0,
use_bn=True, use_relu=True):
moduels = [nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride, padding, output_padding)]
if use_bn:
moduels.append(nn.BatchNorm2d(out_channels))
if use_relu:
moduels.append(nn.ReLU(True))
return nn.ModuleList(moduels)
class BaseVAE(nn.Module):
def __init__(self, latent_dim, input_dim):
super().__init__()
self.input_dim = input_dim
self.latent_dim = latent_dim
def sampling(self, mu, log_var):
n_samples = mu.shape[0]
epsilon = torch.randn((n_samples, self.latent_dim))
if mu.is_cuda:
epsilon = epsilon.cuda()
return mu + torch.exp(0.5*log_var) * epsilon
@abstractmethod
def encode(self, inputs):
raise NotImplemented
@abstractmethod
def decode(self, z):
raise NotImplemented
def forward(self, x):
z, mu, log_var = self.encode(x)
x_hat = self.decode(z)
return x_hat, mu, log_var
def dist_predict(self, z):
dist_param = self.decode(z)
dist = Bernoulli(dist_param)
return dist.sample()
class LinearVAE(BaseVAE):
def __init__(self, latent_dim=2, input_dim=(28, 28)) -> None:
super().__init__(latent_dim, input_dim)
self.latent_dim = latent_dim
self.input_dim = input_dim
flatten_dim = int(np.prod(input_dim))
self.encoder = nn.Sequential(
nn.Linear(flatten_dim, 400),
nn.ReLU(),
nn.Linear(400, 300),
nn.ReLU(),
nn.Linear(300, 2*self.latent_dim)
)
self.decoder = nn.Sequential(
nn.Linear(self.latent_dim, 300),
nn.ReLU(),
nn.Linear(300, 400),
nn.ReLU(),
nn.Linear(400, self.flatten_dim)
)
def encode(self, x):
x = nn.Flatten()(x)
x = self.encoder(x)
mu, log_var = torch.chunk(x, chunks=2, dim=-1)
z = self.sampling(mu, log_var)
return z, mu, log_var
def decode(self, z):
x = self.decoder(z)
x = torch.reshape(x, (x.shape[0], *self.input_dim))
x = torch.sigmoid(x)
return x
class VAE(BaseVAE):
def __init__(self, latent_dim=2, input_dim=(1, 28, 28)):
super().__init__(latent_dim, input_dim)
self.latent_dim = latent_dim
self.input_dim = input_dim
self.cnn_encoder = nn.Sequential(
nn.Conv2d(in_channels=self.input_dim[0], out_channels=32, kernel_size=5, stride=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
self.shape, self.flatten_shape = self.get_shape()
self.fc_encoder = nn.Sequential(
nn.Linear(self.flatten_shape, 1024),
nn.ReLU(),
nn.Linear(in_features=1024, out_features=2*self.latent_dim)
)
self.fc_decoder = nn.Sequential(
nn.Linear(in_features=self.latent_dim, out_features=1024),
nn.ReLU(),
nn.BatchNorm1d(num_features=1024),
nn.Linear(in_features=1024, out_features=self.flatten_shape),
nn.ReLU(),
nn.BatchNorm1d(self.flatten_shape)
)
self.cnn_decoder = nn.Sequential(
nn.ConvTranspose2d(in_channels=64, out_channels=64, kernel_size=4, stride=2, output_padding=1),
nn.ReLU(),
nn.BatchNorm2d(num_features=64),
nn.ConvTranspose2d(in_channels=64, out_channels=self.input_dim[0], kernel_size=4, stride=2),
)
# self.output_decoder = nn.ConvTranspose2d(in_channels=32, out_channels=1, kernel_size=4, stride=2, padding=1)
def get_shape(self):
x = torch.zeros(1, *self.input_dim)
x = self.cnn_encoder(x)
return x.shape[1:], torch.prod(torch.tensor(x.shape)).item()
def encode(self, x):
x = self.cnn_encoder(x)
x = torch.flatten(x, start_dim=1)
x = self.fc_encoder(x)
mu, log_var = torch.chunk(x, chunks=2, dim=-1)
z = self.sampling(mu, log_var)
return z, mu, log_var
def decode(self, z):
x = self.fc_decoder(z)
x = torch.reshape(x, (x.shape[0], *self.shape))
x = self.cnn_decoder(x)
x = torch.sigmoid(x)
return x
class CelebaVAE(BaseVAE):
def __init__(self, latent_dim, input_dim):
super().__init__(latent_dim, input_dim)
self.latent_dim = latent_dim
self.input_dim = input_dim
hidden_dims = [3, 32, 32, 64, 64]
modules = []
for i, _ in enumerate(hidden_dims[:-1]):
modules.extend(
conv_block(in_channels=hidden_dims[i], out_channels=hidden_dims[i+1],
kernel_size=4, stride=2, padding=1, use_bn=False)
)
modules.extend(
conv_block(in_channels=hidden_dims[-1], out_channels=256,
kernel_size=4, stride=1, padding=0, use_bn=False)
)
self.cnn_encoder = nn.Sequential(*modules)
self.shape, self.flatten_shape = self.get_shape()
# modules = []
# modules.extend(fc_block(in_features=self.flatten_shape, out_features=256, use_bn=False))
# modules.extend(fc_block(in_features=256, out_features=256, use_bn=False))
# modules.extend(fc_block(in_features=256, out_features=self.latent_dim*2, use_bn=False))
self.fc_encoder = nn.Linear(in_features=self.flatten_shape, out_features=self.latent_dim*2)
# modules = []
# modules.extend(fc_block(in_features=self.latent_dim, out_features=256, use_bn=False))
# modules.extend(fc_block(in_features=256, out_features=256, use_bn=False))
# modules.extend(fc_block(in_features=256, out_features=self.flatten_shape, use_bn=False))
self.fc_decoder = nn.Linear(in_features=self.latent_dim, out_features=self.flatten_shape)
hidden_dims.reverse()
modules = []
modules.extend(
deconv_block(in_channels=256, out_channels=hidden_dims[0],
kernel_size=4, stride=1, padding=0, use_bn=False)
)
for i, _ in enumerate(hidden_dims[:-2]):
# # adding output_padding to one layer before the last layer.
# output_padding = 1 if i == len(hidden_dims) - 3 else 0
modules.extend(
deconv_block(in_channels=hidden_dims[i], out_channels=hidden_dims[i+1],
kernel_size=4, stride=2, padding=1, use_bn=False)
)
modules.append(
nn.ConvTranspose2d(in_channels=hidden_dims[-2], out_channels=hidden_dims[-1],
kernel_size=4, stride=2, padding=1)
)
self.cnn_decoder = nn.Sequential(*modules)
self.apply(kaiming_init)
def get_shape(self):
x = torch.zeros(1, *self.input_dim)
x = self.cnn_encoder(x)
return x.shape[1:], torch.prod(torch.tensor(x.shape)).item()
def encode(self, x):
x = self.cnn_encoder(x)
x = torch.flatten(x, start_dim=1)
x = self.fc_encoder(x)
mu, log_var = torch.chunk(x, chunks=2, dim=-1)
z = self.sampling(mu, log_var)
return z, mu, log_var
def decode(self, z):
x = self.fc_decoder(z)
x = torch.reshape(x, (x.shape[0], *self.shape))
x = self.cnn_decoder(x)
x = torch.sigmoid_(x)
return x
class CelebaVAE(BaseVAE):
def __init__(self, latent_dim, input_dim):
super().__init__(latent_dim, input_dim)
self.latent_dim = latent_dim
self.input_dim = input_dim
hidden_dims = [3, 32, 32, 64, 64]
modules = []
for i, _ in enumerate(hidden_dims[:-1]):
modules.extend(
conv_block(in_channels=hidden_dims[i], out_channels=hidden_dims[i+1],
kernel_size=4, stride=2, padding=1, use_bn=False)
)
modules.extend(
conv_block(in_channels=hidden_dims[-1], out_channels=256,
kernel_size=4, stride=1, padding=0, use_bn=False)
)
self.cnn_encoder = nn.Sequential(*modules)
self.shape, self.flatten_shape = self.get_shape()
self.fc_encoder = nn.Linear(in_features=self.flatten_shape, out_features=self.latent_dim*2)
self.fc_decoder = nn.Linear(in_features=self.latent_dim, out_features=self.flatten_shape)
hidden_dims.reverse()
modules = []
modules.extend(
deconv_block(in_channels=256, out_channels=hidden_dims[0],
kernel_size=4, stride=1, padding=0, use_bn=False)
)
for i, _ in enumerate(hidden_dims[:-2]):
modules.extend(
deconv_block(in_channels=hidden_dims[i], out_channels=hidden_dims[i+1],
kernel_size=4, stride=2, padding=1, use_bn=False)
)
modules.append(
nn.ConvTranspose2d(in_channels=hidden_dims[-2], out_channels=hidden_dims[-1],
kernel_size=4, stride=2, padding=1)
)
self.cnn_decoder = nn.Sequential(*modules)
self.apply(kaiming_init)
def get_shape(self):
x = torch.zeros(1, *self.input_dim)
x = self.cnn_encoder(x)
return x.shape[1:], torch.prod(torch.tensor(x.shape)).item()
def encode(self, x):
x = self.cnn_encoder(x)
x = torch.flatten(x, start_dim=1)
x = self.fc_encoder(x)
mu, log_var = torch.chunk(x, chunks=2, dim=-1)
z = self.sampling(mu, log_var)
return z, mu, log_var
def decode(self, z):
x = self.fc_decoder(z)
x = torch.reshape(x, (x.shape[0], *self.shape))
x = self.cnn_decoder(x)
x = torch.sigmoid_(x)
return x
class Encoder(nn.Module):
def __init__(self, input_shape=(1, 1, 28, 28), num_classes=10) -> None:
super().__init__()
kernel_size = 3
filters = 64
self.input_shape = input_shape
self.num_classes = num_classes
encoder_modules = nn.ModuleList()
encoder_modules.append(nn.Conv2d(in_channels=1, out_channels=filters, kernel_size=kernel_size))
encoder_modules.append(nn.ReLU())
for _ in range(2):
encoder_modules.append(nn.Conv2d(in_channels=filters, out_channels=filters, kernel_size=kernel_size))
encoder_modules.append(nn.ReLU())
self.encoder = nn.Sequential(*encoder_modules)
def forward(self, x):
return self.encoder(x)
class Decoder(nn.Module):
def __init__(self) -> None:
super().__init__()
kernel_size = 3
filters = 64
decoder_modules = nn.ModuleList()
for _ in range(2):
decoder_modules.append(nn.ConvTranspose2d(in_channels=filters, out_channels=filters, kernel_size=kernel_size))
decoder_modules.append(nn.ReLU())
decoder_modules.append(nn.ConvTranspose2d(in_channels=filters, out_channels=1, kernel_size=kernel_size))
decoder_modules.append(nn.ReLU())
self.decoder = nn.Sequential(*decoder_modules)
def forward(self, x):
return self.decoder(x)
class LatentEncoder(nn.Module):
def __init__(self, input_shape=(1, 1, 28, 28), num_classes=10) -> None:
super().__init__()
self.input_shape = input_shape
self.num_classes = num_classes
self.encoder = Encoder(input_shape, num_classes)
self.decoder = Decoder()
drop_out_rate = 0.2
self.shape, self.flatten_shape = self.get_shape()
self.classifier = nn.Sequential(
nn.Dropout(p=drop_out_rate),
nn.Linear(self.flatten_shape, self.num_classes)
)
def get_shape(self):
x = torch.zeros(self.input_shape)
x = self.encoder(x)
return x.shape[1:], torch.prod(torch.tensor(x.shape)).item()
def encode(self, x):
return self.encoder(x)
def decode(self, x):
return self.decoder(x)
def forward(self, x):
x = self.encoder(x)
features = torch.flatten(x, start_dim=1)
out = self.classifier(features)
return features, out
class AutoEncoder(nn.Module):
def __init__(self, vae: VAE, latent_encoder: LatentEncoder, freeze: bool = True):
super().__init__()
self.vae = vae
self.encoder = latent_encoder
if freeze:
self._freeze_vae()
def forward(self, x):
return self.encode(x)
def encode(self, x):
x = self.vae.decode(x)
x = self.encoder.encode(x)
x = torch.flatten(x, start_dim=1)
return x
def decode(self, x):
x = x.reshape((-1, *self.encoder.shape))
x = self.encoder.decode(x)
z, _, _ = self.vae.encode(x)
return z
def _freeze_vae(self):
for param in self.vae.parameters():
param.requires_grad_(False)
class LatentClassifier(nn.Module):
def __init__(self, input_dim, num_classes):
super().__init__()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.linear = nn.Linear(input_dim, num_classes).to(device)
self.softmax = nn.Softmax(dim=1).to(device)
def forward(self, x):
return self.linear(x).squeeze()
def predict(self, x):
_, labels = torch.max(self.linear(x), dim=-1)
return labels
def logits(self, x):
return self.softmax(self.linear(x))
class DataModel(nn.Module):
def __init__(self, encoder: AutoEncoder, classifier: LatentClassifier):
super().__init__()
self.encoder = encoder
self.classifier = classifier
def encode(self, x):
return self.encoder.encode(x)
def classify(self, x):
return self.classifier(x)
def forward(self, x):
return self.classify(self.encode(x))
def predict(self, z):
x = self.encode(z)
return self.classifier.predict(x)
def freeze(self):
for param in self.parameters():
param.requires_grad_(False)
class ClassifierCelebA(nn.Module):
def __init__(self, input_dim):
super().__init__()
self.input_dim = input_dim
filters = [3, 64, 64, 128, 128, 256, 256]
modules = []
for i, _ in enumerate(filters[:-1]):
modules.extend(
conv_block(in_channels=filters[i], out_channels=filters[i+1],
kernel_size=4, stride=2, padding=1)
)
self.cnn = nn.Sequential(*modules)
self.shape, self.flatten_shape = self.get_shape()
self.classifier = nn.Sequential(
*fc_block(in_features=self.flatten_shape, out_features=1024),
nn.Linear(in_features=1024, out_features=1)
)
@torch.no_grad()
def get_shape(self):
self.eval()
x = torch.zeros(1, *self.input_dim)
x = self.cnn(x)
return tuple(x.shape[1:]), np.prod(x.shape)
def forward(self, x):
x = self.cnn(x).flatten(1)
x = self.classifier(x)
return x.ravel()
def predict(self, x):
logits = self.forward(x)
return torch.where(logits > 0, 1.0, 0.0)
# class ClassifierCelebA(nn.Module):
# def __init__(self, input_dim):
# super().__init__()
# self.input_dim = input_dim
# self.model = models.AlexNet(num_classes=1)
# def forward(self, x):
# return self.model(x).squeeze()
# def predict(self, x):
# logits = self.forward(x)
# return torch.where(logits > 0, 1.0, 0.0)