-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
360 lines (284 loc) · 11.4 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
import torch
from torch import nn
from torch.nn import functional as F
from blocks import VectorQuantizer, Residual
from configs import autoencoder_config, classifier_config
import os
class Classifier(nn.Module):
def __init__(self) -> None:
super().__init__()
# model should forward unnormalized logits
self.net = nn.Sequential(
nn.Linear(autoencoder_config.embedding_dim, 64),
nn.ReLU(),
nn.Linear(64, 32),
nn.ReLU(),
nn.Linear(32, 16),
nn.ReLU(),
nn.Linear(16, classifier_config.num_classes)
)
def reparameterize(self, mu, log_var):
std = torch.exp(1 / 2 * log_var)
eps = torch.rand_like(std)
return eps * std + mu
def forward(self, mu: torch.Tensor, log_var: torch.Tensor):
x = self.reparameterize(mu, log_var)
return self.net(x)
def save(self):
filename = classifier_config.weights_path
if not os.path.isdir("classifier_weights"):
os.makedirs("classifier_weights")
torch.save(self.state_dict(), filename)
def load(self, filename=classifier_config.weights_path):
state_dict = torch.load(filename, map_location=classifier_config.device)
self.load_state_dict(state_dict)
def get_model_size(self):
return sum(p.numel() for p in self.parameters() if p.requires_grad)
class VQVAE(nn.Module):
'''
https://arxiv.org/abs/1711.00937
'''
def __init__(
self,
in_channels=autoencoder_config.in_channels,
embedding_dim=autoencoder_config.embedding_dim,
num_embeddings=autoencoder_config.num_embeddings,
hidden_dims=autoencoder_config.hidden_dims,
beta=autoencoder_config.beta):
super(VQVAE, self).__init__()
self.in_channels = in_channels
self.embedding_dim = embedding_dim
self.num_embeddings = num_embeddings
self.beta = beta
self.hidden_dims = hidden_dims
# encoder
modules = self.build_encoder(self.hidden_dims)
self.encoder = nn.Sequential(*modules)
self.vq_layer = VectorQuantizer(self.num_embeddings, self.embedding_dim, self.beta)
self.hidden_dims.reverse()
# decoder
modules = self.build_decoder(self.hidden_dims)
self.decoder = nn.Sequential(*modules)
def build_encoder(self, hidden_dims):
modules = []
in_channels = self.in_channels
for dim in hidden_dims:
modules.append(
nn.Sequential(
nn.Conv2d(in_channels, dim, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU()
)
)
in_channels = dim
modules.append(
nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU()
)
)
for _ in range(6):
modules.append(Residual(in_channels, in_channels))
modules.append(nn.LeakyReLU())
modules.append(nn.Sequential(
nn.Conv2d(in_channels, self.embedding_dim, kernel_size=1, stride=1),
nn.LeakyReLU()
))
return modules
def build_decoder(self, hidden_dims):
modules = []
modules.append(nn.Sequential(
nn.Conv2d(self.embedding_dim, hidden_dims[0], kernel_size=3, stride=1, padding=1),
nn.LeakyReLU()
))
for _ in range(6):
modules.append(Residual(hidden_dims[0], hidden_dims[0]))
modules.append(nn.LeakyReLU())
for i in range(len(hidden_dims) - 1):
modules.append(
nn.Sequential(
nn.ConvTranspose2d(hidden_dims[i], hidden_dims[i + 1], kernel_size=4, stride=2, padding=1),
nn.LeakyReLU()
)
)
modules.append(nn.Sequential(
nn.ConvTranspose2d(hidden_dims[-1], out_channels=autoencoder_config.in_channels, kernel_size=4, stride=2, padding=1),
nn.Tanh()
))
return modules
def encode(self, x: torch.Tensor):
'''
:param x: input tensor to encoder [N x C x H x W]
:return: list of latent codes
'''
return self.encoder(x)
def decode(self, z: torch.Tensor):
'''
maps latent codes onto the image space
:param z: Tensor [B x D x H x W]
:return: Tensor [B x C x H x W]
'''
return self.decoder(z)
@torch.no_grad()
def generate(self, x):
'''
returns the reconstructed image [B x C x H x W]
'''
return self.forward_prop(x)[0]
def forward_prop(self, x: torch.Tensor):
encoded = self.encoder(x)
quantized, vq_loss, perplexity, encoded = self.vq_layer(encoded)
return [self.decode(quantized), x, vq_loss, perplexity, encoded]
def forward(self, x: torch.Tensor):
#return self.forward_prop(x)[0]
return self.forward_prop(x)
def loss_function(self, *args):
'''
see self.forward_prop() for details of args
'''
reconstructed, x, vq_loss, perplexity, _ = args
reconstruction_loss = F.mse_loss(reconstructed, x)
loss = reconstruction_loss + vq_loss
return loss, reconstruction_loss, perplexity
def sample(self, num_samples, device=None):
'''
samples from the latent space and maps to the image space
'''
if device is None:
device = autoencoder_config.device
scale_factor = 2 ** len(self.hidden_dims)
#z = torch.rand(num_samples, 1, self.vq_layer.K, self.vq_layer.D)
z = torch.rand(num_samples, self.vq_layer.D, autoencoder_config.image_height // scale_factor, autoencoder_config.image_width // scale_factor)
z.to(device)
quantized_latents, _, _, _ = self.vq_layer(z)
return self.decode(quantized_latents)
def save(self):
filename = autoencoder_config.weights_path
if not os.path.isdir("autoencoder_weights"):
os.makedirs("autoencoder_weights")
torch.save(self.state_dict(), filename)
def load(self, filename=autoencoder_config.weights_path):
state_dict = torch.load(filename, map_location=autoencoder_config.device)
self.load_state_dict(state_dict)
def get_model_size(self):
return sum(p.numel() for p in self.parameters() if p.requires_grad)
class VAE(nn.Module):
'''
https://arxiv.org/abs/1312.6114
'''
def __init__(
self,
in_channels=autoencoder_config.in_channels,
latent_dim=autoencoder_config.embedding_dim,
hidden_dims=None):
super(VAE, self).__init__()
self.in_channels = in_channels
self.latent_dim = latent_dim
if hidden_dims is None:
hidden_dims = [2 ** i for i in range(5, 10)]
modules = self.build_encoder(hidden_dims)
self.encoder = nn.Sequential(*modules)
self.fc_mean = nn.Linear(hidden_dims[-1], latent_dim)
self.fc_variance = nn.Linear(hidden_dims[-1], latent_dim)
self.decoder_input = nn.Linear(latent_dim, hidden_dims[-1])
hidden_dims.reverse()
modules = self.build_decoder(hidden_dims)
self.decoder = nn.Sequential(*modules)
self.final_layer = nn.Sequential(
nn.ConvTranspose2d(hidden_dims[-1], hidden_dims[-1], kernel_size=3, stride=2, padding=1, output_padding=1),
nn.BatchNorm2d(hidden_dims[-1]),
nn.LeakyReLU(),
nn.Conv2d(hidden_dims[-1], out_channels=3, kernel_size=3, padding=1),
nn.Tanh()
)
def build_encoder(self, hidden_dims):
modules = []
in_channels = self.in_channels
for dim in hidden_dims:
modules.append(
nn.Sequential(
nn.Conv2d(in_channels, out_channels=dim, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(dim),
nn.LeakyReLU())
)
in_channels = dim
return modules
def build_decoder(self, hidden_dims):
modules = []
for i in range(len(hidden_dims) - 1):
modules.append(nn.Sequential(
nn.ConvTranspose2d(hidden_dims[i], hidden_dims[i + 1], kernel_size=3, stride=2, padding=1, output_padding=1),
nn.BatchNorm2d(hidden_dims[i + 1]),
nn.LeakyReLU()
))
return modules
def generate(self, x):
'''
returns the reconstructed image
:param x: tensor [B x C x H x W]
:return: tensor [B x C x H x W]
'''
return self.forward(x)[0]
def sample(self, num_samples, device):
'''
Samples from the latent space and maps to image space map
'''
z = torch.randn(num_samples, self.latent_dim)
z.to(device)
return self.decode(z)
def forward_prop(self, x):
mu, log_var = self.encode(x)
z = self.reparameterize(mu, log_var)
return [self.decode(z), x, mu, log_var]
def forward(self, x):
return self.forward_prop(x)[0]
def reparameterize(self, mu, log_var):
'''
Reparameterization trick to sample N(mu, var) from N(0, 1)
:param mu: Mean of the latent Gaussian [B x D]
:param log_var: standard deviation of the latent Gaussian [B x D]
'''
std = torch.exp(1 / 2 * log_var)
eps = torch.rand_like(std)
return eps * std + mu
def decode(self, z):
'''
reconstruct the image given the latent embedding
:param z: latent codes [B x D]
:return: tensor [B x C x H x W]
'''
res = self.decoder_input(z).view(-1, 512, 1, 1)
res = self.decoder(res)
return self.final_layer(res)
def encode(self, x: torch.Tensor):
'''
Encodes the input by passing through the encoder network and returns latent codes
:param x: (Tensor) input tensor to encoder [N x C x H x W]
:return: list of parameters of latent Gaussian distribution
'''
res = torch.flatten(self.encoder(x), start_dim=1)
mu = self.fc_mean(res)
log_variance = self.fc_variance(res)
return [mu, log_variance]
def loss_function(self, args, weight=autoencoder_config.kld_weight):
'''
Computes the VAE loss function (KL divergence)
'''
# see self.forward_prop() for details of unpacking the args
x_hat, x, mu, log_var = args
# KL divergence weight
if weight is None:
weight = 1
reconstruction_loss = F.mse_loss(x_hat, x)
kld_loss = torch.mean(-0.5 * torch.sum(1 + log_var - torch.square(mu) - log_var.exp(), dim=1), dim=0)
loss = reconstruction_loss + weight * kld_loss
return loss, reconstruction_loss, kld_loss
def save(self):
filename = autoencoder_config.weights_path
if not os.path.isdir("autoencoder_weights"):
os.makedirs("autoencoder_weights")
torch.save(self.state_dict(), filename)
def load(self, filename=autoencoder_config.weights_path):
state_dict = torch.load(filename, map_location=autoencoder_config.device)
self.load_state_dict(state_dict)
def get_model_size(self):
return sum(p.numel() for p in self.parameters() if p.requires_grad)