#!/bin/bash # Wait for Kermany SD-VAE stage2, then sample and run C-sdvae downstream. set -euo pipefail export https_proxy=http://10.140.15.68:3128 http_proxy=http://10.140.15.68:3128 export HF_DATASETS_CACHE=/data/temp/qinshengqian/c3/hf_cache export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True BASE=/data/temp/qinshengqian/c3 CODE=$BASE/Code/RAEv2 PIPE=$BASE/kermany_pipeline DOWN=$BASE/kermany_downstream SYN=$DOWN/synth/sdvae JSONS=$DOWN/jsons LOGS=$DOWN/logs CFGS=$DOWN/cfgs mkdir -p "$SYN" "$JSONS" "$LOGS" "$CFGS" FINAL_EPOCH=${FINAL_EPOCH:-50} STOP_TRAINING_AFTER_FINAL=${STOP_TRAINING_AFTER_FINAL:-1} FINAL=$(printf "%s/results/stage2/KERMANY_sdvae/checkpoints/ep-%07d.pt" "$CODE" "$FINAL_EPOCH") RUNLOG=$DOWN/sdvae_completion.log exec >>"$RUNLOG" 2>&1 ts() { date '+%Y-%m-%d %H:%M:%S'; } echo "[$(ts)] waiting for $FINAL" while [ ! -f "$FINAL" ]; do if ! pgrep -f "configs/stage2/training/KERMANY/sdvae" >/dev/null 2>&1; then echo "[$(ts)] sdvae training process not found before final checkpoint; abort" exit 1 fi sleep 120 done echo "[$(ts)] final checkpoint found" if [ "$STOP_TRAINING_AFTER_FINAL" = "1" ]; then echo "[$(ts)] stopping SD-VAE training after checkpoint ep-$FINAL_EPOCH" pkill -TERM -f "configs/stage2/training/KERMANY/sdvae_8gpu_gb64.yaml" || true sleep 30 pkill -KILL -f "configs/stage2/training/KERMANY/sdvae_8gpu_gb64.yaml" || true fi CSV=$SYN/synth.csv if [ ! -f "$CSV" ] || [ "$(tail -n +2 "$CSV" 2>/dev/null | wc -l)" -lt 8000 ]; then SCFG=$CFGS/sample_sdvae.yaml cd "$CODE" /root/miniconda3/envs/rae_v2/bin/python "$PIPE/build_kermany_sample_cfg.py" sdvae "$SCFG" rm -f "$SYN"/synth_*.csv "$CSV" CLASSES=(CNV DME DRUSEN NORMAL) for i in "${!CLASSES[@]}"; do c=${CLASSES[$i]} CUDA_VISIBLE_DEVICES=$i PYTHONPATH=src /root/miniconda3/envs/rae_v2/bin/python sample_perclass_kermany.py \ "$SCFG" "$SYN" 2000 80 "$c" "$c" >"$LOGS/sample_sdvae_${c}.log" 2>&1 & echo "[$(ts)] launched SD-VAE sampling class=$c gpu=$i pid=$!" done sample_fail=0 for pid in $(jobs -rp); do wait "$pid" || sample_fail=$((sample_fail + 1)) done [ "$sample_fail" -eq 0 ] || { echo "[$(ts)] sampling failed count=$sample_fail"; exit 1; } head -1 "$SYN/synth_CNV.csv" > "$CSV" for c in "${CLASSES[@]}"; do tail -n +2 "$SYN/synth_${c}.csv" >> "$CSV"; done fi rows=$(tail -n +2 "$CSV" | wc -l) echo "[$(ts)] synth csv rows=$rows" [ "$rows" -ge 8000 ] || { echo "[$(ts)] synth csv too small"; exit 1; } DOSES=(0.05 0.25 1.0) SEEDS=(0 1 2) GPUS=(0 1 2 3 4 5 6 7) MAXJOBS=8 wait_for_slot() { while [ "$(jobs -rp | wc -l)" -ge "$MAXJOBS" ]; do sleep 10 done } for seed in "${SEEDS[@]}"; do out="$JSONS/A_full_s${seed}.json" if [ -f "$out" ]; then echo "[$(ts)] skip existing full-train baseline $out" continue fi wait_for_slot gpu=${GPUS[$((seed % ${#GPUS[@]}))]} log="$LOGS/A_full_s${seed}.log" /root/miniconda3/envs/rae_v2/bin/python "$PIPE/kermany_dose_rn50.py" \ --dose 1.0 --regime A --seed "$seed" --device "cuda:$gpu" \ --real-cap-per-class 0 --epochs 15 --out-json "$out" >"$log" 2>&1 & echo "[$(ts)] launched A_full seed=$seed gpu=$gpu pid=$! out=$out" sleep 3 done i=0 for dose in "${DOSES[@]}"; do for seed in "${SEEDS[@]}"; do out="$JSONS/C-sdvae_d${dose}_s${seed}.json" if [ -f "$out" ]; then echo "[$(ts)] skip existing $out" continue fi wait_for_slot gpu=${GPUS[$((i % ${#GPUS[@]}))]} log="$LOGS/C-sdvae_d${dose}_s${seed}.log" /root/miniconda3/envs/rae_v2/bin/python "$PIPE/kermany_dose_rn50.py" \ --dose "$dose" --regime C --seed "$seed" --device "cuda:$gpu" \ --synth-csv "$CSV" --epochs 15 --out-json "$out" >"$log" 2>&1 & echo "[$(ts)] launched C-sdvae d=$dose seed=$seed gpu=$gpu pid=$! out=$out" i=$((i + 1)) sleep 3 done done for seed in "${SEEDS[@]}"; do out="$JSONS/C-sdvae_full_s${seed}.json" if [ -f "$out" ]; then echo "[$(ts)] skip existing full-train $out" continue fi wait_for_slot gpu=${GPUS[$((i % ${#GPUS[@]}))]} log="$LOGS/C-sdvae_full_s${seed}.log" /root/miniconda3/envs/rae_v2/bin/python "$PIPE/kermany_dose_rn50.py" \ --dose 1.0 --regime C --seed "$seed" --device "cuda:$gpu" \ --real-cap-per-class 0 --synth-csv "$CSV" --epochs 15 --out-json "$out" >"$log" 2>&1 & echo "[$(ts)] launched C-sdvae full seed=$seed gpu=$gpu pid=$! out=$out" i=$((i + 1)) sleep 3 done fail=0 for pid in $(jobs -rp); do wait "$pid" || fail=$((fail + 1)) done /root/miniconda3/envs/rae_v2/bin/python "$PIPE/aggregate_kermany.py" --arms sdvae --metric macro >"$DOWN/SUMMARY_sdvae_macro.txt" /root/miniconda3/envs/rae_v2/bin/python "$PIPE/aggregate_kermany.py" --arms sdvae --metric disease_mean >"$DOWN/SUMMARY_sdvae_disease_mean.txt" echo "[$(ts)] KERMANY_SDVAe_COMPLETION_DONE fail=$fail" echo "KERMANY_SDVAe_COMPLETION_DONE fail=$fail $(date)" > "$DOWN/SDVAE_COMPLETION_DONE" exit "$fail"