| #!/usr/bin/env bash |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| set -eo pipefail |
| cd "$(dirname "$0")/.." |
|
|
| [ -f .env ] && set -a && . ./.env && set +a |
|
|
| |
| 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" |
|
|
| |
| 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 |
|
|
| |
| if [[ -z "$RUN_DIR" && -z "$EXPLICIT_CKPT" ]]; then |
| |
| 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 |
|
|
| |
| 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" |
|
|
| |
| 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 |
|
|
| |
| |
| 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" |
| } |
|
|
| |
| 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 |
|
|