egolm-protocol-v2-code / scripts /get_motion_code.py
po03087's picture
EgoLM baseline code (Ego3DLM snapshot, unmodified) + upload notes
3de4238 verified
Raw History Blame Contribute Delete
2.13 kB
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()