Checkpoint 6

This commit is contained in:
Johnny Fernandes
2026-05-11 10:35:48 +01:00
parent b457155538
commit fce0e0c786
27 changed files with 194 additions and 704 deletions
+35 -54
View File
@@ -1,21 +1,16 @@
# Training pipeline
Behavior cloning of analytic herding teachers into a neural-network
policy that runs under LiDAR perception in Webots.
Two stages, strictly sequential:
```
sim demos (active-scan teacher on tracker output, K=4 frame stack)
sim demos (Strömbom on tracker output, K=4 frame stack)
bc_pretrain.py ──► runs/bc (BC baseline)
bc_pretrain.py ──► runs/bc (Strömbom-imitated MLP)
▼ KL-regularised PPO fine-tune (training/train_ppo.py)
▼ KL-regularised PPO fine-tune
runs/rl (deployed `rl` mode)
# optional branch — kept for reference, not deployed:
runs/bc_dagger (Webots-grounded DAgger refinement, useful if a
modified world breaks sim-to-real transfer)
runs/rl (deployed `rl` mode — beats BC and Strömbom)
```
## Files
@@ -23,10 +18,9 @@ runs/bc_dagger (Webots-grounded DAgger refinement, useful if a
```
herding_env.py — Gymnasium env (LiDAR raycast + tracker by default)
bc_pretrain.py — MSE + cosine BC of (obs, action) demos into MlpPolicy
eval.py — analytic teachers + BC policies, full n=1..10 grid
parity_test.py — shape / determinism / baseline smoke test
runs/ — checkpoints (most are .gitignored; the deployed
ones are whitelisted in the top-level .gitignore)
train_ppo.py — KL-regularised PPO fine-tune of a BC checkpoint
eval.py — multi-seed analytic / learned policy comparison
runs/ — checkpoints (whitelisted entries in top-level .gitignore)
```
## Setup
@@ -39,75 +33,62 @@ CPU is the default and recommended device — SB3 PPO with an MLP policy
of this size runs faster on CPU than GPU because the bottleneck is
rollout collection, not gradient compute.
## The BC pipeline
## End-to-end pipeline
```
```bash
# 1. Sim demos with the active-scan + Strömbom teacher under LiDAR
# perception. K=4 frame stack so the MLP has temporal context.
python -m tools.collect_demos --teacher strombom \
--out demos.npz --seeds-per-n 15 --subsample 3 --frame-stack 4
--out training/demos.npz --seeds-per-n 15 --subsample 3 --frame-stack 4
# 2. Behavior-clone.
python -m training.bc_pretrain --demos demos.npz \
--out runs/bc --epochs 60 --net-arch 512,512
# 2. Behaviour-clone.
python -m training.bc_pretrain --demos training/demos.npz \
--out training/runs/bc --epochs 60 --net-arch 512,512
# 3. Evaluate.
python -m training.eval --policy runs/bc \
--max-flock 10 --max-steps 8000 --n-seeds 5
# 3. KL-regularised PPO fine-tune of bc.
python -m training.train_ppo \
--bc training/runs/bc --out training/runs/rl \
--total-timesteps 1000000
# 4. Multi-seed eval (env-side, fast).
python -m training.eval --policy training/runs/rl \
--max-flock 10 --max-steps 15000 --n-seeds 10
```
`bc_pretrain.py` saves the **best-val_cos** snapshot, not the final
epoch — multi-modal teachers make training noisy and the last epoch is
often worse than an earlier one.
## DAgger from Webots
Sim-only BC plateaus because the env's 2D raycast can't reproduce all
the false-positive clusters Webots generates from real geometry. The
fix is to collect (obs, teacher_action) pairs from inside Webots:
```
# Headless DAgger collection: 5 flock sizes × 3 runs each.
tools/auto_dagger.sh 3 60
# Merge with the sim baseline + retrain.
python -m tools.dagger_merge_train --out runs/bc_dagger
```
Iterate by re-running collection with the new student in the driver's
seat:
```
HERDING_POLICY_DIR=$PWD/training/runs/bc_dagger \
HERDING_DAGGER_DRIVER=student \
tools/auto_dagger.sh 3 60
python -m tools.dagger_merge_train --out runs/bc_dagger
```
`train_ppo.py` loads BC weights into both a trainable policy and a
frozen reference, fixes `log_std` small, and adds `β · KL(π‖π_ref)` to
the loss so the policy can only move within a trust region around BC.
See the file header for hyperparameter rationale.
## Available analytic teachers
| Name | What it does | Notes |
|---|---|---|
| `strombom` | Canonical Strömbom — collect when flock is scattered, drive CoM otherwise | Default; works well for n=110 under tight cohesion |
| `strombom` | Strömbom 2014 — collect when flock is scattered, drive CoM otherwise | Default; works for n=110 under tight cohesion |
| `sequential` | Pick the sheep closest to the pen and drive only it | Alternative; needs loose-cohesion regime |
Both are wrapped at demo-collection time in
`herding/active_scan.py:ActiveScanTeacher`, which adds an opening
in-place rotation, walk-to-centre when the LiDAR sees nothing, and
near-sheep speed modulation (the same modulation `herding/control.py`
applies to every dog mode at inference).
`herding/control/active_scan.py:ActiveScanTeacher`, which adds an
opening in-place rotation, walk-to-centre when the LiDAR sees
nothing, and near-sheep speed modulation (same modulation
`herding/control/modulation.py` applies to every dog mode at
inference).
## Evaluating analytic teachers directly
```
python -m training.eval --policy strombom --max-flock 10 --max-steps 8000 --n-seeds 5
python -m training.eval --policy sequential --max-flock 10 --max-steps 8000 --n-seeds 5
python -m training.eval --policy strombom --max-flock 10 --max-steps 15000 --n-seeds 10
python -m training.eval --policy sequential --max-flock 10 --max-steps 15000 --n-seeds 10
```
## Webots inference
```
tools/run_webots.sh 10 rl
tools/run_webots.sh 10 bc # or rl, strombom, sequential
```
The dog controller loads `runs/bc` for `bc` mode and `runs/rl` for