MinPair / examples /leg_height.py
lschiweck's picture
MinPair model, inference code and model card
e58a265 verified
Raw History Blame Contribute Delete
2.04 kB
"""Leg height example: one word changes, the noise stays the same, three motions.
python examples/leg_height.py [--model <local folder or repo id>] [--out leg_height_output]
Generates the part caption in leg_height.json with the left leg lifting to ankle, knee and hip height, saves each motion
as .npz and prints how high the left foot rises above its standing level, in standing knee heights of the same body.
"""
import argparse
import json
import sys
from pathlib import Path
import torch
from transformers import AutoModel
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
from measure_foot_height import foot_height # noqa: E402
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--model", default=str(HERE.parent), help="local folder or Hugging Face repo id")
ap.add_argument("--out", default="leg_height_output")
ap.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu")
a = ap.parse_args()
spec = json.loads((HERE / "leg_height.json").read_text())
model = AutoModel.from_pretrained(a.model, trust_remote_code=True).to(a.device)
prompts = []
for word in spec["values"]:
p = json.loads(json.dumps(spec["prompt"]).replace("{}", word))
p["id"] = f"leg_{word}"
prompts.append(p)
motions = model.generate_motion(prompts)
out = Path(a.out)
out.mkdir(parents=True, exist_ok=True)
m = spec["measure"]
print(f"{'height word':12s} {'foot lift (cm)':>14s} {'knee heights':>12s}")
results = {}
for word, motion in zip(spec["values"], motions):
motion.save(out / f"{motion.id}.npz")
r = foot_height(motion.joints, motion.fps, m["side"], m["start"], m["end"])
results[word] = {k: round(v, 4) for k, v in r.items()}
print(f"{word:12s} {100 * r['lift_m']:14.1f} {r['lift_knees']:12.2f}")
(out / "foot_height.json").write_text(json.dumps(results, indent=1) + "\n")
print(f"motions and measurements in {out}/")
if __name__ == "__main__":
main()