-
Notifications
You must be signed in to change notification settings - Fork 21
/
model.py
145 lines (121 loc) · 4.17 KB
/
model.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
import torch
from torch.autograd import Variable
from torch import nn
class VAE(nn.Module):
def __init__(self, label, image_size, channel_num, kernel_num, z_size):
# configurations
super().__init__()
self.label = label
self.image_size = image_size
self.channel_num = channel_num
self.kernel_num = kernel_num
self.z_size = z_size
# encoder
self.encoder = nn.Sequential(
self._conv(channel_num, kernel_num // 4),
self._conv(kernel_num // 4, kernel_num // 2),
self._conv(kernel_num // 2, kernel_num),
)
# encoded feature's size and volume
self.feature_size = image_size // 8
self.feature_volume = kernel_num * (self.feature_size ** 2)
# q
self.q_mean = self._linear(self.feature_volume, z_size, relu=False)
self.q_logvar = self._linear(self.feature_volume, z_size, relu=False)
# projection
self.project = self._linear(z_size, self.feature_volume, relu=False)
# decoder
self.decoder = nn.Sequential(
self._deconv(kernel_num, kernel_num // 2),
self._deconv(kernel_num // 2, kernel_num // 4),
self._deconv(kernel_num // 4, channel_num),
nn.Sigmoid()
)
def forward(self, x):
# encode x
encoded = self.encoder(x)
# sample latent code z from q given x.
mean, logvar = self.q(encoded)
z = self.z(mean, logvar)
z_projected = self.project(z).view(
-1, self.kernel_num,
self.feature_size,
self.feature_size,
)
# reconstruct x from z
x_reconstructed = self.decoder(z_projected)
# return the parameters of distribution of q given x and the
# reconstructed image.
return (mean, logvar), x_reconstructed
# ==============
# VAE components
# ==============
def q(self, encoded):
unrolled = encoded.view(-1, self.feature_volume)
return self.q_mean(unrolled), self.q_logvar(unrolled)
def z(self, mean, logvar):
std = logvar.mul(0.5).exp_()
eps = (
Variable(torch.randn(std.size())).cuda() if self._is_on_cuda else
Variable(torch.randn(std.size()))
)
return eps.mul(std).add_(mean)
def reconstruction_loss(self, x_reconstructed, x):
return nn.BCELoss(size_average=False)(x_reconstructed, x) / x.size(0)
def kl_divergence_loss(self, mean, logvar):
return ((mean**2 + logvar.exp() - 1 - logvar) / 2).mean()
# =====
# Utils
# =====
@property
def name(self):
return (
'VAE'
'-{kernel_num}k'
'-{label}'
'-{channel_num}x{image_size}x{image_size}'
).format(
label=self.label,
kernel_num=self.kernel_num,
image_size=self.image_size,
channel_num=self.channel_num,
)
def sample(self, size):
z = Variable(
torch.randn(size, self.z_size).cuda() if self._is_on_cuda() else
torch.randn(size, self.z_size)
)
z_projected = self.project(z).view(
-1, self.kernel_num,
self.feature_size,
self.feature_size,
)
return self.decoder(z_projected).data
def _is_on_cuda(self):
return next(self.parameters()).is_cuda
# ======
# Layers
# ======
def _conv(self, channel_size, kernel_num):
return nn.Sequential(
nn.Conv2d(
channel_size, kernel_num,
kernel_size=4, stride=2, padding=1,
),
nn.BatchNorm2d(kernel_num),
nn.ReLU(),
)
def _deconv(self, channel_num, kernel_num):
return nn.Sequential(
nn.ConvTranspose2d(
channel_num, kernel_num,
kernel_size=4, stride=2, padding=1,
),
nn.BatchNorm2d(kernel_num),
nn.ReLU(),
)
def _linear(self, in_size, out_size, relu=True):
return nn.Sequential(
nn.Linear(in_size, out_size),
nn.ReLU(),
) if relu else nn.Linear(in_size, out_size)