-
Notifications
You must be signed in to change notification settings - Fork 0
/
vqvae.py
288 lines (240 loc) · 9.61 KB
/
vqvae.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
import numpy as np
import torch
from torch import nn
from torch.autograd import Function
from torchvision.models.resnet import resnet18
from math import exp
from vae import VAE
class ResBlock(nn.Module):
def __init__(self, dim):
super().__init__()
self.block = nn.Sequential(
nn.ReLU(True),
nn.Conv2d(dim, dim, 3, 1, 1),
nn.BatchNorm2d(dim),
nn.ReLU(True),
nn.Conv2d(dim, dim, 1),
nn.BatchNorm2d(dim)
)
def forward(self, x):
return x + self.block(x)
class VectorQuantization(Function):
@staticmethod
def forward(ctx, inputs, codebook):
with torch.no_grad():
embedding_size = codebook.size(1)
inputs_size = inputs.size()
inputs_flatten = inputs.view(-1, embedding_size)
codebook_sqr = torch.sum(codebook ** 2, dim=1)
inputs_sqr = torch.sum(inputs_flatten ** 2, dim=1, keepdim=True)
# Compute the distances to the codebook
distances = torch.addmm(codebook_sqr + inputs_sqr,
inputs_flatten, codebook.t(), alpha=-2.0, beta=1.0)
_, indices_flatten = torch.min(distances, dim=1)
indices = indices_flatten.view(*inputs_size[:-1])
ctx.mark_non_differentiable(indices)
return indices
@staticmethod
def backward(ctx, grad_output):
raise RuntimeError('Trying to call `.grad()` on graph containing '
'`VectorQuantization`. The function `VectorQuantization` '
'is not differentiable. Use `VectorQuantizationStraightThrough` '
'if you want a straight-through estimator of the gradient.')
class VectorQuantizationStraightThrough(Function):
@staticmethod
def forward(ctx, inputs, codebook):
indices = vq(inputs, codebook)
indices_flatten = indices.view(-1)
ctx.save_for_backward(indices_flatten, codebook)
ctx.mark_non_differentiable(indices_flatten)
codes_flatten = torch.index_select(codebook, dim=0,
index=indices_flatten)
codes = codes_flatten.view_as(inputs)
return (codes, indices_flatten)
@staticmethod
def backward(ctx, grad_output, grad_indices):
grad_inputs, grad_codebook = None, None
if ctx.needs_input_grad[0]:
# Straight-through estimator
grad_inputs = grad_output.clone()
if ctx.needs_input_grad[1]:
# Gradient wrt. the codebook
indices, codebook = ctx.saved_tensors
embedding_size = codebook.size(1)
grad_output_flatten = (grad_output.contiguous()
.view(-1, embedding_size))
grad_codebook = torch.zeros_like(codebook)
grad_codebook.index_add_(0, indices, grad_output_flatten)
return (grad_inputs, grad_codebook)
vq = VectorQuantization.apply
vq_st = VectorQuantizationStraightThrough.apply
class VQEmbedding(nn.Module):
def __init__(self, K, D):
super().__init__()
self.embedding = nn.Embedding(K, D)
self.embedding.weight.data.uniform_(-1./K, 1./K)
def forward(self, z_e_x):
z_e_x_ = z_e_x.permute(0, 2, 3, 1).contiguous()
latents = vq(z_e_x_, self.embedding.weight)
return latents
def straight_through(self, z_e_x):
z_e_x_ = z_e_x.permute(0, 2, 3, 1).contiguous()
z_q_x_, indices = vq_st(z_e_x_, self.embedding.weight.detach())
z_q_x = z_q_x_.permute(0, 3, 1, 2).contiguous()
z_q_x_bar_flatten = torch.index_select(self.embedding.weight,
dim=0, index=indices)
z_q_x_bar_ = z_q_x_bar_flatten.view_as(z_e_x_)
z_q_x_bar = z_q_x_bar_.permute(0, 3, 1, 2).contiguous()
return z_q_x, z_q_x_bar
class VQVAE(VAE):
def __init__(self, img_size, nb_channels, latent_img_size, z_dim,
rec_loss="xent", beta=1, delta=1, gamma=1, dist="mse",
num_embed=512, dataset="mvtec"):
# NOTE all the parameters from VAE are still existing and taking memory
# even thouhgh not used
super().__init__(img_size, nb_channels, latent_img_size, z_dim, rec_loss,
beta, delta)
self.gamma = gamma
self.dist = dist
self.codebook = VQEmbedding(num_embed, z_dim)
self.num_embed = num_embed
input_dim = nb_channels
dim = z_dim
# overrides VAE final_encoder because there we need to double z_dim
# NOTE that it is not used if we used Oord encoder
self.final_encoder = nn.Sequential(
nn.Conv2d(self.max_depth_conv, self.z_dim, kernel_size=1, stride=1,
padding=0)
)
if dataset in ["mvtec", "got", "semmacape", "kelonia", "miad"]:
self.oord_encoder = nn.Sequential(
nn.Conv2d(input_dim, dim, 4, 2, 1),
nn.BatchNorm2d(dim),
nn.ReLU(True),
nn.Conv2d(dim, dim, 4, 2, 1),
nn.BatchNorm2d(dim),
nn.ReLU(True),
nn.Conv2d(dim, dim, 4, 2, 1),
ResBlock(dim),
ResBlock(dim),
ResBlock(dim),
)
self.oord_decoder = nn.Sequential(
ResBlock(dim),
ResBlock(dim),
ResBlock(dim),
nn.ReLU(True),
nn.ConvTranspose2d(dim, dim, 4, 2, 1),
nn.BatchNorm2d(dim),
nn.ReLU(True),
nn.ConvTranspose2d(dim, dim, 4, 2, 1),
nn.BatchNorm2d(dim),
nn.ReLU(True),
nn.ConvTranspose2d(dim, input_dim, 4, 2, 1),
# nn.Tanh()
)
if dataset == "UCSDped1" or dataset == "cifar10":
# Delete one convolutional layer for 32x32 latent space
self.oord_encoder = nn.Sequential(
nn.Conv2d(input_dim, dim, 4, 2, 1),
nn.BatchNorm2d(dim),
nn.ReLU(True),
nn.Conv2d(dim, dim, 4, 2, 1),
ResBlock(dim),
ResBlock(dim),
ResBlock(dim),
)
self.oord_decoder = nn.Sequential(
ResBlock(dim),
ResBlock(dim),
ResBlock(dim),
nn.ReLU(True),
nn.ConvTranspose2d(dim, dim, 4, 2, 1),
nn.BatchNorm2d(dim),
nn.ReLU(True),
nn.ConvTranspose2d(dim, input_dim, 4, 2, 1),
)
def encoder(self, x):
z_e_x = self.oord_encoder(x)
return z_e_x
def decoder(self, z_q_x_st):
''' can also be z_q_x simply but a difference is made is forward for
the gradient to flow '''
x = self.oord_decoder(z_q_x_st)
x = nn.Sigmoid()(x)
return x
def forward(self, x):
z_e_x = self.encoder(x)
self.z_e_x = z_e_x
z = self.codebook(z_e_x) # this is where gradient cannot flow
z_q_x_st, z_q_x = self.codebook.straight_through(z_e_x)
self.z_q_x = z_q_x
x = self.decoder(z_q_x_st)
return x, (z_e_x, z, z_q_x)
def generate(self, nb_samples):
z = torch.randint(
low=0,
high=self.num_embed,
size=(nb_samples, self.latent_img_size, self.latent_img_size)
)
z_q_x = self.codebook.embedding(z).permute(0, 3, 1, 2)
return self.decoder(z_q_x), z
def loss_function(self, recon_x, x, z_e_x, z_q_x):
if self.rec_loss == "xent":
rec_term = torch.mean(self.xent_continuous_ber(recon_x, x))
elif self.rec_loss == "mse":
rec_term = torch.mean(-self.mse(recon_x, x))
# this trains the codebook this is alignment loss
# can be seen as a Kmeans clustering where the codebook centroids move
if self.dist == "mse":
loss_e = torch.mean(
torch.sum(
nn.functional.mse_loss(
z_q_x,
z_e_x.detach(),
reduction='none'
),
dim=(1, 2, 3)
),
dim=0
)
elif self.dist == "cos":
loss_e = torch.mean(
nn.functional.cosine_similarity(z_q_x, z_e_x.detach())
)
# this trains the inputs embedding this is comitment loss
# this forces the input embeding to be close comitted to their codebook
# vector
if self.dist == "mse":
loss_c = torch.mean(
torch.sum(
nn.functional.mse_loss(
z_e_x,
z_q_x.detach(),
reduction='none'
),
dim=(1, 2, 3)
),
dim=0
)
elif self.dist == "cos":
loss_c = torch.mean(
nn.functional.cosine_similarity(z_e_x, z_q_x.detach())
)
loss = (rec_term - self.beta * loss_e - self.gamma * loss_c)
loss_dict = {
'loss': loss,
'rec_term': rec_term,
'beta*loss_e': self.beta * loss_e,
'gamma*loss_c': self.gamma * loss_c
}
return loss, loss_dict
def step(self, input_mb, anneal_tau=False):
# move tau (parameter of the codebook) at each 100 step
recon_mb, (z_e_x, z, z_q_x) = self.forward(input_mb)
loss, loss_dict = self.loss_function(recon_mb, input_mb,
z_e_x, z_q_x)
if self.rec_loss == "xent":
# NOTE do this after the loss function
recon_mb = self.mean_from_lambda(recon_mb)
return loss, recon_mb, loss_dict