-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM21AIE225_Lab_Assignment_2.py
500 lines (403 loc) · 15.4 KB
/
M21AIE225_Lab_Assignment_2.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# %%
import seaborn as sns
from sklearn import metrics
from sklearn.manifold import TSNE
from matplotlib import offsetbox
from time import time
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
import numpy as np
import matplotlib.pyplot as plt
# %%
transform = transforms.ToTensor()
traindataset = datasets.FashionMNIST(
'./downloads/', download=True, train=True, transform=transform)
testdataset = datasets.FashionMNIST(
'./downloads/', download=True, train=False, transform=transform)
# %%
bs = 1000
trainloader = torch.utils.data.DataLoader(
traindataset, batch_size=bs, shuffle=True, num_workers=4)
testloader = torch.utils.data.DataLoader(
testdataset, batch_size=bs, shuffle=False, num_workers=4)
# %%
def view_samples():
test_images, labels = next(iter(testloader))
fig = plt.figure(figsize=(20, 7))
nrows = 3
ncols = 10
b = np.random.randint(0, test_images.shape[0]-nrows*ncols)
for i in range(nrows*ncols):
inp = test_images.view(-1, 28, 28)
plot = fig.add_subplot(nrows, ncols, i+1)
plot.set_title(testdataset.classes[labels[i+b].cpu().numpy()])
imgplot = plt.imshow(inp[i+b].cpu(), cmap='gray')
plt.show()
view_samples()
# %%
class AutoEncoder(nn.Module):
def __init__(self):
super(AutoEncoder, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(784, 512),
nn.ReLU(),
nn.Linear(512, 256),
nn.ReLU(),
nn.Linear(256, 128),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
)
self.decoder = nn.Sequential(
nn.Linear(64, 128),
nn.Sigmoid(),
nn.Linear(128, 256),
nn.Sigmoid(),
nn.Linear(256, 512),
nn.Sigmoid(),
nn.Linear(512, 784),
nn.Sigmoid()
)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x
# %%
model = AutoEncoder()
# %%
model
# %%
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
criterion = nn.MSELoss()
# %%
epochs = 60
noise_factor = 0.2
loss_list = []
for epoch in range(epochs):
train_loss = 0.0
val_loss = 0.0
for images, labels in trainloader:
# add random noise to the input images
noisy_imgs = images + noise_factor * torch.randn(*images.shape)
# Clip the images to be between 0 and 1
noisy_imgs = np.clip(noisy_imgs, 0., 1.)
# clear the gradients of all optimized variables
optimizer.zero_grad()
# forward pass: compute predicted outputs by passing *noisy* images to the model
outputs = model(noisy_imgs.view(-1, 784))
# calculate the loss
# the "target" is still the original, not-noisy images
loss = criterion(outputs, images.view(-1, 784))
# backward pass: compute gradient of the loss with respect to model parameters
loss.backward()
# perform a single optimization step (parameter update)
optimizer.step()
# update running training loss
train_loss += loss.item()*images.size(0)
with torch.no_grad():
for images, labels in testloader:
# add random noise to the input images
noisy_imgs = images + noise_factor * torch.randn(*images.shape)
# Clip the images to be between 0 and 1
noisy_imgs = np.clip(noisy_imgs, 0., 1.)
# forward pass: compute predicted outputs by passing *noisy* images to the model
outputs = model(noisy_imgs.view(-1, 784))
# calculate the loss
# the "target" is still the original, not-noisy images
loss = criterion(outputs, images.view(-1, 784))
# update running training loss
val_loss += loss.item()*images.size(0)
# print avg training statistics
train_loss = train_loss/len(trainloader)
val_loss = val_loss/len(testloader)
print(
f'Epoch: {epoch+1} \tTraining Loss: {train_loss:.6f}, \tTest Loss: {val_loss:.6f}, ')
loss_list.append([train_loss, val_loss])
loss_list = np.array(loss_list)
# %%
plt.title("Training vs Validation loss")
plt.plot(loss_list[:, 0], linestyle='--', label="Training Loss")
plt.plot(loss_list[:, 1], linestyle='-', label="Validation Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()
plt.show()
# %%
test_images, test_labels = next(iter(testloader))
noisy_test_imgs = np.clip(test_images + noise_factor *
torch.randn(*test_images.shape), 0., 1.)
test_output = model(noisy_test_imgs.view(-1, 784))
# %% [markdown]
# ### Some original,noise imposed and reconstructed images from above DAE
# %%
fig = plt.figure(figsize=(20, 7))
nrows = 10
b = np.random.randint(0, test_images.shape[0]-nrows)
for i in range(nrows):
plot = fig.add_subplot(3, nrows, 0*nrows+i+1)
plot.set_title('Original Image')
imgplot = plt.imshow(test_images.view(-1, 28, 28)
[b+i].cpu().detach(), cmap='gray')
plot = fig.add_subplot(3, nrows, 1*nrows+i+1)
plot.set_title('Noisy Image')
imgplot = plt.imshow(noisy_test_imgs.view(-1, 28, 28)
[b+i].cpu().detach(), cmap='gray')
plot = fig.add_subplot(3, nrows, 2*nrows+i+1)
plot.set_title('Reconstructed')
imgplot = plt.imshow(test_output.view(-1, 28, 28)
[b+i].cpu().detach(), cmap='gray')
plt.show()
# %%
# Function to Scale and visualize the embedding vectors
def plot_embedding(X_embed, y, title=None):
with torch.no_grad():
X = TSNE(n_components=2, learning_rate='auto',
init='random', perplexity=40).fit_transform(X_embed)
x_min, x_max = np.min(X, 0), np.max(X, 0)
X = (X - x_min) / (x_max - x_min)
plt.figure(figsize=(12, 10))
ax = plt.subplot(111)
for i in range(X.shape[0]):
plt.text(X[i, 0], X[i, 1], str(y[i]),
color=plt.cm.Set1(y[i] / 10.),
fontdict={'weight': 'bold', 'size': 9})
if hasattr(offsetbox, 'AnnotationBbox'):
# only print thumbnails with matplotlib > 1.0
shown_images = np.array([[1., 1.]]) # just something big
for i in range(X.shape[0]):
dist = np.sum((X[i] - shown_images) ** 2, 1)
if np.min(dist) < 20e-3:
# don't show points that are too close
continue
shown_images = np.r_[shown_images, [X[i]]]
imagebox = offsetbox.AnnotationBbox(
offsetbox.OffsetImage(test_images[i].reshape(
28, 28), cmap=plt.cm.gray_r),
X[i])
ax.add_artist(imagebox)
plt.xticks([]), plt.yticks([])
if title is not None:
plt.title(title)
# %%
test_embed = model.encoder(test_images.view(-1, 784))
# %%
with torch.no_grad():
plot_embedding(test_embed, test_labels.numpy())
# %% [markdown]
# ### 1 FC layer with sigmoid activation for 10 class classification
# %% [markdown]
# ##### Roll = M21AIE225
# ##### X = 4, as last digit of roll no. is odd
# ##### Y = sigmoid, as last digit of roll no. is odd
# %%
class SingleFCLayerClassifier(nn.Module):
def __init__(self):
super(SingleFCLayerClassifier, self).__init__()
self.encoder = model.encoder
self.fc_layer = nn.Sequential(
nn.Linear(64, 10),
nn.Sigmoid()
)
def forward(self, x):
x = self.encoder(x)
x = self.fc_layer(x)
return x
# %%
classifier_1fc = SingleFCLayerClassifier()
print(classifier_1fc)
# %%
optimizer_1fc = torch.optim.Adam(classifier_1fc.parameters(), lr=0.001)
criterion_1fc = nn.CrossEntropyLoss()
# %%
epochs = 20
loss_list = []
for epoch in range(epochs):
train_loss = 0.0
val_loss = 0.0
for images, labels in trainloader:
# clear the gradients of all optimized variables
optimizer_1fc.zero_grad()
# forward pass: compute predicted outputs by passing *noisy* images to the model
outputs = classifier_1fc(images.view(-1, 784))
# calculate the loss
# the "target" is still the original, not-noisy images
loss = criterion_1fc(outputs, labels)
# backward pass: compute gradient of the loss with respect to model parameters
loss.backward()
# perform a single optimization step (parameter update)
optimizer_1fc.step()
# update running training loss
train_loss += loss.item()*images.size(0)
with torch.no_grad():
for images, labels in testloader:
# forward pass: compute predicted outputs by passing *noisy* images to the model
outputs = classifier_1fc(images.view(-1, 784))
# calculate the loss
# the "target" is still the original, not-noisy images
loss = criterion_1fc(outputs, labels)
# update running training loss
val_loss += loss.item()*images.size(0)
# print avg training statistics
train_loss = train_loss/len(trainloader)
val_loss = val_loss/len(testloader)
print(
f'Epoch: {epoch+1} \tTraining Loss: {train_loss:.6f}, \tTest Loss: {val_loss:.6f}, ')
loss_list.append([train_loss, val_loss])
loss_list = np.array(loss_list)
# %%
plt.title("Training vs Validation loss for Model with 1FC Layer")
plt.plot(loss_list[:, 0], linestyle='--', label="Training Loss")
plt.plot(loss_list[:, 1], linestyle='-', label="Validation Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()
plt.show()
# %%
test_output_1fc = classifier_1fc(test_images.view(-1, 784))
# %%
#sns.set(rc={'axes.facecolor': 'lightblue', 'figure.facecolor': 'lightblue'})
def confusionMatrixAndAccuracyReport(Y_test, Y_pred, label):
cm = metrics.confusion_matrix(Y_test, Y_pred)
overallAccuracy = np.trace(cm)/sum(cm.flatten())
classwiseAccuracy = cm.diagonal()/cm.sum(axis=1)
plt.figure(figsize=(10, 10))
plt.title('Accuracy Score: {0:3.3f}'.format(overallAccuracy), size=14)
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
sns.heatmap(data=cm, annot=True, square=True, cmap='Blues', fmt='g')
plt.show()
print('Overall Accuracy Score: {0:3.3f}'.format(overallAccuracy))
print('Classwise Accuracy Score: {0}'.format(classwiseAccuracy))
# %%
with torch.no_grad():
confusionMatrixAndAccuracyReport(
test_labels, test_output_1fc.argmax(axis=1), testdataset.classes)
# %%
test_embed = model.encoder(test_images.view(-1, 784))
with torch.no_grad():
plot_embedding(test_embed, test_labels.numpy())
# %%
fig = plt.figure(figsize=(20, 7))
nrows = 10
b = np.random.randint(0, test_images.shape[0]-nrows)
for i in range(nrows):
plot = fig.add_subplot(3, nrows, 0*nrows+i+1)
plot.set_title('Original Image')
imgplot = plt.imshow(test_images.view(-1, 28, 28)
[b+i].cpu().detach(), cmap='gray')
plot = fig.add_subplot(3, nrows, 1*nrows+i+1)
plot.set_title('Noisy Image')
imgplot = plt.imshow(noisy_test_imgs.view(-1, 28, 28)
[b+i].cpu().detach(), cmap='gray')
plot = fig.add_subplot(3, nrows, 2*nrows+i+1)
plot.set_title('Reconstructed')
imgplot = plt.imshow(test_output.view(-1, 28, 28)
[b+i].cpu().detach(), cmap='gray')
plt.show()
# %%
class ThreeFCLayerClassifier(nn.Module):
def __init__(self):
super(ThreeFCLayerClassifier, self).__init__()
self.encoder = model.encoder
self.fc_layer = nn.Sequential(
nn.Linear(64, 32),
nn.Sigmoid(),
nn.Linear(32, 16),
nn.Sigmoid(),
nn.Linear(16, 10),
nn.Sigmoid(),
)
def forward(self, x):
x = self.encoder(x)
x = self.fc_layer(x)
return x
# %%
classifier_3fc = ThreeFCLayerClassifier()
print(classifier_3fc)
# %%
optimizer_3fc = torch.optim.Adam(classifier_3fc.parameters(), lr=0.001)
criterion_3fc = nn.CrossEntropyLoss()
# %%
epochs = 20
loss_list = []
for epoch in range(epochs):
train_loss = 0.0
val_loss = 0.0
for images, labels in trainloader:
# clear the gradients of all optimized variables
optimizer_3fc.zero_grad()
# forward pass: compute predicted outputs by passing *noisy* images to the model
outputs = classifier_3fc(images.view(-1, 784))
# calculate the loss
# the "target" is still the original, not-noisy images
loss = criterion_3fc(outputs, labels)
# backward pass: compute gradient of the loss with respect to model parameters
loss.backward()
# perform a single optimization step (parameter update)
optimizer_3fc.step()
# update running training loss
train_loss += loss.item()*images.size(0)
with torch.no_grad():
for images, labels in testloader:
# forward pass: compute predicted outputs by passing *noisy* images to the model
outputs = classifier_3fc(images.view(-1, 784))
# calculate the loss
# the "target" is still the original, not-noisy images
loss = criterion_3fc(outputs, labels)
# update running training loss
val_loss += loss.item()*images.size(0)
# print avg training statistics
train_loss = train_loss/len(trainloader)
val_loss = val_loss/len(testloader)
print(
f'Epoch: {epoch+1} \tTraining Loss: {train_loss:.6f}, \tTest Loss: {val_loss:.6f}, ')
loss_list.append([train_loss, val_loss])
loss_list = np.array(loss_list)
# %%
plt.title("Training vs Validation loss for Model with 3FC Layer")
plt.plot(loss_list[:, 0], linestyle='--', label="Training Loss")
plt.plot(loss_list[:, 1], linestyle='-', label="Validation Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()
plt.show()
# %%
test_output_3fc = classifier_3fc(test_images.view(-1, 784))
# %%
with torch.no_grad():
confusionMatrixAndAccuracyReport(
test_labels, test_output_3fc.argmax(axis=1), testdataset.classes)
# %%
test_embed = model.encoder(test_images.view(-1, 784))
with torch.no_grad():
plot_embedding(test_embed, test_labels.numpy())
# %%
fig = plt.figure(figsize=(20, 7))
nrows = 10
b = np.random.randint(0, test_images.shape[0]-nrows)
for i in range(nrows):
plot = fig.add_subplot(3, nrows, 0*nrows+i+1)
plot.set_title('Original Image')
imgplot = plt.imshow(test_images.view(-1, 28, 28)
[b+i].cpu().detach(), cmap='gray')
plot = fig.add_subplot(3, nrows, 1*nrows+i+1)
plot.set_title('Noisy Image')
imgplot = plt.imshow(noisy_test_imgs.view(-1, 28, 28)
[b+i].cpu().detach(), cmap='gray')
plot = fig.add_subplot(3, nrows, 2*nrows+i+1)
plot.set_title('Reconstructed Image')
imgplot = plt.imshow(test_output.view(-1, 28, 28)
[b+i].cpu().detach(), cmap='gray')
plt.show()
# %% [markdown]
# Comparison
# %%
print('Classification Report for 1FC Layer')
print(metrics.classification_report(test_labels, test_output_1fc.argmax(axis=1)))
print('Classification Report for 3FC Layer')
print(metrics.classification_report(test_labels, test_output_3fc.argmax(axis=1)))
print(
f'Accuracy Score for \t 1FC Layer {metrics.accuracy_score(test_labels,test_output_1fc.argmax(axis=1))} \t 3FC Layer {metrics.accuracy_score(test_labels,test_output_3fc.argmax(axis=1))}')
print(f'F1 Score for \t\t 1FC Layer {metrics.f1_score(test_labels,test_output_1fc.argmax(axis=1),average="weighted"):0.3} \t 3FC Layer {metrics.f1_score(test_labels,test_output_3fc.argmax(axis=1),average="weighted"):0.3}')