fairtalking-second-work / scripts /analysis /run_robustness_sweep.sh
huahua123313's picture
Add files using upload-large-folder tool
eea47ad verified
Raw
History Blame Contribute Delete
3.33 kB
#!/usr/bin/env bash
# Sweep all 6 perturbations x 5 levels = 30 robustness runs for CTA.
# Mirrors X-AVDT's robustness sweep.
#
# Usage:
# bash scripts/analysis/run_robustness_sweep.sh \
# <ckpt> <data_config> [demographics_csv]
#
# Examples:
# # On the diffusion-only test split (held-out 3 families inside ours):
# bash scripts/analysis/run_robustness_sweep.sh \
# outputs/cta_diffusion_combined_20260604_205145/checkpoints/epoch16-valauc1.0000.ckpt \
# fairtalking_diffusion_only \
# /apdcephfs_gy4/share_303628665/joywu/research/test.csv
#
# # Or on the held-out family test sets one by one:
# bash scripts/analysis/run_robustness_sweep.sh <ckpt> fairtalking_test_sadtalker
# bash scripts/analysis/run_robustness_sweep.sh <ckpt> fairtalking_test_edtalk
# bash scripts/analysis/run_robustness_sweep.sh <ckpt> fairtalking_test_float
#
# Output: results appended to outputs/analysis/robustness/cta_runs_<TS>.json
set -euo pipefail
cd "$(dirname "$0")/../.."
[ -f .env ] && set -a && . ./.env && set +a
CKPT="${1:-}"
DATA="${2:-fairtalking_diffusion_only}"
DEMO="${3:-}"
if [[ -z "$CKPT" ]]; then
echo "Usage: $0 <ckpt> [data_config] [demographics_csv]" >&2
exit 1
fi
if [[ ! -f "$CKPT" ]]; then
echo "ckpt not found: $CKPT" >&2
exit 1
fi
# resolve python (mirror of other batch scripts)
PY=""
if command -v python3 >/dev/null 2>&1 && python3 -c 'import hydra,cv2' >/dev/null 2>&1; then
PY="$(command -v python3)"
elif [[ -x /opt/conda/envs/pytorch/bin/python3 ]] && \
/opt/conda/envs/pytorch/bin/python3 -c 'import hydra,cv2' >/dev/null 2>&1; then
PY="/opt/conda/envs/pytorch/bin/python3"
fi
if [[ -z "$PY" ]]; then
echo "FATAL: cannot find python with hydra+opencv installed" >&2
exit 1
fi
echo "[robustness] python: $PY"
TS="$(date +%Y%m%d_%H%M%S)"
OUT_JSON="outputs/analysis/robustness/cta_runs_${TS}.json"
mkdir -p "$(dirname "$OUT_JSON")"
PERTURBATIONS=(color_saturation color_contrast block_wise gaussian_noise gaussian_blur pixelate jpeg_quality)
DEMO_FLAG=()
if [[ -n "$DEMO" ]]; then
DEMO_FLAG=(--demographics_csv "$DEMO")
fi
echo "============================================================"
echo "[robustness sweep] ckpt: $CKPT"
echo "[robustness sweep] data: $DATA"
echo "[robustness sweep] demographics: ${DEMO:-<none, fairness skipped>}"
echo "[robustness sweep] output JSON: $OUT_JSON"
echo "[robustness sweep] 6 x 5 = 30 runs"
echo "============================================================"
for p in "${PERTURBATIONS[@]}"; do
for L in 1 2 3 4 5; do
echo ""
echo ">>> [robustness] $p / level=$L ($(date '+%H:%M:%S'))"
"$PY" scripts/analysis/evaluate_robustness.py \
--ckpt "$CKPT" \
--data "$DATA" \
--perturbation "$p" \
--level "$L" \
--save_json "$OUT_JSON" \
"${DEMO_FLAG[@]}" \
|| { echo "[robustness] FAILED on $p/L$L (skipping)"; continue; }
done
done
echo ""
echo "============================================================"
echo "[robustness sweep] DONE"
echo "[robustness sweep] runs: $(grep -c '"perturbation":' "$OUT_JSON" 2>/dev/null || echo "?") / 30"
echo "[robustness sweep] JSON: $OUT_JSON"
echo "============================================================"