-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
367 lines (305 loc) · 12.2 KB
/
main.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python3
# install(suppress=[torch])
import os
from typing import Union
import numpy as np
import tqdm
from args import parse_args
from simple_einet.data import build_dataloader, get_data_shape
from simple_einet.dist import DataType, Dist, get_data_type_from_dist, Domain
from simple_einet.layers.distributions.categorical import Categorical
from simple_einet.layers.distributions.piecewise_linear import PiecewiseLinear
from simple_einet.utils import preprocess
import torch
from torch.nn import functional as F
import torchvision
from simple_einet.layers.distributions.binomial import Binomial
from simple_einet.layers.distributions.normal import RatNormal, Normal
from simple_einet.einet import Einet, EinetConfig
from simple_einet.mixture import Mixture
import lightning as L
def log_likelihoods(outputs, targets=None):
"""Compute the likelihood of an Einet."""
if targets is None:
num_roots = outputs.shape[-1]
if num_roots == 1:
lls = outputs
else:
num_roots = torch.tensor(float(num_roots), device=outputs.device)
lls = torch.logsumexp(outputs - torch.log(num_roots), -1)
else:
lls = outputs.gather(-1, targets.unsqueeze(-1))
return lls
def train(args, model: Union[Einet, Mixture], device, train_loader, optimizer, epoch):
model.train()
pbar = tqdm.tqdm(train_loader)
for batch_idx, (data, target) in enumerate(pbar):
# Stop after a few batches in debug mode
if args.debug and batch_idx > 2:
break
# Prepare data
data = preprocess(
data,
n_bits,
n_bins,
dequantize=True,
has_gauss_dist=has_gauss_dist,
)
optimizer.zero_grad()
# Generate outputs
outputs = model(data)
if args.classification:
model.posterior(data)
loss = F.nll_loss(outputs, target, reduction="mean")
else:
loss = log_likelihoods(outputs).mean()
loss = -1 * loss
# Compute gradients
fabric.backward(loss)
# Update weights
optimizer.step()
# Logging
if batch_idx % args.log_interval == 0:
if args.classification:
_, predicted = outputs.max(1)
correct = predicted.eq(target).sum().item()
acc_term = " Accuracy: {:.2f}".format(100.0 * correct / len(data))
else:
acc_term = ""
pbar.set_description(
"Train Epoch: {} [{}/{}] Loss: {:.2f}{}".format(
epoch,
batch_idx * len(data),
len(train_loader.dataset),
loss.item(),
acc_term,
)
)
if args.dry_run:
break
def test(model, device, loader, tag):
model.eval()
test_loss = 0
test_losses = []
if args.classification:
correct = 0
total = 0
with torch.no_grad():
for data, target in loader:
data = preprocess(
data,
n_bits,
n_bins,
dequantize=True,
has_gauss_dist=has_gauss_dist,
)
outputs = model(data)
lls = log_likelihoods(outputs)
test_loss += -1 * lls.sum()
test_losses += lls.squeeze().cpu().tolist()
if args.classification:
_, predicted = outputs.max(1)
total += target.size(0)
correct += predicted.eq(target).sum().item()
if args.classification:
print("Accuracy: {:.2f}".format(100.0 * correct / total))
test_loss /= len(loader.dataset)
print()
print("{} set: Average loss: {:.4f}".format(tag, test_loss))
print()
if __name__ == "__main__":
args = parse_args()
torch.manual_seed(args.seed)
n_bits = args.n_bits
n_bins = 2**n_bits
device = torch.device(args.device)
# digits = [0, 1, 5, 8]
digits = list(range(10))
# digits = [0, 1]
# Construct Einet
num_classes = len(digits) if args.classification else 1
if args.dist == "binomial":
leaf_type = Binomial
leaf_kwargs = {"total_count": n_bins - 1}
elif args.dist == "normal":
leaf_type = Normal
leaf_kwargs = {}
elif args.dist == "normal_rat":
leaf_type = RatNormal
leaf_kwargs = {"min_sigma": args.min_sigma, "max_sigma": args.max_sigma}
elif args.dist == "categorical":
leaf_type = Categorical
leaf_kwargs = {"num_bins": n_bins}
elif args.dist == "piecewise_linear":
leaf_type = PiecewiseLinear
leaf_kwargs = {}
# num_classes = 18
data_shape = get_data_shape(args.dataset)
num_features = np.prod(data_shape[1:])
config = EinetConfig(
num_features=num_features,
num_channels=data_shape[0],
depth=args.D,
num_sums=args.S,
num_leaves=args.I,
num_repetitions=args.R,
num_classes=num_classes,
leaf_type=leaf_type,
leaf_kwargs=leaf_kwargs,
layer_type=args.layer,
dropout=0.0,
structure=args.structure,
)
fabric = L.Fabric(accelerator=args.device, devices=args.num_devices, precision="16-mixed")
fabric.launch()
model = Einet(config)
print(
"Number of parameters:",
sum(p.numel() for p in model.parameters() if p.requires_grad),
)
has_gauss_dist = type(model.leaf.base_leaf) in (Normal, RatNormal)
# Optimize Einet parameters (weights and leaf params)
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=2, gamma=1e-1, verbose=True)
model, optimizer = fabric.setup(model, optimizer)
print(model)
home_dir = os.getenv("HOME")
result_dir = os.path.join(home_dir, "results", "simple-einet", args.dataset)
os.makedirs(result_dir, exist_ok=True)
data_dir = os.path.join("~", "data")
train_loader, val_loader, test_loader = build_dataloader(
dataset_name=args.dataset,
batch_size=args.batch_size,
data_dir=data_dir,
num_workers=os.cpu_count(),
normalize=False,
loop=False,
seed=args.seed,
)
train_loader, val_loader, test_loader = fabric.setup_dataloaders(train_loader, val_loader, test_loader)
if args.train:
for epoch in range(1, args.epochs + 1):
train(args, model, device, train_loader, optimizer, epoch)
# lr_scheduler.step()
torch.save(model.state_dict(), os.path.join(result_dir, "model.pth"))
# test(model, device, train_loader, "Train")
# test(model, device, val_loader, "Val")
# test(model, device, test_loader, "Test")
else:
model.load_state_dict(torch.load(os.path.join(result_dir, "model.pth")))
test(model, device, train_loader, "Train")
test(model, device, val_loader, "Val")
test(model, device, test_loader, "Test")
# Don't sample when doing classification
if not args.classification:
model.eval()
################
# ground-truth #
################
test_x, _ = next(iter(test_loader))
test_x = test_x[:100]
test_x = preprocess(
test_x,
n_bits,
n_bins,
dequantize=False,
has_gauss_dist=has_gauss_dist,
).float()
if not has_gauss_dist:
grid_kwargs = dict(nrow=10, normalize=True, padding=1, pad_value=1.0)
else:
grid_kwargs = dict(
nrow=10,
normalize=True,
value_range=(-0.5, 0.5),
padding=1,
pad_value=1.0,
)
grid = torchvision.utils.make_grid(test_x.view(-1, *data_shape), **grid_kwargs)
torchvision.utils.save_image(grid, os.path.join(result_dir, "ground_truth.png"))
#######################
# Some random samples #
#######################
for diff in [False, True]:
suffix = "-diff" if diff else ""
for mpe_at_leaves in [False, True]:
suffix_mpe_at_leaves = "-mpe-leaves" if mpe_at_leaves else ""
if type(model._original_module) == Einet:
samples = model.sample(
num_samples=100,
temperature_sums=args.temperature_sums,
temperature_leaves=args.temperature_leaves,
is_differentiable=diff,
mpe_at_leaves=mpe_at_leaves,
seed=0,
)
else:
samples = model.sample(
num_samples_per_cluster=8,
temperature_sums=args.temperature_sums,
temperature_leaves=args.temperature_leaves,
mpe_at_leaves=mpe_at_leaves,
seed=0,
)
samples = samples.view(-1, *data_shape)
if not has_gauss_dist:
samples = samples / n_bins
grid = torchvision.utils.make_grid(samples, **grid_kwargs)
torchvision.utils.save_image(
grid, os.path.join(result_dir, f"samples{suffix}{suffix_mpe_at_leaves}.png")
)
###################
# reconstructions #
###################
image_scope = np.array(range(np.prod(list(data_shape)))).reshape(data_shape)
marginalized_scopes = list(image_scope[:, 0 : round(data_shape[-1] / 2), :].reshape(-1))
num_samples = 1
reconstructions = None
for k in range(num_samples):
if reconstructions is None:
reconstructions = model.sample(
evidence=test_x,
temperature_leaves=args.temperature_leaves,
marginalized_scopes=marginalized_scopes,
mpe_at_leaves=mpe_at_leaves,
is_differentiable=diff,
seed=0,
).cpu()
else:
reconstructions += model.sample(
evidence=test_x,
temperature_leaves=args.temperature_leaves,
marginalized_scopes=marginalized_scopes,
mpe_at_leaves=mpe_at_leaves,
is_differentiable=diff,
seed=0,
).cpu()
reconstructions = reconstructions.float() / num_samples
if not has_gauss_dist:
reconstructions = reconstructions / n_bins
reconstructions = reconstructions.squeeze()
reconstructions = reconstructions.view(-1, *data_shape)
grid = torchvision.utils.make_grid(reconstructions, **grid_kwargs)
torchvision.utils.save_image(
grid, os.path.join(result_dir, f"reconstructions{suffix}{suffix_mpe_at_leaves}.png")
)
#######
# MPE #
#######
mpe = model.mpe(evidence=None, is_differentiable=diff)
mpe = mpe.view(-1, *data_shape)
torchvision.utils.save_image(mpe, os.path.join(result_dir, f"mpe{suffix}.png"), **grid_kwargs)
#######################
# reconstructions-mpe #
#######################
reconstructions_mpe = model.mpe(
evidence=test_x, marginalized_scopes=marginalized_scopes, is_differentiable=diff
).cpu()
if not has_gauss_dist:
reconstructions_mpe = reconstructions_mpe / n_bins
reconstructions_mpe = reconstructions_mpe.squeeze()
reconstructions_mpe = reconstructions_mpe.view(-1, *data_shape)
grid = torchvision.utils.make_grid(reconstructions_mpe, **grid_kwargs)
torchvision.utils.save_image(grid, os.path.join(result_dir, f"reconstructions_mpe{suffix}.png"))
print(f"Result directory: {result_dir}")
print("Done.")