-
Notifications
You must be signed in to change notification settings - Fork 2
/
Conditional_VAE_AmirPourmand_99210259.py
270 lines (204 loc) · 8.19 KB
/
Conditional_VAE_AmirPourmand_99210259.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import torch
import numpy as np
import torch.nn.functional as F
import torchvision
import os, time
from tqdm import tqdm
import torch.nn as nn
from collections import OrderedDict
from sklearn.preprocessing import LabelBinarizer
# In[2]:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# In[3]:
class Flatten(nn.Module):
def __init__(self):
super(Flatten, self).__init__()
def forward(self, x):
batch_size = x.shape[0]
return x.view(batch_size, -1)
class MLP(nn.Module):
def __init__(self, hidden_size, last_activation = True):
super(MLP, self).__init__()
q = []
for i in range(len(hidden_size)-1):
in_dim = hidden_size[i]
out_dim = hidden_size[i+1]
q.append(("Linear_%d" % i, nn.Linear(in_dim, out_dim)))
if (i < len(hidden_size)-2) or ((i == len(hidden_size) - 2) and (last_activation)):
q.append(("BatchNorm_%d" % i, nn.BatchNorm1d(out_dim)))
q.append(("ReLU_%d" % i, nn.ReLU(inplace=True)))
self.mlp = nn.Sequential(OrderedDict(q))
def forward(self, x):
return self.mlp(x)
class Encoder(nn.Module):
def __init__(self, shape, nhid = 16, ncond = 0):
super(Encoder, self).__init__()
c, h, w = shape
ww = ((w-8)//2 - 4)//2
hh = ((h-8)//2 - 4)//2
self.encode = nn.Sequential(nn.Conv2d(c, 16, 5, padding = 0), nn.BatchNorm2d(16), nn.ReLU(inplace = True),
nn.Conv2d(16, 32, 5, padding = 0), nn.BatchNorm2d(32), nn.ReLU(inplace = True),
nn.MaxPool2d(2, 2),
nn.Conv2d(32, 64, 3, padding = 0), nn.BatchNorm2d(64), nn.ReLU(inplace = True),
nn.Conv2d(64, 64, 3, padding = 0), nn.BatchNorm2d(64), nn.ReLU(inplace = True),
nn.MaxPool2d(2, 2),
Flatten(), MLP([ww*hh*64, 256, 128])
)
self.calc_mean = MLP([128+ncond, 64, nhid], last_activation = False)
self.calc_logvar = MLP([128+ncond, 64, nhid], last_activation = False)
def forward(self, x, y = None):
x = self.encode(x)
if (y is None):
return self.calc_mean(x), self.calc_logvar(x)
else:
return self.calc_mean(torch.cat((x, y), dim=1)), self.calc_logvar(torch.cat((x, y), dim=1))
class Decoder(nn.Module):
def __init__(self, shape, nhid = 16, ncond = 0):
super(Decoder, self).__init__()
c, w, h = shape
self.shape = shape
self.decode = nn.Sequential(MLP([nhid+ncond, 64, 128, 256, c*w*h], last_activation = False), nn.Sigmoid())
def forward(self, z, y = None):
c, w, h = self.shape
if (y is None):
return self.decode(z).view(-1, c, w, h)
else:
return self.decode(torch.cat((z, y), dim=1)).view(-1, c, w, h)
class VAE(nn.Module):
def __init__(self, shape, nhid = 16):
super(VAE, self).__init__()
self.dim = nhid
self.encoder = Encoder(shape, nhid)
self.decoder = Decoder(shape, nhid)
def sampling(self, mean, logvar):
eps = torch.randn(mean.shape).to(device)
sigma = 0.5 * torch.exp(logvar)
return mean + eps * sigma
def forward(self, x):
mean, logvar = self.encoder(x)
z = self.sampling(mean, logvar)
return self.decoder(z), mean, logvar
def generate(self, batch_size = None):
z = torch.randn((batch_size, self.dim)).to(device) if batch_size else torch.randn((1, self.dim)).to(device)
res = self.decoder(z)
if not batch_size:
res = res.squeeze(0)
return res
class cVAE(nn.Module):
def __init__(self, shape, nclass, nhid = 16, ncond = 16):
super(cVAE, self).__init__()
self.dim = nhid
self.encoder = Encoder(shape, nhid, ncond = ncond)
self.decoder = Decoder(shape, nhid, ncond = ncond)
self.label_embedding = nn.Embedding(nclass, ncond)
def sampling(self, mean, logvar):
eps = torch.randn(mean.shape).to(device)
sigma = 0.5 * torch.exp(logvar)
return mean + eps * sigma
def forward(self, x, y):
y = self.label_embedding(y)
mean, logvar = self.encoder(x, y)
z = self.sampling(mean, logvar)
return self.decoder(z, y), mean, logvar
def generate(self, class_idx):
if (type(class_idx) is int):
class_idx = torch.tensor(class_idx)
class_idx = class_idx.to(device)
if (len(class_idx.shape) == 0):
batch_size = None
class_idx = class_idx.unsqueeze(0)
z = torch.randn((1, self.dim)).to(device)
else:
batch_size = class_idx.shape[0]
z = torch.randn((batch_size, self.dim)).to(device)
y = self.label_embedding(class_idx)
res = self.decoder(z, y)
if not batch_size:
res = res.squeeze(0)
return res
BCE_loss = nn.BCELoss(reduction = "sum")
def loss(X, X_hat, mean, logvar):
reconstruction_loss = BCE_loss(X_hat, X)
KL_divergence = 0.5 * torch.sum(-1 - logvar + torch.exp(logvar) + mean**2)
return reconstruction_loss + KL_divergence
def weights_init(m):
if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
torch.nn.init.normal_(m.weight, 0.0, 0.1)
if isinstance(m, nn.BatchNorm2d):
torch.nn.init.normal_(m.weight, 0.0, 0.1)
def crop(x, low, high):
x[x<=low] = low
x[x>=high] = high
return x
from torchvision.utils import make_grid
import matplotlib.pyplot as plt
def plot_images(image, num_images=6, size=(1, 28, 28)):
image_grid = make_grid(image.detach().cpu()[:num_images], nrow=3)
plt.imshow(image_grid.permute(1, 2, 0).squeeze())
plt.show()
# In[4]:
transform = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Lambda(lambda x: crop(x, 0., 1.))
])
train_data = torchvision.datasets.MNIST(root='../../Datasets', train=True, download=True, transform=transform)
test_data = torchvision.datasets.MNIST(root='../../Datasets', train=False, download=True, transform=transform)
# In[6]:
vae = VAE((1, 28, 28), nhid = 4)
vae.apply(weights_init)
vae.to(device)
batch_size = 64
optimizer = torch.optim.Adam(vae.parameters(), lr= 0.001, weight_decay = 0.001)
train_loader = torch.utils.data.DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_data, batch_size=batch_size, shuffle=False)
# In[8]:
n_epochs = 10
frequency = 2
for epoch in range(n_epochs):
train_loss= []
for x,y in tqdm(train_loader):
x = x.to(device)
x = (x>0.5).float()
optimizer.zero_grad()
x_hat, mean, logvar = vae(x)
kl_bce_loss = loss(x, x_hat, mean, logvar).to(device)
kl_bce_loss.backward()
optimizer.step()
train_loss += [kl_bce_loss.item()]
print(f'epoch {epoch}, train loss {np.mean(train_loss)}')
if epoch % frequency ==0:
plot_images(vae.generate(20))
# In[10]:
frequency = 4
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
net = cVAE((1, 28, 28), 10, nhid = 2, ncond = 16)
net.to(device)
print(net)
lr = 0.01
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=lr, weight_decay = 0.0001)
def adjust_lr(optimizer, decay_rate=0.95):
for param_group in optimizer.param_groups:
param_group['lr'] *= decay_rate
max_epochs = 3
net = net.to(device)
print("training on ", device)
for epoch in range(max_epochs):
train_loss, n, start = 0.0, 0, time.time()
for X, y in tqdm(train_loader, ncols = 50):
X = X.to(device)
y = y.to(device)
X_hat, mean, logvar = net(X, y)
l = loss(X, X_hat, mean, logvar).to(device)
optimizer.zero_grad()
l.backward()
optimizer.step()
train_loss += l.cpu().item()
n += X.shape[0]
train_loss /= n
print('epoch %d, train loss %.4f , time %.1f sec'
% (epoch, train_loss, time.time() - start))
adjust_lr(optimizer)
# In[ ]: