-
Notifications
You must be signed in to change notification settings - Fork 9
/
train_reflow.py
549 lines (417 loc) · 19.7 KB
/
train_reflow.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import torch.multiprocessing as mp
import torch.nn as nn
import torch.optim as optim
import torch.utils.data
import argparse
from torch.distributions import Normal
from utils.file_utils import *
from utils.visualize import *
from model.pvcnn_generation import PVCNN2Base
import torch.distributed as dist
from datasets.shapenet_data_pc import ShapeNet15kPointClouds
class Flowmodel:
def __init__(self, opt):
self.num_timesteps = opt.time_num
return
def p_mean(self, denoise_fn, data, t):
model_output = denoise_fn(data, t)
model_mean = data + model_output * 1 / self.num_timesteps
return model_mean
''' samples '''
def p_sample(self, denoise_fn, data, t, noise_fn, clip_denoised=False, return_pred_xstart=False):
"""
Sample from the model
"""
model_mean = self.p_mean(denoise_fn, data=data, t=t)
return model_mean
def p_sample_loop(self, denoise_fn, shape, device,
noise_fn=torch.randn, clip_denoised=True, keep_running=False):
"""
Generate samples
keep_running: True if we run 2 x num_timesteps, False if we just run num_timesteps
"""
assert isinstance(shape, (tuple, list))
img_t = noise_fn(size=shape, dtype=torch.float, device=device)
for t in range(self.num_timesteps):
t_ = torch.empty(shape[0], dtype=torch.int64, device=device).fill_(t)
img_t = self.p_sample(denoise_fn=denoise_fn, data=img_t,t=t_, noise_fn=noise_fn,
clip_denoised=clip_denoised, return_pred_xstart=False)
assert img_t.shape == shape
return img_t
def p_sample_loop_trajectory(self, denoise_fn, shape, device, freq,
noise_fn=torch.randn,clip_denoised=True, keep_running=False):
"""
Generate samples, returning intermediate images
Useful for visualizing how denoised images evolve over time
Args:
repeat_noise_steps (int): Number of denoising timesteps in which the same noise
is used across the batch. If >= 0, the initial noise is the same for all batch elemements.
"""
assert isinstance(shape, (tuple, list))
total_steps = self.num_timesteps if not keep_running else len(self.betas)
img_t = noise_fn(size=shape, dtype=torch.float, device=device)
imgs = [img_t]
for t in range(self.num_timesteps):
t_ = torch.empty(shape[0], dtype=torch.int64, device=device).fill_(t)
img_t = self.p_sample(denoise_fn=denoise_fn, data=img_t, t=t_, noise_fn=noise_fn,
clip_denoised=clip_denoised,
return_pred_xstart=False)
if t % freq == 0 or t == total_steps-1:
imgs.append(img_t)
assert imgs[-1].shape == shape
return imgs
@torch.no_grad()
def sample_pairs(self, x0 = None, x1 = None):
if x0 is None:
x0 = torch.randn_like(data)
data = x1
z0 = x0
t= torch.rand((data.shape[0], 1, 1)).to(data.device)
inter_data = t * data + (1.-t) * z0
target = data - z0
return inter_data, t * 999, target
def p_losses(self, denoise_fn, data_start, t, noise=None):
"""
Training loss calculation
"""
x0, x1 = data_start
data_start = x1
inter_data, t, target = self.sample_pairs(x0 = x0, x1 = x1)
t = t.squeeze()
data_t = inter_data
eps_recon = denoise_fn(data_t, t)
losses = ((target - eps_recon)**2).mean(dim=list(range(1, len(data_start.shape))))
return losses
class PVCNN2(PVCNN2Base):
sa_blocks = [
((32, 2, 32), (1024, 0.1, 32, (32, 64))),
((64, 3, 16), (256, 0.2, 32, (64, 128))),
((128, 3, 8), (64, 0.4, 32, (128, 256))),
(None, (16, 0.8, 32, (256, 256, 512))),
]
fp_blocks = [
((256, 256), (256, 3, 8)),
((256, 256), (256, 3, 8)),
((256, 128), (128, 2, 16)),
((128, 128, 64), (64, 2, 32)),
]
def __init__(self, num_classes, embed_dim, use_att,dropout, extra_feature_channels=3, width_multiplier=1,
voxel_resolution_multiplier=1):
super().__init__(
num_classes=num_classes, embed_dim=embed_dim, use_att=use_att,
dropout=dropout, extra_feature_channels=extra_feature_channels,
width_multiplier=width_multiplier, voxel_resolution_multiplier=voxel_resolution_multiplier
)
class Model(nn.Module):
def __init__(self, args, betas, loss_type: str, model_mean_type: str, model_var_type:str):
super(Model, self).__init__()
self.flow = Flowmodel(args)
self.model = PVCNN2(num_classes=args.nc, embed_dim=args.embed_dim, use_att=args.attention,
dropout=args.dropout, extra_feature_channels=0)
def _denoise(self, data, t):
B, D,N= data.shape
out = self.model(data, t)
return out
def get_loss_iter(self, data, noises=None):
x0, x1 = data
data = x0
B, D, N = data.shape
t = torch.randint(0, 1000, size=(B,), device=data.device)
if noises is not None:
noises[t!=0] = torch.randn((t!=0).sum(), *noises.shape[1:]).to(noises)
data = [x0, x1]
losses = self.flow.p_losses(
denoise_fn=self._denoise, data_start=data, t=t, noise=noises)
assert losses.shape == t.shape == torch.Size([B])
return losses
def gen_samples(self, shape, device, noise_fn=torch.randn,
clip_denoised=True,
keep_running=False):
return self.flow.p_sample_loop(self._denoise, shape=shape, device=device, noise_fn=noise_fn,
clip_denoised=clip_denoised,
keep_running=keep_running)
def gen_sample_traj(self, shape, device, freq, noise_fn=torch.randn,
clip_denoised=True,keep_running=False):
return self.flow.p_sample_loop_trajectory(self._denoise, shape=shape, device=device, noise_fn=noise_fn, freq=freq,
clip_denoised=clip_denoised,
keep_running=keep_running)
def train(self):
self.model.train()
def eval(self):
self.model.eval()
def multi_gpu_wrapper(self, f):
self.model = f(self.model)
def get_betas(schedule_type, b_start, b_end, time_num):
if schedule_type == 'linear':
betas = np.linspace(b_start, b_end, time_num)
elif schedule_type == 'warm0.1':
betas = b_end * np.ones(time_num, dtype=np.float64)
warmup_time = int(time_num * 0.1)
betas[:warmup_time] = np.linspace(b_start, b_end, warmup_time, dtype=np.float64)
elif schedule_type == 'warm0.2':
betas = b_end * np.ones(time_num, dtype=np.float64)
warmup_time = int(time_num * 0.2)
betas[:warmup_time] = np.linspace(b_start, b_end, warmup_time, dtype=np.float64)
elif schedule_type == 'warm0.5':
betas = b_end * np.ones(time_num, dtype=np.float64)
warmup_time = int(time_num * 0.5)
betas[:warmup_time] = np.linspace(b_start, b_end, warmup_time, dtype=np.float64)
else:
raise NotImplementedError(schedule_type)
return betas
def get_dataset(dataroot, npoints,category):
tr_dataset = ShapeNet15kPointClouds(root_dir=dataroot,
categories=[category], split='train',
tr_sample_size=npoints,
te_sample_size=npoints,
scale=1.,
reflow = True,
normalize_per_shape=False,
normalize_std_per_axis=False,
random_subsample=True)
te_dataset = ShapeNet15kPointClouds(root_dir=dataroot,
categories=[category], split='val',
tr_sample_size=npoints,
te_sample_size=npoints,
scale=1.,
reflow = True,
normalize_per_shape=False,
normalize_std_per_axis=False,
all_points_mean=tr_dataset.all_points_mean,
all_points_std=tr_dataset.all_points_std,
)
return tr_dataset, te_dataset
def get_dataloader(opt, train_dataset, test_dataset=None):
if opt.distribution_type == 'multi':
train_sampler = torch.utils.data.distributed.DistributedSampler(
train_dataset,
num_replicas=opt.world_size,
rank=opt.rank
)
if test_dataset is not None:
test_sampler = torch.utils.data.distributed.DistributedSampler(
test_dataset,
num_replicas=opt.world_size,
rank=opt.rank
)
else:
test_sampler = None
else:
train_sampler = None
test_sampler = None
train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=opt.bs,sampler=train_sampler,
shuffle=train_sampler is None, num_workers=int(opt.workers), drop_last=True)
if test_dataset is not None:
test_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=opt.bs,sampler=test_sampler,
shuffle=False, num_workers=int(opt.workers), drop_last=False)
else:
test_dataloader = None
return train_dataloader, test_dataloader, train_sampler, test_sampler
def train(gpu, opt, output_dir, noises_init):
set_seed(opt)
logger = setup_logging(output_dir)
if opt.distribution_type == 'multi':
should_diag = gpu==0
else:
should_diag = True
if should_diag:
outf_syn, = setup_output_subdirs(output_dir, 'syn')
if opt.distribution_type == 'multi':
if opt.dist_url == "env://" and opt.rank == -1:
opt.rank = int(os.environ["RANK"])
base_rank = opt.rank * opt.ngpus_per_node
opt.rank = base_rank + gpu
dist.init_process_group(backend=opt.dist_backend, init_method=opt.dist_url,
world_size=opt.world_size, rank=opt.rank)
opt.bs = int(opt.bs / opt.ngpus_per_node)
opt.workers = 0
opt.saveIter = int(opt.saveIter / opt.ngpus_per_node)
opt.diagIter = int(opt.diagIter / opt.ngpus_per_node)
opt.vizIter = int(opt.vizIter / opt.ngpus_per_node)
''' data '''
train_dataset, _ = get_dataset(opt.dataroot, opt.npoints, opt.category)
dataloader, _, train_sampler, _ = get_dataloader(opt, train_dataset, None)
'''
create networks
'''
betas = get_betas(opt.schedule_type, opt.beta_start, opt.beta_end, opt.time_num)
model = Model(opt, betas, opt.loss_type, opt.model_mean_type, opt.model_var_type)
if opt.distribution_type == 'multi': # Multiple processes, single GPU per process
def _transform_(m):
return nn.parallel.DistributedDataParallel(
m, device_ids=[gpu], output_device=gpu)
torch.cuda.set_device(gpu)
model.cuda(gpu)
model.multi_gpu_wrapper(_transform_)
elif opt.distribution_type == 'single':
def _transform_(m):
return nn.parallel.DataParallel(m)
model = model.cuda()
model.multi_gpu_wrapper(_transform_)
elif gpu is not None:
torch.cuda.set_device(gpu)
model = model.cuda(gpu)
else:
raise ValueError('distribution_type = multi | single | None')
if should_diag:
logger.info(opt)
optimizer= optim.Adam(model.parameters(), lr=opt.lr, weight_decay=opt.decay, betas=(opt.beta1, 0.999))
lr_scheduler = optim.lr_scheduler.ExponentialLR(optimizer, opt.lr_gamma)
if opt.model != '':
ckpt = torch.load(opt.model)
model.load_state_dict(ckpt['model_state'])
#optimizer.load_state_dict(ckpt['optimizer_state'])
if opt.model != '':
start_epoch = 0
else:
start_epoch = 0
def new_x_chain(x, num_chain):
return torch.randn(num_chain, *x.shape[1:], device=x.device)
for epoch in range(start_epoch, opt.niter):
if opt.distribution_type == 'multi':
train_sampler.set_epoch(epoch)
lr_scheduler.step(epoch)
for i, data in enumerate(dataloader):
x0 = data['train_points0']
x1 = data['train_points1']
noises_batch = noises_init[data['idx']].transpose(1,2)
'''
train diffusion
'''
if opt.distribution_type == 'multi' or (opt.distribution_type is None and gpu is not None):
x0 = x0.cuda(gpu)
x1 = x1.cuda(gpu)
noises_batch = noises_batch.cuda(gpu)
elif opt.distribution_type == 'single':
x = x.cuda()
noises_batch = noises_batch.cuda()
x = [x0, x1]
loss = model.get_loss_iter(x, noises_batch).mean()
optimizer.zero_grad()
loss.backward()
#netpNorm, netgradNorm = getGradNorm(model)
#if opt.grad_clip is not None:
# torch.nn.utils.clip_grad_norm_(model.parameters(), opt.grad_clip)
optimizer.step()
if i % opt.print_freq == 0 and should_diag:
logger.info('[{:>3d}/{:>3d}][{:>3d}/{:>3d}] loss: {:>10.4f}, '
.format(
epoch, opt.niter, i, len(dataloader),loss.item(),
))
if (epoch + 1) % opt.vizIter == 0 and should_diag:
logger.info('Generation: eval')
model.eval()
x = x1
with torch.no_grad():
x_gen_eval = model.gen_samples(new_x_chain(x, 25).shape, x.device, clip_denoised=False)
x_gen_list = model.gen_sample_traj(new_x_chain(x, 1).shape, x.device, freq=40, clip_denoised=False)
x_gen_all = torch.cat(x_gen_list, dim=0)
gen_stats = [x_gen_eval.mean(), x_gen_eval.std()]
gen_eval_range = [x_gen_eval.min().item(), x_gen_eval.max().item()]
logger.info(' [{:>3d}/{:>3d}] '
'eval_gen_range: [{:>10.4f}, {:>10.4f}] '
'eval_gen_stats: [mean={:>10.4f}, std={:>10.4f}] '
.format(
epoch, opt.niter,
*gen_eval_range, *gen_stats,
))
visualize_pointcloud_batch('%s/epoch_%03d_samples_eval.png' % (outf_syn, epoch),
x_gen_eval.transpose(1, 2), None, None,
None)
visualize_pointcloud_batch('%s/epoch_%03d_samples_eval_all.png' % (outf_syn, epoch),
x_gen_all.transpose(1, 2), None,
None,
None)
visualize_pointcloud_batch('%s/epoch_%03d_x.png' % (outf_syn, epoch), x.transpose(1, 2), None,
None,
None)
logger.info('Generation: train')
model.train()
if (epoch + 1) % opt.saveIter == 0:
if should_diag:
save_dict = {
'epoch': epoch,
'model_state': model.state_dict(),
'optimizer_state': optimizer.state_dict()
}
torch.save(save_dict, '%s/epoch_%d.pth' % (output_dir, epoch))
if opt.distribution_type == 'multi':
dist.barrier()
map_location = {'cuda:%d' % 0: 'cuda:%d' % gpu}
model.load_state_dict(
torch.load('%s/epoch_%d.pth' % (output_dir, epoch), map_location=map_location)['model_state'])
dist.destroy_process_group()
def main():
opt = parse_args()
if 1:
opt.beta_start = 1e-5
opt.beta_end = 0.008
opt.schedule_type = 'warm0.1'
exp_id = os.path.splitext(os.path.basename(__file__))[0]
dir_id = os.path.dirname(__file__)
output_dir = get_output_dir(dir_id, exp_id)
copy_source(__file__, output_dir)
''' workaround '''
train_dataset, _ = get_dataset(opt.dataroot, opt.npoints, opt.category)
noises_init = torch.randn(len(train_dataset), opt.npoints, opt.nc)
if opt.dist_url == "env://" and opt.world_size == -1:
opt.world_size = int(os.environ["WORLD_SIZE"])
if opt.distribution_type == 'multi':
opt.ngpus_per_node = torch.cuda.device_count()
opt.world_size = opt.ngpus_per_node * opt.world_size
mp.spawn(train, nprocs=opt.ngpus_per_node, args=(opt, output_dir, noises_init))
else:
train(opt.gpu, opt, output_dir, noises_init)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--dataroot', default='./data/ShapeNetCore.v2.PC15k/')
parser.add_argument('--category', default='chair')
parser.add_argument('--bs', type=int, default=96, help='input batch size')
parser.add_argument('--workers', type=int, default=16, help='workers')
parser.add_argument('--niter', type=int, default=10000, help='number of epochs to train for')
parser.add_argument('--nc', default=3)
parser.add_argument('--npoints', default=2048)
'''model'''
parser.add_argument('--beta_start', default=0.0001)
parser.add_argument('--beta_end', default=0.02)
parser.add_argument('--schedule_type', default='linear')
parser.add_argument('--time_num', default=1000)
#params
parser.add_argument('--attention', default=True)
parser.add_argument('--dropout', default=0.1)
parser.add_argument('--embed_dim', type=int, default=64)
parser.add_argument('--loss_type', default='mse')
parser.add_argument('--model_mean_type', default='eps')
parser.add_argument('--model_var_type', default='fixedsmall')
parser.add_argument('--lr', type=float, default=2e-5, help='learning rate for E, default=0.0002')
parser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5')
parser.add_argument('--decay', type=float, default=0, help='weight decay for EBM')
parser.add_argument('--grad_clip', type=float, default=None, help='weight decay for EBM')
parser.add_argument('--lr_gamma', type=float, default=0.998, help='lr decay for EBM')
parser.add_argument('--model', default='', help="path to model (to continue training)")
'''distributed'''
parser.add_argument('--world_size', default=1, type=int,
help='Number of distributed nodes.')
parser.add_argument('--dist_url', default='tcp://127.0.0.1:9991', type=str,
help='url used to set up distributed training')
parser.add_argument('--dist_backend', default='nccl', type=str,
help='distributed backend')
parser.add_argument('--distribution_type', default='multi', choices=['multi', 'single', None],
help='Use multi-processing distributed training to launch '
'N processes per node, which has N GPUs. This is the '
'fastest way to use PyTorch for either single node or '
'multi node data parallel training')
parser.add_argument('--rank', default=0, type=int,
help='node rank for distributed training')
parser.add_argument('--gpu', default=None, type=int,
help='GPU id to use. None means using all available GPUs.')
'''eval'''
parser.add_argument('--saveIter', default=100, help='unit: epoch')
parser.add_argument('--diagIter', default=100, help='unit: epoch')
parser.add_argument('--vizIter', default=100, help='unit: epoch')
parser.add_argument('--print_freq', default=50, help='unit: iter')
parser.add_argument('--manualSeed', default=42, type=int, help='random seed')
opt = parser.parse_args()
return opt
if __name__ == '__main__':
main()