-
Notifications
You must be signed in to change notification settings - Fork 1
/
vae.py
221 lines (184 loc) · 8.25 KB
/
vae.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
from __future__ import print_function
import configparser
import torch
import torch.utils.data
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
import numpy as np
from tensorboardX import SummaryWriter
import random
import os
class Encoder(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(Encoder, self).__init__()
self.input_dim = input_dim
self.fc1 = nn.Linear(input_dim, hidden_dim)
self.fc21 = nn.Linear(hidden_dim, output_dim)
self.fc22 = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
x = x.view(-1, self.input_dim)
h1 = F.relu(self.fc1(x))
mu, logvar = self.fc21(h1), self.fc22(h1)
return mu, logvar
class Decoder(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(Decoder, self).__init__()
self.fc3 = nn.Linear(input_dim, hidden_dim)
self.fc4 = nn.Linear(hidden_dim, output_dim)
def forward(self, z):
h3 = F.relu(self.fc3(z))
h4 = torch.sigmoid(self.fc4(h3))
return h4
class VAE(nn.Module):
def __init__(self, sample_dim=784, hidden_dim=400, z_dim=20):
super(VAE, self).__init__()
self.encode = Encoder(input_dim=sample_dim, hidden_dim=hidden_dim, output_dim=z_dim)
self.decode = Decoder(input_dim=z_dim, hidden_dim=hidden_dim, output_dim=sample_dim)
def reparameterize(self, mu, logvar):
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std
def forward(self, x):
mu, logvar = self.encode(x)
z = self.reparameterize(mu, logvar)
output = self.decode(z)
return output, mu, logvar
def loss_function(recon_x, x, mu, logvar):
# ELBO
# Reconstruction + KL divergence losses summed over all elements and batch
BCE = F.binary_cross_entropy(recon_x, x.view(-1, sample_dim), reduction='sum')
# see Appendix B from VAE paper:
# Kingma and Welling. Auto-Encoding Variational Bayes. ICLR, 2014
# https://arxiv.org/abs/1312.6114
# 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
KLD = 0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
return BCE - KLD
def train(epoch):
model.train()
train_loss = 0
for batch_idx, (data, _) in enumerate(train_loader):
data = data.to(device)
optimizer.zero_grad()
recon_batch, mu, logvar = model(data)
loss = loss_function(recon_batch, data, mu, logvar)
loss.backward()
train_loss += loss.item()
optimizer.step()
# log
batch_count = (epoch - 1) * len(train_loader) + batch_idx + 1
writer.add_scalar('train_batch_loss', loss.item() / len(data), batch_count)
if batch_idx % log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader),
loss.item() / len(data)))
train_loss /= len(train_loader.dataset)
writer.add_scalar('train_epoch_loss', train_loss, epoch)
print('====> Epoch: {} Average loss: {:.4f}'.format(epoch, train_loss))
# save model
# torch.save(model.state_dict(), config['vae']['model_path'] + 'vae.pth')
def dev(epoch):
global best_epoch, best_loss
model.eval()
dev_loss = 0
with torch.no_grad():
for i, (data, _) in enumerate(dev_loader):
data = data.to(device)
recon_batch, mu, logvar = model(data)
dev_loss += loss_function(recon_batch, data, mu, logvar).item()
# comparison between original data and reconstructed data
if i == 0:
n = min(data.size(0), 8)
comparison = torch.cat([data[:n],
recon_batch.view(batch_size, 1, data_side, data_side)[:n]])
save_image(comparison.cpu(),
config['vae']['result_path'] + 'reconstruction_' + str(epoch) + '.png', nrow=n)
dev_loss /= len(dev_loader.dataset)
writer.add_scalar('dev_epoch_loss', dev_loss, epoch)
print('====> Dev set loss: {:.4f}'.format(dev_loss))
# save model
if dev_loss < best_loss:
print('Better loss! Saving model!')
torch.save(model.state_dict(), config['vae']['model_path'] + 'vae.pth')
best_epoch, best_loss = epoch, dev_loss
def test():
model.eval()
test_loss = 0
with torch.no_grad():
for i, (data, _) in enumerate(test_loader):
data = data.to(device)
recon_batch, mu, logvar = model(data)
test_loss += loss_function(recon_batch, data, mu, logvar).item()
# comparison between original data and reconstructed data
if i == 0:
n = min(data.size(0), 8)
comparison = torch.cat([data[:n],
recon_batch.view(batch_size, 1, data_side, data_side)[:n]])
save_image(comparison.cpu(),
config['vae']['result_path'] + 'reconstruction_test.png', nrow=n)
test_loss /= len(test_loader.dataset)
writer.add_scalar('test_loss', test_loss)
print('====> Test set loss: {:.4f}'.format(test_loss))
def main():
epochs = int(config['vae']['epochs'])
for epoch in range(1, epochs + 1):
train(epoch)
dev(epoch)
# generate result every epoch
with torch.no_grad():
sample = torch.randn(64, z_dim).to(device)
sample = model.decode(sample).cpu()
save_image(sample.view(64, 1, data_side, data_side),
config['vae']['result_path'] + 'sample_' + str(epoch) + '.png')
print('Reload the best model on epoch', str(best_epoch), 'with min loss', str(best_loss))
ckpt = torch.load(config['vae']['model_path'] + 'vae.pth')
model.load_state_dict(ckpt)
test()
if __name__ == "__main__":
# config
config = configparser.ConfigParser()
config.read("config.ini")
# seed
seed = int(config['vae']['seed'])
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# log_interval
log_interval = int(config['vae']['log_interval'])
# data set
os.makedirs(config['data']['path'], exist_ok=True)
full_set = datasets.MNIST(config['data']['path'], train=True, download=True, transform=transforms.ToTensor())
train_amount = int(len(full_set) * (1. - float(config['data']['dev_ratio'])))
train_set = torch.utils.data.dataset.Subset(full_set, np.arange(train_amount))
dev_set = torch.utils.data.dataset.Subset(full_set, np.arange(train_amount, len(full_set)))
test_set = datasets.MNIST(config['data']['path'], train=False, download=True, transform=transforms.ToTensor())
print('dataset size', len(train_set), len(dev_set), len(test_set))
print('data size', train_set[0][0].shape)
# dim
data_side = train_set[0][0].shape[1]
print('side', data_side)
sample_dim = train_set[0][0].shape[1] * train_set[0][0].shape[2]
hidden_dim = int(config['vae']['hidden_dim'])
z_dim = int(config['vae']['z_dim'])
print('sample_dim', sample_dim, 'hidden_dim', hidden_dim, 'z_dim', z_dim)
# data loader
batch_size = int(config['vae']['batch_size'])
kwargs = {'num_workers': 4, 'pin_memory': True} if torch.cuda.is_available() else {}
train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=True, **kwargs)
dev_loader = torch.utils.data.DataLoader(dev_set, batch_size=batch_size, shuffle=False, **kwargs)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=False, **kwargs)
# model & optimizer
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = VAE(sample_dim=sample_dim, hidden_dim=hidden_dim, z_dim=z_dim).to(device)
optimizer = optim.Adam(model.parameters(), lr=float(config['vae']['lr']))
# writer
os.makedirs(config['vae']['log_path'], exist_ok=True)
os.makedirs(config['vae']['model_path'], exist_ok=True)
os.makedirs(config['vae']['result_path'], exist_ok=True)
writer = SummaryWriter(config['vae']['log_path'])
best_epoch, best_loss = 0, float('inf')
main()
writer.close()