-
Notifications
You must be signed in to change notification settings - Fork 0
/
MVAE.py
415 lines (316 loc) · 15.9 KB
/
MVAE.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
import warnings
from collections import OrderedDict
import matplotlib.pyplot as plt
import numpy as np
import pytorch_lightning as pl
import torch
import torch.nn.functional as F
from torch import nn
from torchvision import utils
warnings.simplefilter("ignore")
# Build Encoder
class Encoder(pl.LightningModule):
def __init__(self,latent_dim,im_size=32,in_channel=3,hiddens=[128,256,512]):
super(Encoder, self).__init__()
'''
@latent_dim : The dimension of the latent space
@im_size : The height/width of the squared image considered
@in_channel : Number of channels for the input images (ex : 3 channels for RGB images)
@hiddens : A list containing the respectives sizes of the hidden spaces involved in the neural network.
Architecture based on 2D convolutions
'''
self.latent_dim = latent_dim
self.im_size=im_size
self.in_channel=in_channel
self.hiddens=hiddens
self.modules=[nn.Conv2d(self.in_channel,hiddens[0], kernel_size=3, stride=2, padding=1), # hiddens[0]*im_size/2xim_size/2
nn.ReLU(),
nn.BatchNorm2d(hiddens[0])]
for i in range(1,len(hiddens)):
# hiddens[i]*(im_size/2^i)^2
self.modules.append(nn.Conv2d(hiddens[i-1],hiddens[i], kernel_size=3, stride=2, padding=1))
self.modules.append(nn.ReLU())
self.modules.append(nn.BatchNorm2d(self.hiddens[i]))
self.modules.append(nn.Tanh())
self.modules.append(nn.Flatten())
final_size=(im_size)//(2**(len(hiddens)))
self.encode=nn.Sequential(*self.modules)
#output : the mean and the variance of the distribution of q(z|x)
self.fc_mu = nn.Linear(self.hiddens[-1] * final_size * final_size, self.latent_dim)
self.fc_var = nn.Linear(self.hiddens[-1] * final_size * final_size, self.latent_dim)
def forward(self, x):
"""
Encodes the input by passing through the encoder network and returns the latent codes.
:param input: (Tensor) Input tensor to encoder [Batch_size x in_channel x H x W]
:return: (Tensor) List of latent codes
"""
result = self.encode(x)
# Split the result into mu and var components of the latent Gaussian distribution
mu = self.fc_mu(result)
log_var = self.fc_var(result)
return [mu, log_var]
# Build Decoder
class Decoder_MLP(pl.LightningModule):
def __init__(self, latent_dim=4,in_channel=3,im_size=64,hiddens=[256,512]):
super(Decoder_MLP, self).__init__()
'''
@latent_dim : The dimension of the latent space
@in_channel : Number of channels for the input images (ex : 3 channels for RGB images)
@im_size : The height/width of the squared image considered
@hiddens : A list containing the respectives sizes of the hidden spaces involved in the neural network.
Architecture based on Linear layers
'''
self.latent_dim = latent_dim
self.im_size = im_size
self.in_channel=in_channel
self.hiddens=hiddens
self.modules=[nn.Linear(latent_dim,hiddens[0])]
for i in range(1,len(hiddens)):
self.modules.append(nn.ReLU())
self.modules.append(nn.Linear(hiddens[i-1],hiddens[i]))
self.modules.append(nn.Tanh())
self.modules.append(nn.Linear(hiddens[-1],self.in_channel*self.im_size*self.im_size))
self.modules.append(nn.Sigmoid())
self.decode=nn.ModuleList(self.modules)
def forward(self, z):
"""
Maps the given latent codes onto the image space.
:param z: (Tensor) [Batch_size x latent_dim]
:return: (Tensor) [Batch_size x in_channel x H x W]
"""
z=z.view(-1,self.latent_dim)
for layer in self.decode:
z = layer(z)
z = z.view(-1,self.in_channel,self.im_size,self.im_size)
return z
# Build Decoder
class Decoder_Conv(pl.LightningModule):
def __init__(self, latent_dim=4, in_channel=3, im_size=64, hiddens=[512,256,128,64],init=4):
super(Decoder_Conv, self).__init__()
'''
@latent_dim : The dimension of the latent space
@in_channel : Number of channels for the input images (ex : 3 channels for RGB images)
@im_size : The height/width of the squared image considered
@hiddens : A list containing the respectives sizes of the hidden spaces involved in the neural network.
Architecture based on 2D Deconvolutions
'''
assert (2**(len(hiddens))*init==im_size),"Take care about the architecture of your decoder, there is something wrong"
self.latent_dim = latent_dim
self.im_size = im_size
self.hiddens=hiddens
self.in_channel = in_channel
self.modules = [nn.ConvTranspose2d(self.latent_dim,hiddens[0],init, 1, 0), #init*init
nn.ReLU(),
nn.BatchNorm2d(hiddens[0])]
for i in range(1, len(hiddens)):
self.modules.append(nn.ConvTranspose2d(hiddens[i-1],hiddens[i], 4, 2, 1)) #im_size=2^(2+i)
self.modules.append(nn.ReLU())
self.modules.append(nn.BatchNorm2d(hiddens[i]))
self.modules.append(nn.ConvTranspose2d(hiddens[-1],self.in_channel, 4, 2, 1)) #im_size*im_size
self.modules.append(nn.Sigmoid())
self.decode = nn.ModuleList(self.modules)
def forward(self, z):
"""
Maps the given latent codes onto the image space.
:param z: (Tensor) [Batch_size x latent_dim]
:return: (Tensor) [Batch_size x in_channel x H x W]
"""
z = z.view(-1,self.latent_dim,1,1)
for layer in self.decode:
z = layer(z)
return z
# Build Decoder
class Decoder_Linear_Conv(pl.LightningModule):
def __init__(self, latent_dim=4, in_channel=3, im_size=64, hiddens=[512,256,128,64],init=4):
super(Decoder_Linear_Conv, self).__init__()
'''
@latent_dim : The dimension of the latent space
@in_channel : Number of channels for the input images (ex : 3 channels for RGB images)
@im_size : The height/width of the squared image considered
@hiddens : A list containing the respectives sizes of the hidden spaces involved in the neural network.
Architecture based on a Linear Layer and 2D Deconvolutions
'''
assert (2**(len(hiddens))*init==im_size),"Take care about the architecture of your decoder, there is something wrong"
self.latent_dim = latent_dim
self.im_size = im_size
self.hiddens= hiddens
self.in_channel = in_channel
self.modules = [nn.Linear(self.latent_dim,self.latent_dim//2),
nn.ReLU(),
nn.ConvTranspose2d(self.latent_dim//2,hiddens[0],init, 1, 0), #init*init
nn.SELU()]
for i in range(1, len(hiddens)):
self.modules.append(nn.ConvTranspose2d(hiddens[i-1],hiddens[i], 4, 2, 1)) #im_size=2^(2+i)
self.modules.append(nn.SELU())
self.modules.append(nn.ConvTranspose2d(hiddens[-1],self.in_channel, 4, 2, 1)) #im_size*im_size
self.modules.append(nn.Sigmoid())
self.decode = nn.ModuleList(self.modules)
def forward(self, z):
"""
Maps the given latent codes onto the image space.
:param z: (Tensor) [Batch_size x latent_dim]
:return: (Tensor) [Batch_size x in_channel x H x W]
"""
z = z.view(-1,self.latent_dim)
for i,layer in enumerate(self.decode):
if i==2:
z=z.view(-1,self.latent_dim//2,1,1)
z=layer(z)
return z
# Decoders=nn.ModuleList([Decoder_MLP(latent_dim=100, in_channel=1, im_size=32, hiddens=[256,512]),
# Decoder_Conv(latent_dim=100, in_channel=1, im_size=32, hiddens=[512,256,128],init=4),
# Decoder_Linear_Conv(latent_dim=100, in_channel=1, im_size=32, hiddens=[512,256,128,64],init=2)])
# cuda0 = torch.device('cuda:0')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class MabVAE(pl.LightningModule):
def __init__(self, train_loader, decoders, eps=0.1, i=0):
super(MabVAE, self).__init__()
'''
@train_loader : A pytorch dataloader containing all the images of the training set
@decoders : A list of proposals for decoders
@epsilon : parameter of the epsilon-greedy strategy for solving the MAB problem
This class implements the MAB-VAE model envisioned in the report using the famous epsilon-greedy strategy
(See Sutton et al,Reinforcement Learning: An Introduction)
'''
self.eps=eps
self.decoders=decoders
self.nb_decoders=len(decoders)
# the latent dims, im_size and in_channels are deduced from the decoders
self.latent_dim=self.decoders[0].latent_dim
self.im_size = self.decoders[0].im_size
self.in_channel=self.decoders[0].in_channel
self.encoder = Encoder(latent_dim=self.latent_dim,in_channel=decoders[0].in_channel,im_size=self.im_size)
#tensor storing information about the number of times a decoder is chosen and the reward associated
self.history=torch.zeros(self.nb_decoders).to(device)
self.NbDraws=torch.zeros(self.nb_decoders).to(device)
#list for storing the reconstruction losses obtained through the time following our strategy
self.strategy_path=[]
self.train_loader = train_loader
#list for storing the best rewards obtainable at each round
self.best_rewards=[]
self.latent = torch.randn(64, self.latent_dim, 1, 1).to(device)
self.i=i #counter of epochs
self.t=0 #counter of steps
def reparameterize(self, mu, logvar):
"""
Reparameterization trick enabling to sample from N(mu, var) using N(0,1).
:param mu: (Tensor) Mean of the latent Gaussian [batch_size x latent_dim]
:param logvar: (Tensor) Standard deviation of the latent Gaussian [batch_size x latent_dim]
:return: (Tensor) [batch_size x latent_dim]
"""
std = torch.exp(0.5 * logvar).type_as(mu)
eps = torch.randn_like(std).type_as(mu)
return eps * std + mu
def loss_function(self, recons, input, mu, log_var):
"""
Computes the VAE loss function.
KL(N(\mu, \sigma), N(0, 1)) = \log \frac{1}{\sigma} + \frac{\sigma^2 + \mu^2}{2} - \frac{1}{2}
Reconstruction_Loss = BCE
"""
recon_loss = F.binary_cross_entropy(recons.view(-1, self.im_size, self.im_size), input.view(-1, self.im_size, self.im_size), reduction='sum')
weight = (len(self.train_loader.dataset) / (recons.size(0)))
#maximize -kl = minimize kl
kld_loss = -0.5 * torch.sum(1 + log_var - mu.pow(2) - log_var.exp())
loss = weight * (recon_loss + kld_loss)
return {'total_loss': loss, 'Reconstruction_Loss': weight * recon_loss, 'KL': weight * kld_loss}
# Optimizers
def configure_optimizers(self):
'''
Configuration of the optimizers used for the encoder and the decoders
'''
opt_list=[]
for decoder in self.decoders:
optimizer = torch.optim.Adam(decoder.parameters())
opt_list.append(optimizer)
opt_list.append(torch.optim.Adam(self.encoder.parameters()))
# return the list of optimizers and second empty list is for schedulers (if any)
return opt_list, []
# Calls after prepare_data for DataLoader
def train_dataloader(self):
return self.train_loader
def forward(self, x):
return self.encoder(x)
def backward(self, loss,optimizer,optimizer_idx):
loss.backward()
# calls before every train step tart
def on_train_batch_start(self,batch,batch_idx,dataloader_idx):
#initialization of all the decoders
#at the first epoch, they all have the same probability to be selected
if self.i==0:
id_to_choose=np.random.randint(self.nb_decoders)
self.NbDraws[id_to_choose]+=1
#eps-greedy behaviour
else:
u=np.random.random()
if u<self.eps:
id_to_choose=np.random.randint(self.nb_decoders)
self.NbDraws[id_to_choose] += 1
else:
average_previous_rewards=(self.history/self.NbDraws)
id_to_choose=torch.argmax(average_previous_rewards).item()
self.NbDraws[id_to_choose] += 1
self.id_to_choose=id_to_choose
# Training Loop
def training_step(self, batch, batch_idx, optimizer_idx):
if optimizer_idx==self.id_to_choose:
# batch returns x and y tensors
real_images, _ = batch
#encoding
mu, log_var = self.encoder(real_images)
mu=mu.type_as(real_images[0])
log_var=log_var.type_as(real_images[0])
#sampling of the latent variable
z = self.reparameterize(mu, log_var).type_as(real_images[0])
id_to_choose=self.id_to_choose
#Computation of the best reward obtainable at time t with respect to all the possible decoders
#**********
with torch.no_grad():
best_reward=-10**(10)
#Computation of the best reward achievable at time t
for decoder in self.decoders:
recons=decoder(z).type_as(real_images[0])
reward=-self.loss_function(recons,real_images,mu,log_var)['Reconstruction_Loss']
if reward>best_reward:
best_reward=reward
self.best_rewards.append(best_reward)
#***********
# Reconstruction with the selected decoder
recons = self.decoders[id_to_choose](z).type_as(real_images[0])
step_dict = self.loss_function(recons,real_images,mu,log_var)
self.history[id_to_choose]-=step_dict['Reconstruction_Loss']
self.strategy_path.append(-step_dict['Reconstruction_Loss'])
# periodic save in order to monitor the evolution of the training for the decoders
if self.t % 100 == 0:
fake = self.decoders[id_to_choose](self.latent).detach()
plt.figure(figsize=(10, 10))
plt.imshow(np.transpose(utils.make_grid(fake, padding=2, normalize=True).cpu(), (1, 2, 0)))
plt.savefig(f'VAE_current_result_decoder={id_to_choose}.png')
plt.close('all')
#We update the parameters of the chosen decoder
output = OrderedDict({
'loss': step_dict['total_loss'],
'progress_bar': step_dict,
'log': step_dict
})
self.t += 1
return output
if optimizer_idx==self.nb_decoders:
real_images, _ = batch
mu, log_var = self.encoder(real_images)
mu=mu.type_as(real_images[0])
log_var=log_var.type_as(real_images[0])
#sampling of the latent variable
z = self.reparameterize(mu, log_var).type_as(real_images[0])
# Reconstruction with the selected decoder
recons = self.decoders[self.id_to_choose](z).type_as(real_images[0])
step_dict = self.loss_function(recons,real_images,mu,log_var)
#We update the parameters of the encoder
output = OrderedDict({
'loss': step_dict['total_loss'],
'progress_bar': step_dict,
'log': step_dict
})
return output
# calls after every epoch ends
def on_epoch_end(self):
self.i+=1