-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.py
287 lines (228 loc) · 9.85 KB
/
modules.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
import torch
from torch import nn
from dpipe.torch import TorchModel, set_lr, to_np, sequence_to_np, to_cuda
from dpipe.torch.utils import to_var, sequence_to_var
def kl_divergence(prior_out: torch.Tensor, post_out: torch.Tensor):
mean_0, log_std_0 = prior_out[:, 0], prior_out[:, 1]
mean_1, log_std_1 = post_out[:, 0], post_out[:, 1]
std_delta = 2 * (log_std_1 - log_std_0)
mean_delta = mean_1 - mean_0
dim = mean_0.shape[1]
trace = torch.exp(-std_delta)
delta = torch.exp(-2 * log_std_1) * mean_delta ** 2
total = trace.sum(1) + delta.sum(1) + std_delta.sum(1)
res = ((total - dim) / 2).mean()
return res
def compute_mmd(x, y):
def compute_kernel(x, y):
x_size = x.size(0)
y_size = y.size(0)
dim = x.size(1)
x = to_cuda(x.unsqueeze(1)) # (x_size, 1, dim)
y = to_cuda(y.unsqueeze(0)) # (1, y_size, dim)
tiled_x = to_cuda(x.expand(x_size, y_size, dim))
tiled_y = to_cuda(y.expand(x_size, y_size, dim))
kernel_input = to_cuda((tiled_x - tiled_y).pow(2).mean(2) / float(dim))
return to_cuda(torch.exp(-kernel_input)) # (x_size, y_size)
x_kernel = compute_kernel(x, x)
y_kernel = compute_kernel(y, y)
xy_kernel = compute_kernel(x, y)
mmd = x_kernel.mean() + y_kernel.mean() - 2*xy_kernel.mean()
return mmd
class AutoEncoder(nn.Module):
def __init__(self, encoder_conv, encoder_lin, decoder_lin, decoder_conv):
super().__init__()
self.encoder_conv = encoder_conv
self.encoder_lin = encoder_lin
self.decoder_lin = decoder_lin
self.decoder_conv = decoder_conv
def forward(self, batch_of_contours):
input_shape = batch_of_contours.shape
batch_size = input_shape[0]
x = nn.functional.interpolate(batch_of_contours, 256)
x = self.encoder_conv(x)
x = x.reshape(batch_size, -1)
z = self.encoder_lin(x)
x = self.decoder_lin(z)
x = x.reshape(batch_size, 128, 32)
x = self.decoder_conv(x)
x = nn.functional.interpolate(x, input_shape[2:])
return z, x
class Wnet(TorchModel):
def __init__(self, logits2pred, logits2loss, dist_loss, optimize, cuda, u_net, autoencoder, to_curve, latent_space_dim,
beta=1, gama=1):
self.u_net = u_net
self.autoencoder = autoencoder
self.dist_loss = dist_loss
self.to_curve = to_curve
self.latent_space_dim = latent_space_dim
self.beta = beta
self.gama = gama
model_core = nn.ModuleDict({"u_net": self.u_net, "autoencoder": self.autoencoder})
super().__init__(model_core, logits2pred, logits2loss, optimize, cuda)
def do_train_step(self, batch_of_images, batch_of_contours, *, lr):
"""
Performs a forward-backward pass, as well as the gradient step, according to the given ``inputs``.
Notes
-----
Note that both input and output are **not** of type `torch.Tensor` - the conversion
to torch.Tensor is made inside this function.
"""
self.model_core.train()
batch_of_images, batch_of_contours = sequence_to_var(batch_of_images, batch_of_contours, cuda=self.model_core)
u_out = self.u_net(batch_of_images)
u_curve = self.to_curve(u_out)
z, reproduced_curve = self.autoencoder(u_curve)
true_samples = Variable(torch.randn(200, self.latent_space_dim), requires_grad=False)
loss_u_curve = self.logits2loss(u_out, batch_of_contours)
loss_mmd = self.dist_loss(true_samples, z)
loss_reproduced_curve = torch.nn.functional.mse_loss(u_curve, reproduced_curve)
loss = loss_u_curve + self.beta*loss_mmd + self.gama*loss_reproduced_curve
set_lr(self.optimizer, lr)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
return sequence_to_np(loss, loss_u_curve, loss_mmd, loss_reproduced_curve)
def do_inf_step(self, batch_of_images):
"""
Returns the prediction for the given ``inputs``.
Notes
-----
Note that both input and output are **not** of type `torch.Tensor` - the conversion
to torch.Tensor is made inside this function.
"""
self.model_core.eval()
with torch.no_grad():
batch_of_images = to_var(batch_of_images, self.model_core)
u_out = self.u_net(batch_of_images)
return to_np(self.logits2pred(u_out))
def get_trio(self, batch_of_images):
"""
Returns the prediction for the given ``inputs``.
Notes
-----
Note that both input and output are **not** of type `torch.Tensor` - the conversion
to torch.Tensor is made inside this function.
"""
self.model_core.eval()
with torch.no_grad():
batch_of_images = to_var(batch_of_images, self.model_core)
u_out = self.u_net(batch_of_images)
u_curve = self.to_curve(u_out)
z, reconstructed = self.autoencoder(u_curve)
return to_np(self.logits2pred(u_out)), to_np(z), to_np(reconstructed)
def z_to_curve(self, z, input):
self.model_core.eval()
with torch.no_grad():
input_shape = input.shape
batch_size = input_shape[0]
z = to_var(z, self.model_core)
x = self.autoencoder.decoder_lin(z)
x = x.reshape(batch_size, 128, 32)
x = self.autoencoder.decoder_conv(x)
reproduced_curve = nn.functional.interpolate(x, input_shape[2:])
return to_np(reproduced_curve)
class PosteriorNet(nn.Module):
def __init__(self, x_net, y_net, mixer):
super().__init__()
self.x_net = x_net
self.y_net = y_net
self.mixer = mixer
def forward(self, images, contours):
batch_size = images.shape[0]
x = self.x_net(images).reshape(batch_size, -1)
y = self.y_net(contours).reshape(batch_size, -1)
comb = torch.cat([x, y], 1)
res = self.mixer(comb)
# return to_cuda(torch.zeros_like(res))
return res
class FinalNet(nn.Module):
def __init__(self, net):
super().__init__()
self.net = net
def plant(self, batch_of_images: torch.Tensor, point: torch.Tensor):
point_tensor = torch.ones(
batch_of_images.shape[0], point.shape[1], *batch_of_images.shape[2:],
device=batch_of_images.device, dtype=batch_of_images.dtype
)
for _ in batch_of_images.shape[2:]:
point = point.unsqueeze(-1)
res = torch.cat([batch_of_images, point_tensor * point], 1)
return res
def forward(self, images, point):
huge_tensor = self.plant(images, point)
res = self.net(huge_tensor)
return res
class False_PriorNet(nn.Module):
def __init__(self, net):
super().__init__()
self.net = net
def forward(self, images):
res = self.net(images)
return to_cuda(torch.zeros_like(res))
def sample(distrib_tensor):
means, log_stds = distrib_tensor[:, 0], distrib_tensor[:, 1]
eps = torch.randn_like(means)
res = means + eps * torch.exp(log_stds)
return res
def visualize_sample(distrib_tensor: torch.Tensor, i, j):
means, log_stds = distrib_tensor[:, 0], distrib_tensor[:, 1]
stds = torch.exp(log_stds)
x = i * stds[0][0] + means[0][0]
y = j * stds[0][1] + means[0][1]
l = [x, y]
point = to_cuda(torch.Tensor(l))
point = point[None, :]
return point
class CondUnet(TorchModel):
def __init__(self, logits2pred, logits2loss, dist_loss, optimize, cuda, u_net, prior_net, post_net, sample,
final_net):
self.u_net = u_net
self.prior_net = prior_net
self.post_net = post_net
self.sample = sample
self.dist_loss = dist_loss
self.final_net = final_net
model_core = nn.ModuleDict({"u_net": self.u_net, "prior_net": self.prior_net, "post_net": self.post_net,
"final_net": self.final_net})
super().__init__(model_core, logits2pred, logits2loss, optimize, cuda)
# functions for CondUnet
def do_train_step(self, batch_of_images, batch_of_contours, *, lr, beta):
"""
Performs a forward-backward pass, as well as the gradient step, according to the given ``inputs``.
Notes
-----
Note that both input and output are **not** of type `torch.Tensor` - the conversion
to torch.Tensor is made inside this function.
"""
self.model_core.train()
batch_of_images, batch_of_contours = sequence_to_var(batch_of_images, batch_of_contours, cuda=self.model_core)
u_out = self.u_net(batch_of_images)
prior_out = self.prior_net(batch_of_images)
post_out = self.post_net(batch_of_images, batch_of_contours)
point = self.sample(post_out)
final_out = self.final_net(u_out, point)
loss_u_net = self.logits2loss(final_out, batch_of_contours)
loss_kl = self.dist_loss(prior_out, post_out)
loss = loss_u_net + beta * loss_kl
set_lr(self.optimizer, lr)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
return sequence_to_np(loss, loss_u_net, loss_kl)
def do_inf_step(self, batch_of_images):
"""
Returns the prediction for the given ``inputs``.
Notes
-----
Note that both input and output are **not** of type `torch.Tensor` - the conversion
to torch.Tensor is made inside this function.
"""
self.model_core.eval()
with torch.no_grad():
batch_of_images = to_var(batch_of_images, self.model_core)
u_out = self.u_net(batch_of_images)
prior_out = self.prior_net(batch_of_images)
point = self.sample(prior_out)
final_out = self.logits2pred(self.final_net(u_out, point))
return to_np(final_out)