-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_dm.py
414 lines (368 loc) · 13.8 KB
/
train_dm.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import os
import copy
import time
import pickle
import argparse
import wandb
import numpy as np
import torch
from torch.utils.data import DataLoader
from cfgs.cfgs_train_dm import parse_arguments
from datasets.dataloader import (
ModelNet40C,
PointDA10,
GraspNet10,
ImbalancedDatasetSampler,
)
from diffusion import diffusion, utils as flow_utils
from diffusion.build_model import get_model, get_optim
from utils import losses, utils, visualizer as vis
def sample(
args, device, generative_model, nodesxsample=torch.tensor([10]), fix_noise=False
):
max_n_nodes = args.n_nodes
assert int(torch.max(nodesxsample)) <= max_n_nodes
batch_size = len(nodesxsample)
node_mask = torch.ones(batch_size, max_n_nodes).unsqueeze(2).to(device)
if args.probabilistic_model == "diffusion":
x = generative_model.sample(
batch_size, max_n_nodes, node_mask, fix_noise=fix_noise
)
else:
raise ValueError(args.probabilistic_model)
return x
def train_epoch(
args,
loader,
epoch,
model,
model_dp,
model_ema,
ema,
device,
dtype,
optim,
gradnorm_queue,
):
model_dp.train()
model.train()
nll_epoch = []
n_iterations = len(loader)
for i, data in enumerate(loader):
x = data[0].to(device, dtype)
# transform batch through flow
nll = loss = losses.compute_loss_and_nll(args, model_dp, x)
# standard nll from forward KL
loss = loss / args.accum_grad
loss.backward()
if args.clip_grad:
grad_norm = utils.gradient_clipping(model, gradnorm_queue)
else:
grad_norm = 0.0
if (i + 1) % args.accum_grad == 0:
optim.step()
optim.zero_grad()
# Update EMA if enabled.
if args.ema_decay > 0:
ema.update_model_average(model_ema, model)
if i % args.n_report_steps == 0:
print(
f"\rEpoch: {epoch}, iter: {i}/{n_iterations}, "
f"Loss {loss.item():.2f}, "
f"GradNorm: {grad_norm:.1f}"
)
nll_epoch.append(nll.item())
if (
(epoch % args.test_epochs == 0)
and (i % args.visualize_every_batch == 0)
and not (epoch == 0 and i == 0)
):
start = time.time()
sample_and_save(
model_ema, args, device, epoch=epoch, batch_id=str(i) + "_ema"
)
sample_and_save(model, args, device, epoch=epoch, batch_id=str(i))
print(f"Sampling took {time.time() - start:.2f} seconds")
obj3d = wandb.Object3D(
{
"type": "lidar/beta",
"points": x[0].cpu().numpy().reshape(-1, 3),
"boxes": np.array(
[
{
"corners": (
np.array(
[
[-1, -1, -1],
[-1, 1, -1],
[-1, -1, 1],
[1, -1, -1],
[1, 1, -1],
[-1, 1, 1],
[1, -1, 1],
[1, 1, 1],
]
)
* 3
).tolist(),
"label": "Box",
"color": [123, 321, 111], # ???
}
]
),
}
)
wandb.log({"3d_example": obj3d})
vis.visualize(f"outputs/{args.exp_name}/epoch_{epoch}_{i}", wandb=wandb)
vis.visualize(
f"outputs/{args.exp_name}/epoch_{epoch}_{i}_ema",
wandb=wandb,
postfix="_ema",
)
wandb.log({"Batch NLL": nll.item()}, commit=True)
wandb.log({"Train Epoch NLL": np.mean(nll_epoch)}, commit=False)
if (i + 1) % args.accum_grad:
optim.step()
optim.zero_grad()
def test(args, loader, epoch, eval_model, device, dtype, partition="Test"):
eval_model.eval()
with torch.no_grad():
nll_epoch = 0
n_samples = 0
n_iterations = len(loader)
for i, data in enumerate(loader):
x = data[0].to(device, dtype)
node_mask = None
batch_size = x.size(0)
# transform batch through flow
nll = losses.compute_loss_and_nll(args, eval_model, x)
# standard nll from forward KL
nll_epoch += nll.item() * batch_size
n_samples += batch_size
if i % args.n_report_steps == 0:
print(
f"\r {partition} NLL \t epoch: {epoch}, iter: {i}/{n_iterations}, "
f"NLL: {nll_epoch/n_samples:.2f}"
)
return nll_epoch / n_samples
def sample_and_save(model, args, device, n_samples=5, epoch=0, batch_id=""):
model.eval()
nodesxsample = torch.tensor([args.n_nodes] * n_samples)
x = sample(args, device, model, nodesxsample=nodesxsample)
vis.save_xyz_file(
f"outputs/{args.exp_name}/epoch_{epoch}_{batch_id}/",
x,
name="pointcloud",
n_nodes=args.n_nodes,
)
model.train()
def main(args):
if args.dataset.startswith("modelnet40"):
dataset_ = ModelNet40C(args, partition="train")
dataset_val = ModelNet40C(args, partition="test")
elif args.dataset in ["modelnet", "shapenet", "scannet"]:
dataset_ = PointDA10(args=args, partition="train")
dataset_val = PointDA10(args=args, partition="val")
elif args.dataset in ["synthetic", "kinect", "realsense"]:
dataset_ = GraspNet10(args=args, partition="train")
dataset_val = GraspNet10(args=args, partition="val")
else:
raise ValueError("UNDEFINED DATASET")
train_loader = DataLoader(
dataset_,
batch_size=args.batch_size,
sampler=None if not args.cls_uniform else ImbalancedDatasetSampler(dataset_),
drop_last=True,
shuffle=True if not args.cls_uniform else False,
num_workers=args.num_workers,
)
val_loader = DataLoader(
dataset_val,
batch_size=args.batch_size,
shuffle=False,
drop_last=False,
num_workers=args.num_workers,
)
args.cuda = not args.no_cuda and torch.cuda.is_available()
device = torch.device("cuda" if args.cuda else "cpu")
dtype = torch.float32
if args.resume is not None:
exp_name = args.exp_name + "_resume"
start_epoch = args.start_epoch
resume = args.resume
wandb_usr = args.wandb_usr
try:
with open(os.path.join(args.resume, "args.pickle"), "rb") as f:
args_ = pickle.load(f)
except:
pass
args.resume = resume
args.exp_name = exp_name
args.start_epoch = start_epoch
args.wandb_usr = wandb_usr
print(args)
utils.create_folders(args)
# Wandb config
if args.no_wandb:
mode = "disabled"
else:
mode = "online" if args.online else "offline"
kwargs = {
"entity": args.wandb_usr,
"name": args.exp_name,
"project": "pc_diffusionTTA",
"config": args,
"settings": wandb.Settings(_disable_stats=True),
"reinit": True,
"mode": mode,
}
wandb.init(**kwargs)
wandb.save("*.txt")
# Create Model
model = get_model(args, device)
model = model.to(device)
optim = get_optim(args, model)
gradnorm_queue = utils.Queue()
gradnorm_queue.add(3000) # Add large value that will be flushed.
if args.lr_gamma < 1:
lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optim, args.lr_gamma)
if args.resume is not None:
flow_state_dict = torch.load(
os.path.join(args.resume, "generative_model_last.npy")
)
optim_state_dict = torch.load(os.path.join(args.resume, "optim_last.npy"))
if args.lr_gamma < 1:
scheduler_state_dict = torch.load(
os.path.join(args.resume, "lr_scheduler_last.npy")
)
lr_scheduler.load_state_dict(scheduler_state_dict)
model.load_state_dict(flow_state_dict)
optim.load_state_dict(optim_state_dict)
print("Resume")
# Initialize dataparallel if enabled and possible.
if args.dp and torch.cuda.device_count() > 1:
print(f"Training using {torch.cuda.device_count()} GPUs")
model_dp = torch.nn.DataParallel(model.cpu())
model_dp = model_dp.cuda()
else:
model_dp = model
# Initialize model copy for exponential moving average of params.
if args.ema_decay > 0:
model_ema = copy.deepcopy(model)
ema = flow_utils.EMA(args.ema_decay)
if args.resume is not None:
ema_state_dict = torch.load(
os.path.join(args.resume, "generative_model_ema_last.npy")
)
model_ema.load_state_dict(ema_state_dict)
if args.dp and torch.cuda.device_count() > 1:
model_ema_dp = torch.nn.DataParallel(model_ema)
else:
model_ema_dp = model_ema
else:
ema = None
model_ema = model
model_ema_dp = model_dp
utils.set_seed(args.random_seed)
best_nll_val = float("inf")
for epoch in range(args.start_epoch, args.n_epochs):
if args.lr_gamma < 1:
print("LR: ", lr_scheduler.get_last_lr())
start_time = time.time()
train_epoch(
args=args,
loader=train_loader,
epoch=epoch,
model=model,
model_dp=model_dp,
model_ema=model_ema,
ema=ema,
device=device,
dtype=dtype,
gradnorm_queue=gradnorm_queue,
optim=optim,
)
print(f"Epoch took {time.time() - start_time:.1f} seconds.")
if args.lr_gamma < 1:
lr_scheduler.step()
if hasattr(model.dynamics, "report_neighbor_stats"):
pass
model.dynamics.report_neighbor_stats()
if epoch % args.test_epochs == 0 and epoch > 0:
if isinstance(model, diffusion.DiffusionModel):
wandb.log(model.log_info(), commit=True)
nll_val = test(
args=args,
loader=val_loader,
epoch=epoch,
eval_model=model_ema_dp,
partition="Val",
device=device,
dtype=dtype,
)
if nll_val < best_nll_val:
best_nll_val = nll_val
if args.save_model:
args.current_epoch = epoch + 1
if args.lr_gamma < 1:
utils.save_model(
lr_scheduler, f"{args.output_dir}/%s/lr_scheduler.npy" % args.exp_name
)
utils.save_model(optim, f"{args.output_dir}/%s/optim.npy" % args.exp_name)
utils.save_model(
model, f"{args.output_dir}/%s/generative_model.npy" % args.exp_name
)
if args.ema_decay > 0:
utils.save_model(
model_ema,
f"{args.output_dir}/%s/generative_model_ema.npy" % args.exp_name,
)
with open(f"{args.output_dir}/%s/args.pickle" % args.exp_name, "wb") as f:
pickle.dump(args, f)
if args.save_model:
utils.save_model(
optim, f"{args.output_dir}/%s/optim_%d.npy" % (args.exp_name, epoch)
)
utils.save_model(
model,
f"{args.output_dir}/%s/generative_model_%d.npy" % (args.exp_name, epoch),
)
if args.ema_decay > 0:
utils.save_model(
model_ema,
f"{args.output_dir}/%s/generative_model_ema_%d.npy"
% (args.exp_name, epoch),
)
with open(
f"{args.output_dir}/%s/args_%d.pickle" % (args.exp_name, epoch), "wb"
) as f:
pickle.dump(args, f)
if args.save_model:
if args.lr_gamma < 1:
utils.save_model(
lr_scheduler,
f"{args.output_dir}/%s/lr_scheduler_%s.npy" % (args.exp_name, "last"),
)
utils.save_model(
optim, f"{args.output_dir}/%s/optim_%s.npy" % (args.exp_name, "last")
)
utils.save_model(
model,
f"{args.output_dir}/%s/generative_model_%s.npy" % (args.exp_name, "last"),
)
if args.ema_decay > 0:
utils.save_model(
model_ema,
f"{args.output_dir}/%s/generative_model_ema_%s.npy"
% (args.exp_name, "last"),
)
with open(
f"{args.output_dir}/%s/args_%s.pickle" % (args.exp_name, "last"), "wb"
) as f:
pickle.dump(args, f)
print("Val loss: %.4f" % nll_val)
print("Best val loss: %.4f" % best_nll_val)
wandb.log({"Val loss ": nll_val}, commit=True)
if __name__ == "__main__":
args = parse_arguments()
main(args)