-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFullModels.py
364 lines (266 loc) · 15.8 KB
/
FullModels.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
import torch
import torch.nn as nn
from codebook_quantizers import *
from encoder_decoder import *
class VanillaAE(nn.Module):
"""
Vanilla Autoencoder Model
This model is a regular autoencoder model which assigns a vector of length
embedding_dim to each voxel from the incoming image.
num_channels = number of input channels. In our case, this is just 1 due to grayscale images
num_filters = number of filters in the first convolutional layer, this doubles with every convolutional layer
embedding_dim = dimensionality of vectors in the embedding space
batchnorm = if True, include batchnorm layers after every convolutional layer
"""
def __init__(self, num_channels, num_filters, embedding_dim = 32, batchnorm = False):
super().__init__()
self.encoder = VQEncoder(num_channels, num_filters, embedding_dim, skip_connections = False, batchnorm = batchnorm)
self.decoder = VQDecoder(num_channels, num_filters, embedding_dim, batchnorm)
def forward(self, x):
ze = self.encoder(x)
x_recon = self.decoder(ze)
outputs = {'x_out': x_recon}
return outputs
class VanillaVAE(nn.Module):
"""
Vanilla Variational Autoencoder Model
This model is a regular variational autoencoder model which samples at the voxel level
num_channels = number of input channels. In our case, this is just 1 due to grayscale images
num_filters = number of filters in the first convolutional layer, this doubles with every convolutional layer
embedding_dim = dimensionality of vectors in the embedding space
batchnorm = if True, include batchnorm layers after every convolutional layer
"""
def __init__(self, num_channels, num_filters, embedding_dim = 32, batchnorm = False):
assert embedding_dim % 2 == 0
super().__init__()
self.encoder_mu = VQEncoder(num_channels, num_filters, embedding_dim, skip_connections = False, batchnorm = batchnorm)
self.encoder_sigma = VQEncoder(num_channels, num_filters, embedding_dim, skip_connections = False, batchnorm = batchnorm)
self.decoder = VQDecoder(num_channels, num_filters, embedding_dim, batchnorm)
self.embedding_dim = embedding_dim
def reparametrize(self, mu, log_var):
std = torch.exp(0.5*log_var)
eps = torch.randn_like(std)
new_sample = mu + (eps*std)
return(new_sample)
def forward(self, x):
ze_mu = self.encoder_mu(x)
ze_sigma = self.encoder_sigma(x)
ze_mu = ze_mu.permute(0, 2, 3, 4, 1).contiguous()
ze_sigma = ze_sigma.permute(0, 2, 3, 4, 1).contiguous()
ze_permuted_shape = ze_mu.shape
ze_mu_flat = ze_mu.view(-1,self.embedding_dim)
ze_log_var_flat = ze_sigma.view(-1,self.embedding_dim)
ze_new = self.reparametrize(ze_mu_flat,ze_log_var_flat)
ze_new = ze_new.reshape(ze_permuted_shape).permute(0,4,1,2,3)
KLD = (ze_log_var_flat.exp() + ze_mu_flat.pow(2) - 0.5*ze_log_var_flat - 1/2).mean()
x_recon = self.decoder(ze_new)
outputs = {'x_out': x_recon,"KLD_loss": KLD}
return outputs
class VQVAE3D(nn.Module):
"""
VQVAE and U-VQVAE model
Replace the latent layer in a variational autoencoder with vector quantization as described in Oord et al. 2017
Has skip connection setting. The output is reconstructed only from the quantized representation
This is the baseline architecture taken from https://github.com/RdoubleA/DWI-inpainting
num_channels = number of input channels. In our case, this is just 1 due to grayscale images
num_filters = number of filters in the first convolutional layer, this doubles with every convolutional layer
embedding_dim = dimensionality of vectors in the embedding space
num_embeddings = number of vectors in the embedding space
skip_connections = if True, the model is U-VQVAE. if False, the model is VQVAE.
batchnorm = if True, include batchnorm layers after every convolutional layer
"""
def __init__(self, num_channels, num_filters, embedding_dim = 32, num_embeddings = 512, commitment_cost = 6, skip_connections = False, batchnorm = True):
super().__init__()
self.skip = skip_connections
self.encoder = VQEncoder(num_channels, num_filters, embedding_dim, skip_connections = skip_connections, batchnorm = batchnorm)
self.quantization = VectorQuantizerEMA(num_embeddings = num_embeddings, embedding_dim = embedding_dim, commitment_cost = commitment_cost)
# If skip connections enabled, use a different class for decoder that uses skip connections
if skip_connections:
self.decoder = VQDecoder_skip(num_channels, num_filters, embedding_dim, batchnorm)
else:
self.decoder = VQDecoder(num_channels, num_filters, embedding_dim, batchnorm)
def forward(self, x):
if self.skip:
x1, x2, x3, ze = self.encoder(x)
loss, zq, perplexity, _ = self.quantization(ze)
x_recon = self.decoder(zq, x1, x2, x3)
else:
ze = self.encoder(x)
loss, zq, perplexity, _ = self.quantization(ze)
x_recon = self.decoder(zq)
outputs = {'x_out': x_recon,
'vq_loss': loss}
return outputs
class NewVQVAE3D(nn.Module):
"""
VQVAE model with 2 reconstruction outputs
Replace the latent layer in a variational autoencoder with vector quantization as described in Oord et al. 2017
Changes the VQVAE3D architecture to reconstruct images from direct encoder output (ze) in addition to embeddings (zq).
num_channels = number of input channels. In our case, this is just 1 due to grayscale images
num_filters = number of filters in the first convolutional layer, this doubles with every convolutional layer
embedding_dim = dimensionality of vectors in the embedding space
num_embeddings = number of vectors in the embedding space
batchnorm = if True, include batchnorm layers after every convolutional layer
"""
def __init__(self, num_channels, num_filters, embedding_dim = 32, num_embeddings = 512, commitment_cost = 6, batchnorm = True):
super().__init__()
self.encoder = VQEncoder(num_channels, num_filters, embedding_dim, skip_connections = False, batchnorm = batchnorm)
self.quantization = VectorQuantizerEMA(num_embeddings = num_embeddings, embedding_dim = embedding_dim, commitment_cost = commitment_cost)
self.decoder = VQDecoder(num_channels, num_filters, embedding_dim, batchnorm)
def forward(self, x):
ze = self.encoder(x)
loss, zq, perplexity, _ = self.quantization(ze)
x_recon_zq = self.decoder(zq)
x_recon_ze = self.decoder(ze)
outputs = {'x_out_zq': x_recon_zq, 'x_out_ze': x_recon_ze, 'vq_loss': loss}
return outputs
class SOMVAE3D(nn.Module):
"""
SOMVAE model
Replaces the latent layer in a variational autoencoder with a self organized map (SOM) as in
"SOM-VAE: INTERPRETABLE DISCRETE REPRESENTATION LEARNING ON TIME SERIES"
Reconstructs image from direct encoder output (ze) in addition to quantized representation (zq)
num_channels = number of input channels. In our case, this is just 1 due to grayscale images
num_filters = number of filters in the first convolutional layer, this doubles with every convolutional layer
embedding_dim = dimensionality of vectors in the embedding space
num_embeddings = number of vectors in the embedding space
som_h = height of SOM map, som_h*som_w must be equal to num_embeddings
som_w = width of SOM map
alpha = commitment cost multiplier
beta = SOM cost multiplier
batchnorm = if True, include batchnorm layers after every convolutional layer
"""
def __init__(self, num_channels, num_filters, embedding_dim = 32, num_embeddings = 256, som_h = 16, som_w = 16, alpha = 6, beta = 1, geometry = "rectangular", batchnorm = True):
super().__init__()
self.encoder = VQEncoder(num_channels, num_filters, embedding_dim, skip_connections = False, batchnorm = batchnorm)
self.quantization = SOMQuantizer(num_embeddings = num_embeddings, embedding_dim = embedding_dim, som_h = som_h, som_w = som_w, alpha = alpha, beta = beta, geometry = geometry)
self.decoder = VQDecoder(num_channels, num_filters, embedding_dim, batchnorm)
def forward(self, x):
ze = self.encoder(x)
loss, zq, _ = self.quantization(ze)
x_recon_zq = self.decoder(zq)
x_recon_ze = self.decoder(ze)
outputs = {'x_out_zq': x_recon_zq, 'x_out_ze': x_recon_ze, 'vq_loss': loss}
return outputs
class SOMVAEContinuous(nn.Module):
"""
SOM VAE with a continuous N(0,I) latent space
This model is a regular variational autoencoder model which samples at the voxel level combined with the
SOMQuantizer.
num_channels = number of input channels. In our case, this is just 1 due to grayscale images
num_filters = number of filters in the first convolutional layer, this doubles with every convolutional layer
embedding_dim = dimensionality of vectors in the embedding space
num_embeddings = number of vectors in the embedding space
som_h = height of SOM map, som_h*som_w must be equal to num_embeddings
som_w = width of SOM map
alpha = commitment cost multiplier
beta = SOM cost multiplier
batchnorm = if True, include batchnorm layers after every convolutional layer
"""
def __init__(self, num_channels, num_filters, embedding_dim = 32, num_embeddings = 256, som_h = 16, som_w = 16, alpha = 6, beta = 1, geometry = "rectangular", batchnorm = True):
assert embedding_dim % 2 == 0
super().__init__()
self.encoder_mu = VQEncoder(num_channels, num_filters, embedding_dim, skip_connections = False, batchnorm = batchnorm)
self.encoder_sigma = VQEncoder(num_channels, num_filters, embedding_dim, skip_connections = False, batchnorm = batchnorm)
self.quantization = SOMQuantizer(num_embeddings = num_embeddings, embedding_dim = embedding_dim, som_h = som_h, som_w = som_w, alpha = alpha, beta = beta, geometry = geometry)
self.decoder = VQDecoder(num_channels, num_filters, embedding_dim, batchnorm)
self.embedding_dim = embedding_dim
def reparametrize(self, mu, log_var):
std = torch.exp(0.5*log_var)
eps = torch.randn_like(std)
new_sample = mu + (eps*std)
return(new_sample)
def forward(self, x):
ze_mu = self.encoder_mu(x)
ze_sigma = self.encoder_sigma(x)
ze_mu = ze_mu.permute(0, 2, 3, 4, 1).contiguous()
ze_sigma = ze_sigma.permute(0, 2, 3, 4, 1).contiguous()
ze_permuted_shape = ze_mu.shape
ze_mu_flat = ze_mu.view(-1,self.embedding_dim)
ze_log_var_flat = ze_sigma.view(-1,self.embedding_dim)
ze_new = self.reparametrize(ze_mu_flat,ze_log_var_flat)
ze_new = ze_new.reshape(ze_permuted_shape).permute(0,4,1,2,3)
#Feed sampled ze into SOM quantizer layer
quantizer_loss, zq_new,_ = self.quantization(ze_new)
#Compute usual KLD Loss
KLD = (ze_log_var_flat.exp() + ze_mu_flat.pow(2) - 0.5*ze_log_var_flat - 1/2).mean()
x_recon_ze = self.decoder(ze_new)
x_recon_zq = self.decoder(zq_new)
outputs = {"x_out_ze": x_recon_ze,"x_out_zq": x_recon_zq,
"KLD_loss": KLD,"vq_loss": quantizer_loss}
return outputs
class PSOMVAE3D(nn.Module):
"""
PSOMVAE model
Replaces the latent layer in a variational autoencoder with a probabilistic self organized map.
Described in https://dl.acm.org/doi/pdf/10.1145/3450439.3451872
Reconstructs image from direct encoder output (ze) only. No quantizer image output.
num_channels = number of input channels. In our case, this is just 1 due to grayscale images
num_filters = number of filters in the first convolutional layer, this doubles with every convolutional layer
embedding_dim = dimensionality of vectors in the embedding space
num_embeddings = number of vectors in the embedding space
som_h = height of SOM map, som_h*som_w must be equal to num_embeddings
som_w = width of SOM map
gamma = CAH cost multiplier
beta = SOM cost multiplier
batchnorm = if True, include batchnorm layers after every convolutional layer
"""
def __init__(self, num_channels, num_filters, embedding_dim = 32, num_embeddings = 256, som_h = 16, som_w = 16, gamma = 1, beta = 1, geometry = "rectangular", batchnorm = True):
super().__init__()
self.encoder = VQEncoder(num_channels, num_filters, embedding_dim, skip_connections = False, batchnorm = batchnorm)
self.quantization = PSOMQuantizer(num_embeddings = num_embeddings, embedding_dim = embedding_dim, som_h = som_h, som_w = som_w, gamma = gamma, beta = beta, geometry = geometry)
self.decoder = VQDecoder(num_channels, num_filters, embedding_dim, batchnorm)
def forward(self, x):
ze = self.encoder(x)
quantization_losses = self.quantization(ze)
vq_loss = quantization_losses["vq_loss"]
x_recon_ze = self.decoder(ze)
outputs = {'x_out': x_recon_ze, 'vq_loss': vq_loss}
return outputs
class PSOMVAEContinuous(nn.Module):
"""
PSOM VAE with a continuous N(0,I) latent space
This model is a regular variational autoencoder model which samples at the voxel level combined with the
PSOMQuantizer.
num_channels = number of input channels. In our case, this is just 1 due to grayscale images
num_filters = number of filters in the first convolutional layer, this doubles with every convolutional layer
embedding_dim = dimensionality of vectors in the embedding space
num_embeddings = number of vectors in the embedding space
som_h = height of SOM map, som_h*som_w must be equal to num_embeddings
som_w = width of SOM map
gamma = CAH cost multiplier
beta = SOM cost multiplier
batchnorm = if True, include batchnorm layers after every convolutional layer
"""
def __init__(self, num_channels, num_filters, embedding_dim = 32, num_embeddings = 256, som_h = 16, som_w = 16, gamma = 1, beta = 1, geometry = "rectangular", batchnorm = True):
assert embedding_dim % 2 == 0
super().__init__()
self.encoder_mu = VQEncoder(num_channels, num_filters, embedding_dim, skip_connections = False, batchnorm = batchnorm)
self.encoder_sigma = VQEncoder(num_channels, num_filters, embedding_dim, skip_connections = False, batchnorm = batchnorm)
self.quantization = PSOMQuantizer(num_embeddings = num_embeddings, embedding_dim = embedding_dim, som_h = som_h, som_w = som_w, gamma = gamma, beta = beta, geometry = geometry)
self.decoder = VQDecoder(num_channels, num_filters, embedding_dim, batchnorm)
self.embedding_dim = embedding_dim
def reparametrize(self, mu, log_var):
std = torch.exp(0.5*log_var)
eps = torch.randn_like(std)
new_sample = mu + (eps*std)
return(new_sample)
def forward(self, x):
ze_mu = self.encoder_mu(x)
ze_sigma = self.encoder_sigma(x)
ze_mu = ze_mu.permute(0, 2, 3, 4, 1).contiguous()
ze_sigma = ze_sigma.permute(0, 2, 3, 4, 1).contiguous()
ze_permuted_shape = ze_mu.shape
ze_mu_flat = ze_mu.view(-1,self.embedding_dim)
ze_log_var_flat = ze_sigma.view(-1,self.embedding_dim)
ze_new = self.reparametrize(ze_mu_flat,ze_log_var_flat)
ze_new = ze_new.reshape(ze_permuted_shape).permute(0,4,1,2,3)
#Feed sampled ze into SOM quantizer layer
quantization_losses = self.quantization(ze_new)
vq_loss = quantization_losses["vq_loss"]
#Compute usual KLD Loss
KLD = (ze_log_var_flat.exp() + ze_mu_flat.pow(2) - 0.5*ze_log_var_flat - 1/2).mean()
x_recon_ze = self.decoder(ze_new)
outputs = {"x_out": x_recon_ze,
"KLD_loss": KLD,"vq_loss": vq_loss}
return outputs