-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo_vae.py
164 lines (136 loc) · 4.99 KB
/
demo_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
import argparse
import os
import torch
import torch.nn.functional as F
from torch import nn
from torch.optim.adamax import Adamax
from torchvision.utils import save_image
from multiobject.pytorch import MultiObjectDataLoader, MultiObjectDataset
epochs = 100
batch_size = 64
lr = 3e-4
dataset_filename = os.path.join(
'dsprites',
'multi_dsprites_color_012.npz')
# dataset_filename = os.path.join(
# 'binary_mnist',
# 'multi_binary_mnist_012.npz')
class VAE(nn.Module):
def __init__(self, color_channels):
super().__init__()
zdim = 64
self.encoder = nn.Sequential(
nn.Conv2d(color_channels, 64, 5, padding=2, stride=2),
nn.Dropout2d(0.2),
nn.LeakyReLU(),
nn.Conv2d(64, 64, 3, padding=1, stride=2),
nn.Dropout2d(0.2),
nn.LeakyReLU(),
nn.BatchNorm2d(64),
nn.Conv2d(64, 64, 3, padding=1, stride=2),
nn.Dropout2d(0.2),
nn.LeakyReLU(),
nn.BatchNorm2d(64),
nn.Conv2d(64, 2 * zdim, 5, padding=2, stride=2),
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(zdim, 64, 5, padding=2, stride=2, output_padding=1),
nn.Dropout2d(0.2),
nn.LeakyReLU(),
nn.BatchNorm2d(64),
nn.ConvTranspose2d(64, 64, 3, padding=1, stride=2, output_padding=1),
nn.Dropout2d(0.2),
nn.LeakyReLU(),
nn.BatchNorm2d(64),
nn.ConvTranspose2d(64, 64, 3, padding=1, stride=2, output_padding=1),
nn.Dropout2d(0.2),
nn.LeakyReLU(),
nn.ConvTranspose2d(64, color_channels, 5, padding=1, stride=2, output_padding=0),
nn.Sigmoid()
)
def forward(self, x):
h, w = tuple(x.shape[2:])
x = self.encoder(x)
mu, lv = torch.chunk(x, 2, dim=1)
std = (lv / 2).exp()
z = torch.randn_like(mu) * std + mu
out = self.decoder(z)
out = out[:, :, :h, :w] # crop 65 to 64
return out, mu, lv
def main():
args = parse_args()
path = os.path.join('generated', args.dataset_path)
os.makedirs('demo_output', exist_ok=True)
# Datasets and dataloaders
print("loading dataset...")
train_set = MultiObjectDataset(path, train=True)
test_set = MultiObjectDataset(path, train=False)
train_loader = MultiObjectDataLoader(
train_set, batch_size=batch_size, shuffle=True, drop_last=True)
test_loader = MultiObjectDataLoader(test_set, batch_size=100)
channels = train_set.x.shape[1]
# Model and optimizer
print("initializing model...")
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = VAE(channels).to(device)
optimizer = Adamax(model.parameters(), lr=lr)
# Training loop
print("training starts")
step = 0
model.train()
for e in range(1, epochs + 1):
for x, labels in train_loader:
# Run model and compute loss
_, loss, recons, kl = forward(model, x, device)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
step += 1
if step % 100 == 0:
print("[{}] elbo: {:.2g} recons: {:.2g} kl: {:.2g}".format(
step, -loss.item(), recons.item(), kl.item()))
# Test
with torch.no_grad():
model.eval()
loss = recons = kl = 0.
for x, labels in test_loader:
out, loss_, recons_, kl_ = forward(model, x, device)
k = len(x) / len(test_set)
loss += loss_.item() * k
recons += recons_.item() * k
kl += kl_.item() * k
model.train()
print("TEST [epoch {}] elbo: {:.2g} recons: {:.2g} kl: {:.2g}".format(
e, -loss, recons, kl))
n = 6
nimg = n ** 2 // 2
fname = os.path.join('demo_output', '{}.png'.format(e))
imgs = torch.stack([x[:nimg], out[:nimg].cpu()])
imgs = imgs.permute(1, 0, 2, 3, 4)
imgs = imgs.reshape(n ** 2, x.size(1), x.size(2), x.size(3))
save_image(imgs, fname, nrow=n)
def forward(model, x, device):
# Forward pass through model
x = x.to(device)
out, mu, lv = model(x)
# Loss = -ELBO
recons = F.binary_cross_entropy(out, x, reduction='none')
kl = -0.5 * (1 + lv - mu ** 2 - lv.exp())
recons = recons.sum((1, 2, 3)).mean()
kl = kl.sum((1, 2, 3)).mean()
loss = recons + kl
return out, loss, recons, kl
def parse_args():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
allow_abbrev=False)
parser.add_argument('--dataset',
type=str,
default=dataset_filename,
metavar='PATH',
dest='dataset_path',
help="relative path of the dataset")
return parser.parse_args()
if __name__ == '__main__':
main()