-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMCEVAE.py
295 lines (268 loc) · 11.4 KB
/
MCEVAE.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
import numpy as np
import sys
import torch
import torch.nn as nn
import torch.nn.functional as F
from VAE_base import Model
from utils import NonLinear, GatedDense
class MCEVAE(Model):
def __init__(self,
in_size=28*28,
aug_dim=16*7*7,
latent_z_c=0,
latent_z_var=5,
mode='SO2',
invariance_decoder='gated',
rec_loss='mse',
div='KL',
in_dim=1,
out_dim=1,
hidden_z_c=300,
hidden_z_var=300,
hidden_tau=32,
activation=nn.Sigmoid,
training_mode = 'supervised',
device = 'cpu',
tag = 'default'):
super(MCEVAE, self).__init__()
self.mode = mode
self.invariance_decoder = invariance_decoder
self.rec_loss = rec_loss
self.div_mode = div
self.hidden_z_c = hidden_z_c
self.hidden_z_var = hidden_z_var
self.hidden_tau = hidden_tau
self.latent_z_c = latent_z_c
self.latent_z_var = latent_z_var
self.in_dim = in_dim
self.out_dim = out_dim
self.aug_dim = aug_dim
self.in_size = in_size
self.device= device
self.training_mode = training_mode
self.tag = tag
print('in_size: {}, latent_z_c: {}, latent_z_var:{}, mode: {}, sem_dec: {}, rec_loss: {}, div: {}'.format(in_size, latent_z_c, latent_z_var, mode, invariance_decoder, rec_loss, div))
# transformation type
if mode == 'SO2':
tau_size = 1
bias = torch.tensor([0], dtype=torch.float)
elif mode == 'SE2':
tau_size = 3
bias = torch.tensor([0, 0, 0], dtype=torch.float)
elif mode == 'SIM2':
tau_size = 4
bias = torch.tensor([0, 0, 0, 0], dtype=torch.float)
elif mode == 'SE3':
tau_size = 6
bias = torch.tensor([0, 0, 0, 0, 0, 0], dtype=torch.float)
elif mode == 'NONE':
tau_size = 0
bias = torch.tensor([], dtype=torch.float)
else:
raise NotImplementedError
self.tau_size = tau_size
# augmented encoder
self.aug_enc = nn.Sequential(
nn.Conv2d(in_dim, 32, kernel_size=5, stride=1, padding=2, bias=False),
nn.BatchNorm2d(32),
nn.ELU(),
nn.Conv2d(32, 32, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(32),
nn.ELU(),
nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(64),
nn.ELU(),
nn.Conv2d(64, 16, kernel_size=5, stride=1, padding=2, bias=True)
)
# transformation extractor
if tau_size > 0:
self.tau_mean = nn.Sequential(
nn.Linear(self.aug_dim, hidden_tau),
nn.ReLU(True),
nn.Linear(hidden_tau, tau_size)
)
self.tau_logvar = nn.Sequential(
nn.Linear(self.aug_dim, hidden_tau),
nn.ReLU(True),
nn.Linear(hidden_tau, tau_size)
)
self.tau_mean[2].weight.data.zero_()
self.tau_mean[2].bias.data.copy_(bias)
self.tau_logvar[2].weight.data.zero_()
self.tau_logvar[2].bias.data.copy_(bias)
# Variational latent space extractor
if self.latent_z_var > 0:
self.q_z_var_mean = nn.Sequential(
nn.Linear(self.aug_dim, hidden_z_var),
nn.Sigmoid(),
nn.Linear(hidden_z_var, hidden_z_var),
nn.Sigmoid(),
nn.Linear(hidden_z_var, latent_z_var)
)
self.q_z_var_logvar = nn.Sequential(
nn.Linear(self.aug_dim, hidden_z_var),
nn.Sigmoid(),
nn.Linear(hidden_z_var, hidden_z_var),
nn.Sigmoid(),
nn.Linear(hidden_z_var, latent_z_var)
)
# semantic/shape extractor 2 = entangled latent space extractor
if self.latent_z_c > 0:
self.q_z_c_mean = nn.Sequential(
nn.Linear(self.aug_dim, hidden_z_c),
nn.Sigmoid(),
nn.Linear(hidden_z_c, hidden_z_c),
nn.Sigmoid(),
nn.Linear(hidden_z_c, latent_z_c)
)
self.q_z_c_logvar = nn.Sequential(
nn.Linear(self.aug_dim, hidden_z_c),
nn.Sigmoid(),
nn.Linear(hidden_z_c, hidden_z_c),
nn.Sigmoid(),
nn.Linear(hidden_z_c, latent_z_c)
)
# invariance decoder
if invariance_decoder == 'linear':
self.p_x_layer = nn.Sequential(
nn.Linear(latent_z_c + latent_z_var, hidden_z_c),
activation(),
nn.Linear(hidden_z_c, hidden_z_c),
activation(),
nn.Linear(hidden_z_c, hidden_z_c),
activation(),
nn.Linear(hidden_z_c, np.prod(in_size))
)
elif invariance_decoder == 'gated':
self.p_x_layer = nn.Sequential(
GatedDense(latent_z_c + latent_z_var, 300),
GatedDense(300, 300),
NonLinear(300, np.prod(in_size), activation=activation())
)
elif invariance_decoder == 'CNN':
self.sem_dec_fc = nn.Linear(latent_z_c + latent_z_var, self.aug_dim)
self.p_x_layer = nn.Sequential(
nn.ConvTranspose2d(16, 32, kernel_size=4, stride=1, padding=2, output_padding=0, bias=False),
nn.BatchNorm2d(32),
nn.ELU(),
nn.ConvTranspose2d(32, 16, kernel_size=5, stride=2, padding=1, output_padding=1, bias=False),
nn.BatchNorm2d(16),
nn.ELU(),
nn.ConvTranspose2d(16, 16, kernel_size=5, stride=2, padding=2, output_padding=1, bias=False),
nn.BatchNorm2d(16),
nn.ELU(),
nn.ConvTranspose2d(16, out_dim, kernel_size=3, stride=1, padding=1, output_padding=0, bias=True)
)
def aug_encoder(self, x):
x = self.aug_enc(x)
z_aug = x.view(-1, self.aug_dim)
return z_aug
def q_z_var(self, z_aug):
if self.latent_z_var == 0:
return torch.FloatTensor([]), torch.FloatTensor([])
z_var_q_mu = self.q_z_var_mean(z_aug)
z_var_q_logvar = self.q_z_var_logvar(z_aug)
return z_var_q_mu, z_var_q_logvar
def q_z_c(self, z_aug):
if self.latent_z_c == 0:
return torch.FloatTensor([]), torch.FloatTensor([]), torch.FloatTensor([])
z_c_q_mu = self.q_z_c_mean(z_aug)
z_c_q_logvar = self.q_z_c_logvar(z_aug)
return z_c_q_mu, z_c_q_logvar, torch.FloatTensor([])
def q_tau(self, z_aug):
if self.tau_size == 0:
return torch.FloatTensor([]), torch.FloatTensor([])
tau_q_mu = self.tau_mean(z_aug)
tau_q_logvar = self.tau_logvar(z_aug)
return tau_q_mu, tau_q_logvar
def get_M(self, tau):
if self.tau_size == 0:
return 1., 0.
params = torch.FloatTensor(tau.size()).fill_(0)
if self.mode == 'SO2':
M = torch.FloatTensor(tau.size()[0], 2, 3).fill_(0)
theta = tau.squeeze()
M[:, 0, 0] = torch.cos(theta)
M[:, 0, 1] = -1 * torch.sin(theta)
M[:, 1, 0] = torch.sin(theta)
M[:, 1, 1] = torch.cos(theta)
params = theta
elif self.mode == 'SE2':
M = torch.FloatTensor(tau.size()[0], 2, 3).fill_(0)
theta = tau[:, 0] + 1.e-20
u_1 = tau[:, 1]
u_2 = tau[:, 2]
M[:, 0, 0] = torch.cos(theta)
M[:, 0, 1] = -1 * torch.sin(theta)
M[:, 1, 0] = torch.sin(theta)
M[:, 1, 1] = torch.cos(theta)
M[:, 0, 2] = u_1 / theta * torch.sin(theta) - u_2 / theta * (1 - torch.cos(theta))
M[:, 1, 2] = u_1 / theta * (1 - torch.cos(theta)) + u_2 / theta * torch.sin(theta)
params[:, 0] = tau[:, 0]
params[:, 1:] = M[:, :, 2]
elif self.mode == 'SIM2':
M = torch.FloatTensor(tau.size()[0], 2, 3).fill_(0)
theta = tau[:, 0] + 1.e-20
u_1 = tau[:, 1]
u_2 = tau[:, 2]
scale = tau[:, 3].reshape(-1,1,1).cpu()
M[:, 0, 0] = torch.cos(theta)
M[:, 0, 1] = -1 * torch.sin(theta)
M[:, 1, 0] = torch.sin(theta)
M[:, 1, 1] = torch.cos(theta)
M[:, 0, 2] = u_1 / theta * torch.sin(theta) - u_2 / theta * (1 - torch.cos(theta))
M[:, 1, 2] = u_1 / theta * (1 - torch.cos(theta)) + u_2 / theta * torch.sin(theta)
M = M*scale
params[:, 0] = tau[:, 0]
params[:, 1:3] = M[:, :, 2]
return M, params
def reconstruct(self, z_var, z_c):
z = torch.cat((z_var, z_c), dim=1)
if self.invariance_decoder == 'CNN':
x = self.sem_dec_fc(z)
x = x.view(-1, 16, 7, 7)
x_mean = self.p_x_layer(x)
x_mean = torch.sigmoid(x_mean)
else:
x_mean = self.p_x_layer(z)
x_min = 1. / 512.
x_max = 1 - x_min
x_rec = torch.clamp(x_mean, min=x_min, max=x_max)
return x_rec, 0.
def transform(self, x, M, direction='forward', padding_mode='zeros'):
if self.tau_size == 0:
return x
if direction == 'reverse':
M_rev = torch.FloatTensor(M.size()).fill_(0)
R_rev = torch.inverse(M[:, :, :2].squeeze())
t = M[:, :, 2:]
t_rev = torch.matmul(R_rev, t).squeeze()
M_rev[:, :, :2] = R_rev
M_rev[:, :, 2] = -1 * t_rev
M = M_rev
elif direction != 'forward':
raise NotImplementedError
grid = F.affine_grid(M, x.size(),align_corners=False).to(self.device)
x = F.grid_sample(x, grid, padding_mode=padding_mode, align_corners=False)
return x
def forward(self, x):
z_aug = self.aug_encoder(x)
z_var_q_mu, z_var_q_logvar = self.q_z_var(z_aug)
z_var_q = self.reparameterize(z_var_q_mu, z_var_q_logvar).to(self.device)
z_c_q_mu, z_c_q_logvar, z_c_q_L = self.q_z_c(z_aug)
z_c_q = self.reparameterize(z_c_q_mu, z_c_q_logvar, z_c_q_L).to(self.device)
tau_q_mu, tau_q_logvar = self.q_tau(z_aug)
tau_q = self.reparameterize(tau_q_mu, tau_q_logvar).to(self.device)
M, params = self.get_M(tau_q)
x_rec, _ = self.reconstruct(z_var_q, z_c_q) #nn.Softmax().forward(z_c_q))
x_rec = x_rec.view(-1, 1, int(np.sqrt(self.in_size)), int(np.sqrt(self.in_size)))
x_hat = self.transform(x_rec, M, direction='forward')
return x_hat, z_var_q, z_var_q_mu, z_var_q_logvar, z_c_q, z_c_q_mu, z_c_q_logvar, z_c_q_L, tau_q, tau_q_mu, tau_q_logvar, x_rec, M
def get_x_ref(self, x, tau_q):
noise = (torch.rand_like(tau_q) - 1)*0.5 + 0.25
noise[:,0] = (torch.rand(noise.shape[0]) - 1)*2*np.pi + np.pi
if self.mode == 'SIM2':
noise[:,-1] = 0.5*torch.rand(noise.shape[0]) + 0.5
M_n, params_n = self.get_M(noise)
x_ref_trans = self.transform(x, M_n, direction='forward')
return x_ref_trans