Download code/case2/verify_rom_parity.py from harryzhang1018/TestRandomization: direct link, hf CLI and curl.
- Browser
- Download file 2.89 kB
-
https://huggingface.co/datasets/harryzhang1018/TestRandomization/resolve/main/code/case2/verify_rom_parity.py
- Command line
-
hf download hf://datasets/harryzhang1018/TestRandomization/code/case2/verify_rom_parity.py
-
curl -L -o verify_rom_parity.py https://huggingface.co/datasets/harryzhang1018/TestRandomization/resolve/main/code/case2/verify_rom_parity.py
2.89 kB
| """Parity check: batched nominal ROM (rom_batch.py) vs the release GoalEnv loop (l2_testing.py logic) with SB3 predict. | |
| Needs a checkout of the original repository (github.com/uwsbel/sbel-reproducibility, folder | |
| 2024/PathFollowingSim2real/hmmwv_goal_reach) and the old `gym` package that its training script imports. | |
| Release GoalEnv is imported unchanged; its np.random.normal draws are scaled to zero (nominal ROM). | |
| Goals: 64 seeded goals (seed 77, radius 10-40 m; not goal set A or B). Writes results/rom_parity.json. | |
| Usage: python verify_rom_parity.py /path/to/sbel-reproducibility/2024/PathFollowingSim2real/hmmwv_goal_reach | |
| """ | |
| import io | |
| import json | |
| import sys | |
| from contextlib import redirect_stdout | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| from stable_baselines3 import PPO | |
| from common import RELEASED_DIR, RESULTS, Actor, sha256 | |
| import rom_batch | |
| PROJECT = Path(sys.argv[1]).resolve() | |
| sys.path.insert(0, str(PROJECT)) | |
| from rl_script.rl_train_goal_reach import GoalEnv # noqa: E402 | |
| torch.set_num_threads(1) | |
| def release_loop(model, goal): | |
| orig = np.random.normal | |
| np.random.normal = lambda loc=0.0, scale=1.0, size=None: orig(loc, 0.0 * scale, size) | |
| try: | |
| with redirect_stdout(io.StringIO()): | |
| env = GoalEnv() | |
| env.goal = [goal[0], goal[1]] | |
| obs = env.relative_state_to_goal() | |
| for i in range(400): | |
| a, _ = model.predict(obs, deterministic=True) | |
| obs, _, _, _ = env.step(a) | |
| if env.old_distance < 3.0: | |
| return round((i + 1) * 0.1, 1) | |
| finally: | |
| np.random.normal = orig | |
| return None | |
| def main(): | |
| rng = np.random.default_rng(77) | |
| r, b = rng.uniform(10, 40, 64), rng.uniform(-np.pi, np.pi, 64) | |
| goals = np.array([[float(ri * np.cos(bi)), float(ri * np.sin(bi))] for ri, bi in zip(r, b)]) | |
| report = dict(goals=len(goals), policies={}) | |
| for k in range(1, 5): | |
| path = RELEASED_DIR / f'ppo_{k}.zip' | |
| model = PPO.load(path, device='cpu') | |
| ref = [release_loop(model, g) for g in goals] | |
| out = rom_batch.simulate(Actor(path), goals, {}, noise_seed=0) | |
| mine = [round(float(t), 1) if s else None for s, t in zip(out['success'], out['time_s'])] | |
| diff = [i for i, (a, b) in enumerate(zip(ref, mine)) if a != b] | |
| report['policies'][f'rel{k}'] = dict(sha256=sha256(path), mismatches_vs_release_loop=len(diff), | |
| mismatch_detail=[(i, ref[i], mine[i]) for i in diff]) | |
| print(k, 'mismatches', len(diff), [(i, ref[i], mine[i]) for i in diff][:5], flush=True) | |
| report['note'] = 'Mismatch = different first-entry step between the release loop and the batched ROM.' | |
| RESULTS.mkdir(exist_ok=True) | |
| (RESULTS / 'rom_parity.json').write_text(json.dumps(report, indent=2) + '\n') | |
| if __name__ == '__main__': | |
| main() | |