Download chess-sim/code/sim/set_norm_stats.py from Machanize/playful: direct link, hf CLI and curl.
- Browser
- Download file 2.35 kB
-
https://huggingface.co/datasets/Machanize/playful/resolve/main/chess-sim/code/sim/set_norm_stats.py
- Command line
-
hf download hf://datasets/Machanize/playful/chess-sim/code/sim/set_norm_stats.py
-
curl -L -o set_norm_stats.py https://huggingface.co/datasets/Machanize/playful/resolve/main/chess-sim/code/sim/set_norm_stats.py
2.35 kB
| """Make a fine-tune keep a checkpoint's normalization. | |
| When `lerobot-train` fine-tunes from a checkpoint it replaces the checkpoint's saved | |
| state/action normalization with the training dataset's statistics | |
| (lerobot_train.py passes them as processor overrides, and those take precedence). | |
| Merging correction data into a dataset changes those statistics, which by itself | |
| shifts the policy's commands by millimetres at the grasp (sim/reports/pipeline_check). | |
| This writes the checkpoint's saved statistics for `observation.state` and `action` | |
| into the dataset's meta/stats.json (the original is kept as meta/stats_original.json), | |
| so the override hands the checkpoint its own normalization back. Image statistics are | |
| left alone (SmolVLA does not normalize images). | |
| Run: .venv/bin/python sim/set_norm_stats.py data/merged_v2 models/baseline | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import os | |
| import shutil | |
| from pathlib import Path | |
| def saved_stats(checkpoint: Path) -> dict: | |
| from safetensors.torch import load_file | |
| f = next(checkpoint.glob("policy_preprocessor_step_*_normalizer_processor.safetensors")) | |
| out: dict = {} | |
| for key, tensor in load_file(str(f)).items(): | |
| feature, stat = key.rsplit(".", 1) | |
| out.setdefault(feature, {})[stat] = tensor.float().numpy().tolist() | |
| return out | |
| def main(): | |
| ap = argparse.ArgumentParser(description=__doc__) | |
| ap.add_argument("dataset") | |
| ap.add_argument("checkpoint") | |
| args = ap.parse_args() | |
| meta = Path(args.dataset) / "meta" | |
| stats_path = meta / "stats.json" | |
| backup = meta / "stats_original.json" | |
| if not backup.exists(): | |
| shutil.copyfile(stats_path, backup) | |
| stats = json.loads(backup.read_text()) | |
| saved = saved_stats(Path(args.checkpoint)) | |
| for feature in ("observation.state", "action"): | |
| for stat, value in saved[feature].items(): | |
| if stat in stats[feature]: | |
| stats[feature][stat] = value | |
| tmp = meta / "stats.json.tmp" # a new file, not an in-place edit: hard-linked copies stay intact | |
| tmp.write_text(json.dumps(stats, indent=2)) | |
| os.replace(tmp, stats_path) | |
| for feature in ("observation.state", "action"): | |
| print(feature, {s: [round(x, 3) for x in stats[feature][s]] for s in ("mean", "std")}) | |
| if __name__ == "__main__": | |
| main() | |