-
Notifications
You must be signed in to change notification settings - Fork 90
/
train_gen.py
225 lines (200 loc) · 8.45 KB
/
train_gen.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
import os
import math
import argparse
import torch
import torch.utils.tensorboard
from torch.utils.data import DataLoader
from torch.nn.utils import clip_grad_norm_
from tqdm.auto import tqdm
from utils.dataset import *
from utils.misc import *
from utils.data import *
from models.vae_gaussian import *
from models.vae_flow import *
from models.flow import add_spectral_norm, spectral_norm_power_iteration
from evaluation import *
# Arguments
parser = argparse.ArgumentParser()
# Model arguments
parser.add_argument('--model', type=str, default='flow', choices=['flow', 'gaussian'])
parser.add_argument('--latent_dim', type=int, default=256)
parser.add_argument('--num_steps', type=int, default=100)
parser.add_argument('--beta_1', type=float, default=1e-4)
parser.add_argument('--beta_T', type=float, default=0.02)
parser.add_argument('--sched_mode', type=str, default='linear')
parser.add_argument('--flexibility', type=float, default=0.0)
parser.add_argument('--truncate_std', type=float, default=2.0)
parser.add_argument('--latent_flow_depth', type=int, default=14)
parser.add_argument('--latent_flow_hidden_dim', type=int, default=256)
parser.add_argument('--num_samples', type=int, default=4)
parser.add_argument('--sample_num_points', type=int, default=2048)
parser.add_argument('--kl_weight', type=float, default=0.001)
parser.add_argument('--residual', type=eval, default=True, choices=[True, False])
parser.add_argument('--spectral_norm', type=eval, default=False, choices=[True, False])
# Datasets and loaders
parser.add_argument('--dataset_path', type=str, default='./data/shapenet.hdf5')
parser.add_argument('--categories', type=str_list, default=['airplane'])
parser.add_argument('--scale_mode', type=str, default='shape_unit')
parser.add_argument('--train_batch_size', type=int, default=128)
parser.add_argument('--val_batch_size', type=int, default=64)
# Optimizer and scheduler
parser.add_argument('--lr', type=float, default=2e-3)
parser.add_argument('--weight_decay', type=float, default=0)
parser.add_argument('--max_grad_norm', type=float, default=10)
parser.add_argument('--end_lr', type=float, default=1e-4)
parser.add_argument('--sched_start_epoch', type=int, default=200*THOUSAND)
parser.add_argument('--sched_end_epoch', type=int, default=400*THOUSAND)
# Training
parser.add_argument('--seed', type=int, default=2020)
parser.add_argument('--logging', type=eval, default=True, choices=[True, False])
parser.add_argument('--log_root', type=str, default='./logs_gen')
parser.add_argument('--device', type=str, default='cuda')
parser.add_argument('--max_iters', type=int, default=float('inf'))
parser.add_argument('--val_freq', type=int, default=1000)
parser.add_argument('--test_freq', type=int, default=30*THOUSAND)
parser.add_argument('--test_size', type=int, default=400)
parser.add_argument('--tag', type=str, default=None)
args = parser.parse_args()
seed_all(args.seed)
# Logging
if args.logging:
log_dir = get_new_log_dir(args.log_root, prefix='GEN_', postfix='_' + args.tag if args.tag is not None else '')
logger = get_logger('train', log_dir)
writer = torch.utils.tensorboard.SummaryWriter(log_dir)
ckpt_mgr = CheckpointManager(log_dir)
log_hyperparams(writer, args)
else:
logger = get_logger('train', None)
writer = BlackHole()
ckpt_mgr = BlackHole()
logger.info(args)
# Datasets and loaders
logger.info('Loading datasets...')
train_dset = ShapeNetCore(
path=args.dataset_path,
cates=args.categories,
split='train',
scale_mode=args.scale_mode,
)
val_dset = ShapeNetCore(
path=args.dataset_path,
cates=args.categories,
split='val',
scale_mode=args.scale_mode,
)
train_iter = get_data_iterator(DataLoader(
train_dset,
batch_size=args.train_batch_size,
num_workers=0,
))
# Model
logger.info('Building model...')
if args.model == 'gaussian':
model = GaussianVAE(args).to(args.device)
elif args.model == 'flow':
model = FlowVAE(args).to(args.device)
logger.info(repr(model))
if args.spectral_norm:
add_spectral_norm(model, logger=logger)
# Optimizer and scheduler
optimizer = torch.optim.Adam(model.parameters(),
lr=args.lr,
weight_decay=args.weight_decay
)
scheduler = get_linear_scheduler(
optimizer,
start_epoch=args.sched_start_epoch,
end_epoch=args.sched_end_epoch,
start_lr=args.lr,
end_lr=args.end_lr
)
# Train, validate and test
def train(it):
# Load data
batch = next(train_iter)
x = batch['pointcloud'].to(args.device)
# Reset grad and model state
optimizer.zero_grad()
model.train()
if args.spectral_norm:
spectral_norm_power_iteration(model, n_power_iterations=1)
# Forward
kl_weight = args.kl_weight
loss = model.get_loss(x, kl_weight=kl_weight, writer=writer, it=it)
# Backward and optimize
loss.backward()
orig_grad_norm = clip_grad_norm_(model.parameters(), args.max_grad_norm)
optimizer.step()
scheduler.step()
logger.info('[Train] Iter %04d | Loss %.6f | Grad %.4f | KLWeight %.4f' % (
it, loss.item(), orig_grad_norm, kl_weight
))
writer.add_scalar('train/loss', loss, it)
writer.add_scalar('train/kl_weight', kl_weight, it)
writer.add_scalar('train/lr', optimizer.param_groups[0]['lr'], it)
writer.add_scalar('train/grad_norm', orig_grad_norm, it)
writer.flush()
def validate_inspect(it):
z = torch.randn([args.num_samples, args.latent_dim]).to(args.device)
x = model.sample(z, args.sample_num_points, flexibility=args.flexibility) #, truncate_std=args.truncate_std)
writer.add_mesh('val/pointcloud', x, global_step=it)
writer.flush()
logger.info('[Inspect] Generating samples...')
def test(it):
ref_pcs = []
for i, data in enumerate(val_dset):
if i >= args.test_size:
break
ref_pcs.append(data['pointcloud'].unsqueeze(0))
ref_pcs = torch.cat(ref_pcs, dim=0)
gen_pcs = []
for i in tqdm(range(0, math.ceil(args.test_size / args.val_batch_size)), 'Generate'):
with torch.no_grad():
z = torch.randn([args.val_batch_size, args.latent_dim]).to(args.device)
x = model.sample(z, args.sample_num_points, flexibility=args.flexibility)
gen_pcs.append(x.detach().cpu())
gen_pcs = torch.cat(gen_pcs, dim=0)[:args.test_size]
# Denormalize point clouds, all shapes have zero mean.
# [WARNING]: Do NOT denormalize!
# ref_pcs *= val_dset.stats['std']
# gen_pcs *= val_dset.stats['std']
with torch.no_grad():
results = compute_all_metrics(gen_pcs.to(args.device), ref_pcs.to(args.device), args.val_batch_size)
results = {k:v.item() for k, v in results.items()}
jsd = jsd_between_point_cloud_sets(gen_pcs.cpu().numpy(), ref_pcs.cpu().numpy())
results['jsd'] = jsd
# CD related metrics
writer.add_scalar('test/Coverage_CD', results['lgan_cov-CD'], global_step=it)
writer.add_scalar('test/MMD_CD', results['lgan_mmd-CD'], global_step=it)
writer.add_scalar('test/1NN_CD', results['1-NN-CD-acc'], global_step=it)
# EMD related metrics
# writer.add_scalar('test/Coverage_EMD', results['lgan_cov-EMD'], global_step=it)
# writer.add_scalar('test/MMD_EMD', results['lgan_mmd-EMD'], global_step=it)
# writer.add_scalar('test/1NN_EMD', results['1-NN-EMD-acc'], global_step=it)
# JSD
writer.add_scalar('test/JSD', results['jsd'], global_step=it)
# logger.info('[Test] Coverage | CD %.6f | EMD %.6f' % (results['lgan_cov-CD'], results['lgan_cov-EMD']))
# logger.info('[Test] MinMatDis | CD %.6f | EMD %.6f' % (results['lgan_mmd-CD'], results['lgan_mmd-EMD']))
# logger.info('[Test] 1NN-Accur | CD %.6f | EMD %.6f' % (results['1-NN-CD-acc'], results['1-NN-EMD-acc']))
logger.info('[Test] Coverage | CD %.6f | EMD n/a' % (results['lgan_cov-CD'], ))
logger.info('[Test] MinMatDis | CD %.6f | EMD n/a' % (results['lgan_mmd-CD'], ))
logger.info('[Test] 1NN-Accur | CD %.6f | EMD n/a' % (results['1-NN-CD-acc'], ))
logger.info('[Test] JsnShnDis | %.6f ' % (results['jsd']))
# Main loop
logger.info('Start training...')
try:
it = 1
while it <= args.max_iters:
train(it)
if it % args.val_freq == 0 or it == args.max_iters:
validate_inspect(it)
opt_states = {
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
}
ckpt_mgr.save(model, args, 0, others=opt_states, step=it)
if it % args.test_freq == 0 or it == args.max_iters:
test(it)
it += 1
except KeyboardInterrupt:
logger.info('Terminating...')