This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
train_ae.py
211 lines (184 loc) · 6.9 KB
/
train_ae.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
import os
import argparse
import torch
import torch.utils.tensorboard
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 utils.transform import *
from models.autoencoder import *
from evaluation import EMD_CD
# Arguments
parser = argparse.ArgumentParser()
# Model arguments
parser.add_argument('--latent_dim', type=int, default=256)
parser.add_argument('--num_steps', type=int, default=200)
parser.add_argument('--beta_1', type=float, default=1e-4)
parser.add_argument('--beta_T', type=float, default=0.05)
parser.add_argument('--sched_mode', type=str, default='linear')
parser.add_argument('--flexibility', type=float, default=0.0)
parser.add_argument('--residual', type=eval, default=True, choices=[True, False])
parser.add_argument('--resume', type=str, default=None)
# 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=32)
parser.add_argument('--rotate', type=eval, default=False, choices=[True, False])
# Optimizer and scheduler
parser.add_argument('--lr', type=float, default=1e-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=150*THOUSAND)
parser.add_argument('--sched_end_epoch', type=int, default=300*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_ae')
parser.add_argument('--device', type=str, default='cuda')
parser.add_argument('--max_iters', type=int, default=float('inf'))
parser.add_argument('--val_freq', type=float, default=1000)
parser.add_argument('--tag', type=str, default=None)
parser.add_argument('--num_val_batches', type=int, default=-1)
parser.add_argument('--num_inspect_batches', type=int, default=1)
parser.add_argument('--num_inspect_pointclouds', type=int, default=4)
args = parser.parse_args()
seed_all(args.seed)
# Logging
if args.logging:
log_dir = get_new_log_dir(args.log_root, prefix='AE_', 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)
else:
logger = get_logger('train', None)
writer = BlackHole()
ckpt_mgr = BlackHole()
logger.info(args)
# Datasets and loaders
transform = None
if args.rotate:
transform = RandomRotate(180, ['pointcloud'], axis=1)
logger.info('Transform: %s' % repr(transform))
logger.info('Loading datasets...')
train_dset = ShapeNetCore(
path=args.dataset_path,
cates=args.categories,
split='train',
scale_mode=args.scale_mode,
transform=transform,
)
val_dset = ShapeNetCore(
path=args.dataset_path,
cates=args.categories,
split='val',
scale_mode=args.scale_mode,
transform=transform,
)
train_iter = get_data_iterator(DataLoader(
train_dset,
batch_size=args.train_batch_size,
num_workers=0,
))
val_loader = DataLoader(val_dset, batch_size=args.val_batch_size, num_workers=0)
# Model
logger.info('Building model...')
if args.resume is not None:
logger.info('Resuming from checkpoint...')
ckpt = torch.load(args.resume)
model = AutoEncoder(ckpt['args']).to(args.device)
model.load_state_dict(ckpt['state_dict'])
else:
model = AutoEncoder(args).to(args.device)
logger.info(repr(model))
# 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
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()
# Forward
loss = model.get_loss(x)
# 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 ' % (it, loss.item(), orig_grad_norm))
writer.add_scalar('train/loss', loss, 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_loss(it):
all_refs = []
all_recons = []
for i, batch in enumerate(tqdm(val_loader, desc='Validate')):
if args.num_val_batches > 0 and i >= args.num_val_batches:
break
ref = batch['pointcloud'].to(args.device)
shift = batch['shift'].to(args.device)
scale = batch['scale'].to(args.device)
with torch.no_grad():
model.eval()
code = model.encode(ref)
recons = model.decode(code, ref.size(1), flexibility=args.flexibility)
all_refs.append(ref * scale + shift)
all_recons.append(recons * scale + shift)
all_refs = torch.cat(all_refs, dim=0)
all_recons = torch.cat(all_recons, dim=0)
metrics = EMD_CD(all_recons, all_refs, batch_size=args.val_batch_size)
cd, emd = metrics['MMD-CD'].item(), metrics['MMD-EMD'].item()
logger.info('[Val] Iter %04d | CD %.6f | EMD %.6f ' % (it, cd, emd))
writer.add_scalar('val/cd', cd, it)
writer.add_scalar('val/emd', emd, it)
writer.flush()
return cd
def validate_inspect(it):
sum_n = 0
sum_chamfer = 0
for i, batch in enumerate(tqdm(val_loader, desc='Inspect')):
x = batch['pointcloud'].to(args.device)
model.eval()
code = model.encode(x)
recons = model.decode(code, x.size(1), flexibility=args.flexibility).detach()
sum_n += x.size(0)
if i >= args.num_inspect_batches:
break # Inspect only 5 batch
writer.add_mesh('val/pointcloud', recons[:args.num_inspect_pointclouds], global_step=it)
writer.flush()
# 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:
with torch.no_grad():
cd_loss = validate_loss(it)
validate_inspect(it)
opt_states = {
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
}
ckpt_mgr.save(model, args, cd_loss, opt_states, step=it)
it += 1
except KeyboardInterrupt:
logger.info('Terminating...')