File size: 2,131 Bytes
3de4238 | 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 | import os
import numpy as np
import pytorch_lightning as pl
import torch
from pathlib import Path
from tqdm import tqdm
from mGPT.config_egovlm import parse_args
# from mGPT.config import parse_args
from mGPT.data.build_data import build_data
from mGPT.models.build_model import build_model
from mGPT.utils.load_checkpoint import load_pretrained_vae
def main():
# parse options
cfg = parse_args(phase="test") # parse config file
cfg.TRAIN.STAGE = "token"
cfg.TRAIN.BATCH_SIZE = 1
# set seed
pl.seed_everything(cfg.SEED_VALUE)
# gpu setting
if cfg.ACCELERATOR == "gpu":
os.environ["PYTHONWARNINGS"] = "ignore"
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# create dataset
datasets = build_data(cfg, phase='token')
print("datasets module initialized")
output_dir = os.path.join(datasets.hparams.data_root, cfg.DATASET.CODE_PATH)
os.makedirs(output_dir, exist_ok=True)
# create model
model = build_model(cfg, datasets)
if hasattr(model, "motion_vae"):
model.vae = model.motion_vae
print("model loaded")
# Strict load vae model
assert cfg.TRAIN.PRETRAINED_VAE is not None
load_pretrained_vae(cfg, model)
if cfg.ACCELERATOR == "gpu":
model = model.to('cuda')
unit_len = cfg.DATASET.UNIT_LEN
for batch in tqdm(datasets.train_dataloader(),
desc=f'motion tokenize'):
name = batch['text']
pose = batch['motion']
# pose_len = [(pose[i]//unit_len)*unit_len for i in range(pose.shape[0])]
# pose = [pose[i, :pose_len[i]] for i in range(pose.shape[0])]
pose = pose.cuda().float()
if pose.shape[1] == 0:
continue
target, _ = model.vae.encode(pose)
target = target.to('cpu').numpy()
target_path = os.path.join(output_dir, name[0] + '.npy')
Path(target_path).parent.mkdir(parents=True, exist_ok=True)
np.save(target_path, target)
print(
f'Motion tokenization done, the motion tokens are saved to {output_dir}'
)
if __name__ == "__main__":
main()
|