#!/usr/bin/env bash
# Official eval of feedback_s2/best.pth after mapping legacy attn.in_proj_* onto q/k/v/out.
# Epoch 0 does not train. This is the reload measurement, not a new Score.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

TAG="${TAG:-frame_asd_F12_feedback_s2_reload}"
RESUME="${RESUME:-$ROOT/save/icbhi_beats_frame_asd_frame_asd_F12_feedback_s2/best.pth}"
OUT="$ROOT/save/icbhi_beats_frame_asd_${TAG}"
PROTECTED="$ROOT/save/icbhi_beats_frame_asd_frame_asd_F12_feedback_s2"

[[ -f "$RESUME" ]] || { echo "Missing $RESUME"; exit 1; }
if [[ "$OUT" == "$PROTECTED" ]]; then
  echo "Refusing to write the protected feedback_s2 directory"
  exit 1
fi
mkdir -p "$OUT"

echo "=== reload eval $TAG resume=$RESUME ==="
python -u train_feedback.py --tag "$TAG" \
  --method frame_asd --model beats --dataset icbhi --seed 1 \
  --save_dir "$ROOT/save" \
  --class_split lungsound --n_cls 4 --batch_size 16 \
  --desired_length 5 --optimizer adam --weight_decay 1e-6 \
  --cosine --test_fold official --pad_types repeat \
  --print_freq 50 \
  --min_se 48 --min_sp 72 --early_stop_patience 8 \
  --mil_pool topk --mil_topk 3 \
  --use_specaug --specaug_policy structured \
  --beats_wave_scale 32768 --threshold_n_points 41 \
  --ablation_stage F12_feedback_s2_reload \
  --feedback_slots 4 --feedback_layers 2 --feedback_drop 0.2 \
  --reason_codes 16 \
  --epochs 0 --eval_first \
  --resume "$RESUME" \
  --num_workers 4 \
  "$@" 2>&1 | tee "$OUT/train.log"
