-
Notifications
You must be signed in to change notification settings - Fork 1
/
vae_mnist_cnn.py
403 lines (284 loc) · 10.4 KB
/
vae_mnist_cnn.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
# -*- coding: utf-8 -*-
"""vae_mnist_cnn.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1FivAIGGFfx8J5R5-2aRbx5PWiWxIwI3j
### Loading Required Libraries
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.manifold import TSNE
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
BATCH_SIZE = 64 # number of data points in each batch
N_EPOCHS = 20 # times to run the model on complete data
INPUT_DIM = 28 * 28 # size of each input
HIDDEN_DIM = 256 # hidden dimension
LATENT_DIM = 10 # latent vector dimension
lr = 1e-3 # learning rate
"""### Loading Data"""
transforms = transforms.Compose([transforms.ToTensor()])
train_dataset = datasets.MNIST(
'./data',
train=True,
download=True,
transform=transforms)
test_dataset = datasets.MNIST(
'./data',
train=False,
download=True,
transform=transforms
)
train_iterator = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
test_iterator = DataLoader(test_dataset, batch_size=BATCH_SIZE)
"""### Batch Visualization of Given Data"""
def visualize_data(batch):
batch = torchvision.utils.make_grid(batch)
batch = batch.numpy()
batch = np.transpose(batch,(1,2,0))
plt.figure(figsize = (8,8))
plt.imshow(batch, cmap = 'Greys_r')
plt.show()
batch,labels = iter(train_iterator).next()
visualize_data(batch)
"""### Creating Model"""
class Encoder(nn.Module):
''' This the encoder part of VAE
'''
def __init__(self, hidden_dim, z_dim):
'''
Args:
input_dim: A integer indicating the size of input (in case of MNIST 28 * 28).
hidden_dim: A integer indicating the size of hidden dimension.
z_dim: A integer indicating the latent dimension.
'''
super().__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels = 16, kernel_size=3)
self.conv2 = nn.Conv2d(in_channels = 16, out_channels = 32, kernel_size = 3)
self.linear = nn.Linear(32*24*24, 512)
self.linear2 = nn.Linear(512, hidden_dim)
self.mu = nn.Linear(hidden_dim, z_dim)
self.var = nn.Linear(hidden_dim, z_dim)
def forward(self, x):
# x is of shape [batch_size, input_dim]
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
bs, ch, w,h = x.shape
x = x.view(bs,ch*w*h)
x = F.relu(self.linear(x))
hidden = F.relu(self.linear2(x))
# hidden is of shape [batch_size, hidden_dim]
z_mu = self.mu(hidden)
# z_mu is of shape [batch_size, latent_dim]
z_var = self.var(hidden)
# z_var is of shape [batch_size, latent_dim]
return z_mu, z_var
class Decoder(nn.Module):
''' This the decoder part of VAE
'''
def __init__(self, z_dim, hidden_dim):
super().__init__()
self.linear = nn.Linear(z_dim, hidden_dim)
self.linear2 = nn.Linear(hidden_dim, 512 )
self.linear3 = nn.Linear(512, 18432)
self.deconv1 = nn.ConvTranspose2d(32, 16, kernel_size=3)
self.deconv2 = nn.ConvTranspose2d(16, 1, kernel_size = 3)
def forward(self, x):
# x is of shape [batch_size, latent_dim]
x = F.relu(self.linear(x))
x = F.relu(self.linear2(x))
x = F.relu(self.linear3(x))
# print(x.shape)
x = x.view(-1, 32, 24,24)
x = F.relu(self.deconv1(x))
predicted = torch.sigmoid(self.deconv2(x))
# predicted is of shape [batch_size, output_dim]
return predicted
class VAE(nn.Module):
def __init__(self, enc, dec):
''' This the VAE, which takes a encoder and decoder.
'''
super().__init__()
self.enc = enc
self.dec = dec
def forward(self, x):
# encode
z_mu, z_var = self.enc(x)
# sample from the distribution having latent parameters z_mu, z_var
# reparameterize
std = torch.exp(z_var / 2)
eps = torch.randn_like(std)
x_sample = eps.mul(std).add_(z_mu)
# decode
predicted = self.dec(x_sample)
return predicted, z_mu, z_var
# encoder
encoder = Encoder( HIDDEN_DIM, LATENT_DIM)
# decoder
decoder = Decoder(LATENT_DIM, HIDDEN_DIM)
# vae
model = VAE(encoder, decoder).cuda()
# optimizer
optimizer = optim.Adam(model.parameters(), lr=lr)
# xavier weight initialization
def init_xavier(m):
if type(m) == nn.Linear:
nn.init.xavier_uniform(m.weight)
model.apply(init_xavier)
print(model)
"""### Training the Model"""
def train():
# set the train mode
model.train()
# loss of the epoch
train_loss = 0
for i, (x, _) in enumerate(train_iterator):
x = x.to(device)
# update the gradients to zero
optimizer.zero_grad()
# forward pass
x_sample, z_mu, z_var = model(x)
# reconstruction loss
recon_loss = F.binary_cross_entropy(x_sample, x, size_average=False)
# kl divergence loss
kl_loss = 0.5 * torch.sum(torch.exp(z_var) + z_mu**2 - 1.0 - z_var)
# total loss
loss = recon_loss + kl_loss
# backward pass
loss.backward()
train_loss += loss.item()
# update the weights
optimizer.step()
return train_loss
def test():
# set the evaluation mode
model.eval()
# test loss for the data
test_loss = 0
with torch.no_grad():
for i, (x, _) in enumerate(test_iterator):
x = x.to(device)
# forward pass
x_sample, z_mu, z_var = model(x)
# reconstruction loss
recon_loss = F.binary_cross_entropy(x_sample, x, size_average=False)
# kl divergence loss
kl_loss = 0.5 * torch.sum(torch.exp(z_var) + z_mu**2 - 1.0 - z_var)
# total loss
loss = recon_loss + kl_loss
test_loss += loss.item()
return test_loss
def generate_data(bs=BATCH_SIZE):
z = torch.randn(bs, LATENT_DIM).to(device)
reconstructed_img = model.dec(z)
img = reconstructed_img.view(-1,1,28, 28).data
img = img.cpu()
visualize_data(img)
N_EPOCHS = 20
def fit():
best_test_loss = float('inf')
print('Before Training : ')
generate_data()
for e in range(N_EPOCHS):
train_loss = train()
test_loss = test()
train_loss /= len(train_dataset)
test_loss /= len(test_dataset)
print(f'Epoch {e}, Train Loss: {train_loss:.2f}, Test Loss: {test_loss:.2f}')
generate_data()
if best_test_loss > test_loss:
best_test_loss = test_loss
patience_counter = 1
else:
patience_counter += 1
if patience_counter > 3:
break
"""### Saving The Weights of Best Model"""
# fit() Training The model
# path = '/content/drive/My Drive/Colab Notebooks/deeplearning/asg3/pickle/best_vae_model_state_dict.pt'
# torch.save(model.state_dict(), path)
# Loading the weights of model
# model = VAE(encoder, decoder)
path = '/content/drive/My Drive/Colab Notebooks/deeplearning/asg3/pickle/best_vae_model_state_dict.pt'
model.load_state_dict(torch.load(path))
"""### Generating grid of data Using VAE Model"""
generate_data() # This function defined above will generate Batch Data
"""### Get Latent data"""
def get_latent_data(data_iterator):
means = np.zeros((1,10))
vars = np.zeros((1,10))
data_labels = []
with torch.no_grad():
for batch, labels in data_iterator:
labels = list(labels.numpy())
batch = batch.to(device)
mean, var = model.enc(batch)
mean = mean.to('cpu').numpy()
var = var.to('cpu').numpy()
means = np.vstack((means,mean))
vars = np.vstack((vars, var))
data_labels.extend(labels)
means = means[1:]
vars = vars[1:]
return means, vars, data_labels
train_means, train_vars, train_labels = get_latent_data(train_iterator)
"""### TSNE"""
mean_tsne = TSNE(n_components = 1, random_state = 0)
var_tsne = TSNE(n_components = 1, random_state = 0)
tsne_mean = mean_tsne.fit_transform(train_means)
tsne_var = var_tsne.fit_transform(train_vars)
df = pd.DataFrame()
df['Mean'] = tsne_mean.squeeze(1)
df['Variance'] = tsne_var.squeeze(1)
df['label'] = train_labels
g = sns.FacetGrid(df, hue = 'label', height = 8).map(plt.scatter, 'Mean','Variance').add_legend()
plt.legend(fontsize='x-large', title_fontsize='40',loc = 'best')
plt.show()
"""### Getting latent data for test data"""
train_latent_data = np.column_stack((train_means, train_vars))
train_latent_df = pd.DataFrame(train_latent_data)
train_labels = np.array(train_labels)
# test latent data
test_means, test_vars, test_labels = get_latent_data(test_iterator)
test_latent_data = np.column_stack((test_means, test_vars))
test_latent_df = pd.DataFrame(test_latent_data)
test_labels = np.array(test_labels)
print('Shape of Training data : ', train_latent_data.shape)
print('Shape of Testing Data : ', test_latent_data.shape)
"""### Training the SVM classifier on Latent Space"""
from sklearn.svm import LinearSVC
clf = LinearSVC(random_state=0, tol=1e-5)
clf.fit(train_latent_df, train_labels)
# prediction on test data
ypreds = clf.predict(test_latent_df)
def plot_confusion_matrix(cm, title, filepath):
df_cm = pd.DataFrame(cm, index = [i for i in "0123456789"],
columns = [i for i in "0123456789"])
plt.figure(figsize = (8,5))
sns.heatmap(df_cm, annot=True, fmt='d')
plt.title(title)
#plt.savefig(filepath)
plt.show()
test_acc = accuracy_score(ytrue, ypreds)
f_score = f1_score(ytrue, ypreds, average='macro')
prec = precision_score(ytrue, ypreds, average='macro')
recall = recall_score(ytrue, ypreds, average='macro')
cm = confusion_matrix(ytrue, ypreds)
print('Test Accuracy : ', test_acc)
print('Test prec : ', prec)
print('Test Recall : ', recall)
print('Test Data F - Score : ', f_score)
plot_confusion_matrix(cm, 'Confusion Matrix ','dummy')