DronePilot / tools /export_model.py
edgarmoreau's picture
Uniform pilot names (CablePilot, DronePilot, ArmPilot) under MacroSwan; links to the moved repos
44ee0a5 verified
Raw History Blame Contribute Delete
2.42 kB
"""Export the SkyJEPA 5k reference (dronepilot models/skyjepa_quad/full = HF MacroSwan/skyjepa) to JSON for the browser,
plus a PyTorch rollout the JS port is checked against (tools/test.mjs).
cd ~/Projects/dronepilot && uv run python ../pilothub/skyjepa-course/tools/export_model.py
"""
import json
from pathlib import Path
import numpy as np
import torch
from dronepilot.bodies.skyjepa_quad.body import SkyJEPAPlant, SkyJEPAQuad
from dronepilot.bodies.skyjepa_quad.sim.quadrotor import HOVER_THRUST
OUT = Path(__file__).resolve().parents[1]
CK = Path("models/skyjepa_quad/full")
jk = torch.load(CK / "jepa.pt", map_location="cpu", weights_only=False)
pk = torch.load(CK / "prober.pt", map_location="cpu", weights_only=False)
t = lambda v: np.asarray(v.detach().float()).round(8).tolist() # noqa: E731
model = {"jepa": {k: t(v) for k, v in jk["model"].items()}, "prober": {k: t(v) for k, v in pk["prober"].items()},
"norm": {k: t(v) for k, v in jk["normalizer"].items()}, "history": int(jk["config"].get("history", 10)),
"source": "MacroSwan/skyjepa (full profile, 5k trajectories): clean-room SkyJEPA, Rao et al. 2026, arXiv:2606.23444"}
(OUT / "core/model.json").write_text(json.dumps(model))
# a short real flight on the plant, then a model rollout of random plans from there
body = SkyJEPAQuad()
wm = body.load_model("full")
plant = SkyJEPAPlant(body.initial_state([1.0, -2.0, 15.0]))
rng = np.random.default_rng(0)
xs, us = [plant.state.copy()], []
for k in range(14):
a = HOVER_THRUST + np.array([0.3, -0.1, 0.2, -0.2]) * np.sin(0.4 * k) + rng.normal(0, 0.05, 4)
us.append(a)
xs.append(plant.step(a))
xs, us = np.array(xs), np.array(us)
sh, ah = xs[-11:], us[-10:]
fut = (HOVER_THRUST + rng.normal(0, 0.6, (3, 20, 4))).astype(np.float32)
out = wm.rollout(sh, ah, fut).numpy()
# the plant too: the same first plan flown for real (JS plant check)
p2 = SkyJEPAPlant(xs[-1])
p2.motor_f = plant.motor_f.copy()
truth = np.array([p2.step(a) for a in fut[0]])
fx = {"state_hist": sh.tolist(), "action_hist": ah.tolist(), "future": fut.tolist(), "rollout": out.tolist(),
"motor_f": plant.motor_f.tolist(), "plant": truth.tolist()}
(OUT / "tools/fixtures.json").write_text(json.dumps(fx))
print("model.json", (OUT / "core/model.json").stat().st_size // 1024, "KB; rollout", out.shape,
"| 1 s model vs plant, plan 0:", np.round(out[0, -1, :3], 3), np.round(truth[-1, :3], 3))