-
Notifications
You must be signed in to change notification settings - Fork 27
/
traineval.py
419 lines (385 loc) · 14.6 KB
/
traineval.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
import argparse
import matplotlib.pyplot as plt
import os
import random
import numpy as np
import torch
import torch.nn.parallel
import torch.optim
from mano_train.datautils import ConcatDataloader
from mano_train.networks.handnet import HandNet
from mano_train.networks import netutils
from mano_train.netscripts.get_datasets import get_dataset
from mano_train.exputils.monitoring import Monitor
from mano_train.exputils import argutils
from mano_train.modelutils import modelio
from mano_train.netscripts import epochpass3d, simulate
from mano_train.options import datasetopts, nets3dopts, expopts
from handobjectdatasets.queries import BaseQueries, TransQueries
plt.switch_backend("agg")
def main(args):
best_score = None
# Initialize randoms seeds
torch.cuda.manual_seed_all(args.manual_seed)
torch.manual_seed(args.manual_seed)
np.random.seed(args.manual_seed)
random.seed(args.manual_seed)
# create checkpoint dir
os.makedirs(args.exp_id, exist_ok=True)
# Initialize model
model = HandNet(
resnet_version=args.resnet_version,
absolute_lambda=args.absolute_lambda,
atlas_separate_encoder=args.atlas_separate_encoder,
atlas_loss=args.atlas_loss,
atlas_lambda=args.atlas_lambda,
atlas_final_lambda=args.atlas_final_lambda,
atlas_mesh=args.atlas_mesh,
atlas_lambda_regul_edges=args.atlas_lambda_regul_edges,
atlas_lambda_laplacian=args.atlas_lambda_laplacian,
atlas_points_nb=args.atlas_points_nb,
atlas_predict_trans=args.atlas_predict_trans,
atlas_predict_scale=args.atlas_predict_scale,
atlas_trans_weight=args.atlas_trans_weight,
atlas_scale_weight=args.atlas_scale_weight,
atlas_use_tanh=False,
atlas_out_factor=200,
contact_target=args.contact_target,
contact_zones=args.contact_zones,
contact_lambda=args.contact_lambda,
contact_thresh=args.contact_thresh,
contact_mode=args.contact_mode,
collision_lambda=args.collision_lambda,
collision_thresh=args.collision_thresh,
collision_mode=args.collision_mode,
mano_neurons=args.hidden_neurons,
mano_center_idx=args.center_idx,
mano_root="misc/mano",
mano_comps=args.mano_comps,
mano_use_pca=args.mano_use_pca,
mano_use_shape=args.mano_use_shape,
mano_lambda_joints3d=args.mano_lambda_joints3d,
mano_lambda_pose_reg=args.mano_lambda_pose_reg,
mano_lambda_joints2d=args.mano_lambda_joints2d,
mano_lambda_shape=args.mano_lambda_shape,
mano_lambda_pca=args.mano_lambda_pca,
mano_lambda_verts=args.mano_lambda_verts,
)
max_queries = [
TransQueries.affinetrans,
TransQueries.images,
TransQueries.verts3d,
TransQueries.center3d,
TransQueries.joints3d,
TransQueries.objpoints3d,
TransQueries.camintrs,
BaseQueries.sides,
]
if args.mano_lambda_joints2d:
max_queries.append(TransQueries.joints2d)
# Optionally freeze parts of the network
if args.freeze_batchnorm:
netutils.freeze_batchnorm_stats(model)
if args.freeze_encoder:
netutils.rec_freeze(model.base_net)
print("Froze encoder")
if args.atlas_separate_encoder and args.atlas_freeze_encoder:
netutils.rec_freeze(model.atlas_base_net)
print("Froze atlas encoder")
if args.atlas_freeze_decoder and hasattr(model, "atlas_branch"):
netutils.rec_freeze(model.atlas_branch.decoder)
print("Froze atlas decoder")
# Optimize unfrozen parts of the network
model_params = filter(lambda p: p.requires_grad, model.parameters())
model_param_names = [
name for name, val in model.named_parameters() if val.requires_grad
]
if args.debug:
print("=== Optimized params === ")
print(model_param_names)
# Initialize optimizer
if args.optimizer == "adam":
optimizer = torch.optim.Adam(
model_params, lr=args.lr, weight_decay=args.weight_decay
)
elif args.optimizer == "rms":
optimizer = torch.optim.RMSprop(
model_params, lr=args.lr, weight_decay=args.weight_decay
)
elif args.optimizer == "sgd":
optimizer = torch.optim.SGD(
model_params,
lr=args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay,
)
# Optionally resume from a checkpoint
model = torch.nn.DataParallel(model)
print("Using {} GPUs !".format(torch.cuda.device_count()))
if args.atlas_resume and args.resume:
raise NotImplementedError(
"resume and atlas_resume incompatible for now"
)
start_epoch = 0
if args.atlas_resume:
# Load atlas encoder and decoder to atlas-specific encoder-decoder
# branch
start_epoch, _ = modelio.load_checkpoint(
model, resume_path=args.atlas_resume, strict=False, load_atlas=True
)
print(
"Loaded ATLAS checkpoint from epoch {},"
"starting from there".format(start_epoch)
)
if args.evaluate:
args.epochs = start_epoch + 1
if args.resume is not None:
# Load full model weights
if len(args.resume) == 1:
start_epoch, _ = modelio.load_checkpoint(
model,
resume_path=args.resume[0],
optimizer=optimizer,
strict=False,
)
print(
"Loaded checkpoint from epoch {}, starting from there".format(
start_epoch
)
)
else:
if not args.evaluate:
raise ValueError(
"Multiple checkpoint resume only works in evaluate mode"
)
start_epoch, _ = modelio.load_checkpoints(
model, args.resume, strict=False
)
if args.evaluate:
args.epochs = start_epoch + 1
model.cuda()
# Override loaded learning rate
for group in optimizer.param_groups:
group["lr"] = args.lr
group["initial_lr"] = args.lr
if args.lr_decay_gamma:
scheduler = torch.optim.lr_scheduler.StepLR(
optimizer, args.lr_decay_step, gamma=args.lr_decay_gamma
)
if args.debug:
num_params = sum(p.numel() for p in model.parameters()) / 1000000.0
print("Total params: {} Million".format(num_params))
if not args.evaluate:
# Initialize train datasets
train_loaders = []
if args.controlled_exp:
# Use subset of datasets so that final dataset size is constant
limit_size = int(args.controlled_size / len(args.train_datasets))
else:
limit_size = None
# Initialize train datasets
for train_split, dat_name in zip(
args.train_splits, args.train_datasets
):
train_dat = get_dataset(
dat_name,
meta={
"mode": args.mode,
"override_scale": args.override_scale,
"fhbhands_split_type": args.fhbhands_split_type,
"fhbhands_split_choice": args.fhbhands_split_choice,
"fhbhands_topology": args.fhbhands_topology,
},
split=train_split,
sides=args.sides,
train_it=True,
max_queries=max_queries,
mini_factor=args.mini_factor,
point_nb=args.atlas_points_nb,
center_idx=args.center_idx,
limit_size=limit_size,
)
print("Final dataset size: {}".format(len(train_dat)))
# Initialize train dataloader
train_loader = torch.utils.data.DataLoader(
train_dat,
batch_size=args.train_batch,
shuffle=True,
num_workers=int(args.workers / len(args.train_splits)),
pin_memory=True,
drop_last=True,
)
train_loaders.append(train_loader)
train_loader = ConcatDataloader(train_loaders)
# Initialize validation datasets
val_loaders = []
# Add black padding if trained only on ganerated
for val_split, dat_name in zip(args.val_splits, args.val_datasets):
val_dat = get_dataset(
dat_name,
max_queries=max_queries,
meta={
"mode": args.mode,
"fhbhands_split_type": args.fhbhands_split_type,
"fhbhands_split_choice": args.fhbhands_split_choice,
"fhbhands_topology": args.fhbhands_topology,
"override_scale": args.override_scale,
},
sides=args.sides,
split=val_split,
train_it=False,
mini_factor=args.mini_factor,
point_nb=args.atlas_points_nb,
center_idx=args.center_idx,
)
# Initialize val dataloader
if args.evaluate:
drop_last = True
else:
drop_last = True # Keeps batch_size constant
val_loader = torch.utils.data.DataLoader(
val_dat,
batch_size=args.test_batch,
shuffle=False,
num_workers=int(args.workers / len(args.val_datasets)),
pin_memory=True,
drop_last=drop_last,
)
val_loaders.append(val_loader)
val_loader = ConcatDataloader(val_loaders)
# Get evaluation indexes
val_idxs = None
train_idxs = None
hosting_folder = os.path.join(args.host_folder, args.exp_id)
monitor = Monitor(args.exp_id, hosting_folder=hosting_folder)
fig = plt.figure(figsize=(12, 12))
for epoch in range(start_epoch, args.epochs):
display = epoch % args.epoch_display_freq == 0
# train for one epoch if not evaluating
if not args.evaluate:
print("Using lr {}".format(optimizer.param_groups[0]["lr"]))
train_avg_meters, train_pck_infos = epochpass3d.epoch_pass(
loader=train_loader,
model=model,
optimizer=optimizer,
freeze_batchnorm=args.freeze_batchnorm,
epoch=epoch,
debug=args.debug,
display_freq=args.train_display_freq,
display=display,
save_path=args.exp_id,
idxs=train_idxs,
train=True,
fig=fig,
)
# Save custom logs
train_dict = {
meter_name: meter.avg
for (
meter_name,
meter,
) in train_avg_meters.average_meters.items()
}
if train_pck_infos:
train_pck_dict = {
"auc": train_pck_infos["auc"],
"epe_mean": train_pck_infos["epe_mean"],
"epe_median": train_pck_infos["epe_median"],
}
else:
train_pck_dict = {}
train_full_dict = {**train_dict, **train_pck_dict}
monitor.log_train(epoch + 1, train_full_dict)
# Evaluate on validation set
with torch.no_grad():
val_avg_meters, val_pck_infos = epochpass3d.epoch_pass(
loader=val_loader,
model=model,
epoch=epoch,
optimizer=None,
debug=args.debug,
display_freq=args.test_display_freq,
display=display,
save_path=args.exp_id,
idxs=val_idxs,
train=False,
fig=fig,
save_results=args.save_results,
)
val_dict = {
meter_name: meter.avg
for meter_name, meter in val_avg_meters.average_meters.items()
}
if val_pck_infos:
val_pck_dict = {
"auc": val_pck_infos["auc"],
"epe_mean": val_pck_infos["epe_mean"],
"epe_median": val_pck_infos["epe_median"],
}
else:
val_pck_dict = {}
val_full_dict = {**val_dict, **val_pck_dict}
monitor.log_val(epoch + 1, val_full_dict)
# Interupt if evaluating
if args.evaluate:
if not args.no_simulate:
# Get simulation results
simulate.full_simul(
exp_id=os.path.join(
args.exp_id, "save_results/val/epoch_{}".format(epoch)
),
workers=args.workers,
cluster=True,
use_gui=False,
vhacd_exe=args.vhacd_exe,
)
return
save_dict = {}
for key in train_full_dict:
save_dict[key] = {}
if key in val_full_dict:
save_dict[key]["val"] = val_full_dict[key]
save_dict[key]["train"] = train_full_dict[key]
monitor.metrics.save_metrics(epoch + 1, save_dict)
monitor.metrics.plot_metrics()
# remember best acc and save checkpoint
if "auc" in val_pck_infos:
best_metric = "auc"
if best_score is None:
best_score = val_full_dict[best_metric]
is_best = val_full_dict[best_metric] > best_score
best_score = max(val_full_dict[best_metric], best_score)
else:
best_metric = "total_loss"
if best_score is None:
best_score = val_full_dict[best_metric]
is_best = val_full_dict[best_metric] < best_score
best_score = min(val_full_dict[best_metric], best_score)
modelio.save_checkpoint(
{
"epoch": epoch + 1,
"network": args.network,
"state_dict": model.state_dict(),
"best_score": best_score,
"optimizer": optimizer.state_dict(),
},
is_best=is_best,
checkpoint=args.exp_id,
snapshot=args.snapshot,
)
if args.lr_decay_gamma:
scheduler.step()
if epoch % args.regul_decay_step == 0:
model.module.decay_regul(args.regul_decay_gamma)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Mano training")
datasetopts.add_dataset_opts(parser)
datasetopts.add_dataset3d_opts(parser)
nets3dopts.add_nets3d_opts(parser)
nets3dopts.add_train3d_opts(parser)
expopts.add_exp_opts(parser)
args = parser.parse_args()
argutils.print_args(args)
argutils.save_args(args, args.exp_id, "opt")
main(args)
print("All done !")