-
Notifications
You must be signed in to change notification settings - Fork 0
/
VAEModel.py
230 lines (182 loc) · 9.6 KB
/
VAEModel.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
import torch
from torch import nn
'''
Implementation of VAE model.
Sizes of layers outputs in the comments a.k.a '# -> ChannelsxWxH'
Are only correct when the input is 128x128
'''
def cond_batchnorm2d_layer(batch_norm, dims):
""" Not used anymore """
if batch_norm:
return nn.BatchNorm2d(dims)
else:
return nn.Identity()
def cond_batchnorm1d_layer(batch_norm, dims):
""" Not used anymore """
if batch_norm:
return nn.BatchNorm1d(dims)
else:
return nn.Identity()
class ResidualConvBlock(nn.Module):
def __init__(self, channels, kernel_size=3, batchnorm=False):
super(ResidualConvBlock, self).__init__()
self.conv1 = nn.Conv2d(channels, channels, kernel_size, padding='same')
self.activation1 = nn.LeakyReLU()
self.norm1 = nn.Identity() #nn.InstanceNorm2d(channels)
self.conv2 = nn.Conv2d(channels, channels, kernel_size, padding='same')
self.activation2 = nn.Identity() #nn.LeakyReLU()
self.norm2 = nn.InstanceNorm2d(channels)
def forward(self, x):
out = self.conv1(x)
out = self.activation1(out)
out = self.norm1(out)
out = self.conv2(out)
out = self.activation2(out)
out = self.norm2(out)
return out + x
class VaeEncoder(nn.Module):
def __init__(self, input_size=128, latent_size=128, fc_layers=(128, 128, 128), res_blocks_per_size=3,
device=None):
super(VaeEncoder, self).__init__()
if device is None:
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
else:
self.device = device
feature_extractor_layers = [
nn.Conv2d(3, 8, kernel_size=1, padding='same'),
nn.LeakyReLU(),
]
for _ in range(res_blocks_per_size):
feature_extractor_layers.append(ResidualConvBlock(8))
feature_extractor_layers.append(nn.PixelUnshuffle(2)) # -> 32x64x64
feature_extractor_layers.append(nn.Conv2d(32, 16, kernel_size=1, padding='same')) # -> 16x64x64
# feature_extractor_layers.append(nn.InstanceNorm2d(16))
for _ in range(res_blocks_per_size):
feature_extractor_layers.append(ResidualConvBlock(16))
feature_extractor_layers.append(nn.PixelUnshuffle(2)) # -> 64x32x32
feature_extractor_layers.append(nn.Conv2d(64, 32, kernel_size=1, padding='same')) # -> 32x32x32
# feature_extractor_layers.append(nn.InstanceNorm2d(32))
for _ in range(res_blocks_per_size):
feature_extractor_layers.append(ResidualConvBlock(32))
feature_extractor_layers.append(nn.PixelUnshuffle(2)) # -> 128x16x16
feature_extractor_layers.append(nn.Conv2d(128, 64, kernel_size=1, padding='same')) # -> 64x16x16
# feature_extractor_layers.append(nn.InstanceNorm2d(64))
for _ in range(res_blocks_per_size):
feature_extractor_layers.append(ResidualConvBlock(64))
feature_extractor_layers.append(nn.Conv2d(64, 32, kernel_size=1, padding='same')) # -> 32x16x16
feature_extractor_layers.append(nn.LeakyReLU())
# feature_extractor_layers.append(nn.InstanceNorm2d(32))
feature_extractor_layers.append(nn.Conv2d(32, 16, kernel_size=1, padding='same')) # -> 16x16x16
feature_extractor_layers.append(nn.LeakyReLU())
# feature_extractor_layers.append(nn.InstanceNorm2d(16))
feature_extractor_layers.append(nn.Conv2d(16, 8, kernel_size=1, padding='same')) # -> 8x16x16
# feature_extractor_layers.append(nn.InstanceNorm2d(8))
self.feature_extractor = nn.Sequential(*feature_extractor_layers)
fmap_size = input_size // 8
shared_fc_layers = [nn.Linear(8 * fmap_size * fmap_size, fc_layers[0])] # shared for mu and sigma
for i in range(1, len(fc_layers)):
shared_fc_layers.append(nn.Linear(fc_layers[i - 1], fc_layers[i]))
shared_fc_layers.append(nn.LeakyReLU())
shared_fc_layers.append(nn.LayerNorm(fc_layers[i]))
self.shared_fc_layers = nn.Sequential(*shared_fc_layers)
self.mu_fc = nn.Sequential(nn.Linear(fc_layers[-1], latent_size),
nn.LeakyReLU(),
nn.Linear(latent_size, latent_size))
self.log_sigma_fc = nn.Sequential(nn.Linear(fc_layers[-1], latent_size),
nn.LeakyReLU(),
nn.Linear(latent_size, latent_size))
def forward(self, x):
features = self.feature_extractor(x)
features = features.view(features.size(0), -1)
features = self.shared_fc_layers(features)
mu = self.mu_fc(features)
log_sigma = self.log_sigma_fc(features)
# sample from normal distribution and reparameterize:
z = torch.rand_like(mu)
z = z * torch.exp(0.5 * log_sigma) + mu
return z, mu, log_sigma
class VaeDecoder(torch.nn.Module):
def __init__(self, output_size=128, latent_size=128, fc_layers=(128, 128, 128),
res_blocks_per_size=3, device=None):
super(VaeDecoder, self).__init__()
if device is None:
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
else:
self.device = device
self.latent_size = latent_size
self.output_size = output_size
self.first_fmap_size = output_size // 8
# additional layers for the decoder to make it compatible with the encoder (shared fc layers)
self.feature_extractor = [nn.Linear(latent_size, latent_size),
nn.LeakyReLU(),
nn.Linear(latent_size, latent_size),
nn.LeakyReLU(),
nn.Linear(latent_size, fc_layers[0])]
for i in range(1, len(fc_layers)):
self.feature_extractor.append(nn.Linear(fc_layers[i - 1], fc_layers[i]))
self.feature_extractor.append(nn.LeakyReLU())
self.feature_extractor.append(nn.LayerNorm(fc_layers[i]))
self.feature_extractor.append(nn.Linear(fc_layers[-1], 8 * self.first_fmap_size * self.first_fmap_size))
self.feature_extractor = nn.Sequential(*self.feature_extractor)
decoder_layers = [nn.Conv2d(8, 16, kernel_size=1, padding='same'),
nn.LeakyReLU(),
# nn.InstanceNorm2d(16),
nn.Conv2d(16, 32, kernel_size=1, padding='same'),
nn.LeakyReLU(),
# nn.InstanceNorm2d(32),
nn.Conv2d(32, 64, kernel_size=1, padding='same')] # -> 64x16x16
for _ in range(res_blocks_per_size):
decoder_layers.append(ResidualConvBlock(64))
decoder_layers.append(nn.Conv2d(64, 128, kernel_size=1, padding='same')) # -> 128x16x16
decoder_layers.append(nn.PixelShuffle(2)) # -> 32x32x32
# decoder_layers.append(nn.InstanceNorm2d(32))
for _ in range(res_blocks_per_size):
decoder_layers.append(ResidualConvBlock(32))
decoder_layers.append(nn.Conv2d(32, 64, kernel_size=1, padding='same')) # -> 64x32x32
decoder_layers.append(nn.PixelShuffle(2)) # -> 16x64x64
# decoder_layers.append(nn.InstanceNorm2d(16))
for _ in range(res_blocks_per_size):
decoder_layers.append(ResidualConvBlock(16))
decoder_layers.append(nn.Conv2d(16, 32, kernel_size=1, padding='same')) # -> 32x64x64
decoder_layers.append(nn.PixelShuffle(2)) # -> 8x128x128
# decoder_layers.append(nn.InstanceNorm2d(8))
for _ in range(res_blocks_per_size):
decoder_layers.append(ResidualConvBlock(8))
decoder_layers.append(nn.Conv2d(8, 3, kernel_size=1, padding='same')) # -> 3x128x128
decoder_layers.append(nn.Sigmoid())
self.decoder = nn.Sequential(*decoder_layers)
def forward(self, z):
features = self.feature_extractor(z)
features = features.view(features.size(0), 8, self.first_fmap_size, self.first_fmap_size)
x_reconstructed = self.decoder(features)
return x_reconstructed
class Vae(torch.nn.Module):
def __init__(self, input_size=128, latent_size=128, fc_layers=(256, 128), res_blocks_per_size=3, device=None):
super(Vae, self).__init__()
if device is None:
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
else:
self.device = device
self.latent_size = latent_size
self.input_size = input_size
self.encoder = VaeEncoder(latent_size=latent_size,
input_size=input_size,
fc_layers=fc_layers,
res_blocks_per_size=res_blocks_per_size,
device=self.device)
decoder_fc = fc_layers[::-1]
self.decoder = VaeDecoder(latent_size=latent_size,
output_size=input_size,
fc_layers=decoder_fc,
res_blocks_per_size=res_blocks_per_size,
device=self.device)
def forward(self, x):
assert x.size(2) == self.input_size and x.size(3) == self.input_size,\
'Input size must be {}'.format(self.input_size)
z, mu, log_sigma = self.encoder(x)
x_reconstructed = self.decoder(z)
return x_reconstructed, mu, log_sigma, z
def sample(self, num_samples=1):
z = torch.randn(num_samples, self.latent_size, device=self.device)
fake_shmooots = self.decoder(z)
return fake_shmooots