ConsDrive / scripts /ablate_nodet_nofold.sh
ZenoNing's picture
Add nodetfold1 policy checkpoint, StreamPETR detector, and ablation scripts
95aa6cf verified
Raw History Blame Contribute Delete
10.1 kB
#!/bin/bash
# Ablation of the perception chain of the best configuration (sp_ft_det4hz_nomask, 5 original runs + 2 runs of
# reproduction repro1, mean 91.5): drop det_v1 and the cross-fitted fold detectors.
# original : FastVLM -> det_v1 (30k) -> sp_v1 (30k) -> sp_fold0/1 (30k each) -> out-of-fold banks -> tracker_sp
# this run : FastVLM ---------------> sp_direct (SP_STEPS) ------------------> in-sample banks -> tracker_sp
# sp_direct is both the bank generator and the closed-loop detector. The policy line is not retrained: tracker_sp
# starts from the original speed-dropout checkpoint, so the perception chain is the only difference.
#
# setsid nohup bash scripts/ablate_nodet_nofold.sh > /tmp/ablate_nodet_nofold.log 2>&1 < /dev/null &
# tail -f outputs/ablate_<tag>/chain.log
#
# Stages (8x RTX PRO 6000):
# sp_direct : train_stream_petr.py --init pretrained/FastVLM-0.5B, sp_v1 hyper-parameters ~7 h at 30k
# banks : tracker banks of all routes through sp_direct, 16 shards ~2.5 h
# tracker_sp: stage 3 fine-tune on the banks, 1 epoch, init SPEEDDROP_CKPT ~40 min
# eval : N_EVAL_REPEATS concurrent 220-route evals with sp_direct at 4 Hz, no speed mask ~1 h
# Total about 11 h.
#
# Knobs (environment):
# ABLATE_TAG=nodetfold1 suffix of every output dir
# SP_STEPS=30000 sp_v1 used 30k on top of det_v1's 30k; 60000 matches the original detector compute
# SPEEDDROP_CKPT=... default: the original speed-dropout epoch 2 (the init of the 5 original runs)
# N_EVAL_REPEATS=3 concurrent evals (pid limit allows at most 3)
# SEED=42 DET_SEED=0 Lightning seed / StreamPETR seed (the originals)
# FORCE=1 start even if other trainings / evals are running
# Afterwards: python scripts/analysis/compare_nodet_nofold.py --tag <tag>
set -u
cd /workspace/code/ConsDrive_v2_eval
PY=/venv/fastvlm/bin/python
REPO=/workspace/code/ConsDrive_v2_eval
TAG=${ABLATE_TAG:-nodetfold1}
SP_STEPS=${SP_STEPS:-30000}
SPEEDDROP_CKPT=${SPEEDDROP_CKPT:-$REPO/outputs/fastvlm_stage3_speeddrop/2026-11-02_06-08-03/checkpoints/epoch=002.ckpt}
N_EVAL_REPEATS=${N_EVAL_REPEATS:-3}
SEED=${SEED:-42}
DET_SEED=${DET_SEED:-0}
STATE=outputs/ablate_$TAG
mkdir -p $STATE eval_results/tracker_offline eval_results/Bench2Drive
LOG=$STATE/chain.log
log() { echo "[$(date '+%F %T')] $*" | tee -a $LOG; }
die() { log "FATAL: $*"; exit 1; }
free_gb() { df -BG --output=avail /workspace | tail -n 1 | tr -dc '0-9'; }
require_disk() { [ "$(free_gb)" -ge "$1" ] || die "only $(free_gb) GB free on /workspace, need $1 GB"; }
SP_DIRECT=outputs/stream_petr/sp_direct_$TAG
TRACKER_SP=outputs/fastvlm_stage3_tracker_sp/$TAG
BANK_FOLDER=tracker_bank_sp_$TAG
DETS_FOLDER=tracker_dets_sp_$TAG
OFFLINE=eval_results/tracker_offline
B=eval_results/Bench2Drive
export FASTVLM_SOURCE=${FASTVLM_SOURCE:-/workspace/code/ml-fastvlm}
export FASTVLM_CHECKPOINT=${FASTVLM_CHECKPOINT:-$REPO/pretrained/FastVLM-0.5B}
# Dataloader workers otherwise size BLAS/OpenCV pools to all 256 cores (pid limit, see run_train_fastvlm_stage3.sh).
export OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 OPENCV_FOR_THREADS_NUM=1
# ------------------------------------------------------------------ preflight
if [ "${FORCE:-0}" != "1" ]; then
if pgrep -f "train_fastvlm.py|train_stream_petr.py|train_detector.py|eval_tracker_offline.py" > /dev/null \
|| tmux ls 2>/dev/null | grep -q '^b2d220_'; then
die "other trainings or evals are running (pgrep / tmux ls); finish them first or set FORCE=1"
fi
fi
[ "$N_EVAL_REPEATS" -le 3 ] || die "N_EVAL_REPEATS=$N_EVAL_REPEATS: the container pid limit allows at most 3 concurrent evals"
require_disk 30
[ -d pretrained/FastVLM-0.5B ] || die "pretrained/FastVLM-0.5B missing"
[ -f "$SPEEDDROP_CKPT" ] || die "SPEEDDROP_CKPT $SPEEDDROP_CKPT missing"
[ -d simlingo_data/object_bank ] || die "simlingo_data/object_bank missing (scripts/build_object_bank.py)"
[ -d simlingo_data/detection_labels ] || die "simlingo_data/detection_labels missing (scripts/build_detection_labels.py)"
N_ORACLE_BANKS=$(find simlingo_data/object_bank -name objects.npz | wc -l)
log "ablation '$TAG' (no det_v1, no folds): sp_direct $SP_STEPS steps, $N_EVAL_REPEATS evals, $(free_gb) GB free"
cat > $STATE/config.env <<EOF
TAG=$TAG SP_STEPS=$SP_STEPS N_EVAL_REPEATS=$N_EVAL_REPEATS SEED=$SEED DET_SEED=$DET_SEED
SPEEDDROP_CKPT=$SPEEDDROP_CKPT
EOF
# ================================================================== 1. StreamPETR straight from FastVLM-0.5B
if [ -f $SP_DIRECT/DONE ]; then
log "sp_direct: done"
else
mkdir -p $SP_DIRECT
resume=()
final=$(printf '%s/val_%06d.json' $SP_DIRECT $SP_STEPS)
if [ -f $SP_DIRECT/last.pt ] && [ ! -f "$final" ]; then
log "sp_direct: resuming from $SP_DIRECT/last.pt"; resume=(--resume $SP_DIRECT/last.pt)
fi
log "sp_direct: training -> $SP_DIRECT on 8 GPUs ($SP_STEPS steps, init pretrained/FastVLM-0.5B)"
$PY -m torch.distributed.run --nproc_per_node=8 --master_port=29630 train_stream_petr.py --out $SP_DIRECT \
--init pretrained/FastVLM-0.5B --steps $SP_STEPS --val-every 2000 --batch 8 --workers 10 --seed $DET_SEED \
"${resume[@]}" >> $SP_DIRECT/train.log 2>&1
rc=$?
echo "TRAIN_EXIT=$rc" >> $SP_DIRECT/train.log
[ $rc -eq 0 ] && [ -f "$final" ] || die "sp_direct failed (exit $rc); see $SP_DIRECT/train.log"
date > $SP_DIRECT/DONE
fi
# ================================================================== 2. in-sample tracker banks of all routes
if [ -f $STATE/banks.done ]; then
log "banks: done"
else
log "tracker banks -> simlingo_data/$BANK_FOLDER (16 shards, every route through sp_direct)"
for s in $(seq 0 15); do
CUDA_VISIBLE_DEVICES=$((s / 2)) OMP_NUM_THREADS=4 $PY scripts/eval_tracker_offline.py \
--detector $SP_DIRECT/last.pt --out $OFFLINE/banks_${TAG} --split all --shard $s/16 \
--write-banks --bank-folder $BANK_FOLDER --skip-existing --decode-threads 6 \
--save-detections --detections-folder $DETS_FOLDER > $OFFLINE/banks_${TAG}_shard$s.log 2>&1 &
done
wait
n_bank=$(find simlingo_data/$BANK_FOLDER -name objects.npz | wc -l)
log "tracker banks: $n_bank / $N_ORACLE_BANKS"
[ "$n_bank" -eq "$N_ORACLE_BANKS" ] || die "bank coverage incomplete (re-run the script: --skip-existing resumes)"
date > $STATE/banks.done
fi
# ================================================================== 3. stage 3 fine-tune on the banks
FINAL_CKPT=$TRACKER_SP/checkpoints/epoch=000.ckpt
if [ -f $TRACKER_SP/DONE ]; then
log "tracker_sp: done ($FINAL_CKPT)"
else
mkdir -p $TRACKER_SP
overrides=()
if [ -f $TRACKER_SP/checkpoints/last.ckpt ] && [ ! -f $FINAL_CKPT ]; then
log "tracker_sp: resuming from $TRACKER_SP/checkpoints/last.ckpt"
overrides=("resume_checkpoint=$REPO/$TRACKER_SP/checkpoints/last.ckpt")
fi
log "tracker_sp: training -> $TRACKER_SP (init $SPEEDDROP_CKPT)"
CONFIG_NAME=fastvlm_stage3_tracker STAGE3_CHECKPOINT=$SPEEDDROP_CKPT TRACKER_BANK_FOLDER=$BANK_FOLDER \
OUTPUT_DIR=$TRACKER_SP bash scripts/run_train_fastvlm_stage3.sh "seed=$SEED" "${overrides[@]}" > /dev/null 2>&1
rc=$?
echo "TRAIN_EXIT=$rc" >> $TRACKER_SP/train.log
[ $rc -eq 0 ] && [ -f $FINAL_CKPT ] || die "tracker_sp failed (exit $rc); see $TRACKER_SP/train.log"
rm -f $TRACKER_SP/checkpoints/last.ckpt
grep -o "val/loss=[0-9.]*" $TRACKER_SP/train.log | tail -n 1 | sed "s/^/tracker_sp final /" | tee -a $LOG
date > $TRACKER_SP/DONE
fi
# ================================================================== 4. closed-loop evals
slots() { # eval index k, slot count n (two workers per slot); same GPU/CARLA rotation as the original chains
local k=$1 n=$2 s="" j
for ((j = 0; j < n; j++)); do s+="$(( (j + 2 * k) % 8 )):$(( (j + k) % 7 )) "; done
echo "${s% }"
}
ds_of() { $PY -c "import json; d=json.load(open('$1/merged.json')); print(f\"DS {d['driving score']:.2f} SR {d['success rate']*100:.2f} n={d['eval num']}\")" 2>/dev/null; }
merge_eval() { # merge a finished run (all 16 workers exited); returns 1 while it is still incomplete
local dir=$1
[ -f $dir/merged.json ] && return 0
[ "$(cat $dir/logs/w*.log 2>/dev/null | grep -c WORKER_EXIT)" -ge 16 ] || return 1
$PY merge_route_json.py -f $dir > /dev/null 2>&1 && [ -f $dir/merged.json ]
}
report_eval() {
local dir=$1
local bad; bad=$(grep -l "out of memory\|can't start new thread\|Retry limit exceeded" $dir/logs/w*.log 2>/dev/null | wc -l)
log "eval $(basename $dir): $(ds_of $dir)$([ "$bad" -gt 0 ] && echo " WARNING: $bad worker logs with OOM / thread / retry-limit failures, check before trusting")"
}
DET_ENV="OBJECT_SOURCE=detector DETECTOR_CKPT=$REPO/$SP_DIRECT/last.pt DETECTOR_UPDATE_EVERY=5"
if tmux ls 2>/dev/null | grep -q "^b2d220_${TAG}_"; then # evals of a previous launch still running
log "waiting for running evals of '$TAG'"
while tmux ls 2>/dev/null | grep -q "^b2d220_${TAG}_"; do sleep 60; done
fi
require_disk 10
launched=0
for ((k = 1; k <= N_EVAL_REPEATS; k++)); do
dir=$B/${TAG}_sp_ft_nomask_r$k
if merge_eval $dir; then log "eval ${TAG}_sp_ft_nomask_r$k: done ($(ds_of $dir))"; continue; fi
rm -rf $dir
RUN_DIR=$TRACKER_SP RESULT_ROOT=$dir SLOTS="$(slots $((k - 1)) 8)" TAG=${TAG}_sp_ft_nomask_r$k TRAJ_VIZ=0 \
PORT_BASE=$((20000 + 2000 * (k - 1))) TM_PORT_BASE=$((30000 + 2000 * (k - 1))) EXTRA_ENV="$DET_ENV" \
bash scripts/run_full220_fastvlm.sh >> $LOG 2>&1 || die "could not launch eval ${TAG}_sp_ft_nomask_r$k"
log "eval ${TAG}_sp_ft_nomask_r$k launched (16 workers)"
launched=$((launched + 1))
done
if [ $launched -gt 0 ]; then
while tmux ls 2>/dev/null | grep -q "^b2d220_${TAG}_"; do sleep 60; done
for ((k = 1; k <= N_EVAL_REPEATS; k++)); do
dir=$B/${TAG}_sp_ft_nomask_r$k
if merge_eval $dir; then report_eval $dir; else log "eval $(basename $dir): incomplete (fewer than 16 WORKER_EXIT lines), re-run the script"; fi
done
fi
log "all stages done; comparison:"
$PY scripts/analysis/compare_nodet_nofold.py --tag $TAG 2>&1 | tee -a $LOG