File size: 10,032 Bytes
7c2871f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | #!/usr/bin/env bash
# Held-out family evaluation for a model trained on the diffusion-only split.
#
# For a given checkpoint, runs three independent test passes — one per
# unseen family (3DMM/SadTalker, AE/EDTalk, Flow-Matching/Float) — and
# also runs an in-distribution diffusion test as a reference baseline
# (so you can directly read the cross-family gap from one CSV).
#
# Each pass produces:
# * test/acc, test/auc on stdout
# * AP, Acc@EER post-hoc from the predictions CSV
# * fairness metrics (race4) — these are computed per pass; in-distribution
# diffusion test will have the most reliable fairness numbers because
# unseen-family tests intentionally use samples the model wasn't trained
# to recognize.
#
# Output directory: outputs/cta_test_result/diffusion_holdout_<TS>/
# ├── results.csv — combined long-table
# ├── logs/<family>__<dataset>.log
# └── runs/<family>/... — one subdir per (family, ckpt)
#
# Usage:
# bash scripts/batch_test_diffusion_holdout.sh # auto-discovers latest cta_diffusion_* run
# bash scripts/batch_test_diffusion_holdout.sh outputs/cta_diffusion_2026.../ # explicit run dir
# bash scripts/batch_test_diffusion_holdout.sh --ckpt /path/to/best.ckpt # explicit ckpt
# bash scripts/batch_test_diffusion_holdout.sh --num-top 1 # only top-1 + last
# bash scripts/batch_test_diffusion_holdout.sh --families sadtalker float # subset
# bash scripts/batch_test_diffusion_holdout.sh --include-diffusion # also test on diffusion (in-distribution)
set -eo pipefail
cd "$(dirname "$0")/.."
[ -f .env ] && set -a && . ./.env && set +a
# ---- python interpreter -----------------------------------------------------
resolve_python() {
if [[ -n "${PY:-}" ]] && "$PY" -c 'import hydra' >/dev/null 2>&1; then
echo "$PY"; return
fi
if command -v python3 >/dev/null 2>&1 && python3 -c 'import hydra' >/dev/null 2>&1; then
command -v python3; return
fi
if [[ -x /opt/conda/envs/av/bin/python3 ]] && \
/opt/conda/envs/av/bin/python3 -c 'import hydra' >/dev/null 2>&1; then
echo "/opt/conda/envs/av/bin/python3"; return
fi
cat <<EOF >&2
[holdout] FATAL: cannot find a Python interpreter with hydra installed.
Activate the project env (e.g. \`conda activate pytorch\`) or pass PY=/path/to/python3.
EOF
exit 1
}
PY="$(resolve_python)"
echo "[holdout] using python: $PY"
# ---- args -------------------------------------------------------------------
RUN_DIR=""
EXPLICIT_CKPT=""
NUM_TOP=3
FAMILIES=("sadtalker" "edtalk" "float")
INCLUDE_DIFFUSION=false
while [[ $# -gt 0 ]]; do
case "$1" in
--ckpt) EXPLICIT_CKPT="$2"; shift 2 ;;
--num-top) NUM_TOP="$2"; shift 2 ;;
--families)
shift
FAMILIES=()
while [[ $# -gt 0 && "$1" != --* ]]; do FAMILIES+=("$1"); shift; done
;;
--include-diffusion) INCLUDE_DIFFUSION=true; shift ;;
-h|--help) grep -E '^#( |$)' "$0" | sed 's/^# \?//'; exit 0 ;;
--*) echo "Unknown flag: $1" >&2; exit 1 ;;
*) RUN_DIR="$1"; shift ;;
esac
done
# ---- locate run dir / checkpoints ------------------------------------------
if [[ -z "$RUN_DIR" && -z "$EXPLICIT_CKPT" ]]; then
# Auto-discover latest cta_diffusion_<TS> dir.
RUN_DIR=$(ls -d outputs/cta_diffusion_[0-9]*_[0-9]* 2>/dev/null | sort -r | head -1 || true)
if [[ -z "$RUN_DIR" ]]; then
echo "[holdout] No cta_diffusion_* run dir found. Train first with bash scripts/train_cta_diffusion.sh, or pass an explicit --ckpt." >&2
exit 1
fi
fi
pick_ckpts() {
local run_dir="$1" num_top="$2"
local ckpt_dir="$run_dir/checkpoints"
[[ -d "$ckpt_dir" ]] || return 0
local top
top=$(ls -1 "$ckpt_dir"/*.ckpt 2>/dev/null | grep -v '/last\.ckpt$' | sort -r || true)
if [[ -n "$top" ]]; then
local i=1
while IFS= read -r p; do
[[ -z "$p" ]] && continue
echo "top${i}:$p"; i=$((i + 1))
if (( i > num_top )); then break; fi
done <<< "$top"
fi
[[ -f "$ckpt_dir/last.ckpt" ]] && echo "last:$ckpt_dir/last.ckpt"
}
if [[ -n "$EXPLICIT_CKPT" ]]; then
[[ -f "$EXPLICIT_CKPT" ]] || { echo "[holdout] checkpoint not found: $EXPLICIT_CKPT" >&2; exit 1; }
CKPTS=("custom:$EXPLICIT_CKPT")
if [[ -z "$RUN_DIR" ]]; then RUN_DIR="$(dirname "$(dirname "$EXPLICIT_CKPT")")"; fi
else
[[ -d "$RUN_DIR" ]] || { echo "[holdout] run dir not found: $RUN_DIR" >&2; exit 1; }
mapfile -t CKPTS < <(pick_ckpts "$RUN_DIR" "$NUM_TOP")
[[ ${#CKPTS[@]} -gt 0 ]] || { echo "[holdout] no checkpoints under $RUN_DIR/checkpoints" >&2; exit 1; }
fi
# ---- output layout ----------------------------------------------------------
TIMESTAMP="$(date +%Y%m%d_%H%M%S)"
BATCH_DIR="outputs/cta_test_result/diffusion_holdout_${TIMESTAMP}"
RESULTS_CSV="${BATCH_DIR}/results.csv"
LOG_DIR="${BATCH_DIR}/logs"
RUNS_DIR="${BATCH_DIR}/runs"
mkdir -p "$LOG_DIR" "$RUNS_DIR"
echo "============================================================"
echo "[holdout] run dir: $RUN_DIR"
echo "[holdout] families: ${FAMILIES[*]}"
$INCLUDE_DIFFUSION && echo "[holdout] also testing on diffusion (in-distribution)"
echo "[holdout] checkpoints:"
for c in "${CKPTS[@]}"; do echo " $c"; done
echo "[holdout] output dir: $BATCH_DIR"
echo "[holdout] results CSV: $RESULTS_CSV"
echo "============================================================"
echo "family,ckpt_kind,ckpt_name,test_acc,test_auc,test_ap,test_acc_at_eer,F_FPR,F_OAE,F_DP,F_MEO,run_dir,test_out_dir,predictions_csv,timestamp" > "$RESULTS_CSV"
# ---- helpers ----------------------------------------------------------------
data_cfg_for() {
case "$1" in
sadtalker) echo "fairtalking_test_sadtalker" ;;
edtalk) echo "fairtalking_test_edtalk" ;;
float) echo "fairtalking_test_float" ;;
diffusion) echo "fairtalking_diffusion_only" ;;
*) echo ""; return 1 ;;
esac
}
extract_metric() {
local key="$1" output="$2"
echo "$output" | grep -E "${key}[[:space:]]" | tail -1 | awk '{
for (i=NF; i>=1; i--) if ($i ~ /^-?[0-9.]+$/) { print $i; exit }
}' | tr -d '\r'
}
extract_kv() {
local out="$1" key="$2"
echo "$out" | tr ' ' '\n' | awk -F= -v k="$key" '$1==k {print $2; exit}' | tr -d '\r'
}
extract_fairness() {
local csv="$1" key="$2"
[[ -f "$csv" ]] || { echo ""; return; }
awk -F, -v k="$key" '$1=="summary" && $(NF-1)==k {print $NF; exit}' "$csv" | tr -d '\r'
}
run_one() {
local family="$1" ckpt_kind="$2" ckpt_path="$3"
local data_cfg ts test_out_dir pred_csv log_path out rc
local acc auc extra ap acc_eer f_fpr f_oae f_dp f_meo
data_cfg=$(data_cfg_for "$family") || true
if [[ -z "$data_cfg" ]]; then
echo "[holdout] unknown family '$family'; skipping" >&2
return
fi
ts="$(date +%Y%m%d_%H%M%S)"
test_out_dir="${RUNS_DIR}/${family}/${ckpt_kind}__${ts}"
mkdir -p "$test_out_dir"
pred_csv="${test_out_dir}/test_predictions.csv"
log_path="${LOG_DIR}/${family}__${ckpt_kind}.log"
echo ""
echo "[holdout] [$family] [$ckpt_kind] testing $(basename "$ckpt_path")"
set +e
out=$("$PY" src/train.py \
method=cta \
data="$data_cfg" \
trainer=ddp \
backbone=timesformer \
+test_only=true \
+test_ckpt="$ckpt_path" \
+test_predictions_csv="$pred_csv" \
output_dir="$test_out_dir" \
hydra.run.dir="$test_out_dir/hydra" \
experiment_name="cta_diffusion_holdout_${family}_${ckpt_kind}" \
2>&1)
rc=$?
set -e
echo "$out" > "$log_path"
if [[ $rc -ne 0 ]]; then
echo "${family},${ckpt_kind},$(basename "$ckpt_path"),ERROR_RC${rc},ERROR_RC${rc},ERROR_RC${rc},ERROR_RC${rc},,,,,$RUN_DIR,$test_out_dir,$pred_csv,$ts" >> "$RESULTS_CSV"
echo "[holdout] [$family] [$ckpt_kind] FAILED (rc=$rc) — see $log_path"
return
fi
acc=$(extract_metric "test/acc" "$out"); acc=${acc:-N/A}
auc=$(extract_metric "test/auc" "$out"); auc=${auc:-N/A}
ap="N/A"; acc_eer="N/A"
if [[ -f "$pred_csv" ]]; then
set +e
extra=$("$PY" scripts/compute_extra_metrics.py "$pred_csv" 2>/dev/null)
if [[ $? -eq 0 ]]; then
ap=$(extract_kv "$extra" "ap"); ap=${ap:-N/A}
acc_eer=$(extract_kv "$extra" "acc_at_eer"); acc_eer=${acc_eer:-N/A}
fi
set -e
fi
# Fairness only meaningful when race4 annotations cover the test split.
# FairTalking's test.csv has them, so all 4 families' test sets carry race4.
f_fpr=""; f_oae=""; f_dp=""; f_meo=""
local fairness_csv="${pred_csv%.csv}_fairness.csv"
f_fpr=$(extract_fairness "$fairness_csv" "F_FPR")
f_oae=$(extract_fairness "$fairness_csv" "F_OAE")
f_dp=$(extract_fairness "$fairness_csv" "F_DP")
f_meo=$(extract_fairness "$fairness_csv" "F_MEO")
echo "${family},${ckpt_kind},$(basename "$ckpt_path"),${acc},${auc},${ap},${acc_eer},${f_fpr},${f_oae},${f_dp},${f_meo},$RUN_DIR,$test_out_dir,$pred_csv,$ts" >> "$RESULTS_CSV"
echo "[holdout] [$family] [$ckpt_kind] acc=$acc auc=$auc ap=$ap acc_at_eer=$acc_eer"
}
# ---- main loop -------------------------------------------------------------
ALL_FAMILIES=()
$INCLUDE_DIFFUSION && ALL_FAMILIES+=("diffusion")
ALL_FAMILIES+=("${FAMILIES[@]}")
for fam in "${ALL_FAMILIES[@]}"; do
for kp in "${CKPTS[@]}"; do
ckpt_kind="${kp%%:*}"
ckpt_path="${kp#*:}"
run_one "$fam" "$ckpt_kind" "$ckpt_path"
done
done
echo ""
echo "============================================================"
echo "[holdout] DONE. results: $RESULTS_CSV"
echo "============================================================"
if command -v column >/dev/null 2>&1; then
column -t -s, "$RESULTS_CSV" | head -40
else
head -40 "$RESULTS_CSV"
fi
|