-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
110 lines (90 loc) · 3.6 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
# %%
import torch.nn as nn
import torch
import numpy as np
import torchvision
import time
import glob
import torch.nn.functional as F
import matplotlib.pyplot as plt
from data_treatment import batch_size, image_size
in_channels = 3
latent_dims = 200
print_every = 200 #in batches
save_every = 15 #in epochs
log_loss_every = 5 #in batches
#device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
device = torch.device('cpu')
maxpool_indexes = [0,1]
# %% Model
class VAE(nn.Module):
def __init__(self, layer_count = 3, zsize = 512):
super(VAE, self).__init__()
self.layer_count = layer_count
self.zsize = zsize
inputs = in_channels
mul = 1
for i in range(self.layer_count):
setattr(self, "conv%d" % (i + 1), nn.Conv2d(inputs, image_size * mul, 4, 2, 1))
setattr(self, "conv%d_bn" % (i + 1), nn.BatchNorm2d(image_size * mul))
if i in maxpool_indexes:
setattr(self, "maxpool%d" % (i + 1), nn.MaxPool2d(2,2))
inputs = image_size * mul
mul *= 2
self.d_max = inputs
self.fc1 = nn.Linear(inputs * 4 * 4, zsize)
self.fc2 = nn.Linear(inputs * 4 * 4, zsize)
self.d1 = nn.Linear(zsize, inputs * 4 * 4)
mul = inputs // image_size // 2
for i in range(1, self.layer_count):
setattr(self, "deconv%d" % (i + 1), nn.ConvTranspose2d(inputs, image_size * mul, 4, 2, 1))
setattr(self, "deconv%d_bn" % (i + 1), nn.BatchNorm2d(image_size * mul))
if i-1 in maxpool_indexes:
setattr(self, "upsample%d" % (i + 1), nn.Upsample(scale_factor = 2))
inputs = image_size * mul
mul //= 2
setattr(self, "deconv%d" % (self.layer_count + 1), nn.ConvTranspose2d(inputs, in_channels, 4, 2, 1))
def encode(self, x):
for i in range(self.layer_count):
#print( torch.cuda.memory_summary(device=None, abbreviated=False))
x = F.relu(getattr(self, "conv%d_bn" % (i + 1))(getattr(self, "conv%d" % (i + 1))(x)))
if i in maxpool_indexes:
x = getattr(self, "maxpool%d" % (i + 1))(x)
#print(x.shape)
x = x.view(x.shape[0], self.d_max * 4 * 4)
h1 = self.fc1(x)
h2 = self.fc2(x)
return h1, h2
def reparameterize(self, mu, logvar):
if self.training:
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return eps.mul(std).add_(mu)
else:
return mu
def decode(self, x):
x = x.view(x.shape[0], self.zsize)
x = self.d1(x)
x = x.view(x.shape[0], self.d_max, 4, 4)
#x = self.deconv1_bn(x)
x = F.leaky_relu(x, 0.2)
for i in range(1, self.layer_count):
x = F.leaky_relu(getattr(self, "deconv%d_bn" % (i + 1))(getattr(self, "deconv%d" % (i + 1))(x)), 0.2)
if i-1 in maxpool_indexes:
x = getattr(self, "upsample%d" % (i + 1))(x)
x = torch.tanh(getattr(self, "deconv%d" % (self.layer_count + 1))(x))
return x
def forward(self, x):
mu, logvar = self.encode(x)
mu = mu.squeeze()
logvar = logvar.squeeze()
z = self.reparameterize(mu, logvar)
return self.decode(z.view(-1, self.zsize, 1, 1)), mu, logvar
def weight_init(self, mean, std):
for m in self._modules:
normal_init(self._modules[m], mean, std)
def normal_init(m, mean, std):
if isinstance(m, nn.ConvTranspose2d) or isinstance(m, nn.Conv2d):
m.weight.data.normal_(mean, std)
m.bias.data.zero_()
# %% Save model state