-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_mardm.py
More file actions
190 lines (160 loc) · 6.04 KB
/
train_mardm.py
File metadata and controls
190 lines (160 loc) · 6.04 KB
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
#
# Copyright 2025-present by A. Mathis Group and contributors. All rights reserved.
#
# This project and all its files are licensed under the MIT License.
# A copy is included in LICENSE.
#
import copy
import os
from os.path import join as pjoin
import numpy as np
import torch
from data.kitchen_dataset import Text2MotionDataset
from models.mardm.AE import AE_models
from models.mardm.MARDM import MARDM_models
from models.mask_transformer.transformer_trainer import MARDMTrainer
from models.t2m_eval_wrapper import EvaluatorModelWrapper
from motion_loaders.dataset_motion_loader import get_kitchen_motion_loader
from options.train_option import TrainT2MOptions
from torch.utils.data import DataLoader
from utils.fixseed import fixseed
from utils.get_opt import get_opt
from utils.motion_process import recover_from_ric
from utils.paramUtil import kit_kinematic_chain, t2m_all_chain, t2m_kinematic_chain
from utils.plot_script import plot_3d_motion
def plot_t2m(data, save_dir, captions, m_lengths):
data = train_dataset.inv_transform(data)
for i, (caption, joint_data) in enumerate(zip(captions, data)):
joint_data = joint_data[: m_lengths[i]]
joint = recover_from_ric(
torch.from_numpy(joint_data).float(), opt.joints_num
).numpy()
save_path = pjoin(save_dir, "%02d.mp4" % i)
plot_3d_motion(
save_path, kinematic_chain, joint, title=caption, fps=fps, radius=radius
)
def load_vq_model():
opt_path = pjoin(opt.checkpoints_dir, opt.dataset_name, opt.vq_name, "opt.txt")
vq_opt = get_opt(opt_path, opt.device)
vq_model = AE_models["AE_Model"](input_width=vq_opt.dim_pose)
ckpt = torch.load(
pjoin(
vq_opt.checkpoints_dir,
vq_opt.dataset_name,
vq_opt.name,
"model",
"net_best_fid.tar",
),
map_location="cpu",
)
model_key = "vq_model" if "vq_model" in ckpt else "net"
vq_model.load_state_dict(ckpt[model_key])
print(f"Loading VQ Model {opt.vq_name}")
return vq_model, vq_opt
if __name__ == "__main__":
parser = TrainT2MOptions()
opt = parser.parse()
fixseed(opt.seed)
opt.device = torch.device("cpu" if opt.gpu_id == -1 else "cuda:" + str(opt.gpu_id))
torch.autograd.set_detect_anomaly(True)
opt.save_root = pjoin(opt.checkpoints_dir, opt.dataset_name, opt.name)
opt.model_dir = pjoin(opt.save_root, "model")
opt.eval_dir = pjoin(opt.save_root, "animation")
opt.log_dir = pjoin("./log/mardm/", opt.dataset_name, opt.name)
os.makedirs(opt.model_dir, exist_ok=True)
os.makedirs(opt.eval_dir, exist_ok=True)
os.makedirs(opt.log_dir, exist_ok=True)
if opt.dataset_name == "t2m":
opt.data_root = "./dataset/HumanML3D"
opt.motion_dir = pjoin(opt.data_root, "new_joint_vecs")
opt.joints_num = 22
opt.max_motion_len = 55
dim_pose = 263
radius = 4
fps = 20
kinematic_chain = t2m_kinematic_chain
dataset_opt_path = "./checkpoints/t2m/Comp_v6_KLD005/opt.txt"
elif opt.dataset_name == "kitchen":
opt.data_root = "dataset/Kitchen"
opt.motion_dir = pjoin(opt.data_root, "new_joint_vecs")
opt.joints_num = 22
opt.max_motion_len = 55
dim_pose = 263
fps = 30
radius = 4
kinematic_chain = t2m_kinematic_chain
dataset_opt_path = "./checkpoints/kitchen/Comp_v6_KLD005/opt.txt"
elif opt.dataset_name == "kitchen_new":
opt.data_root = "dataset/New_kitchen_data"
opt.motion_dir = pjoin(opt.data_root, "new_joint_vecs")
opt.img_dir = pjoin(opt.data_root, "holo_images")
opt.joints_num = 52
opt.max_motion_len = 55
dim_pose = 327
fps = 30
opt.radius = 4
kinematic_chain = t2m_all_chain
dataset_opt_path = "./checkpoints/kitchen_new/Comp_v6_KLD005/opt.txt"
opt.clip_version = "ViT-B/32"
elif opt.dataset_name == "kit": # TODO
opt.data_root = "./dataset/KIT-ML"
opt.motion_dir = pjoin(opt.data_root, "new_joint_vecs")
opt.joints_num = 21
radius = 240 * 8
fps = 12.5
dim_pose = 251
opt.max_motion_len = 55
kinematic_chain = kit_kinematic_chain
dataset_opt_path = "./checkpoints/kit/Comp_v6_KLD005/opt.txt"
else:
raise KeyError("Dataset Does Not Exist")
opt.text_dir = "dataset/Kitchen/Kitchen/Annotations"
vq_model, vq_opt = load_vq_model()
mardm = MARDM_models[opt.model_type](ae_dim=vq_opt.code_dim, cond_mode="text")
ema_mardm = copy.deepcopy(mardm)
ema_mardm.eval()
for param in ema_mardm.parameters():
param.requires_grad_(False)
all_params = 0
pc_transformer = sum(
param.numel()
for param in [
p
for name, p in mardm.named_parameters()
if not name.startswith("clip_model.")
]
)
all_params += pc_transformer
print("Total parameters of all models: {:.2f}M".format(all_params / 1000_000))
mean = np.load(pjoin(opt.data_root, "Mean.npy"))
std = np.load(pjoin(opt.data_root, "Std.npy"))
train_dataset = Text2MotionDataset(opt, mean, std, "train", opt.motion_type)
val_dataset = Text2MotionDataset(opt, mean, std, "test", opt.motion_type)
train_loader = DataLoader(
train_dataset,
batch_size=opt.batch_size,
num_workers=4,
shuffle=True,
drop_last=True,
)
val_loader = DataLoader(
val_dataset,
batch_size=opt.batch_size,
num_workers=4,
shuffle=True,
drop_last=True,
)
eval_val_loader, _ = get_kitchen_motion_loader(
dataset_opt_path, opt.batch_size, opt.motion_type, device=opt.device
)
wrapper_opt = get_opt(dataset_opt_path, torch.device("cuda"))
wrapper_opt.motion_type = opt.motion_type
eval_wrapper = EvaluatorModelWrapper(wrapper_opt)
trainer = MARDMTrainer(opt, mardm, vq_model, ema_mardm)
trainer.train(
train_loader,
val_loader,
eval_val_loader,
eval_wrapper=eval_wrapper,
plot_eval=plot_t2m,
)