-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodel.py
130 lines (116 loc) · 5.09 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
import torch
import numpy as np
import torch.nn as nn
from collections import OrderedDict
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class Flatten(nn.Module):
def __init__(self):
super(Flatten, self).__init__()
def forward(self, x):
batch_size = x.shape[0]
return x.view(batch_size, -1)
class MLP(nn.Module):
def __init__(self, hidden_size, last_activation = True):
super(MLP, self).__init__()
q = []
for i in range(len(hidden_size)-1):
in_dim = hidden_size[i]
out_dim = hidden_size[i+1]
q.append(("Linear_%d" % i, nn.Linear(in_dim, out_dim)))
if (i < len(hidden_size)-2) or ((i == len(hidden_size) - 2) and (last_activation)):
q.append(("BatchNorm_%d" % i, nn.BatchNorm1d(out_dim)))
q.append(("ReLU_%d" % i, nn.ReLU(inplace=True)))
self.mlp = nn.Sequential(OrderedDict(q))
def forward(self, x):
return self.mlp(x)
class Encoder(nn.Module):
def __init__(self, shape, nhid = 16, ncond = 0):
super(Encoder, self).__init__()
c, h, w = shape
ww = ((w-8)//2 - 4)//2
hh = ((h-8)//2 - 4)//2
self.encode = nn.Sequential(nn.Conv2d(c, 16, 5, padding = 0), nn.BatchNorm2d(16), nn.ReLU(inplace = True),
nn.Conv2d(16, 32, 5, padding = 0), nn.BatchNorm2d(32), nn.ReLU(inplace = True),
nn.MaxPool2d(2, 2),
nn.Conv2d(32, 64, 3, padding = 0), nn.BatchNorm2d(64), nn.ReLU(inplace = True),
nn.Conv2d(64, 64, 3, padding = 0), nn.BatchNorm2d(64), nn.ReLU(inplace = True),
nn.MaxPool2d(2, 2),
Flatten(), MLP([ww*hh*64, 256, 128])
)
self.calc_mean = MLP([128+ncond, 64, nhid], last_activation = False)
self.calc_logvar = MLP([128+ncond, 64, nhid], last_activation = False)
def forward(self, x, y = None):
x = self.encode(x)
if (y is None):
return self.calc_mean(x), self.calc_logvar(x)
else:
return self.calc_mean(torch.cat((x, y), dim=1)), self.calc_logvar(torch.cat((x, y), dim=1))
class Decoder(nn.Module):
def __init__(self, shape, nhid = 16, ncond = 0):
super(Decoder, self).__init__()
c, w, h = shape
self.shape = shape
self.decode = nn.Sequential(MLP([nhid+ncond, 64, 128, 256, c*w*h], last_activation = False), nn.Sigmoid())
def forward(self, z, y = None):
c, w, h = self.shape
if (y is None):
return self.decode(z).view(-1, c, w, h)
else:
return self.decode(torch.cat((z, y), dim=1)).view(-1, c, w, h)
class VAE(nn.Module):
def __init__(self, shape, nhid = 16):
super(VAE, self).__init__()
self.dim = nhid
self.encoder = Encoder(shape, nhid)
self.decoder = Decoder(shape, nhid)
def sampling(self, mean, logvar):
eps = torch.randn(mean.shape).to(device)
sigma = 0.5 * torch.exp(logvar)
return mean + eps * sigma
def forward(self, x):
mean, logvar = self.encoder(x)
z = self.sampling(mean, logvar)
return self.decoder(z), mean, logvar
def generate(self, batch_size = None):
z = torch.randn((batch_size, self.dim)).to(device) if batch_size else torch.randn((1, self.dim)).to(device)
res = self.decoder(z)
if not batch_size:
res = res.squeeze(0)
return res
class cVAE(nn.Module):
def __init__(self, shape, nclass, nhid = 16, ncond = 16):
super(cVAE, self).__init__()
self.dim = nhid
self.encoder = Encoder(shape, nhid, ncond = ncond)
self.decoder = Decoder(shape, nhid, ncond = ncond)
self.label_embedding = nn.Embedding(nclass, ncond)
def sampling(self, mean, logvar):
eps = torch.randn(mean.shape).to(device)
sigma = 0.5 * torch.exp(logvar)
return mean + eps * sigma
def forward(self, x, y):
y = self.label_embedding(y)
mean, logvar = self.encoder(x, y)
z = self.sampling(mean, logvar)
return self.decoder(z, y), mean, logvar
def generate(self, class_idx):
if (type(class_idx) is int):
class_idx = torch.tensor(class_idx)
class_idx = class_idx.to(device)
if (len(class_idx.shape) == 0):
batch_size = None
class_idx = class_idx.unsqueeze(0)
z = torch.randn((1, self.dim)).to(device)
else:
batch_size = class_idx.shape[0]
z = torch.randn((batch_size, self.dim)).to(device)
y = self.label_embedding(class_idx)
res = self.decoder(z, y)
if not batch_size:
res = res.squeeze(0)
return res
BCE_loss = nn.BCELoss(reduction = "sum")
def loss(X, X_hat, mean, logvar):
reconstruction_loss = BCE_loss(X_hat, X)
KL_divergence = 0.5 * torch.sum(-1 - logvar + torch.exp(logvar) + mean**2)
return reconstruction_loss + KL_divergence