-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvauto.py
More file actions
173 lines (141 loc) · 5.63 KB
/
Copy pathconvauto.py
File metadata and controls
173 lines (141 loc) · 5.63 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 10 13:11:57 2020
@author: lionel
Here using Pytorch we build a convolutional autoencoder for which we would
like to train and test on the MNIST data-set. The MLP autoencoder was only able
to achieve a test loss of 0.045 after training. We expect a Convnet autoencoder
to perform better.
"""
import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
''' define our convnet autoencoder '''
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
## encoder layers ##
# conv layer (depth from 3 --> 16), 3x3 kernels
self.conv1 = nn.Conv2d(1, 16, 3, padding=1)
# conv layer (depth from 16 --> 4), 3x3 kernels
self.conv2 = nn.Conv2d(16, 4, 3, padding=1)
# pooling layer to reduce x-y dims by two; kernel and stride of 2
self.pool = nn.MaxPool2d(2, 2)
## decoder layers ##
## a kernel of 2 and a stride of 2 will increase the spatial dims by 2
self.t_conv1 = nn.ConvTranspose2d(4, 16, 2, stride=2)
self.t_conv2 = nn.ConvTranspose2d(16, 1, 2, stride=2)
def forward(self, x):
## encode ##
# add hidden layers with relu activation function
# and maxpooling after
# print(x.shape)
x = torch.relu(self.conv1(x))
x = self.pool(x)
# add second hidden layer
x = torch.relu(self.conv2(x))
x = self.pool(x) # compressed representation
## decode ##
# add transpose conv layers, with relu activation function
x = torch.relu(self.t_conv1(x))
# output layer (with sigmoid for scaling from 0 to 1)
x = torch.sigmoid(self.t_conv2(x))
return x
''''''''''''''''''''''''''
np.random.seed(0)
transform = transforms.Compose([transforms.ToTensor()])
mnist_trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
train_loader = torch.utils.data.DataLoader(mnist_trainset,
batch_size=50,
shuffle=True,
num_workers=2)
mnist_testset = torchvision.datasets.MNIST(root='./data', train=False, download=True, transform=transform)
test_loader = torch.utils.data.DataLoader(mnist_testset,
batch_size=50,
shuffle=True,
num_workers=2)
''' display random images '''
def imshow(img):
# img = img / 2 + 0.5 # unnormalize
try: npimg = img.numpy()
except: npimg = img.detach().numpy()
plt.figure()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
# get some random training images
dataiter = iter(train_loader)
images, labels = dataiter.next()
# show images
# imshow(torchvision.utils.make_grid(images))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
net = Net().to(device)
criterion = nn.MSELoss()
optimizer = optim.Adam(net.parameters(), lr=0.001)
print_every = 100
train_epoch_loss=[]
test_epoch_loss=[]
for epoch in range(20): # loop over the dataset multiple times
running_loss = 0.0
test_loss = 0.0
for i, data in enumerate(zip(train_loader,test_loader), 0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data[0]
inputs = inputs.to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net.forward(inputs)
loss = criterion(outputs, inputs)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
''' predict on the test set'''
tinputs, tlabels = data[1]
tinputs = tinputs.to(device)
toutputs = net.forward(tinputs)
tloss = criterion(toutputs, tinputs)
test_loss += tloss.item()
if i % print_every == print_every-1: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / print_every))
train_epoch_loss.append(running_loss / print_every)
test_epoch_loss.append(test_loss / print_every)
running_loss = 0.0
test_loss = 0.0
print('Finished Training')
''' observe reconstructed images'''
for i, data in enumerate(test_loader, 0):
''' get data from the test dataset'''
inputs, labels = data
''' display samples '''
imshow(torchvision.utils.make_grid(inputs))
plt.title('test samples')
plt.savefig('convauto_test_samples.png')
''' run the test samples through the autoencoder and
get the reconstructed output'''
inputs = inputs.to(device)
outputs = net(inputs)
outputs = outputs.cpu()
''' display the reconstructed images '''
imshow(torchvision.utils.make_grid(outputs))
plt.title('test samples reconstructed')
plt.savefig('convauto_test_reconstructed.png')
break
xaxis = range(len(train_epoch_loss))
plt.figure()
plt.plot(xaxis,train_epoch_loss,c='red', label='train loss')
plt.plot(xaxis,test_epoch_loss,c='blue', label='test loss')
plt.scatter(xaxis,train_epoch_loss,c='red', s=5)
plt.scatter(xaxis,test_epoch_loss,c='blue', s=5)
plt.xlabel('batches')
plt.ylabel('avg loss per %s mini-batches' % print_every)
plt.legend()
plt.tight_layout()
plt.savefig('convauto_perf_mnist.png')