fairtalking-second-work / scripts /batch_test_cta_ablation.sh
huahua123313's picture
Add files using upload-large-folder tool
07001d0 verified
Raw
History Blame Contribute Delete
10.9 kB
#!/usr/bin/env bash
# Batch test all CTA ablation variants on 4 datasets: ours / thb / ff++ / hdtf.
#
# For each ablation variant we discover its training output dir under
# outputs/cta_ablation_<VARIANT>*/checkpoints/{best.ckpt,last.ckpt}
# and run the standard test entry point with method=cta_ablation,
# overriding method.ablation_variant=<VARIANT>.
#
# Outputs:
# * One CSV per (variant, dataset) under
# outputs/cta_ablation_<VARIANT>_*/test_<dataset>_<timestamp>.csv
# (per-sample predictions; fairness CSV alongside it for the "ours" run)
# * One summary CSV listing acc/auc (and fairness scalars when available):
# cta_ablation_batch_results_<timestamp>.csv
#
# Fairness:
# * "ours" dataset (data=fairtalking) is the only one with race4 annotations,
# so it auto-emits *_fairness.csv via BaseMethod._compute_and_log_fairness_metrics.
# * thb / ff++ / hdtf only report test/acc and test/auc.
#
# Usage:
# bash scripts/batch_test_cta_ablation.sh # all discovered variants
# bash scripts/batch_test_cta_ablation.sh M1_video_only M3_intra_modal # explicit list
# bash scripts/batch_test_cta_ablation.sh --group M # only Group M
# bash scripts/batch_test_cta_ablation.sh --datasets ours thb # subset of datasets
set -eo pipefail
cd "$(dirname "$0")/.."
# load .env if present
[ -f .env ] && set -a && . ./.env && set +a
OUTPUT_DIR="outputs"
TIMESTAMP="$(date +%Y%m%d_%H%M%S)"
RESULTS_CSV="cta_ablation_batch_results_${TIMESTAMP}.csv"
# All known variants (mirror of run_all_ablations.sh)
ALL_VARIANTS=(
A1_full A2_no_asym A3_no_ltotal A4_asym_only A5_pooled_only
B1_real_only B2_all_samples B3_no_predictor
C1_no_loss_asym C2_no_loss_aux C3_no_loss_av C4_no_loss_va C5_no_detach
D1_depth_1 D2_depth_4 D3_depth_8 D4_mlp_predictor D5_shared_predictor
E1_video_freeze_0 E2_video_freeze_07 E3_video_freeze_10
F1_audio_freeze_0 F2_audio_freeze_08
G2_random_crop
M1_video_only M2_audio_only M3_intra_modal M4_noise_target M5_shuffle_pair
M6_drop_audio_infer M7_drop_video_infer
)
declare -A GROUP_VARIANTS
GROUP_VARIANTS[A]="A1_full A2_no_asym A3_no_ltotal A4_asym_only A5_pooled_only"
GROUP_VARIANTS[B]="B1_real_only B2_all_samples B3_no_predictor"
GROUP_VARIANTS[C]="C1_no_loss_asym C2_no_loss_aux C3_no_loss_av C4_no_loss_va C5_no_detach"
GROUP_VARIANTS[D]="D1_depth_1 D2_depth_4 D3_depth_8 D4_mlp_predictor D5_shared_predictor"
GROUP_VARIANTS[E]="E1_video_freeze_0 E2_video_freeze_07 E3_video_freeze_10"
GROUP_VARIANTS[F]="F1_audio_freeze_0 F2_audio_freeze_08"
GROUP_VARIANTS[G]="G2_random_crop"
GROUP_VARIANTS[M]="M1_video_only M2_audio_only M3_intra_modal M4_noise_target M5_shuffle_pair M6_drop_audio_infer M7_drop_video_infer"
# ---- args ------------------------------------------------------------------
VARIANTS=()
DATASETS=("ours" "thb" "ff++" "hdtf")
GROUP=""
DRY_RUN=false
while [[ $# -gt 0 ]]; do
case "$1" in
--group) GROUP="$2"; shift 2 ;;
--datasets)
shift
DATASETS=()
while [[ $# -gt 0 && "$1" != --* ]]; do
DATASETS+=("$1"); shift
done
;;
--dry-run) DRY_RUN=true; shift ;;
-h|--help)
grep -E '^#( |$)' "$0" | sed 's/^# \?//'
exit 0
;;
*) VARIANTS+=("$1"); shift ;;
esac
done
if [[ -n "$GROUP" ]]; then
if [[ -z "${GROUP_VARIANTS[$GROUP]+_}" ]]; then
echo "Unknown group: $GROUP. Valid: A B C D E F G M"; exit 1
fi
# Append group variants to any explicitly listed ones
for v in ${GROUP_VARIANTS[$GROUP]}; do VARIANTS+=("$v"); done
fi
if [[ ${#VARIANTS[@]} -eq 0 ]]; then
VARIANTS=("${ALL_VARIANTS[@]}")
fi
echo "============================================================"
echo "[batch_test_ablation] timestamp: $TIMESTAMP"
echo "[batch_test_ablation] results CSV: $RESULTS_CSV"
echo "[batch_test_ablation] variants: ${VARIANTS[*]}"
echo "[batch_test_ablation] datasets: ${DATASETS[*]}"
$DRY_RUN && echo "[batch_test_ablation] DRY RUN: will only show what would be tested"
echo "============================================================"
# ---- result CSV header -----------------------------------------------------
# We always include fairness columns for schema stability; non-"ours" rows
# leave them blank.
echo "variant,group,dataset,checkpoint,test_acc,test_auc,F_FPR,F_OAE,F_DP,F_MEO,run_dir,predictions_csv,timestamp" > "$RESULTS_CSV"
# ---- helpers ---------------------------------------------------------------
group_of() {
case "$1" in
A*) echo A ;;
B*) echo B ;;
C*) echo C ;;
D*) echo D ;;
E*) echo E ;;
F*) echo F ;;
G*) echo G ;;
M*) echo M ;;
*) echo "?" ;;
esac
}
# Find the most-recent training output directory for a variant.
# We accept either:
# outputs/cta_ablation_<VARIANT>/ (no timestamp suffix)
# outputs/cta_ablation_<VARIANT>_<timestamp>/ (timestamped)
find_run_dir() {
local variant="$1"
local exact="$OUTPUT_DIR/cta_ablation_${variant}"
local match
# Prefer the timestamped versions (most recent)
match=$(ls -d "$OUTPUT_DIR"/cta_ablation_${variant}_* 2>/dev/null | sort -r | head -1 || true)
if [[ -n "$match" ]]; then
echo "$match"; return
fi
if [[ -d "$exact" ]]; then echo "$exact"; return; fi
echo ""
}
# Find the best checkpoint inside a run dir; fall back to last.ckpt.
find_ckpt() {
local run_dir="$1"
local ckpt_dir="$run_dir/checkpoints"
[[ -d "$ckpt_dir" ]] || { echo ""; return; }
# Prefer best (anything not last.ckpt), pick the most-recent
local best
best=$(ls -1 "$ckpt_dir"/*.ckpt 2>/dev/null | grep -v '/last.ckpt$' | sort -r | head -1 || true)
if [[ -n "$best" ]]; then echo "$best"; return; fi
[[ -f "$ckpt_dir/last.ckpt" ]] && { echo "$ckpt_dir/last.ckpt"; return; }
echo ""
}
# Map dataset name -> hydra data config
data_cfg_for() {
case "$1" in
ours) echo "fairtalking" ;;
thb) echo "fairtalking_thb" ;;
ff++) echo "fairtalking_ffpp" ;;
hdtf) echo "fairtalking_hdtf_paired" ;;
*) echo ""; return 1 ;;
esac
}
# Extract a metric line from output. Lightning prints e.g. "test/auc 0.8932"
extract_metric() {
local key="$1" output="$2"
echo "$output" | grep -E "^[[:space:]]*${key}[[:space:]]" | tail -1 | awk '{print $NF}'
}
# Extract a fairness scalar from a fairness CSV (section=summary, metric=key).
extract_fairness_csv() {
local csv="$1" key="$2"
[[ -f "$csv" ]] || { echo ""; return; }
# Schema: section,group,n,...,metric,value
awk -F, -v k="$key" '$1=="summary" && $(NF-1)==k {print $NF; exit}' "$csv"
}
# ---- main loop -------------------------------------------------------------
for variant in "${VARIANTS[@]}"; do
grp=$(group_of "$variant")
run_dir=$(find_run_dir "$variant")
if [[ -z "$run_dir" ]]; then
echo "[$variant] NO run directory found under $OUTPUT_DIR/cta_ablation_${variant}*; skipping"
for ds in "${DATASETS[@]}"; do
echo "$variant,$grp,$ds,NO_RUN_DIR,N/A,N/A,,,,,,,$TIMESTAMP" >> "$RESULTS_CSV"
done
continue
fi
ckpt=$(find_ckpt "$run_dir")
if [[ -z "$ckpt" ]]; then
echo "[$variant] NO checkpoint in $run_dir/checkpoints; skipping"
for ds in "${DATASETS[@]}"; do
echo "$variant,$grp,$ds,NO_CKPT,N/A,N/A,,,,,$run_dir,,$TIMESTAMP" >> "$RESULTS_CSV"
done
continue
fi
echo ""
echo "============================================================"
echo "[$variant] run dir: $run_dir"
echo "[$variant] checkpoint: $ckpt"
echo "============================================================"
for ds in "${DATASETS[@]}"; do
data_cfg=$(data_cfg_for "$ds") || true
if [[ -z "$data_cfg" ]]; then
echo "[$variant] unknown dataset '$ds'; skipping"
continue
fi
ts="$(date +%Y%m%d_%H%M%S)"
pred_csv="$run_dir/test_${ds}_${ts}.csv"
echo ""
echo "[$variant] testing on dataset='$ds' (data=$data_cfg)"
echo "[$variant] predictions -> $pred_csv"
if $DRY_RUN; then
echo "[$variant][$ds] DRY RUN: skipping actual python call"
echo "$variant,$grp,$ds,$(basename "$ckpt"),DRY,DRY,,,,,$run_dir,$pred_csv,$ts" >> "$RESULTS_CSV"
continue
fi
# Run test. Hydra-style overrides: method=cta_ablation, the variant,
# data=<cfg>, the checkpoint, and force-redirect predictions CSV.
set +e
out=$(python3 src/train.py \
method=cta_ablation \
method.ablation_variant="$variant" \
data="$data_cfg" \
trainer=ddp \
backbone=timesformer \
+test_only=true \
+test_ckpt="$ckpt" \
+test_predictions_csv="$pred_csv" \
experiment_name="cta_ablation_${variant}_test_${ds}" \
2>&1)
rc=$?
set -e
# Print full output to stdout for debugging.
echo "$out"
if [[ $rc -ne 0 ]]; then
echo "[$variant][$ds] python exited with rc=$rc"
echo "$variant,$grp,$ds,$(basename "$ckpt"),ERROR,ERROR,,,,,$run_dir,$pred_csv,$ts" >> "$RESULTS_CSV"
continue
fi
acc=$(extract_metric "test/acc" "$out"); acc=${acc:-N/A}
auc=$(extract_metric "test/auc" "$out"); auc=${auc:-N/A}
# Fairness only for "ours". The fairness CSV lives next to pred_csv with
# suffix "_fairness.csv".
f_fpr=""; f_oae=""; f_dp=""; f_meo=""
if [[ "$ds" == "ours" ]]; then
fairness_csv="${pred_csv%.csv}_fairness.csv"
if [[ -f "$fairness_csv" ]]; then
f_fpr=$(extract_fairness_csv "$fairness_csv" "F_FPR")
f_oae=$(extract_fairness_csv "$fairness_csv" "F_OAE")
f_dp=$(extract_fairness_csv "$fairness_csv" "F_DP")
f_meo=$(extract_fairness_csv "$fairness_csv" "F_MEO")
echo "[$variant][$ds] fairness: F_FPR=$f_fpr F_OAE=$f_oae F_DP=$f_dp F_MEO=$f_meo"
else
echo "[$variant][$ds] fairness CSV not found at $fairness_csv (annotations missing?)"
fi
fi
echo "$variant,$grp,$ds,$(basename "$ckpt"),$acc,$auc,$f_fpr,$f_oae,$f_dp,$f_meo,$run_dir,$pred_csv,$ts" >> "$RESULTS_CSV"
echo "[$variant][$ds] acc=$acc auc=$auc"
done
done
echo ""
echo "============================================================"
echo "[batch_test_ablation] DONE. Summary CSV: $RESULTS_CSV"
echo "============================================================"
if command -v column >/dev/null 2>&1; then
column -t -s, "$RESULTS_CSV" | head -50
fi