xlerobot-sim / tools /eval_policy.mjs
VectorW's picture
XLeRobot Sim 0.2: MuJoCo-GS-Web's robot and scenes, a kitchen, skills, a learned policy and an agent
7a35d14 verified
Raw History Blame Contribute Delete
2 kB
// Evaluate an exported pick policy in Node with the page's own code: ONNX Runtime Web's WebAssembly
// backend, src/policy/runner.js and the same physics. Layouts use seeds the demonstrations did not.
//
// usage: node tools/eval_policy.mjs [public/policies/xlerobot_pick] [episodes=50] [seed=100000]
import { readFileSync } from "node:fs";
import { join } from "node:path";
import * as ort from "onnxruntime-web";
import { ROOT } from "./node_env.mjs";
import { pickScene } from "./pick_layout.mjs";
import { makeRng } from "../src/core/sim.js";
import { runSkillAsync } from "../src/robots/skills.js";
import { PolicyRunner, policyPick } from "../src/policy/runner.js";
const dir = process.argv[2] ?? join(ROOT, "public", "policies", "xlerobot_pick");
const episodes = Number(process.argv[3] ?? 50);
const seed0 = Number(process.argv[4] ?? 100000);
ort.env.wasm.numThreads = 1;
const meta = JSON.parse(readFileSync(join(dir, "policy.json"), "utf8"));
const runner = await PolicyRunner.load(ort, readFileSync(join(dir, meta.onnx)), meta);
const { sim, robot, layout } = await pickScene();
let ok = 0;
let n = 0;
const byObject = {};
let inferMs = 0;
let inferences = 0;
for (let s = seed0; n < episodes; s++) {
const ep = layout(makeRng(s));
if (!ep) continue;
n++;
const predict = runner.predict.bind(runner);
runner.predict = async (obs) => {
const out = await predict(obs);
inferMs += runner.inferenceMs;
inferences++;
return out;
};
const result = await runSkillAsync(sim, robot, policyPick(robot, runner, ep.id, ep.side), { maxSeconds: 14 });
runner.predict = predict;
const key = `${ep.name}/${ep.side}`;
byObject[key] ??= [0, 0];
byObject[key][1]++;
if (result.ok) {
ok++;
byObject[key][0]++;
}
}
console.log(`policy picked ${ok}/${episodes} (${((ok / episodes) * 100).toFixed(0)}%), ${(inferMs / inferences).toFixed(1)} ms per inference in Node`);
console.log(Object.entries(byObject).map(([k, [a, b]]) => `${k} ${a}/${b}`).join(", "));