-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributions.py
319 lines (275 loc) · 11.6 KB
/
distributions.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
import torch
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
from torch.distributions import Normal, Uniform
from utils import LowerBound
from modules import ConvLSTMCell
class NormalDistribution:
'''
A normal distribution
'''
def __init__(self, loc, scale):
assert loc.shape == scale.shape
self.loc = loc
self.scale = scale
@property
def mean(self):
return self.loc.detach()
def std_cdf(self, inputs):
half = 0.5
const = -(2**-0.5)
return half * torch.erfc(const * inputs)
def sample(self):
return self.scale * torch.randn_like(self.scale) + self.loc
def likelihood(self, x, min=1e-9):
x = torch.abs(x - self.loc)
upper = self.std_cdf((.5 - x) / self.scale)
lower = self.std_cdf((-.5 - x) / self.scale)
return LowerBound.apply(upper - lower, min)
def scaled_likelihood(self, x, s=1, min=1e-9):
x = torch.abs(x - self.loc)
s = s * .5
upper = self.std_cdf((s - x) / self.scale)
lower = self.std_cdf((-s - x) / self.scale)
return LowerBound.apply(upper - lower, min)
class PriorFunction(nn.Module):
# A Custom Function described in Balle et al 2018. https://arxiv.org/pdf/1802.01436.pdf
__constants__ = ['bias', 'in_features', 'out_features']
def __init__(self, parallel_dims, in_features, out_features, scale, bias=True):
super(PriorFunction, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = nn.Parameter(torch.Tensor(parallel_dims, 1, 1, in_features, out_features))
if bias:
self.bias = nn.Parameter(torch.Tensor(parallel_dims, 1, 1, 1, out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters(scale)
def reset_parameters(self, scale):
nn.init.constant_(self.weight, scale)
if self.bias is not None:
nn.init.uniform_(self.bias, -0.5, 0.5)
def forward(self, input, detach=False):
# input shape (channel, batch_size, in_features)
if detach:
return torch.matmul(input, F.softplus(self.weight.detach())) + self.bias.detach()
return torch.matmul(input, F.softplus(self.weight)) + self.bias
def extra_repr(self):
return 'in_features={}, out_features={}, bias={}'.format(self.in_features, self.out_features, self.bias
is not None)
class FlexiblePrior(nn.Module):
'''
A prior model described in Balle et al 2018 Appendix 6.1 https://arxiv.org/pdf/1802.01436.pdf
return the boxshape likelihood
'''
def __init__(self, channels=256, dims=[3, 3, 3], init_scale=10.):
super(FlexiblePrior, self).__init__()
dims = [1] + dims + [1]
self.chain_len = len(dims) - 1
scale = init_scale**(1 / self.chain_len)
h_b = []
for i in range(self.chain_len):
init = np.log(np.expm1(1 / scale / dims[i + 1]))
h_b.append(PriorFunction(channels, dims[i], dims[i + 1], init))
self.affine = nn.ModuleList(h_b)
self.a = nn.ParameterList(
[nn.Parameter(torch.zeros(channels, 1, 1, 1, dims[i + 1])) for i in range(self.chain_len - 1)])
# optimize the medians to fix the offset issue
self._medians = nn.Parameter(torch.zeros(1, channels, 1, 1))
@property
def medians(self):
return self._medians.detach()
def cdf(self, x, logits=True, detach=False):
x = x.transpose(0, 1).unsqueeze(-1) # C, N, H, W, 1
if detach:
for i in range(self.chain_len - 1):
x = self.affine[i](x, detach)
x += torch.tanh(self.a[i].detach()) * torch.tanh(x)
if logits:
return self.affine[-1](x, detach).squeeze(-1).transpose(0, 1)
return torch.sigmoid(self.affine[-1](x, detach)).squeeze(-1).transpose(0, 1)
# not detached
for i in range(self.chain_len - 1):
x = self.affine[i](x)
x += torch.tanh(self.a[i]) * torch.tanh(x)
if logits:
return self.affine[-1](x).squeeze(-1).transpose(0, 1)
return torch.sigmoid(self.affine[-1](x)).squeeze(-1).transpose(0, 1)
def pdf(self, x):
cdf = self.cdf(x, False)
jac = torch.ones_like(cdf)
pdf = torch.autograd.grad(cdf, x, grad_outputs=jac)[0]
return pdf
def get_extraloss(self):
target = 0
logits = self.cdf(self._medians, detach=True)
extra_loss = torch.abs(logits - target).sum()
return extra_loss
def likelihood(self, x, min=1e-9):
lower = self.cdf(x - 0.5, True)
upper = self.cdf(x + 0.5, True)
sign = -torch.sign(lower + upper).detach()
upper = torch.sigmoid(upper * sign)
lower = torch.sigmoid(lower * sign)
return LowerBound.apply(torch.abs(upper - lower), min)
def scaled_likelihood(self, x, s, min=1e-9):
lower = self.cdf(x - 0.5 * s, True)
upper = self.cdf(x + 0.5 * s, True)
sign = -torch.sign(lower + upper).detach()
upper = torch.sigmoid(upper * sign)
lower = torch.sigmoid(lower * sign)
return LowerBound.apply(torch.abs(upper - lower), min)
def icdf(self, xi, method='bisection', max_iterations=1000, tol=1e-9, **kwargs):
if method == 'bisection':
init_interval = [-1, 1]
left_endpoints = torch.ones_like(xi) * init_interval[0]
right_endpoints = torch.ones_like(xi) * init_interval[1]
def f(z):
return self.cdf(z, logits=False, detach=True) - xi
while True:
if (f(left_endpoints) < 0).all():
break
else:
left_endpoints = left_endpoints * 2
while True:
if (f(right_endpoints) > 0).all():
break
else:
right_endpoints = right_endpoints * 2
for i in range(max_iterations):
mid_pts = 0.5 * (left_endpoints + right_endpoints)
mid_vals = f(mid_pts)
pos = mid_vals > 0
non_pos = torch.logical_not(pos)
neg = mid_vals < 0
non_neg = torch.logical_not(neg)
left_endpoints = left_endpoints * non_neg.float() + mid_pts * neg.float()
right_endpoints = right_endpoints * non_pos.float() + mid_pts * pos.float()
if (torch.logical_and(non_pos, non_neg)).all() or torch.min(right_endpoints - left_endpoints) <= tol:
print(f'bisection terminated after {i} its')
break
return mid_pts
else:
raise NotImplementedError
def sample(self, img, shape):
uni = torch.rand(shape, device=img.device)
return self.icdf(uni)
class SpatialAutoregressivePrior(nn.Module):
'''
Spatial autoregressive prior
'''
def __init__(self, filters=(1, 3, 1), inter_z_size=512, z_size=256, dims=[3, 3, 3], scale=10., bound=1e-9):
super(SpatialAutoregressivePrior, self).__init__()
self.init_prior = FlexiblePrior(z_size, dims, scale, bound)
conv_stack = []
for i in range(len(filters)):
if i == 0:
conv_stack.append(nn.ConvTranspose2d(z_size, inter_z_size, filters[i]))
else:
conv_stack.append(nn.ConvTranspose2d(inter_z_size, inter_z_size, filters[i]))
conv_stack.append(nn.LeakyReLU(0.2, True))
self.conv = nn.Sequential(*conv_stack)
self.loc_conv = nn.ConvTranspose2d(inter_z_size, z_size, 1)
self.scale_conv = nn.ConvTranspose2d(inter_z_size, z_size, 1)
def kl(self, x):
def get_ring(x):
transposed = False
if x.shape[2] > x.shape[3]:
x = x.transpose(2, 3)
transposed = True
N, C, H, W = x.shape
a = x[:, :, 0, :]
c = x[:, :, H - 1, :]
if H == 1:
ring = a
elif H == 2:
ring = torch.cat([a, c.flip(-1)], -1)
else:
b = x[:, :, 1:-1, -1]
d = x[:, :, 1:-1, 0].flip(-1)
ring = torch.cat([a, b, c.flip(-1), d], -1)
if transposed:
ring = ring.flip(-1).roll(1, -1)
return ring
kl, dist = 0, None
N, C, H, W = x.shape
if H % 2 == 0:
tanchor = H // 2 - 1
banchor = H // 2 + 1
else:
tanchor = H // 2
banchor = H // 2 + 1
lanchor = tanchor
ranchor = W - tanchor
while tanchor >= 0:
y = x[:, :, tanchor:banchor, lanchor:ranchor]
if dist is None:
kl += -self.init_prior(y).log2().reshape(N, -1).sum(1)
else:
ring = get_ring(y)
kl += -dist.likelihood(ring).log2().reshape(N, -1).sum(1)
h = self.conv(y)
mu = self.loc_conv(h)
sigma = self.scale_conv(h)
mu = get_ring(mu)
sigma = get_ring(sigma).exp()
dist = NormalExt(mu, sigma)
tanchor -= 1
banchor += 1
lanchor -= 1
ranchor += 1
return kl
class LSTMPrior(nn.Module):
'''
Auto-regressive prior modeled by a ConvLSTM
'''
def __init__(self, filter_size, rnn_hidden_size):
super(LSTMPrior, self).__init__()
self.lstm = ConvLSTMCell(filter_size, rnn_hidden_size, 3)
self.loc_conv = nn.Conv2d(rnn_hidden_size, filter_size, 3, 1, 1)
self.scale_conv = nn.Conv2d(rnn_hidden_size, filter_size, 3, 1, 1)
def forward(self, h, hidden=None):
if hidden is None:
h, c = self.lstm(h)
else:
h, c = self.lstm(h, hidden)
loc = self.loc_conv(h)
scale = self.scale_conv(h).exp()
return NormalExt(loc, scale), (h, c)
class MarkovPrior(nn.Module):
'''
markov autoregressive prior
'''
def __init__(self, filter_size, hyper_filter_size, img_c):
super(MarkovPrior, self).__init__()
self.loc_conv = nn.Sequential(nn.Conv2d(img_c, filter_size, 4, 2, 1), nn.ReLU(True),
nn.Conv2d(filter_size, filter_size, 4, 2, 1), nn.ReLU(True),
nn.Conv2d(filter_size, filter_size, 4, 2, 1), nn.ReLU(True),
nn.Conv2d(filter_size, hyper_filter_size, 4, 2, 1))
self.scale_conv = nn.Sequential(nn.Conv2d(img_c, filter_size, 4, 2, 1), nn.ReLU(True),
nn.Conv2d(filter_size, filter_size, 4, 2, 1), nn.ReLU(True),
nn.Conv2d(filter_size, filter_size, 4, 2, 1), nn.ReLU(True),
nn.Conv2d(filter_size, hyper_filter_size, 4, 2, 1))
def forward(self, x):
loc = self.loc_conv(x)
scale = LowerBound.apply(self.scale_conv(x).exp(), 0.11)
return NormalExt(loc, scale)
class Posterior(nn.Module):
'''
Posterior for P frames
'''
def __init__(self, filter_size=None):
super(Posterior, self).__init__()
def forward(self, h):
return Uniform(h - 0.5, h + 0.5)
class InitPosterior(nn.Module):
'''
Posterior for I frames
'''
def __init__(self, img_filter_size=None):
super(InitPosterior, self).__init__()
# self.loc_conv = nn.Conv2d(img_filter_size, img_filter_size, 3, 1, 1)
def forward(self, loc):
# loc = self.loc_conv(loc)
return Uniform(loc - 0.5, loc + 0.5)