Files
TIR_PROJ/training
Johnny Fernandes dd5ac669e5 Webots sim-to-real fixes, DAgger pipeline, 360° proto variant
Today's session worked across the full Webots delivery stack — found and
fixed a cluster of bugs blocking the BC/RL transfer, then explored
training-side mitigations for the residual perception gap.

Bug fixes:
- Makefile FP_RATE default 2.0 → 0.0: BC demos used fp_rate=0 but RL
  fine-tune defaulted to fp_rate=2, poisoning the BC obs distribution
  and stalling PPO at 0% success across 1.46M+ steps.
- controllers/{shepherd_dog,sheep}/runtime.ini: Webots was launching
  controllers under system python3 (no numpy) and they were crashing
  silently. Pinned to the conda tir env.
- herding/config.py HERDING_WEBOTS preset: pen_latch_depth 0.5 → 2.0,
  max_new_tracks_per_step 3 → 1, static_reject 0.8 → 1.2. Stops phantom
  FPs near the gate from latching as permanently-penned tracks.
- herding/perception/sheep_tracker.py: penned tracks now decay at
  forget_steps × 8 instead of living forever. Adds get_positions
  min_freshness filter for deploy-time use.

Training/eval matches deployment:
- training/bc/collect.py: --dagger-policy flag for DAgger rollouts
  (policy drives, teacher labels) + --use-webots-preset for matched
  140° tracker + DR config.
- controllers/shepherd_dog/shepherd_dog.py: scan-fallback (0, 0.6) when
  BC/RL sees empty sheep_positions — recovers from FOV gaps.

Tooling:
- tools/dagger_round.sh: one-shot DAgger round (collect + concat + bc).
- tools/webots_sweep_gt.sh: full sweep with HERDING_USE_GT=1 for the
  perception-gap diagnosis matrix.
- protos/ShepherdDog360.proto: 360° FOV variant for the FOV-ablation
  comparison. Canonical proto stays at 140° per project spec.

Artifacts: v1 BC/RL policies for all 4 (drive × world) combos trained
in clean gym (success: diff/field 90-100%, diff/round 58%, mec/field
60-100%, mec/round 50-100%). DAgger r1/r2 BCs for diff/field show
12%→38% progression on gym HERDING_WEBOTS proxy but did not close
to actual Webots LiDAR (0/5 throughout). Next: LSTM policy or
learned tracker per the project-state memory.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 17:21:02 +00:00
..
2026-05-07 22:00:10 +01:00
2026-05-13 07:49:17 +00:00
2026-05-12 22:41:03 +01:00
2026-05-11 12:21:51 +01:00

Training and Evaluation Details

This file is the command-level companion to the root README. It focuses on data collection, BC, PPO fine-tuning, evaluation flags, and generated artifacts; use the root README for the high-level architecture and Webots demo quick start.

Two stages, strictly sequential:

sim demos (Strömbom on tracker output, K=4 frame stack)
    │
    ▼
bc/pretrain.py  ──►  runs/bc   (Strömbom-imitated MLP)
    │
    ▼  KL-regularised PPO fine-tune
    │
runs/rl                        (deployed `rl` mode — beats BC and Strömbom)

Files

herding_env.py     — Gymnasium env (LiDAR raycast + tracker by default)
bc/pretrain.py     — MSE + cosine BC of (obs, action) demos into MlpPolicy
rl/train.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)

(Unit + integration tests live in the top-level ``tests/`` directory;
run with ``python -m pytest tests/``.)

End-to-end pipeline

The simplest way to run everything is the Makefile at the project root: make does the full chain, make rl rebuilds whatever's needed up to that point, etc. The individual stages below are kept explicit for cases where you want to tune a single step.

# 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 training.bc.collect --teacher strombom \
    --out training/bc/demos.npz --seeds-per-n 15 --subsample 3 --frame-stack 4

# 2. Behaviour-clone.
python -m training.bc.pretrain --demos training/bc/demos.npz \
    --out training/runs/bc --epochs 60 --net-arch 512,512

# 3. KL-regularised PPO fine-tune of bc.
python -m training.rl.train \
    --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.

rl/train.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 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/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 15000 --n-seeds 10
python -m training.eval --policy sequential  --max-flock 10 --max-steps 15000 --n-seeds 10