-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodels.py
235 lines (179 loc) · 7.46 KB
/
models.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
'''
Implementation of DARLA preprocessing, as found in DARLA: Improving Zero-Shot Transfer in Reinforcement Learning
by Higgins and Pal et al (https://arxiv.org/pdf/1707.08475.pdf):
DAE:
X_noisy --J--> Z ----> X_hat
minimizing (X_noisy-X_hat)^2
Beta VAE:
X ----> Z ----> X_hat
minimizing (J(X) - J(X_hat))^2 + beta*KL(Q(Z|X) || P(Z))
Right now this just trains the model using MNIST dataset
rythei
'''
import torch
import torch.nn as nn
import torchvision.datasets as dsets
from torchvision import datasets, transforms
from torch.autograd import Variable
from utils import *
from torch.nn import functional as F
cuda = False
class DAE(nn.Module):
def __init__(self):
super(DAE, self).__init__()
self.image_dim = 28 # a 28x28 image corresponds to 4 on the FC layer, a 64x64 image corresponds to 13
# can calculate this using output_after_conv() in utils.py
self.latent_dim = 100
self.noise_scale = 0
self.batch_size = 50
self.encoder = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=4, stride=1),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=4, stride=1),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=4, stride=2),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=4, stride=2),
nn.ReLU())
self.fc1 = nn.Linear(32*4*4, self.latent_dim)
self.fc2 = nn.Linear(self.latent_dim, 32*4*4)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(32, 32, kernel_size=4, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(32, 32, kernel_size=4, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(32, 32, kernel_size=4, stride=1),
nn.ReLU(),
nn.ConvTranspose2d(32, 1, kernel_size=4, stride=1),
nn.Sigmoid())
def forward(self, x):
n = x.size()[0]
if cuda:
noise = Variable(self.noise_scale*torch.randn(n, 1, self.image_dim, self.image_dim)).cuda()
else:
noise = Variable(self.noise_scale * torch.randn(n, 1, self.image_dim, self.image_dim))
x = torch.add(x, noise)
z = self.encoder(x)
z = z.view(-1, 32*4*4)
z = self.fc1(z)
x_hat = self.fc2(z)
x_hat = x_hat.view(-1, 32, 4, 4)
x_hat = self.decoder(x_hat)
return z, x_hat
def encode(self, x):
#x = x.unsqueeze(0)
z, x_hat = self.forward(x)
return z
def train_dae(num_epochs = 100, batch_size = 128, learning_rate = 1e-3):
train_dataset = dsets.MNIST(root='./data/', #### testing that it works with MNIST data
train=True,
transform=transforms.ToTensor(),
download=True)
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=True, download=True,
transform=transforms.ToTensor()), batch_size=batch_size, shuffle=True)
dae = DAE()
if cuda:
dae.cuda()
dae.batch_size = batch_size
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(dae.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
if cuda:
x = Variable(images).cuda()
else:
x = Variable(images)
# Forward + Backward + Optimize
optimizer.zero_grad()
z, x_hat = dae(x)
loss = criterion(x_hat, x)
loss.backward()
optimizer.step()
if (i + 1) % 1 == 0:
print('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f'
% (epoch + 1, num_epochs, i + 1, len(train_dataset) // batch_size, loss.data[0]))
torch.save(dae.state_dict(), 'dae-test-model.pkl')
class BetaVAE(nn.Module):
def __init__(self):
super(BetaVAE, self).__init__()
self.image_dim = 28 # a 28x28 image corresponds to 4 on the FC layer, a 64x64 image corresponds to 13
# can calculate this using output_after_conv() in utils.py
self.latent_dim = 100
self.batch_size = 50
self.encoder = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=4, stride=1),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=4, stride=1),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=4, stride=2),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=4, stride=2),
nn.ReLU())
self.fc_mu = nn.Linear(32*4*4, self.latent_dim)
self.fc_sigma = nn.Linear(32 * 4 * 4, self.latent_dim)
self.fc_up = nn.Linear(self.latent_dim, 32*4*4)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(32, 32, kernel_size=4, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(32, 32, kernel_size=4, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(32, 32, kernel_size=4, stride=1),
nn.ReLU(),
nn.ConvTranspose2d(32, 1, kernel_size=4, stride=1),
nn.Sigmoid())
def forward(self, x):
n = x.size()[0]
z = self.encoder(x)
z = z.view(-1, 32*4*4)
mu_z = self.fc_mu(z)
log_sigma_z = self.fc_sigma(z)
sample_z = mu_z + log_sigma_z.exp()*Variable(torch.randn(n, self.latent_dim)).cuda()
x_hat = self.fc_up(sample_z)
x_hat = x_hat.view(-1, 32, 4, 4)
x_hat = self.decoder(x_hat)
return mu_z, log_sigma_z, x_hat
def bvae_loss_function(z_hat, z, mu, logvar, beta=1, batch_size=128):
RCL = F.mse_loss(z, z_hat) #reconstruction loss
KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp()) #KL divergence
# Normalise by same number of elements as in reconstruction
KLD /= batch_size
return RCL + beta*KLD
def train_bvae(num_epochs = 100, batch_size = 128, learning_rate = 1e-4):
train_dataset = dsets.MNIST(root='./data/', #### testing that it works with MNIST data
train=True,
transform=transforms.ToTensor(),
download=True)
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=True, download=True,
transform=transforms.ToTensor()), batch_size=batch_size, shuffle=True)
bvae = BetaVAE()
if cuda:
bvae.cuda()
bvae.batch_size = batch_size
dae = DAE()
if cuda:
dae.cuda()
dae.load_state_dict(torch.load('dae-test-model.pkl'))
dae.batch_size = batch_size
dae.eval()
optimizer = torch.optim.Adam(bvae.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
if cuda:
x = Variable(images).cuda()
else:
x = Variable(images)
# Forward + Backward + Optimize
optimizer.zero_grad()
mu_z, log_sigma_z, x_hat = bvae(x)
loss = bvae_loss_function(dae.encode(x_hat), dae.encode(x), mu_z, 2*log_sigma_z, batch_size=batch_size)
loss.backward()
optimizer.step()
if (i + 1) % 1 == 0:
print('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f'
% (epoch + 1, num_epochs, i + 1, len(train_dataset) // batch_size, loss.data[0]))
torch.save(bvae.state_dict(), 'bvae-test-model.pkl')
if __name__ == '__main__':
train_dae() #first need to dave a DAE model trained
train_bvae()