-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
380 lines (289 loc) · 13.7 KB
/
run.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
import torch
from torch.utils.data import DataLoader
import tqdm
import trimesh
import torch.optim as optim
from models.contact_map_generator import ContactMapGenerator
from models.frame_len_predictor import FrameLenPredictor
from models.hand_refiner import HandRefiner
from models.model import THOI
from models.motion_generator import MotionGenerator
from models.components.mano.utils import get_mano_result, mano_layer
from utils.loss import (
binary_cross_entropy_loss,
dice_loss,
dm_loss,
contect_loss,
kl_divergence_loss,
l2_loss,
orient_loss,
penetrate_loss,
refine_loss,
criterion)
from utils.metrics import FrechetDistance
from utils.utils import (
Mesh,
clone_detach_dict_tensor,
estimated_distance_maps,
get_deformed_obj_point_cloud,
get_frame_mask,
get_hand_motion_mask,
get_padding_mask,
params_to_device,
relative_3d_orientation_diff)
from utils.arguments import CFGS
from utils.code import (
ADD_OPTIMIZERS,
EXAM_GRAD,
INITIALIZER,
MODELS_SET_MODE,
MODELS_SET_ZERO_GRAD,
OPTIMIZER_STEP,
log,
SAVE_LOG)
from utils.code import LOAD_WEIGHT, SAVE_CHECKPOINT, LOAD_CHECKPOINT
from datasets.dataset import Dataset
def tests():
global DEVICE, MODEL, DATASET, DATALOADER
# global lhand_mano_layer, rhand_mano_layer
# MODELS = MODELS_SET_MODE(MODELS, train=False)
# loader_len = len(DATALOADER)
# loader_iter = iter(DATALOADER)
# with torch.no_grad():
# for batch_idx in tqdm.tqdm(range(loader_len)):
# data = next(loader_iter)
# data = params_to_device(data, DEVICE)
# B = len(data["prompt"])
# MODELS = MODELS_SET_ZERO_GRAD(MODELS)
# # Contact Map Generator results
# contact_map_generator_result = MODELS["contact_map_generator"](
# data["prompt"],
# data["obj_verts"],
# inference=False,
# contact_map=data["contact_map"])
# # Predict motion frame length.
# pred_frame_len = MODELS["frame_len_predictor"](
# contact_map_generator_result["text_feature"])
# pred_frame_len = pred_frame_len.long()
# # Get left and right hand type masks base on input prompt.
# hand_motion_mask = get_hand_motion_mask(
# contact_map_generator_result["text_feature"],
# DEVICE)
# # Get frame masks base to mask padding area.
# pred_frame_mask = get_frame_mask(B, CFGS.max_frame, pred_frame_len, DEVICE)
# obj_feat = torch.cat([
# contact_map_generator_result["global_feature"],
# contact_map_generator_result["ref_contact_map"].squeeze(dim=2),
# contact_map_generator_result["object_scale"].unsqueeze(dim=1)], dim=1)
# # Contact Motion Generator results
# pred_motion_lhand, pred_motion_rhand, pred_motion_obj = MODELS["motion_generator"](
# obj_feat,
# contact_map_generator_result["text_feature"],
# CFGS.max_frame,
# True,
# hand_motion_mask, pred_frame_mask)
# # Deformed object point cloud based on predict object motion
# ref_point_cloud_pred = get_deformed_obj_point_cloud(
# pred_motion_obj,
# contact_map_generator_result["point_cloud"])
# pred_mano_lhand = get_mano_result(
# pred_motion_lhand, lhand_mano_layer, hand_motion_mask["mask_lhand"], pred_frame_mask,
# CFGS.max_frame, B, DEVICE)
# pred_mano_rhand = get_mano_result(
# pred_motion_rhand, rhand_mano_layer, hand_motion_mask["mask_rhand"], pred_frame_mask,
# CFGS.max_frame, B, DEVICE)
# ref_motion_lhand, ref_motion_rhand = MODELS["hand_refinement_network"](
# pred_motion_lhand, pred_motion_rhand,
# pred_mano_lhand["hand_joint"], pred_mano_rhand["hand_joint"],
# contact_map_generator_result["ref_contact_map"].unsqueeze(dim=2),
# ref_point_cloud_pred,
# hand_motion_mask, pred_frame_mask
# )
# ref_mano_lhand = get_mano_result(
# ref_motion_lhand,
# lhand_mano_layer,
# hand_motion_mask["mask_lhand"],
# pred_frame_mask,
# CFGS.max_frame, B, DEVICE)
# ref_mano_rhand = get_mano_result(
# ref_motion_rhand,
# rhand_mano_layer,
# hand_motion_mask["mask_rhand"],
# pred_frame_mask,
# CFGS.max_frame, B, DEVICE)
# meshl = Mesh(vert=ref_mano_lhand['hand_verts'][2, 0, :].cpu().numpy(),
# face=ref_mano_lhand['hand_faces'].cpu().numpy())
# meshr = Mesh(vert=ref_mano_rhand['hand_verts'][2, 0, :].cpu().numpy(),
# face=ref_mano_rhand['hand_faces'].cpu().numpy())
# scene = trimesh.Scene([meshl, meshr])
# scene.show()
# pass
# SAVE_LOG()
def train_one_epoch(epoch):
global DEVICE, MODEL, DATASET, DATALOADER, OPTIMIZERS
global lhand_mano_layer, rhand_mano_layer
MODEL = MODELS_SET_MODE(MODEL, train=True)
# MODEL.train()
loader_len = len(DATALOADER)
loader_iter = iter(DATALOADER)
for batch_idx in tqdm.tqdm(range(loader_len)):
data = next(loader_iter)
data = params_to_device(data, DEVICE)
B = len(data["prompt"])
MODEL = MODELS_SET_ZERO_GRAD(MODEL)
# MODEL.zero_grad()
[contact_map_generator_result, pred_frame_len, hand_motion_mask, pred_frame_mask,
pred_motion_lhand, pred_motion_rhand, pred_motion_obj, ref_point_cloud_pred,
pred_mano_lhand, pred_mano_rhand, ref_motion_lhand, ref_motion_rhand,
ref_mano_lhand, ref_mano_rhand] = MODEL['MODEL'](data, False)
contact_map_BCE_loss = binary_cross_entropy_loss(
contact_map_generator_result["ref_contact_map"],
contact_map_generator_result["sample_contact_map"])
contact_map_dice_loss = dice_loss(
contact_map_generator_result["ref_contact_map"],
contact_map_generator_result["sample_contact_map"])
Reparameterization_loss = kl_divergence_loss(
contact_map_generator_result["mu"],
contact_map_generator_result["log_var"])
# Loss of Contact Map Generator
loss_contact_map_generator = contact_map_BCE_loss + contact_map_dice_loss + Reparameterization_loss
# contact_map_generator_result = clone_detach_dict_tensor(contact_map_generator_result)
# Predict motion frame length.
# Ground-truth motion frame length.
true_frame_len = data["nframe"]
# Loss of Frame Length Predictor
loss_frame_len_predictor = criterion.smooth_l1_loss(pred_frame_len.float(), true_frame_len.float())
# Get left and right hand type masks base on input prompt.
# Get frame masks base to mask padding area.
true_frame_mask = get_frame_mask(B, CFGS.max_frame, true_frame_len, DEVICE)
# Deformed object point cloud based on ground-truth object motion
ref_point_cloud = get_deformed_obj_point_cloud(
data["obj_motion"],
contact_map_generator_result["point_cloud"])
ddpm_denoise_loss = \
l2_loss(pred_motion_lhand, data["lh_motion"]) + \
l2_loss(pred_motion_rhand, data["rh_motion"]) + \
l2_loss(pred_motion_obj, data["obj_motion"])
true_mano_lhand = get_mano_result(
data["lh_motion"], lhand_mano_layer, hand_motion_mask["mask_lhand"], true_frame_mask,
CFGS.max_frame, B, DEVICE)
true_mano_rhand = get_mano_result(
data["rh_motion"], rhand_mano_layer, hand_motion_mask["mask_rhand"], true_frame_mask,
CFGS.max_frame, B, DEVICE)
lhand_dist_map_diff = estimated_distance_maps(
ref_point_cloud_pred,
ref_point_cloud,
pred_mano_lhand["hand_joint"],
true_mano_lhand["hand_joint"],
hand_motion_mask["mask_lhand"],
true_frame_mask, pred_frame_mask,
CFGS.tau)
rhand_dist_map_diff = estimated_distance_maps(
ref_point_cloud_pred,
ref_point_cloud,
pred_mano_rhand["hand_joint"],
true_mano_rhand["hand_joint"],
hand_motion_mask["mask_rhand"],
true_frame_mask, pred_frame_mask,
CFGS.tau)
dist_map_loss = dm_loss(lhand_dist_map_diff, rhand_dist_map_diff)
lhand_relative_orientation_diff_pred = relative_3d_orientation_diff(
pred_motion_lhand, pred_motion_obj,
hand_motion_mask["mask_lhand"], pred_frame_mask)
lhand_relative_orientation_diff = relative_3d_orientation_diff(
data["lh_motion"], data["obj_motion"],
hand_motion_mask["mask_lhand"], true_frame_mask)
rhand_relative_orientation_diff_pred = relative_3d_orientation_diff(
pred_motion_rhand, pred_motion_obj,
hand_motion_mask["mask_rhand"], pred_frame_mask)
rhand_relative_orientation_diff = relative_3d_orientation_diff(
data["rh_motion"], data["obj_motion"],
hand_motion_mask["mask_rhand"], true_frame_mask)
orientation_loss = orient_loss(
lhand_relative_orientation_diff_pred,
lhand_relative_orientation_diff,
rhand_relative_orientation_diff_pred,
rhand_relative_orientation_diff,
hand_motion_mask["mask_lhand"],
hand_motion_mask["mask_rhand"]
)
# Loss of Motion Generator
loss_motion_generator = ddpm_denoise_loss + dist_map_loss + orientation_loss
# pred_motion_lhand, pred_motion_rhand, pred_motion_obj = \
# pred_motion_lhand.clone().detach(), pred_motion_rhand.clone().detach(), pred_motion_obj.clone().detach()
# pred_mano_lhand = clone_detach_dict_tensor(pred_mano_lhand)
# pred_mano_rhand = clone_detach_dict_tensor(pred_mano_rhand)
# ref_point_cloud_pred = ref_point_cloud_pred.clone().detach()
refine_hand_motions_loss = refine_loss(
ref_motion_lhand, data["lh_motion"],
ref_motion_rhand, data["rh_motion"])
ref_penet_loss = penetrate_loss(
ref_point_cloud_pred,
ref_mano_lhand["hand_verts"],
ref_mano_lhand["hand_faces"],
hand_motion_mask["mask_lhand"],
ref_mano_rhand["hand_verts"],
ref_mano_rhand["hand_faces"],
hand_motion_mask["mask_rhand"],
pred_frame_mask
)
ref_contect_loss = contect_loss(
ref_point_cloud_pred,
ref_mano_lhand["hand_joint"],
hand_motion_mask["mask_lhand"],
ref_mano_rhand["hand_joint"],
hand_motion_mask["mask_rhand"],
pred_frame_mask,
CFGS.tau
)
# Loss of Hand refiner
loss_hand_refiner = refine_hand_motions_loss + ref_penet_loss + CFGS.contect_loss_lambda * ref_contect_loss
loss = loss_contact_map_generator + loss_frame_len_predictor + loss_motion_generator + loss_hand_refiner
loss.backward()
OPTIMIZERS = OPTIMIZER_STEP(OPTIMIZERS)
if batch_idx % 1 == 0:
log(f"TRAIN - epoch: {epoch} - batch: {batch_idx + 1} / {loader_len} - " +
f"loss: {loss} - {loss_contact_map_generator}; {loss_frame_len_predictor}; {loss_motion_generator}; {loss_hand_refiner}")
def train():
global DEVICE, MODEL, OPTIMIZERS, START_EPOCH
for epoch in tqdm.tqdm(range(START_EPOCH + 1, CFGS.epoch + 1)):
train_one_epoch(epoch)
SAVE_CHECKPOINT(epoch, OPTIMIZERS, MODEL)
log(f'Epoch {epoch} done.')
SAVE_LOG()
pass
if __name__ == "__main__":
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# MANO layers for left and right hands.
lhand_mano_layer = mano_layer(CFGS.mano_model_path, CFGS.batch_size, is_rhand=False).to(DEVICE)
rhand_mano_layer = mano_layer(CFGS.mano_model_path, CFGS.batch_size, is_rhand=True).to(DEVICE)
# MODELS = {
# # Contact map generator.
# 'contact_map_generator': ContactMapGenerator(CFGS, DEVICE).to(DEVICE),
# # Motion frame length predictor.
# 'frame_len_predictor': FrameLenPredictor(DEVICE).to(DEVICE),
# # Motion generator.
# 'motion_generator': MotionGenerator(CFGS, DEVICE).to(DEVICE),
# # Hand refinement.
# 'hand_refinement_network': HandRefiner(CFGS, DEVICE).to(DEVICE),
# }
MODEL = {
"MODEL": THOI(DEVICE, CFGS, lhand_mano_layer, rhand_mano_layer)
}
START_EPOCH = 0
if not CFGS.inferencing:
log("training...")
DATASET = Dataset(CFGS.dataset_dir, CFGS.dataset, maxfn=CFGS.max_frame, train=True)
DATALOADER = DataLoader(DATASET, batch_size=CFGS.batch_size, shuffle=True, drop_last=True)
OPTIMIZERS = ADD_OPTIMIZERS(MODEL)
if CFGS.resume:
START_EPOCH, OPTIMIZERS, MODELS = LOAD_CHECKPOINT(OPTIMIZERS, MODEL)
train()
else:
log("testing...")
DATASET = Dataset(CFGS.dataset_dir, CFGS.dataset, maxfn=CFGS.max_frame, train=False)
DATALOADER = DataLoader(DATASET, batch_size=CFGS.batch_size, shuffle=True)
MODELS = LOAD_WEIGHT(MODEL)
tests()
log("done.")
SAVE_LOG()