-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_experiments.py
316 lines (259 loc) · 12.1 KB
/
plot_experiments.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
import argparse
from datasets import celeb_dataset_loader, cifar10_data_loader
from jax import random
from experiments import Experiment
import os
import yaml
import evaluate_experiments
import pathlib
if(__name__ == '__main__'):
# Load the command line arguments
parser = argparse.ArgumentParser(description='Plot comparisons between models')
parser.add_argument('--names', nargs='+', help='Experiments to compare', required=True)
parser.add_argument('--quantize',
action='store',
type=int,
help='The number of bits to use in quantization',
default=3)
parser.add_argument('--experiment_root',
action='store',
type=str,
help='The root directory of the experiments folder',
default='Experiments')
parser.add_argument('--checkpoint',
action='store',
type=int,
help='What checkpoint to use',
default=-1)
parser.add_argument('--results_root',
action='store',
type=str,
help='The root directory of the results folder',
default='Results')
parser.add_argument('--basic_samples',
action='store_true',
help='')
parser.add_argument('--figure2',
action='store_true',
help='')
parser.add_argument('--compare_baseline_samples',
action='store_true',
help='')
parser.add_argument('--compare_vertical',
action='store_true',
help='')
parser.add_argument('--compare_samples',
action='store_true',
help='')
parser.add_argument('--compare_full_samples',
action='store_true',
help='')
parser.add_argument('--reconstructions',
action='store_true',
help='')
parser.add_argument('--compare_reconstructions',
action='store_true',
help='')
parser.add_argument('--vary_t',
action='store_true',
help='')
parser.add_argument('--vary_s',
action='store_true',
help='')
parser.add_argument('--compare_t',
action='store_true',
help='')
parser.add_argument('--interpolations',
action='store_true',
help='')
parser.add_argument('--best_s_for_nll',
action='store_true',
help='')
parser.add_argument('--nll_comparison',
action='store_true',
help='')
parser.add_argument('--manifold_penalty',
action='store_true',
help='')
parser.add_argument('--save_embedding',
action='store_true',
help='')
parser.add_argument('--plot_embeddings',
action='store_true',
help='')
parser.add_argument('--prob_diff',
action='store_true',
help='')
args = parser.parse_args()
# Load all of the experiments
all_experiments = []
results_folders = []
for i, name in enumerate(args.names):
exp = Experiment(name,
args.quantize,
None,
start_it=args.checkpoint,
experiment_root=args.experiment_root)
exp.load_experiment()
results_folders.append(os.path.join(args.results_root, '%s_%d'%(exp.experiment_name, exp.current_iteration)))
sampler = exp.get_jitted_sampler()
encoder = exp.get_jitted_forward()
decoder = exp.get_jitted_inverse()
all_experiments.append((exp, sampler, encoder, decoder))
# Save the plots to the results folder
folder_name = '_'.join(['%s_%d'%(exp.experiment_name, exp.current_iteration) for exp, _, _, _ in all_experiments])
results_folder = os.path.join(args.results_root, folder_name)
pathlib.Path(results_folder).mkdir(parents=True, exist_ok=True)
# Basic plots for appendix
if(args.basic_samples):
nf = all_experiments[0]
nif = all_experiments[1]
nf_path = os.path.join(results_folder, 'basic_samples_nf.pdf')
nif_s0_path = os.path.join(results_folder, 'basic_samples_nif_s0.pdf')
nif_s1_path = os.path.join(results_folder, 'basic_samples_nif_s1.pdf')
key = random.PRNGKey(0)
n_rows = 8
n_cols = 8
evaluate_experiments.plot_samples(key, nf, nf_path, n_rows, n_cols, n_samples_per_batch=8, sigma=1.0)
evaluate_experiments.plot_samples(key, nif, nif_s0_path, n_rows, n_cols, n_samples_per_batch=8, sigma=0.0)
evaluate_experiments.plot_samples(key, nif, nif_s1_path, n_rows, n_cols, n_samples_per_batch=8, sigma=1.0)
# Save Embeddings
if(args.save_embedding):
save_path = os.path.join(results_folder, 'embedding')
key = random.PRNGKey(0)
data_key = random.PRNGKey(0)
for experiment in all_experiments:
name = experiment[0].experiment_name
save_path = os.path.join(results_folder, '%s_test_embeddings.npz'%name)
evaluate_experiments.save_test_embeddings(key, experiment, save_path, n_samples_per_batch=64)
if(args.plot_embeddings):
embedding_paths = []
for experiment in all_experiments:
name = experiment[0].experiment_name
path = os.path.join(results_folder, '%s_test_embeddings.npz'%name)
embedding_paths.append(path)
save_path = os.path.join(results_folder, 'embedding_plot.pdf')
assert all_experiments[3][0].experiment_name == 'cifar_64'
titles = ['NF', 'NIF-64']
evaluate_experiments.plot_embeddings(embedding_paths, titles, save_path)
# Create figure 2
if(args.figure2):
n_samples = 8
key = random.PRNGKey(0)
keys = random.split(key, 10)
nf_exp = all_experiments[0]
assert nf_exp[0].is_nf == True, 'Need to pass the normalizing flow first'
for nif_exp in all_experiments[1:]:
for j, key in enumerate(keys):
z_dim = nif_exp[0].model.z_shape[0]
save_path = os.path.join(results_folder, 'dim_%d_figure_2_%d.pdf'%(z_dim, j))
evaluate_experiments.figure_2_plots(key, nf_exp, nif_exp, n_samples, save_path)
# Compare samples between a model and a baseline
if(args.compare_baseline_samples):
n_samples = 16
key = random.PRNGKey(0)
save_path = os.path.join(results_folder, 'compare_baseline_samples.pdf')
baseline_sampler = all_experiments[0][1]
sampler = all_experiments[1][1]
evaluate_experiments.compare_manifold_vs_full_samples(key, sampler, baseline_sampler, n_samples, save_path)
# Compare samples between the all of the models vertically
if(args.compare_vertical):
n_samples = 3
key = random.PRNGKey(1)
save_path = os.path.join(results_folder, 'compare_vertical.pdf')
evaluate_experiments.compare_vertical(key, all_experiments, n_samples, save_path)
# Compare samples between the all of the models
if(args.compare_samples):
n_samples = 12
key = random.PRNGKey(3)
save_path = os.path.join(results_folder, 'compare_samples.pdf')
evaluate_experiments.compare_samples(key, all_experiments, n_samples, save_path)
# Compare samples between the all of the models
if(args.compare_full_samples):
n_samples = 8
key = random.PRNGKey(0)
save_path = os.path.join(results_folder, 'compare_full_samples.pdf')
evaluate_experiments.compare_samples(key, all_experiments, n_samples, save_path, sigma=1.0)
# Plot reconstructions for each of the models
if(args.compare_reconstructions):
n_samples = 12
key = random.PRNGKey(0)
data_key = random.PRNGKey(3)
save_path = os.path.join(results_folder, 'compare_reconstructions.pdf')
evaluate_experiments.compare_reconstructions(data_key, key, all_experiments, save_path, n_samples, exp.quantize_level_bits)
# Plot reconstructions for each of the models
if(args.reconstructions):
n_samples = 8
key = random.PRNGKey(0)
data_key = random.PRNGKey(0)
for exp, _, encoder, decoder in all_experiments:
save_path = os.path.join(results_folder, 'reconstructions_%s.pdf'%exp.experiment_name)
evaluate_experiments.reconstructions(data_key, key, exp.data_loader, encoder, decoder, save_path, n_samples, exp.quantize_level_bits)
# Compare different temperature samples
if(args.vary_t):
n_samples = 8
key = random.PRNGKey(0)
data_key = random.PRNGKey(0)
save_path = os.path.join(results_folder, 'vary_t.pdf')
evaluate_experiments.samples_vary_t(data_key, key, all_experiments, n_samples, save_path, reuse_key=True)
# Compare different sigma samples
if(args.vary_s):
n_samples = 8
key = random.PRNGKey(0)
keys = random.split(key, 2)
fixed_key = random.PRNGKey(0)
for exp in all_experiments:
if(exp[0].is_nf):
continue
for j, key in enumerate(keys):
z_dim = exp[0].model.z_shape[0]
save_path = os.path.join(results_folder, 'dim_%d_vary_s_%d.pdf'%(z_dim, j))
evaluate_experiments.vary_s(key, fixed_key, exp, n_samples, save_path, reuse_key=True)
# # Compare different sigma samples
# if(args.vary_s):
# n_samples = 8
# key = random.PRNGKey(0)
# data_key = random.PRNGKey(0)
# save_path = os.path.join(results_folder, 'vary_s.pdf')
# evaluate_experiments.samples_vary_s(data_key, key, all_experiments, n_samples, save_path, reuse_key=True)
# Compare different temperature samples
if(args.compare_t):
n_samples = 8
key = random.PRNGKey(0)
data_key = random.PRNGKey(0)
glow = all_experiments[0]
for exp in all_experiments[1:]:
name = exp[0].experiment_name
for i, key in enumerate(random.split(key, 6)):
save_path = os.path.join(results_folder, '%s_compare_t_%d.pdf'%(name, i))
evaluate_experiments.compare_t(key, [glow, exp], n_samples, save_path)
# Plot some interpolations
if(args.interpolations):
n_pairs = 10
n_interp = 10
key = random.PRNGKey(0)
data_key = random.PRNGKey(0)
for experiment in all_experiments:
exp = experiment[0]
save_path = os.path.join(results_folder, 'interpolation_%s.pdf'%exp.experiment_name)
evaluate_experiments.interpolate_pairs(data_key, key, experiment, n_pairs, n_interp, save_path)
# Compute the best value of s
if(args.best_s_for_nll):
key = random.PRNGKey(0)
save_path = os.path.join(results_folder, 'best_s_for_nll.yaml')
evaluate_experiments.save_best_s_for_nll(key, all_experiments, save_path)
# Compare the log likelihoods
if(args.nll_comparison):
key = random.PRNGKey(0)
best_s_path = os.path.join(results_folder, 'best_s_for_nll.yaml')
save_path = os.path.join(results_folder, 'nll_comparison.yaml')
evaluate_experiments.validation_nll_from_best_s(key, all_experiments, best_s_path, save_path)
# See what images correspond to the varying manifold penalties
if(args.manifold_penalty):
key = random.PRNGKey(0)
evaluate_experiments.manifold_penalty(key, all_experiments[0][0], None)
if(args.prob_diff):
key = random.PRNGKey(0)
exp1, exp2 = all_experiments[0], all_experiments[1]
save_path = os.path.join(results_folder, 'test.yaml')
evaluate_experiments.save_probability_difference(key, exp1, exp2, save_path)