forked from Mathux/TEMOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interact.py
149 lines (114 loc) · 4.83 KB
/
interact.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
import logging
import hydra
import os
from pathlib import Path
from omegaconf import DictConfig, OmegaConf
import temos.launch.prepare # noqa
logger = logging.getLogger(__name__)
@hydra.main(version_base=None, config_path="configs", config_name="interact")
def _interact(cfg: DictConfig):
return interact(cfg)
def cfg_mean_nsamples_resolution(cfg):
if cfg.mean and cfg.number_of_samples > 1:
logger.error("All the samples will be the mean.. cfg.number_of_samples=1 will be forced.")
cfg.number_of_samples = 1
return cfg.number_of_samples == 1
def load_checkpoint(model, last_ckpt_path, *, eval_mode):
# Load the last checkpoint
# model = model.load_from_checkpoint(last_ckpt_path)
# this will overide values
# for example relative to rots2joints
# So only load state dict is preferable
import torch
model.load_state_dict(torch.load(last_ckpt_path)["state_dict"])
logger.info("Model weights restored.")
if eval_mode:
model.eval()
logger.info("Model in eval mode.")
def interact(newcfg: DictConfig) -> None:
# Load last config
output_dir = Path(hydra.utils.to_absolute_path(newcfg.folder))
last_ckpt_path = newcfg.last_ckpt_path
# Load previous config
prevcfg = OmegaConf.load(output_dir / ".hydra/config.yaml")
# Overload it
cfg = OmegaConf.merge(prevcfg, newcfg)
oneinteract = cfg_mean_nsamples_resolution(cfg)
text = cfg.text
logger.info(f"Interaction script. The result will be saved there: {cfg.saving}")
logger.info(f"The sentence is: {text}")
filename = (text
.lower()
.strip()
.replace(" ", "_")
.replace(".", "") + "_len_" + str(cfg.length)
)
os.makedirs(cfg.saving, exist_ok=True)
path = Path(cfg.saving)
import pytorch_lightning as pl
import numpy as np
import torch
from hydra.utils import instantiate
pl.seed_everything(cfg.seed)
logger.info("Loading model")
if cfg.jointstype == "vertices":
assert cfg.gender in ["male", "female", "neutral"]
logger.info(f"The topology will be {cfg.gender}.")
cfg.model.transforms.rots2joints.gender = cfg.gender
if cfg.transforms.name == "XYZTransform":
nfeats = 64
elif cfg.transforms.name == "SMPLTransform":
nfeats = 135
else:
raise NotImplementedError()
model = instantiate(cfg.model,
nfeats=nfeats,
logger_name="none",
nvids_to_save=None,
_recursive_=False)
logger.info(f"Model '{cfg.model.modelname}' loaded")
load_checkpoint(model, last_ckpt_path, eval_mode=True)
if "amass" in cfg.data.dataname and "xyz" not in cfg.data.dataname:
model.transforms.rots2joints.jointstype = cfg.jointstype
model.sample_mean = cfg.mean
model.fact = cfg.fact
if not model.hparams.vae and cfg.number_of_samples > 1:
raise TypeError("Cannot get more than 1 sample if it is not a VAE.")
from temos.data.tools.collate import collate_text_and_length
from temos.data.sampling import upsample
from rich.progress import Progress
from rich.progress import track
# remove printing for changing the seed
logging.getLogger('pytorch_lightning.utilities.seed').setLevel(logging.WARNING)
import torch
with torch.no_grad():
if True:
# with Progress(transient=True) as progress:
# task = progress.add_task("Sampling", total=len(dataset.keyids))
# progress.update(task, description=f"Sampling {keyid}..")
for index in range(cfg.number_of_samples):
# batch_size = 1 for reproductability
element = {"text": text, "length": cfg.length}
batch = collate_text_and_length([element])
# fix the seed
pl.seed_everything(50 + index)
if cfg.jointstype == "vertices":
vertices = model(batch)[0]
motion = vertices.numpy()
# no upsampling here to keep memory
# vertices = upinteract(vertices, cfg.data.framerate, 100)
else:
joints = model(batch)[0]
motion = joints.numpy()
# upscaling to compare with other methods
motion = upsample(motion, cfg.data.framerate, 100)
if cfg.number_of_samples > 1:
npypath = path / f"{filename}_{index}.npy"
else:
npypath = path / f"{filename}.npy"
np.save(npypath, motion)
# progress.update(task, advance=1)
logger.info("All the sampling are done")
logger.info(f"All the sampling are done. You can find them here:\n{path}")
if __name__ == '__main__':
_interact()