-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixelcnn_surrogate_posterior.py
196 lines (167 loc) · 7.71 KB
/
pixelcnn_surrogate_posterior.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
import random
import os
import pickle
import time
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_probability as tfp
from tensorflow_probability.python.internal import prefer_static as ps
import pixelcnn_original
from metrics import negative_elbo, forward_kl
from surrogate_posteriors import get_surrogate_posterior
import surrogate_posteriors
tfd = tfp.distributions
tfb = tfp.bijectors
tfk = tf.keras
tfkl = tf.keras.layers
Root = tfd.JointDistributionCoroutine.Root
image_side_size = 28
image_shape = (image_side_size, image_side_size, 1)
dist = pixelcnn_original.PixelCNN(
image_shape=image_shape,
num_resnet=1,
num_hierarchies=2,
num_filters=32,
num_logistic_mix=5,
dropout_p=.3,
use_weight_norm=False,
low=-1,
high=1
)
dist.network.load_weights(f'pcnn_weights/MNIST_{image_side_size}/')
seed = 15
def pixelcnn_as_jd(image_side_size=28,
num_observed_pixels=5, seed=None):
@tf.function
def forward_step(sampled_image, s, update_idxs):
if tf.is_tensor(s):
s = tf.clip_by_value(s, -1., 1.)
if len(ps.shape(s)) <= 1:
s = [s]
sampled_image = tf.tensor_scatter_nd_update(sampled_image,
update_idxs,
tf.unstack(s))
num_logistic_mix, locs, scales = dist.network(sampled_image)
return sampled_image, num_logistic_mix, locs, scales
def sample_channels(component_logits, locs, scales, row, col):
if row == 0 and col == 0:
return tfd.MixtureSameFamily(mixture_distribution=tfd.Categorical(logits=component_logits[0, row, col]),
components_distribution=tfd.Independent(tfd.Normal(loc=locs[0, row, col], scale=scales[0, row, col]), reinterpreted_batch_ndims=1),
name=f"pixel_{row}_{col}")
else:
return tfd.MixtureSameFamily(mixture_distribution=tfd.Categorical(logits=component_logits[:, row, col]),
components_distribution=tfd.Independent(tfd.Normal(loc=locs[:, row, col], scale=scales[:, row, col]), reinterpreted_batch_ndims=1),
name=f"pixel_{row}_{col}")
@tfd.JointDistributionCoroutine
def model() -> object:
sampled_image = tf.zeros([1, image_side_size, image_side_size, 1])
s = None
batch_size = 0
for i in range(image_side_size):
for j in range(image_side_size):
sampled_image, num_logistic_mix, locs, scales = forward_step(sampled_image, s, [[b, i, j] for b in
range(batch_size)])
next_pixel = sample_channels(num_logistic_mix, locs, scales, row=i,
col=j)
if i == 0 and j == 0:
s = yield Root(next_pixel)
if len(ps.shape(s)) > 1:
batch_size = ps.shape(s)[0]
sampled_image = tf.repeat(sampled_image, batch_size, 0)
else:
batch_size = 1
else:
s = yield next_pixel
if ps.shape(s)[0] < batch_size:
s = tf.repeat(s, batch_size, 0)
'''@tfd.JointDistributionCoroutine
def model() -> object:
sampled_image = tf.zeros([1, image_side_size, image_side_size, 1])
for i in range(image_side_size):
for j in range(image_side_size):
num_logistic_mix, locs, scales = network(sampled_image)
next_pixel = sample_channels(num_logistic_mix, locs, scales, row=i,
col=j)
if i == 0 and j == 0:
s = yield Root(next_pixel)
if len(ps.shape(s)) > 1:
batch_size = ps.shape(s)[0]
sampled_image = tf.repeat(sampled_image, batch_size, 0)
else:
batch_size = 1
else:
s = yield next_pixel
s = tf.clip_by_value(s, -1., 1.)
if len(ps.shape(s)) <= 1:
s = [s]
if ps.shape(s)[0] < batch_size:
s = tf.repeat(s, batch_size, 0)
sampled_image = tf.tensor_scatter_nd_update(sampled_image,
[[b, i, j] for b in
range(batch_size)],
[s[c] for c in
range(batch_size)])'''
ground_truth = model.sample(1, seed=seed)
random.seed(seed)
observations_idx = sorted(tf.math.top_k(tf.squeeze(ground_truth), k=num_observed_pixels, sorted=False).indices.numpy()) # assuming squared images
observations = {f'pixel_{i//image_side_size}_{i%image_side_size}': ground_truth[i] for i in observations_idx}
pixelcnn_prior = model.experimental_pin(**observations)
observations = [tf.squeeze(o) for o in observations.values()]
ground_truth_idx = [i for i in range(image_side_size ** 2) if
i not in observations_idx]
def log_prob(*samples):
batch_size = ps.shape(samples[0])[0]
s = tf.squeeze(tf.convert_to_tensor(samples), -1)
o = tf.repeat(tf.expand_dims(tf.convert_to_tensor(observations), axis=1),
batch_size, 1)
image_tensor = tf.reshape(tf.transpose(tf.clip_by_value(tf.dynamic_stitch([ground_truth_idx, observations_idx], [s,o]), -1,1), [1,0]), [-1, image_side_size, image_side_size])
return dist.log_prob(tf.expand_dims(image_tensor, -1))
return pixelcnn_prior, [ground_truth[i] for i in
ground_truth_idx], log_prob, observations, ground_truth_idx, observations_idx
prior, ground_truth, target_log_prob, observations, ground_truth_idx, observations_idx = pixelcnn_as_jd(image_side_size=image_side_size, num_observed_pixels=5,
seed=seed)
surrogate_posterior_name = 'normalizing_program'
backbone_posterior_name = 'iaf'
num_steps = 100
surrogate_posterior = get_surrogate_posterior(prior, surrogate_posterior_name,
backbone_posterior_name)
surrogate_posterior.sample()
trainable_variables = list(surrogate_posterior.trainable_variables)
trainable_variables.extend(surrogate_posteriors.residual_fraction_vars.values())
print(surrogate_posteriors.residual_fraction_vars)
dist.network.trainable = False
start = time.time()
losses = tfp.vi.fit_surrogate_posterior(target_log_prob,
surrogate_posterior,
optimizer=tf.keras.optimizers.Adam(
learning_rate=5e-5),
# , gradient_transformers=[scale_grad_by_factor]),
num_steps=num_steps,
sample_size=10,
trainable_variables=trainable_variables)
print(surrogate_posteriors.residual_fraction_vars)
print(f'Time taken: {time.time()-start}')
'''plt.plot(losses)
plt.show()'''
elbo = negative_elbo(target_log_prob, surrogate_posterior, num_samples=10,
seed=seed)
fkl = forward_kl(surrogate_posterior, ground_truth)
print(f'ELBO: {elbo}')
print(f'FORWARD_KL: {fkl}')
ground_truth = [tf.squeeze(g) for g in ground_truth]
samples = tf.convert_to_tensor(surrogate_posterior.sample(10))
results = {'loss': losses,
'elbo': elbo,
'fkl': fkl,
'ground_truth': ground_truth,
'observations': observations,
'ground_truth_idx': ground_truth_idx,
'observations_idx': observations_idx,
'samples': samples}
if backbone_posterior_name:
surrogate_posterior_name = f'{surrogate_posterior_name}_{backbone_posterior_name}'
repo_name = f'pixelcnn/{surrogate_posterior_name}'
if not os.path.exists(repo_name):
os.makedirs(repo_name)
with open(f'{repo_name}/res.pickle', 'wb') as handle:
pickle.dump(results, handle, protocol=pickle.HIGHEST_PROTOCOL)