-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeep_autoencoder.py
More file actions
152 lines (118 loc) · 3.95 KB
/
deep_autoencoder.py
File metadata and controls
152 lines (118 loc) · 3.95 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
# Autoencoder using convolutional layers
# Dataset : MNIST
# Requires : PIL, matplotlib
# Inspired by https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html
# To compress data : net.encode(data)
# To decompress data : net.decode(data)
# To mutate data : net(data)
import os
import numpy as np
import matplotlib.pyplot as plt
import torch as T
from torch import nn
from torch import cuda
import torch.nn.functional as F
from torchvision import transforms
import torchvision
from torchvision.datasets import MNIST
from torch.nn import ReLU, Linear, Sigmoid, Conv2d, ConvTranspose2d, MaxPool2d
import PIL.Image as im
from utils import dataset_dir, models_dir
# Displays an image (1 dim tensor)
# t has values in [0, 1]
def imshow(t):
transforms.ToPILImage()(t).show()
# Show in matplotlib
def gridshow(img):
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
class Net(nn.Module):
def __init__(self, hidden_size, latent_size):
super().__init__()
self.latent_size = latent_size
self.encodeConv1 = Conv2d(1, 16, 4)
self.encodeConv2 = Conv2d(16, 32, 2)
self.encodeFC1 = Linear(800, hidden_size)
self.encodeFC2 = Linear(hidden_size, self.latent_size)
self.decodeFC1 = Linear(self.latent_size, 13 * 13)
self.decodeConv1 = ConvTranspose2d(1, 1, 2)
self.decodeFC2 = Linear(14 * 14, 28 * 28)
def encode(self, x):
x = MaxPool2d(2)(F.relu(self.encodeConv1(x)))
x = MaxPool2d(2)(F.relu(self.encodeConv2(x)))
x = x.view(-1, 800)
x = F.relu(self.encodeFC1(x))
x = T.sigmoid(self.encodeFC2(x))
return x
def decode(self, x):
x = F.relu(self.decodeFC1(x))
x = x.view(-1, 1, 13, 13)
x = F.relu(self.decodeConv1(x))
x = x.view(-1, 14 * 14)
x = T.sigmoid(self.decodeFC2(x))
x = x.view(-1, 1, 28, 28)
return x
def forward(self, x):
return self.decode(self.encode(x))
# Hyper params
latent_size = 10
hidden_size = 256
epochs = 3
batch_size = 10
learning_rate = .0002
train_or_test = 'test'
path = models_dir + '/deep_autoencoder'
# Training device
device = T.device('cuda:0' if cuda.is_available() else 'cpu')
# Dataset
trans = transforms.ToTensor()
dataset = MNIST(root=dataset_dir, train=True, download=True, transform=trans)
loader = T.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=0)
# Model
net = Net(hidden_size, latent_size)
net.to(device)
if train_or_test == 'train':
# Load
if os.path.exists(path):
net.load_state_dict(T.load(path))
print('Model loaded')
# Train
optim = T.optim.Adam(net.parameters(), lr=learning_rate, betas=(.9, .999))
criterion = nn.MSELoss()
for e in range(epochs):
avg_loss = 0
for i, data in enumerate(loader, 0):
# Only inputs (no labels)
inputs, _ = data
# Zero the parameter gradients
optim.zero_grad()
# Predictions
x = inputs.to(device)
y = net(x)
# Back prop
loss = criterion(y, x)
loss.backward()
optim.step()
avg_loss += loss.item()
# Stats
print_freq = 100
if i % print_freq == print_freq - 1:
print(f'Epoch {e + 1:2d}, Batch {i + 1:5d}, Loss {avg_loss / print_freq:.3f}')
avg_loss = 0.0
# Save
T.save(net.state_dict(), path)
print('Model trained and saved')
else:
# Load
net.load_state_dict(T.load(path))
# Test
dataiter = iter(loader)
images, _ = dataiter.next()
# Show ground truth
gridshow(torchvision.utils.make_grid(images))
# Show predictions
with T.no_grad():
preds = T.cat([net(images[i].view(1, 1, 28, 28).to(device)).view(1, 1, 28, 28).cpu() for i in range(batch_size)])
preds = T.tensor(preds)
gridshow(torchvision.utils.make_grid(preds))