-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathconv_vae.py
182 lines (152 loc) · 6.78 KB
/
conv_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
from __future__ import print_function
import argparse
import torch
import torch.utils.data
from torch import nn, optim
from torch.autograd import Variable
import torch.nn as nn
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
parser = argparse.ArgumentParser(description='VAE MNIST Example')
parser.add_argument('--batch-size', type=int, default=128, metavar='N',
help='input batch size for training (default: 128)')
parser.add_argument('--epochs', type=int, default=10, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='enables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
help='how many batches to wait before logging training status')
parser.add_argument('--hidden-size', type=int, default=20, metavar='N',
help='how big is z')
parser.add_argument('--intermediate-size', type=int, default=128, metavar='N',
help='how big is linear around z')
# parser.add_argument('--widen-factor', type=int, default=1, metavar='N',
# help='how wide is the model')
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}
train_loader = torch.utils.data.DataLoader(
datasets.CIFAR10('../data', train=True, download=True,
transform=transforms.ToTensor()),
batch_size=args.batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.CIFAR10('../data', train=False, transform=transforms.ToTensor()),
batch_size=args.batch_size, shuffle=False, **kwargs)
class VAE(nn.Module):
def __init__(self):
super(VAE, self).__init__()
# Encoder
self.conv1 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(3, 32, kernel_size=2, stride=2, padding=0)
self.conv3 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1)
self.conv4 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(16 * 16 * 32, args.intermediate_size)
# Latent space
self.fc21 = nn.Linear(args.intermediate_size, args.hidden_size)
self.fc22 = nn.Linear(args.intermediate_size, args.hidden_size)
# Decoder
self.fc3 = nn.Linear(args.hidden_size, args.intermediate_size)
self.fc4 = nn.Linear(args.intermediate_size, 8192)
self.deconv1 = nn.ConvTranspose2d(32, 32, kernel_size=3, stride=1, padding=1)
self.deconv2 = nn.ConvTranspose2d(32, 32, kernel_size=3, stride=1, padding=1)
self.deconv3 = nn.ConvTranspose2d(32, 32, kernel_size=2, stride=2, padding=0)
self.conv5 = nn.Conv2d(32, 3, kernel_size=3, stride=1, padding=1)
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
def encode(self, x):
out = self.relu(self.conv1(x))
out = self.relu(self.conv2(out))
out = self.relu(self.conv3(out))
out = self.relu(self.conv4(out))
out = out.view(out.size(0), -1)
h1 = self.relu(self.fc1(out))
return self.fc21(h1), self.fc22(h1)
def reparameterize(self, mu, logvar):
if self.training:
std = logvar.mul(0.5).exp_()
eps = Variable(std.data.new(std.size()).normal_())
return eps.mul(std).add_(mu)
else:
return mu
def decode(self, z):
h3 = self.relu(self.fc3(z))
out = self.relu(self.fc4(h3))
# import pdb; pdb.set_trace()
out = out.view(out.size(0), 32, 16, 16)
out = self.relu(self.deconv1(out))
out = self.relu(self.deconv2(out))
out = self.relu(self.deconv3(out))
out = self.sigmoid(self.conv5(out))
return out
def forward(self, x):
mu, logvar = self.encode(x)
z = self.reparameterize(mu, logvar)
return self.decode(z), mu, logvar
model = VAE()
if args.cuda:
model.cuda()
optimizer = optim.RMSprop(model.parameters(), lr=1e-3)
# Reconstruction + KL divergence losses summed over all elements and batch
def loss_function(recon_x, x, mu, logvar):
BCE = F.binary_cross_entropy(recon_x.view(-1, 32 * 32 * 3),
x.view(-1, 32 * 32 * 3), size_average=False)
# 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 = Variable(data)
if args.cuda:
data = data.cuda()
optimizer.zero_grad()
recon_batch, mu, logvar = model(data)
loss = loss_function(recon_batch, data, mu, logvar)
loss.backward()
train_loss += loss.data[0]
optimizer.step()
if batch_idx % args.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.data[0] / len(data)))
print('====> Epoch: {} Average loss: {:.4f}'.format(
epoch, train_loss / len(train_loader.dataset)))
def test(epoch):
model.eval()
test_loss = 0
for i, (data, _) in enumerate(test_loader):
if args.cuda:
data = data.cuda()
data = Variable(data, volatile=True)
recon_batch, mu, logvar = model(data)
test_loss += loss_function(recon_batch, data, mu, logvar).data[0]
if epoch == args.epochs and i == 0:
n = min(data.size(0), 8)
comparison = torch.cat([data[:n],
recon_batch[:n]])
save_image(comparison.data.cpu(),
'snapshots/conv_vae/reconstruction_' + str(epoch) +
'.png', nrow=n)
test_loss /= len(test_loader.dataset)
print('====> Test set loss: {:.4f}'.format(test_loss))
for epoch in range(1, args.epochs + 1):
train(epoch)
test(epoch)
if epoch == args.epochs:
sample = Variable(torch.randn(64, args.hidden_size))
if args.cuda:
sample = sample.cuda()
sample = model.decode(sample).cpu()
save_image(sample.data.view(64, 3, 32, 32),
'snapshots/conv_vae/sample_' + str(epoch) + '.png')