-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvae_mnist.py
More file actions
156 lines (104 loc) · 3.31 KB
/
vae_mnist.py
File metadata and controls
156 lines (104 loc) · 3.31 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision.utils import save_image
from torch.distributions import Normal
import pickle
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('dark_background')
class Loader():
def __init__(self, path = '', max_items = 60000):
self.path = path
self.max_count = max_items
self.counter = 0
def sample(self,batch_size = 32, use_cuda = False):
data = np.zeros((batch_size, 784))
ind = np.random.randint(0,self.max_count, (batch_size))
for i in range(batch_size):
name = self.path+'{}'.format(ind[i])
image = pickle.load(open(name,'rb'))
data[i,:] = image.reshape(784)
self.counter = (self.counter+1)%self.max_count
data /= 255.
if use_cuda:
tensor = torch.cuda.FloatTensor(data).float()
else:
tensor = torch.from_numpy(data).float()
return tensor
code_size = 20
class VAE(nn.Module):
def __init__(self):
nn.Module.__init__(self)
self.encoder = nn.ModuleList([nn.Linear(784,400), nn.Linear(400,128)])
self.sigma = nn.Linear(128,code_size)
self.mu = nn.Linear(128, code_size)
self.decoder = nn.ModuleList([nn.Linear(code_size, 128), nn.Linear(128,400), nn.Linear(400,784)])
def forward(self, x):
for l in self.encoder:
x = F.leaky_relu(l(x), 0.1)
stds = torch.exp(self.sigma(x))
means = self.mu(x)
z = self.reparam(means, stds)
for i, l in enumerate(self.decoder):
if i == 0:
recon = F.leaky_relu(l(z), 0.1)
else:
recon = F.leaky_relu(l(recon), 0.1)
return recon, means, stds, z
def reparam(self, m, s):
if self.training:
eps = torch.randn_like(s)
z = m + s*eps
else:
z = m
return z
def sample(self, nb):
x = torch.randn(nb, code_size)
for i, l in enumerate(self.decoder):
if i == 0:
recon = F.leaky_relu(l(x), 0.1)
else:
recon = F.leaky_relu(l(recon), 0.1)
return recon
def loss_fn(x, recon, mean, logvar):
recon_loss = F.mse_loss(recon, x, size_average = False)
kl_loss = -0.5 * torch.sum(1 + logvar - mean.pow(2) - logvar.exp())
return kl_loss + recon_loss
def normal_init(model, mean = 0., s = 0.02):
for m in model._modules:
if isinstance(model._modules[m], nn.Linear):
model._modules[m].weight.data.normal_(mean ,s)
model._modules[m].bias.data.zero_()
elif isinstance(model._modules[m], nn.ModuleList):
size = len(model._modules[m])
for i in range(size):
model._modules[m][i].weight.data.normal_(mean ,s)
model._modules[m][i].bias.data.zero_()
l = Loader('/home/mehdi/Codes/MNIST/', 60000)
x = l.sample()
vae = VAE()
normal_init(vae)
adam = optim.Adam(vae.parameters(), 2e-4)
epochs = 5000
for epoch in range(1,epochs+1):
x = l.sample(64)
recon, m, std, _ = vae(x)
loss = loss_fn(x, recon, m, std)
adam.zero_grad()
loss.backward()
adam.step()
if epoch%100 == 0:
x = l.sample(32)
recon,_,_,_ = vae(x)
samples = vae.sample(32)
recon = recon.reshape(32, 1, 28,28)
samples = samples.reshape(32, 1, 28,28)
image = torch.cat([recon, samples], 0)
save_image(image, 'results/{}.png'.format(epoch))
x = l.sample(64)
recon,_,_,_ = vae(x)
samples = vae.sample(64)
save_image(recon.reshape(64,1,28,28), 'results/FinalRecon.png'.format(epoch))
save_image(samples.reshape(64,1,28,28), 'results/FinalSamples.png'.format(epoch))