-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathmld.py
912 lines (814 loc) · 36.2 KB
/
mld.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
import inspect
import os
from mld.transforms.rotation2xyz import Rotation2xyz
import numpy as np
import torch
from torch import Tensor
from torch.optim import AdamW
from torchmetrics import MetricCollection
import time
from mld.config import instantiate_from_config
from os.path import join as pjoin
from mld.models.architectures import (
mld_denoiser,
mld_vae,
vposert_vae,
t2m_motionenc,
t2m_textenc,
vposert_vae,
)
from mld.models.losses.mld import MLDLosses
from mld.models.modeltype.base import BaseModel
from mld.utils.temos_utils import remove_padding
from .base import BaseModel
class MLD(BaseModel):
"""
Stage 1 vae
Stage 2 diffusion
"""
def __init__(self, cfg, datamodule, **kwargs):
super().__init__()
self.cfg = cfg
self.stage = cfg.TRAIN.STAGE
self.condition = cfg.model.condition
self.is_vae = cfg.model.vae
self.predict_epsilon = cfg.TRAIN.ABLATION.PREDICT_EPSILON
self.nfeats = cfg.DATASET.NFEATS
self.njoints = cfg.DATASET.NJOINTS
self.debug = cfg.DEBUG
self.latent_dim = cfg.model.latent_dim
self.guidance_scale = cfg.model.guidance_scale
self.guidance_uncodp = cfg.model.guidance_uncondp
self.datamodule = datamodule
try:
self.vae_type = cfg.model.vae_type
except:
self.vae_type = cfg.model.motion_vae.target.split(
".")[-1].lower().replace("vae", "")
self.text_encoder = instantiate_from_config(cfg.model.text_encoder)
if self.vae_type != "no":
self.vae = instantiate_from_config(cfg.model.motion_vae)
# Don't train the motion encoder and decoder
if self.stage == "diffusion":
if self.vae_type in ["mld", "vposert","actor"]:
self.vae.training = False
for p in self.vae.parameters():
p.requires_grad = False
elif self.vae_type == "no":
pass
else:
self.motion_encoder.training = False
for p in self.motion_encoder.parameters():
p.requires_grad = False
self.motion_decoder.training = False
for p in self.motion_decoder.parameters():
p.requires_grad = False
self.denoiser = instantiate_from_config(cfg.model.denoiser)
if not self.predict_epsilon:
cfg.model.scheduler.params['prediction_type'] = 'sample'
cfg.model.noise_scheduler.params['prediction_type'] = 'sample'
self.scheduler = instantiate_from_config(cfg.model.scheduler)
self.noise_scheduler = instantiate_from_config(
cfg.model.noise_scheduler)
if self.condition in ["text", "text_uncond"]:
self._get_t2m_evaluator(cfg)
if cfg.TRAIN.OPTIM.TYPE.lower() == "adamw":
self.optimizer = AdamW(lr=cfg.TRAIN.OPTIM.LR,
params=self.parameters())
else:
raise NotImplementedError(
"Do not support other optimizer for now.")
if cfg.LOSS.TYPE == "mld":
self._losses = MetricCollection({
split: MLDLosses(vae=self.is_vae, mode="xyz", cfg=cfg)
for split in ["losses_train", "losses_test", "losses_val"]
})
else:
raise NotImplementedError(
"MotionCross model only supports mld losses.")
self.losses = {
key: self._losses["losses_" + key]
for key in ["train", "test", "val"]
}
self.metrics_dict = cfg.METRIC.TYPE
self.configure_metrics()
# If we want to overide it at testing time
self.sample_mean = False
self.fact = None
self.do_classifier_free_guidance = self.guidance_scale > 1.0
if self.condition in ['text', 'text_uncond']:
self.feats2joints = datamodule.feats2joints
elif self.condition == 'action':
self.rot2xyz = Rotation2xyz(smpl_path=cfg.DATASET.SMPL_PATH)
self.feats2joints_eval = lambda sample, mask: self.rot2xyz(
sample.view(*sample.shape[:-1], 6, 25).permute(0, 3, 2, 1),
mask=mask,
pose_rep='rot6d',
glob=True,
translation=True,
jointstype='smpl',
vertstrans=True,
betas=None,
beta=0,
glob_rot=None,
get_rotations_back=False)
self.feats2joints = lambda sample, mask: self.rot2xyz(
sample.view(*sample.shape[:-1], 6, 25).permute(0, 3, 2, 1),
mask=mask,
pose_rep='rot6d',
glob=True,
translation=True,
jointstype='vertices',
vertstrans=False,
betas=None,
beta=0,
glob_rot=None,
get_rotations_back=False)
def _get_t2m_evaluator(self, cfg):
"""
load T2M text encoder and motion encoder for evaluating
"""
# init module
self.t2m_textencoder = t2m_textenc.TextEncoderBiGRUCo(
word_size=cfg.model.t2m_textencoder.dim_word,
pos_size=cfg.model.t2m_textencoder.dim_pos_ohot,
hidden_size=cfg.model.t2m_textencoder.dim_text_hidden,
output_size=cfg.model.t2m_textencoder.dim_coemb_hidden,
)
self.t2m_moveencoder = t2m_motionenc.MovementConvEncoder(
input_size=cfg.DATASET.NFEATS - 4,
hidden_size=cfg.model.t2m_motionencoder.dim_move_hidden,
output_size=cfg.model.t2m_motionencoder.dim_move_latent,
)
self.t2m_motionencoder = t2m_motionenc.MotionEncoderBiGRUCo(
input_size=cfg.model.t2m_motionencoder.dim_move_latent,
hidden_size=cfg.model.t2m_motionencoder.dim_motion_hidden,
output_size=cfg.model.t2m_motionencoder.dim_motion_latent,
)
# load pretrianed
dataname = cfg.TEST.DATASETS[0]
dataname = "t2m" if dataname == "humanml3d" else dataname
t2m_checkpoint = torch.load(
os.path.join(cfg.model.t2m_path, dataname,
"text_mot_match/model/finest.tar"))
self.t2m_textencoder.load_state_dict(t2m_checkpoint["text_encoder"])
self.t2m_moveencoder.load_state_dict(
t2m_checkpoint["movement_encoder"])
self.t2m_motionencoder.load_state_dict(
t2m_checkpoint["motion_encoder"])
# freeze params
self.t2m_textencoder.eval()
self.t2m_moveencoder.eval()
self.t2m_motionencoder.eval()
for p in self.t2m_textencoder.parameters():
p.requires_grad = False
for p in self.t2m_moveencoder.parameters():
p.requires_grad = False
for p in self.t2m_motionencoder.parameters():
p.requires_grad = False
def sample_from_distribution(
self,
dist,
*,
fact=None,
sample_mean=False,
) -> Tensor:
fact = fact if fact is not None else self.fact
sample_mean = sample_mean if sample_mean is not None else self.sample_mean
if sample_mean:
return dist.loc.unsqueeze(0)
# Reparameterization trick
if fact is None:
return dist.rsample().unsqueeze(0)
# Resclale the eps
eps = dist.rsample() - dist.loc
z = dist.loc + fact * eps
# add latent size
z = z.unsqueeze(0)
return z
def forward(self, batch):
texts = batch["text"]
lengths = batch["length"]
if self.cfg.TEST.COUNT_TIME:
self.starttime = time.time()
if self.stage in ['diffusion', 'vae_diffusion']:
# diffusion reverse
if self.do_classifier_free_guidance:
uncond_tokens = [""] * len(texts)
if self.condition == 'text':
uncond_tokens.extend(texts)
elif self.condition == 'text_uncond':
uncond_tokens.extend(uncond_tokens)
texts = uncond_tokens
text_emb = self.text_encoder(texts)
z = self._diffusion_reverse(text_emb, lengths)
elif self.stage in ['vae']:
motions = batch['motion']
z, dist_m = self.vae.encode(motions, lengths)
with torch.no_grad():
# ToDo change mcross actor to same api
if self.vae_type in ["mld","actor"]:
feats_rst = self.vae.decode(z, lengths)
elif self.vae_type == "no":
feats_rst = z.permute(1, 0, 2)
if self.cfg.TEST.COUNT_TIME:
self.endtime = time.time()
elapsed = self.endtime - self.starttime
self.times.append(elapsed)
if len(self.times) % 100 == 0:
meantime = np.mean(
self.times[-100:]) / self.cfg.TEST.BATCH_SIZE
print(
f'100 iter mean Time (batch_size: {self.cfg.TEST.BATCH_SIZE}): {meantime}',
)
if len(self.times) % 1000 == 0:
meantime = np.mean(
self.times[-1000:]) / self.cfg.TEST.BATCH_SIZE
print(
f'1000 iter mean Time (batch_size: {self.cfg.TEST.BATCH_SIZE}): {meantime}',
)
with open(pjoin(self.cfg.FOLDER_EXP, 'times.txt'), 'w') as f:
for line in self.times:
f.write(str(line))
f.write('\n')
joints = self.feats2joints(feats_rst.detach().cpu())
return remove_padding(joints, lengths)
def gen_from_latent(self, batch):
z = batch["latent"]
lengths = batch["length"]
feats_rst = self.vae.decode(z, lengths)
# feats => joints
joints = self.feats2joints(feats_rst.detach().cpu())
return remove_padding(joints, lengths)
def recon_from_motion(self, batch):
feats_ref = batch["motion"]
length = batch["length"]
z, dist = self.vae.encode(feats_ref, length)
feats_rst = self.vae.decode(z, length)
# feats => joints
joints = self.feats2joints(feats_rst.detach().cpu())
joints_ref = self.feats2joints(feats_ref.detach().cpu())
return remove_padding(joints,
length), remove_padding(joints_ref, length)
def _diffusion_reverse(self, encoder_hidden_states, lengths=None):
# init latents
bsz = encoder_hidden_states.shape[0]
if self.do_classifier_free_guidance:
bsz = bsz // 2
if self.vae_type == "no":
assert lengths is not None, "no vae (diffusion only) need lengths for diffusion"
latents = torch.randn(
(bsz, max(lengths), self.cfg.DATASET.NFEATS),
device=encoder_hidden_states.device,
dtype=torch.float,
)
else:
latents = torch.randn(
(bsz, self.latent_dim[0], self.latent_dim[-1]),
device=encoder_hidden_states.device,
dtype=torch.float,
)
# scale the initial noise by the standard deviation required by the scheduler
latents = latents * self.scheduler.init_noise_sigma
# set timesteps
self.scheduler.set_timesteps(
self.cfg.model.scheduler.num_inference_timesteps)
timesteps = self.scheduler.timesteps.to(encoder_hidden_states.device)
# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
# eta (η) is only used with the DDIMScheduler, and between [0, 1]
extra_step_kwargs = {}
if "eta" in set(
inspect.signature(self.scheduler.step).parameters.keys()):
extra_step_kwargs["eta"] = self.cfg.model.scheduler.eta
# reverse
for i, t in enumerate(timesteps):
# expand the latents if we are doing classifier free guidance
latent_model_input = (torch.cat(
[latents] *
2) if self.do_classifier_free_guidance else latents)
lengths_reverse = (lengths * 2 if self.do_classifier_free_guidance
else lengths)
# latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
# predict the noise residual
noise_pred = self.denoiser(
sample=latent_model_input,
timestep=t,
encoder_hidden_states=encoder_hidden_states,
lengths=lengths_reverse,
)[0]
# perform guidance
if self.do_classifier_free_guidance:
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + self.guidance_scale * (
noise_pred_text - noise_pred_uncond)
# text_embeddings_for_guidance = encoder_hidden_states.chunk(
# 2)[1] if self.do_classifier_free_guidance else encoder_hidden_states
latents = self.scheduler.step(noise_pred, t, latents,
**extra_step_kwargs).prev_sample
# if self.predict_epsilon:
# latents = self.scheduler.step(noise_pred, t, latents,
# **extra_step_kwargs).prev_sample
# else:
# # predict x for standard diffusion model
# # compute the previous noisy sample x_t -> x_t-1
# latents = self.scheduler.step(noise_pred,
# t,
# latents,
# **extra_step_kwargs).prev_sample
# [batch_size, 1, latent_dim] -> [1, batch_size, latent_dim]
latents = latents.permute(1, 0, 2)
return latents
def _diffusion_reverse_tsne(self, encoder_hidden_states, lengths=None):
# init latents
bsz = encoder_hidden_states.shape[0]
if self.do_classifier_free_guidance:
bsz = bsz // 2
if self.vae_type == "no":
assert lengths is not None, "no vae (diffusion only) need lengths for diffusion"
latents = torch.randn(
(bsz, max(lengths), self.cfg.DATASET.NFEATS),
device=encoder_hidden_states.device,
dtype=torch.float,
)
else:
latents = torch.randn(
(bsz, self.latent_dim[0], self.latent_dim[-1]),
device=encoder_hidden_states.device,
dtype=torch.float,
)
# scale the initial noise by the standard deviation required by the scheduler
latents = latents * self.scheduler.init_noise_sigma
# set timesteps
self.scheduler.set_timesteps(
self.cfg.model.scheduler.num_inference_timesteps)
timesteps = self.scheduler.timesteps.to(encoder_hidden_states.device)
# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
# eta (η) is only used with the DDIMScheduler, and between [0, 1]
extra_step_kwargs = {}
if "eta" in set(
inspect.signature(self.scheduler.step).parameters.keys()):
extra_step_kwargs["eta"] = self.cfg.model.scheduler.eta
# reverse
latents_t = []
for i, t in enumerate(timesteps):
# expand the latents if we are doing classifier free guidance
latent_model_input = (torch.cat(
[latents] *
2) if self.do_classifier_free_guidance else latents)
lengths_reverse = (lengths * 2 if self.do_classifier_free_guidance
else lengths)
# latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
# predict the noise residual
noise_pred = self.denoiser(
sample=latent_model_input,
timestep=t,
encoder_hidden_states=encoder_hidden_states,
lengths=lengths_reverse,
)[0]
# perform guidance
if self.do_classifier_free_guidance:
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + self.guidance_scale * (
noise_pred_text - noise_pred_uncond)
# text_embeddings_for_guidance = encoder_hidden_states.chunk(
# 2)[1] if self.do_classifier_free_guidance else encoder_hidden_states
latents = self.scheduler.step(noise_pred, t, latents,
**extra_step_kwargs).prev_sample
# [batch_size, 1, latent_dim] -> [1, batch_size, latent_dim]
latents_t.append(latents.permute(1,0,2))
# [1, batch_size, latent_dim] -> [t, batch_size, latent_dim]
latents_t = torch.cat(latents_t)
return latents_t
def _diffusion_process(self, latents, encoder_hidden_states, lengths=None):
"""
heavily from https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/train_dreambooth.py
"""
# our latent [batch_size, n_token=1 or 5 or 10, latent_dim=256]
# sd latent [batch_size, [n_token0=64,n_token1=64], latent_dim=4]
# [n_token, batch_size, latent_dim] -> [batch_size, n_token, latent_dim]
latents = latents.permute(1, 0, 2)
# Sample noise that we'll add to the latents
# [batch_size, n_token, latent_dim]
noise = torch.randn_like(latents)
bsz = latents.shape[0]
# Sample a random timestep for each motion
timesteps = torch.randint(
0,
self.noise_scheduler.config.num_train_timesteps,
(bsz, ),
device=latents.device,
)
timesteps = timesteps.long()
# Add noise to the latents according to the noise magnitude at each timestep
noisy_latents = self.noise_scheduler.add_noise(latents.clone(), noise,
timesteps)
# Predict the noise residual
noise_pred = self.denoiser(
sample=noisy_latents,
timestep=timesteps,
encoder_hidden_states=encoder_hidden_states,
lengths=lengths,
return_dict=False,
)[0]
# Chunk the noise and noise_pred into two parts and compute the loss on each part separately.
if self.cfg.LOSS.LAMBDA_PRIOR != 0.0:
noise_pred, noise_pred_prior = torch.chunk(noise_pred, 2, dim=0)
noise, noise_prior = torch.chunk(noise, 2, dim=0)
else:
noise_pred_prior = 0
noise_prior = 0
n_set = {
"noise": noise,
"noise_prior": noise_prior,
"noise_pred": noise_pred,
"noise_pred_prior": noise_pred_prior,
}
if not self.predict_epsilon:
n_set["pred"] = noise_pred
n_set["latent"] = latents
return n_set
def train_vae_forward(self, batch):
feats_ref = batch["motion"]
lengths = batch["length"]
if self.vae_type in ["mld", "vposert", "actor"]:
motion_z, dist_m = self.vae.encode(feats_ref, lengths)
feats_rst = self.vae.decode(motion_z, lengths)
else:
raise TypeError("vae_type must be mcross or actor")
# prepare for metric
recons_z, dist_rm = self.vae.encode(feats_rst, lengths)
# joints recover
if self.condition == "text":
joints_rst = self.feats2joints(feats_rst)
joints_ref = self.feats2joints(feats_ref)
elif self.condition == "action":
mask = batch["mask"]
joints_rst = self.feats2joints(feats_rst, mask)
joints_ref = self.feats2joints(feats_ref, mask)
if dist_m is not None:
if self.is_vae:
# Create a centred normal distribution to compare with
mu_ref = torch.zeros_like(dist_m.loc)
scale_ref = torch.ones_like(dist_m.scale)
dist_ref = torch.distributions.Normal(mu_ref, scale_ref)
else:
dist_ref = dist_m
# cut longer part over max length
min_len = min(feats_ref.shape[1], feats_rst.shape[1])
rs_set = {
"m_ref": feats_ref[:, :min_len, :],
"m_rst": feats_rst[:, :min_len, :],
# [bs, ntoken, nfeats]<= [ntoken, bs, nfeats]
"lat_m": motion_z.permute(1, 0, 2),
"lat_rm": recons_z.permute(1, 0, 2),
"joints_ref": joints_ref,
"joints_rst": joints_rst,
"dist_m": dist_m,
"dist_ref": dist_ref,
}
return rs_set
def train_diffusion_forward(self, batch):
feats_ref = batch["motion"]
lengths = batch["length"]
# motion encode
with torch.no_grad():
if self.vae_type in ["mld", "vposert", "actor"]:
z, dist = self.vae.encode(feats_ref, lengths)
elif self.vae_type == "no":
z = feats_ref.permute(1, 0, 2)
else:
raise TypeError("vae_type must be mcross or actor")
if self.condition in ["text", "text_uncond"]:
text = batch["text"]
# classifier free guidance: randomly drop text during training
text = [
"" if np.random.rand(1) < self.guidance_uncodp else i
for i in text
]
# text encode
cond_emb = self.text_encoder(text)
elif self.condition in ['action']:
action = batch['action']
# text encode
cond_emb = action
else:
raise TypeError(f"condition type {self.condition} not supported")
# diffusion process return with noise and noise_pred
n_set = self._diffusion_process(z, cond_emb, lengths)
return {**n_set}
def test_diffusion_forward(self, batch, finetune_decoder=False):
lengths = batch["length"]
if self.condition in ["text", "text_uncond"]:
# get text embeddings
if self.do_classifier_free_guidance:
uncond_tokens = [""] * len(lengths)
if self.condition == 'text':
texts = batch["text"]
uncond_tokens.extend(texts)
elif self.condition == 'text_uncond':
uncond_tokens.extend(uncond_tokens)
texts = uncond_tokens
cond_emb = self.text_encoder(texts)
elif self.condition in ['action']:
cond_emb = batch['action']
if self.do_classifier_free_guidance:
cond_emb = torch.cat(
cond_emb,
torch.zeros_like(batch['action'],
dtype=batch['action'].dtype))
else:
raise TypeError(f"condition type {self.condition} not supported")
# diffusion reverse
with torch.no_grad():
z = self._diffusion_reverse(cond_emb, lengths)
with torch.no_grad():
if self.vae_type in ["mld", "vposert", "actor"]:
feats_rst = self.vae.decode(z, lengths)
elif self.vae_type == "no":
feats_rst = z.permute(1, 0, 2)
else:
raise TypeError("vae_type must be mcross or actor or mld")
joints_rst = self.feats2joints(feats_rst)
rs_set = {
"m_rst": feats_rst,
# [bs, ntoken, nfeats]<= [ntoken, bs, nfeats]
"lat_t": z.permute(1, 0, 2),
"joints_rst": joints_rst,
}
# prepare gt/refer for metric
if "motion" in batch.keys() and not finetune_decoder:
feats_ref = batch["motion"].detach()
with torch.no_grad():
if self.vae_type in ["mld", "vposert", "actor"]:
motion_z, dist_m = self.vae.encode(feats_ref, lengths)
recons_z, dist_rm = self.vae.encode(feats_rst, lengths)
elif self.vae_type == "no":
motion_z = feats_ref
recons_z = feats_rst
joints_ref = self.feats2joints(feats_ref)
rs_set["m_ref"] = feats_ref
rs_set["lat_m"] = motion_z.permute(1, 0, 2)
rs_set["lat_rm"] = recons_z.permute(1, 0, 2)
rs_set["joints_ref"] = joints_ref
return rs_set
def t2m_eval(self, batch):
texts = batch["text"]
motions = batch["motion"].detach().clone()
lengths = batch["length"]
word_embs = batch["word_embs"].detach().clone()
pos_ohot = batch["pos_ohot"].detach().clone()
text_lengths = batch["text_len"].detach().clone()
# start
start = time.time()
if self.trainer.datamodule.is_mm:
texts = texts * self.cfg.TEST.MM_NUM_REPEATS
motions = motions.repeat_interleave(self.cfg.TEST.MM_NUM_REPEATS,
dim=0)
lengths = lengths * self.cfg.TEST.MM_NUM_REPEATS
word_embs = word_embs.repeat_interleave(
self.cfg.TEST.MM_NUM_REPEATS, dim=0)
pos_ohot = pos_ohot.repeat_interleave(self.cfg.TEST.MM_NUM_REPEATS,
dim=0)
text_lengths = text_lengths.repeat_interleave(
self.cfg.TEST.MM_NUM_REPEATS, dim=0)
if self.stage in ['diffusion', 'vae_diffusion']:
# diffusion reverse
if self.do_classifier_free_guidance:
uncond_tokens = [""] * len(texts)
if self.condition == 'text':
uncond_tokens.extend(texts)
elif self.condition == 'text_uncond':
uncond_tokens.extend(uncond_tokens)
texts = uncond_tokens
text_emb = self.text_encoder(texts)
z = self._diffusion_reverse(text_emb, lengths)
elif self.stage in ['vae']:
if self.vae_type in ["mld", "vposert", "actor"]:
z, dist_m = self.vae.encode(motions, lengths)
else:
raise TypeError("Not supported vae type!")
if self.condition in ['text_uncond']:
# uncond random sample
z = torch.randn_like(z)
with torch.no_grad():
if self.vae_type in ["mld", "vposert", "actor"]:
feats_rst = self.vae.decode(z, lengths)
elif self.vae_type == "no":
feats_rst = z.permute(1, 0, 2)
# end time
end = time.time()
self.times.append(end - start)
# joints recover
joints_rst = self.feats2joints(feats_rst)
joints_ref = self.feats2joints(motions)
# renorm for t2m evaluators
feats_rst = self.datamodule.renorm4t2m(feats_rst)
motions = self.datamodule.renorm4t2m(motions)
# t2m motion encoder
m_lens = lengths.copy()
m_lens = torch.tensor(m_lens, device=motions.device)
align_idx = np.argsort(m_lens.data.tolist())[::-1].copy()
motions = motions[align_idx]
feats_rst = feats_rst[align_idx]
m_lens = m_lens[align_idx]
m_lens = torch.div(m_lens,
self.cfg.DATASET.HUMANML3D.UNIT_LEN,
rounding_mode="floor")
recons_mov = self.t2m_moveencoder(feats_rst[..., :-4]).detach()
recons_emb = self.t2m_motionencoder(recons_mov, m_lens)
motion_mov = self.t2m_moveencoder(motions[..., :-4]).detach()
motion_emb = self.t2m_motionencoder(motion_mov, m_lens)
# t2m text encoder
text_emb = self.t2m_textencoder(word_embs, pos_ohot,
text_lengths)[align_idx]
rs_set = {
"m_ref": motions,
"m_rst": feats_rst,
"lat_t": text_emb,
"lat_m": motion_emb,
"lat_rm": recons_emb,
"joints_ref": joints_ref,
"joints_rst": joints_rst,
}
return rs_set
def a2m_eval(self, batch):
actions = batch["action"]
actiontexts = batch["action_text"]
motions = batch["motion"].detach().clone()
lengths = batch["length"]
if self.do_classifier_free_guidance:
cond_emb = torch.cat((torch.zeros_like(actions), actions))
if self.stage in ['diffusion', 'vae_diffusion']:
z = self._diffusion_reverse(cond_emb, lengths)
elif self.stage in ['vae']:
if self.vae_type in ["mld", "vposert","actor"]:
z, dist_m = self.vae.encode(motions, lengths)
else:
raise TypeError("vae_type must be mcross or actor")
with torch.no_grad():
if self.vae_type in ["mld", "vposert","actor"]:
feats_rst = self.vae.decode(z, lengths)
elif self.vae_type == "no":
feats_rst = z.permute(1, 0, 2)
else:
raise TypeError("vae_type must be mcross or actor or mld")
mask = batch["mask"]
joints_rst = self.feats2joints(feats_rst, mask)
joints_ref = self.feats2joints(motions, mask)
joints_eval_rst = self.feats2joints_eval(feats_rst, mask)
joints_eval_ref = self.feats2joints_eval(motions, mask)
rs_set = {
"m_action": actions,
"m_ref": motions,
"m_rst": feats_rst,
"m_lens": lengths,
"joints_rst": joints_rst,
"joints_ref": joints_ref,
"joints_eval_rst": joints_eval_rst,
"joints_eval_ref": joints_eval_ref,
}
return rs_set
def a2m_gt(self, batch):
actions = batch["action"]
actiontexts = batch["action_text"]
motions = batch["motion"].detach().clone()
lengths = batch["length"]
mask = batch["mask"]
joints_ref = self.feats2joints(motions.to('cuda'), mask.to('cuda'))
rs_set = {
"m_action": actions,
"m_text": actiontexts,
"m_ref": motions,
"m_lens": lengths,
"joints_ref": joints_ref,
}
return rs_set
def eval_gt(self, batch, renoem=True):
motions = batch["motion"].detach().clone()
lengths = batch["length"]
# feats_rst = self.datamodule.renorm4t2m(feats_rst)
if renoem:
motions = self.datamodule.renorm4t2m(motions)
# t2m motion encoder
m_lens = lengths.copy()
m_lens = torch.tensor(m_lens, device=motions.device)
align_idx = np.argsort(m_lens.data.tolist())[::-1].copy()
motions = motions[align_idx]
m_lens = m_lens[align_idx]
m_lens = torch.div(m_lens,
self.cfg.DATASET.HUMANML3D.UNIT_LEN,
rounding_mode="floor")
word_embs = batch["word_embs"].detach()
pos_ohot = batch["pos_ohot"].detach()
text_lengths = batch["text_len"].detach()
motion_mov = self.t2m_moveencoder(motions[..., :-4]).detach()
motion_emb = self.t2m_motionencoder(motion_mov, m_lens)
# t2m text encoder
text_emb = self.t2m_textencoder(word_embs, pos_ohot,
text_lengths)[align_idx]
# joints recover
joints_ref = self.feats2joints(motions)
rs_set = {
"m_ref": motions,
"lat_t": text_emb,
"lat_m": motion_emb,
"joints_ref": joints_ref,
}
return rs_set
def allsplit_step(self, split: str, batch, batch_idx):
if split in ["train", "val"]:
if self.stage == "vae":
rs_set = self.train_vae_forward(batch)
rs_set["lat_t"] = rs_set["lat_m"]
elif self.stage == "diffusion":
rs_set = self.train_diffusion_forward(batch)
elif self.stage == "vae_diffusion":
vae_rs_set = self.train_vae_forward(batch)
diff_rs_set = self.train_diffusion_forward(batch)
t2m_rs_set = self.test_diffusion_forward(batch,
finetune_decoder=True)
# merge results
rs_set = {
**vae_rs_set,
**diff_rs_set,
"gen_m_rst": t2m_rs_set["m_rst"],
"gen_joints_rst": t2m_rs_set["joints_rst"],
"lat_t": t2m_rs_set["lat_t"],
}
else:
raise ValueError(f"Not support this stage {self.stage}!")
loss = self.losses[split].update(rs_set)
if loss is None:
raise ValueError(
"Loss is None, this happend with torchmetrics > 0.7")
# Compute the metrics - currently evaluate results from text to motion
if split in ["val", "test"]:
if self.condition in ['text', 'text_uncond']:
# use t2m evaluators
rs_set = self.t2m_eval(batch)
elif self.condition == 'action':
# use a2m evaluators
rs_set = self.a2m_eval(batch)
# MultiModality evaluation sperately
if self.trainer.datamodule.is_mm:
metrics_dicts = ['MMMetrics']
else:
metrics_dicts = self.metrics_dict
for metric in metrics_dicts:
if metric == "TemosMetric":
phase = split if split != "val" else "eval"
if eval(f"self.cfg.{phase.upper()}.DATASETS")[0].lower(
) not in [
"humanml3d",
"kit",
]:
raise TypeError(
"APE and AVE metrics only support humanml3d and kit datasets now"
)
getattr(self, metric).update(rs_set["joints_rst"],
rs_set["joints_ref"],
batch["length"])
elif metric == "TM2TMetrics":
getattr(self, metric).update(
# lat_t, latent encoded from diffusion-based text
# lat_rm, latent encoded from reconstructed motion
# lat_m, latent encoded from gt motion
# rs_set['lat_t'], rs_set['lat_rm'], rs_set['lat_m'], batch["length"])
rs_set["lat_t"],
rs_set["lat_rm"],
rs_set["lat_m"],
batch["length"],
)
elif metric == "UncondMetrics":
getattr(self, metric).update(
recmotion_embeddings=rs_set["lat_rm"],
gtmotion_embeddings=rs_set["lat_m"],
lengths=batch["length"],
)
elif metric == "MRMetrics":
getattr(self, metric).update(rs_set["joints_rst"],
rs_set["joints_ref"],
batch["length"])
elif metric == "MMMetrics":
getattr(self, metric).update(rs_set["lat_rm"].unsqueeze(0),
batch["length"])
elif metric == "HUMANACTMetrics":
getattr(self, metric).update(rs_set["m_action"],
rs_set["joints_eval_rst"],
rs_set["joints_eval_ref"],
rs_set["m_lens"])
elif metric == "UESTCMetrics":
# the stgcn model expects rotations only
getattr(self, metric).update(
rs_set["m_action"],
rs_set["m_rst"].view(*rs_set["m_rst"].shape[:-1], 6,
25).permute(0, 3, 2, 1)[:, :-1],
rs_set["m_ref"].view(*rs_set["m_ref"].shape[:-1], 6,
25).permute(0, 3, 2, 1)[:, :-1],
rs_set["m_lens"])
else:
raise TypeError(f"Not support this metric {metric}")
# return forward output rather than loss during test
if split in ["test"]:
return rs_set["joints_rst"], batch["length"]
return loss