-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
439 lines (353 loc) · 17.1 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import os
import torch
from torch import nn
from torch.distributions import Normal
from torch.nn import functional as F
# replace this with torch.distributions
def kl_divergence(encoding_mu, encoding_logvar):
return -0.5 * torch.sum(encoding_logvar - (encoding_logvar).exp() - encoding_mu.pow(2) + 1)
def conv3x3(in_planes, out_planes, stride=1):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=False)
def conv1x1(in_planes, out_planes, stride=1):
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
class DeconvBottleneck(nn.Module):
def __init__(self, inplanes, planes, stride=1, upsample=None):
super(DeconvBottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.ConvTranspose2d(planes, planes ,
kernel_size=4,
stride=2, bias=False,
padding=1)
self.bn2 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.upsample = upsample
def forward(self, x):
shortcut = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.upsample is not None:
shortcut = self.upsample(x)
out += shortcut
out = self.relu(out)
return out
class VAE_Encoder(nn.Module):
def __init__(self, in_channels, z_dimensions, need_logvar=True, zero_init_residual=False):
super(VAE_Encoder, self).__init__()
self.inplanes = 32
self.conv1 = nn.Conv2d(in_channels, 32, kernel_size=5, stride=2, padding=2, bias=False)
self.bn1 = nn.BatchNorm2d(32)
self.relu = nn.ReLU(inplace=True)
self.layer1 = self._make_layer(BasicBlock, 32, 1, stride=2)
self.layer2 = self._make_layer(BasicBlock, 64, 1, stride=2)
self.layer3 = self._make_layer(BasicBlock, 128, 1, stride=2)
self.layer4 = self._make_layer(BasicBlock, 256, 1, stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.conv_mu = conv1x1(256, z_dimensions, 1)
self.conv_logvar = None
if need_logvar:
self.conv_logvar = conv1x1(256, z_dimensions, 1)
if zero_init_residual:
for m in self.modules():
if isinstance(m, BasicBlock):
nn.init.constant_(m.bn2.weight, 0)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
mu = self.conv_mu(x)
logvar = None
if self.conv_logvar is not None:
logvar = self.conv_logvar(x)
return (mu, logvar)
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion, stride),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def rsample(self, mu, logvar):
m = Normal(mu, torch.exp(logvar * 0.5))
return m.rsample()
class VAE_Decoder(nn.Module):
def __init__(self, in_channels, out_channels, input_image_size=32, zero_init_residual=False):
super(VAE_Decoder, self).__init__()
self.input_image_size = input_image_size
self.in_channels = 128
self.conv1 = nn.ConvTranspose2d(in_channels, self.in_channels,
kernel_size=2, stride=1, padding=0,
bias=False)
self.bn1 = nn.BatchNorm2d(self.in_channels)
self.uplayer1 = self._make_up_block(DeconvBottleneck, 128, 1, stride=1)
self.uplayer2 = self._make_up_block(DeconvBottleneck, 64, 1, stride=1)
self.uplayer3 = self._make_up_block(DeconvBottleneck, 32, 1, stride=1)
self.uplayer4 = self._make_up_block(DeconvBottleneck, 16, 1, stride=1)
if self.input_image_size > 32:
self.uplayer5 = self._make_up_block(DeconvBottleneck, 16, 1, stride=1)
self.conv2 = nn.Conv2d(16, out_channels, 3, 1, 1)
self.bn2 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
if zero_init_residual:
for m in self.modules():
if isinstance(m, DeconvBottleneck):
nn.init.constant_(m.bn2.weight, 0)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.uplayer1(x)
x = self.uplayer2(x)
x = self.uplayer3(x)
x = self.uplayer4(x)
if self.input_image_size > 32:
x = self.uplayer5(x)
x = self.bn2(self.conv2(x))
return x
def _make_up_block(self, block, init_channels, num_layer, stride=1):
upsample = nn.Sequential(
nn.ConvTranspose2d(self.in_channels, init_channels,
kernel_size=4,
stride=2, bias=False,
padding=1),
nn.BatchNorm2d(init_channels)
)
layers = []
for i in range(1, num_layer):
layers.append(block(self.in_channels, init_channels))
layers.append(block(self.in_channels, init_channels, stride, upsample))
self.in_channels = init_channels
return nn.Sequential(*layers)
class MaskedConv2d(nn.Conv2d):
def __init__(self, mask_type, *args, **kwargs):
super(MaskedConv2d, self).__init__(*args, **kwargs)
assert mask_type in {'A', 'B'}
self.register_buffer('mask', self.weight.data.clone())
_, _, kH, kW = self.weight.size()
self.mask.fill_(1)
self.mask[:, :, kH // 2, kW // 2 + (mask_type == 'B'):] = 0
self.mask[:, :, kH // 2 + 1:] = 0
def forward(self, x):
self.weight.data *= self.mask
return super(MaskedConv2d, self).forward(x)
class PixelCNN(nn.Module):
def __init__(self, in_channels, intermediate_channels, out_channels, layers=4, activation="ReLu"):
super(PixelCNN, self).__init__()
self.bn = nn.ModuleList([nn.InstanceNorm2d(intermediate_channels)] * (layers - 1))
self.bn.append(nn.InstanceNorm2d(out_channels))
self.bn1 = nn.InstanceNorm2d(in_channels)
self.layers = []
for i in range(layers):
if i == 0:
self.layers.append(MaskedConv2d('A', in_channels, intermediate_channels, 7, 1, 3, bias=True))
elif i == layers - 1:
self.layers.append(MaskedConv2d('B', intermediate_channels, out_channels, 7, 1, 3, bias=True))
else:
self.layers.append(MaskedConv2d('B', intermediate_channels, intermediate_channels, 7, 1, 3, bias=True))
self.layers = nn.ModuleList(self.layers)
if activation == "ReLu":
self.lu = nn.ReLU()
elif activation == "Elu":
self.lu = nn.ELU()
def forward(self, x):
x = self.bn1(x)
for i in range(len(self.layers) - 1):
x = self.layers[i](x)
x = self.bn[i](x)
x = self.lu(x)
x = self.layers[i + 1](x)
return x
class VAE(nn.Module):
def __init__(self, in_channels, intermediate_channels, decoder_out_channels=1, pixelcnn_out_channels=2,
z_dimension=32,
pixelcnn=True, only_pixelcnn=True, pixelcnn_layers=4, pixelcnn_activation="ReLu", nll=1, kl=1, mmd=0,
require_rsample=True, sigma_decoder=0.1, input_image_size=64):
'''
Args:
in_channels: In channels for the VAE (1 for MNIST, 3 for CIFAR)
intermediate_channels: Intermediate Channels for Encoder/Decoder/PixelCNN
out_channels: Out channels for the VAE (1 or 256 for MNIST)
z_dimension: Bottleneck dimension for the VAE (default : 32)
nll: Coefficient of the nll term in loss
kl: Coefficient of the klterm in loss
mmd: Coefficient of the mmd term in loss
require_rsample: Is it required for the output of decoder to be follow normal distribution?
sigma_decoder: If decoder follows normal distribution, with how much standard deviation do we sample p(x/z)
'''
super(VAE, self).__init__()
self.in_channels = in_channels
self.z_dimensions = z_dimension
self.decoder_out_channels = decoder_out_channels
self.pixelcnn_out_channels = pixelcnn_out_channels
self.num_pixelcnn_layers = pixelcnn_layers
self.require_rsample = require_rsample
self.nll, self.kl, self.mmd = nll, kl, mmd
self.sigma_decoder = sigma_decoder
self.input_image_size = input_image_size
self.only_pixelcnn = only_pixelcnn
if not only_pixelcnn:
if pixelcnn:
# TODO : Add abilitiy to choose if you to stack 256 channels of VAE output with 1 channel of input
# TODO : Or stack 256 channel of VAE output with 256 channel of VAE input
self.pixelcnn = PixelCNN(decoder_out_channels + in_channels, intermediate_channels,
pixelcnn_out_channels,
pixelcnn_layers, \
pixelcnn_activation)
else:
self.pixelcnn = None
self.encoder = VAE_Encoder(in_channels, z_dimension, require_rsample,
zero_init_residual=False)
self.decoder = VAE_Decoder(z_dimension, decoder_out_channels, input_image_size, zero_init_residual=False)
if input_image_size > 32:
self.adjust = (64 - input_image_size) // 2
else:
self.adjust = (32 - input_image_size) // 2
else:
self.pixelcnn = PixelCNN(in_channels, intermediate_channels, pixelcnn_out_channels, pixelcnn_layers, \
pixelcnn_activation)
def forward(self, x, sample=None):
mu, logvar, encoding, reconstruction = None, None, None, None
if not self.only_pixelcnn:
mu, logvar = self.encoder(x)
if self.require_rsample:
encoding = self.encoder.rsample(mu, logvar)
else:
encoding = mu
decoder_output = self.decoder(encoding)
if self.adjust != 0:
decoder_output = decoder_output[:, :, self.adjust:-self.adjust, self.adjust:-self.adjust]
if self.pixelcnn is not None:
if self.training:
concat = torch.cat([decoder_output, x], dim=1)
else:
concat = torch.cat([decoder_output, sample], dim=1)
reconstruction = self.pixelcnn(concat)
else:
reconstruction = decoder_output
else:
reconstruction = self.pixelcnn(x)
return mu, logvar, encoding, reconstruction
def get_z_image(self, encoding):
decoder_output = self.decoder(encoding)
if self.adjust != 0:
decoder_output = decoder_output[:, :, self.adjust:-self.adjust, self.adjust:-self.adjust]
return decoder_output
def run_pixelcnn(self, concat):
return self.pixelcnn(concat)
def get_reconstruction(self, encoding, sample=None):
decoder_output = self.decoder(encoding)
if self.adjust != 0:
decoder_output = decoder_output[:, :, self.adjust:-self.adjust, self.adjust:-self.adjust]
if self.pixelcnn is not None:
concat = torch.cat([decoder_output, sample], dim=1)
reconstruction = self.pixelcnn(concat)
else:
reconstruction = decoder_output
return reconstruction
def kl_divergence(self, encoding_mu, encoding_logvar):
return -0.5 * torch.sum(encoding_logvar - (encoding_logvar).exp() - encoding_mu.pow(2) + 1)
def compute_kernel(self, x, y):
x_size = x.size(0)
y_size = y.size(0)
dim = x.size(1)
x = x.unsqueeze(1) # (x_size, 1, dim)
y = y.unsqueeze(0) # (1, y_size, dim)
tiled_x = x.expand(x_size, y_size, dim)
tiled_y = y.expand(x_size, y_size, dim)
kernel_input = (tiled_x - tiled_y).pow(2).mean(2) / float(dim)
return torch.exp(-kernel_input) # (x_size, y_size)
def compute_mmd(self, x, y):
x_kernel = self.compute_kernel(x, x)
y_kernel = self.compute_kernel(y, y)
xy_kernel = self.compute_kernel(x, y)
mmd = x_kernel.sum() + y_kernel.sum() - 2 * xy_kernel.sum()
return mmd
def loss(self, target, encoding_mu, encoding_logvar, encoding, reconstruction, device, args):
kl = torch.tensor(0.).to(device)
mmd = torch.tensor(0.).to(device)
# In case of requrie rsample to be False, encoding_logvar would be None
if encoding_mu is not None and encoding_logvar is not None:
kl = self.kl_divergence(encoding_mu, encoding_logvar)
# In case of only pixelcnn architecture the encoding would be none
if encoding is not None:
true_samples = torch.randn(target.shape[0], encoding.shape[1]).to(device)
mmd = self.compute_mmd(true_samples, encoding.view(-1, encoding.shape[1]))
# Need cross entropy when pixelcnn exists or when it is plain vae, but number of output_channels from deocder is
# greater than number of input_channels
if self.pixelcnn is not None or (self.pixelcnn is None and self.decoder_out_channels > self.in_channels):
px_given_z = self.nll * F.cross_entropy(reconstruction, target, reduction='none',
weight=args.data_ratio_of_labels).sum()
else:
px_given_z = - self.nll * Normal(reconstruction, self.sigma_decoder).log_prob(target).sum()
loss = (px_given_z + (self.kl * kl) + (self.mmd * mmd)) / target.shape[0]
return loss, px_given_z.item() / target.shape[0], kl.item() / target.shape[0], mmd.item() / target.shape[0]
def __repr__(self):
string = ""
pixelcnn_input = str(self.input_image_size) + "x" + str(self.input_image_size) + "x"
if self.only_pixelcnn:
pixelcnn_used = "by itself"
pixelcnn_input += str(self.in_channels)
else:
pixelcnn_used = "in the decoder"
pixelcnn_input += str(self.in_channels + self.decoder_out_channels)
rsample_text = ""
if self.require_rsample:
rsample_text = " Where Z is rsampled from a Normal Distribution."
string += "We are using an encoder which takes input of " + str(self.input_image_size) + "x" + str(
self.input_image_size) + "x" + str(self.in_channels) + " and encodes into " + str(self.z_dimensions) + \
" dimensional latent space." + rsample_text + \
" \nIt is then pushed into a decoder which outputs an image of dimension " + \
str(self.input_image_size) + "x" + str(self.input_image_size) + "x" + str(
self.decoder_out_channels) + ".\n"
if self.pixelcnn is None:
if self.decoder_out_channels == self.in_channels:
string += "We assume p(x/z) follows a normal distribution with mean x_recon and sigma " + str(
self.sigma_decoder) + ".\n"
else:
string += "We assume p(x/z) follows a categorical distribution. \n"
else:
string += "We are using PixelCNN " + pixelcnn_used + " which takes an input of " + pixelcnn_input + \
" dimension goes through " + str(self.num_pixelcnn_layers) + " layers and outputs a " \
+ str(self.input_image_size) + "x" + str(self.input_image_size) + "x" + str(
self.pixelcnn_out_channels) + " dimensions image"
return string