MaybeRichard commited on
Commit
f9037c2
·
verified ·
1 Parent(s): 739717f

hotfix Kermany FM pipeline env and prepare skip

Browse files
code/kermany_pipeline/run_kermany_fm_pipeline.sh ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ # Run the full Kermany2018 OCT FM pipeline in OCT_8/Baseline/RAE-main.
3
+ set -euo pipefail
4
+
5
+ export https_proxy=http://10.140.15.68:3128 http_proxy=http://10.140.15.68:3128
6
+ export HF_HOME=/data/temp/qinshengqian/c3/hf_cache
7
+ export TRANSFORMERS_CACHE=/data/temp/qinshengqian/c3/hf_cache
8
+ export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
9
+
10
+ ROOT=/data/temp/qinshengqian/c3
11
+ PIPE=$ROOT/kermany_pipeline
12
+ DOWN=$ROOT/kermany_downstream
13
+ BASE=/mnt/tidal-alsh-share2/dataset/qinshengqian/research/c3/Data/Classification/OCT_8/Baseline/RAE-main
14
+ DATA=$ROOT/kermany_imagefolder/train
15
+ REAL=$ROOT/kermany_imagefolder/train
16
+ RESULTS=$BASE/results_kermany
17
+ LOGS=$RESULTS/logs
18
+ JSONS=$DOWN/jsons
19
+ SYNROOT=$DOWN/synth
20
+ EVAL=$DOWN/eval
21
+ RUNLOG=$DOWN/kermany_fm_pipeline.log
22
+
23
+ CORE_ARMS=(RETFound VisionFM DINOv2L MAEL)
24
+ EXTRA_ARMS=(EyeCLIP FMUE UrFound SigLIP2L)
25
+ RUN_EXTRA_ARMS=${RUN_EXTRA_ARMS:-0}
26
+ if [ "$RUN_EXTRA_ARMS" = "1" ]; then
27
+ ARMS=("${CORE_ARMS[@]}" "${EXTRA_ARMS[@]}")
28
+ else
29
+ ARMS=("${CORE_ARMS[@]}")
30
+ fi
31
+ GPUS=(0,1 2,3 4,5 6,7)
32
+ PORTS=(29600 29601 29602 29603)
33
+ DOSES=(0.05 0.25 1.0)
34
+ SEEDS=(0 1 2)
35
+
36
+ mkdir -p "$LOGS" "$JSONS" "$SYNROOT" "$EVAL"
37
+ exec >>"$RUNLOG" 2>&1
38
+
39
+ ts() { date '+%Y-%m-%d %H:%M:%S'; }
40
+
41
+ wait_for_sdvae() {
42
+ local marker=$DOWN/SDVAE_COMPLETION_DONE
43
+ echo "[$(ts)] waiting for SD-VAE completion marker: $marker"
44
+ while [ ! -f "$marker" ]; do
45
+ sleep 600
46
+ done
47
+ echo "[$(ts)] SD-VAE marker found"
48
+ }
49
+
50
+ env_on() {
51
+ source ~/miniconda3/etc/profile.d/conda.sh
52
+ conda activate rae_v2
53
+ cd "$BASE"
54
+ }
55
+
56
+ prepare() {
57
+ echo "[$(ts)] preparing Kermany ImageFolder and configs"
58
+ if [ -d "$ROOT/kermany_imagefolder/train/CNV" ] && [ -d "$ROOT/kermany_imagefolder/test/NORMAL" ]; then
59
+ echo "[$(ts)] Kermany ImageFolder already exists; skip rebuild"
60
+ else
61
+ /root/miniconda3/envs/rae_v2/bin/python "$PIPE/kermany_make_imagefolder.py"
62
+ fi
63
+ env_on
64
+ local missing=0
65
+ for arm in "${ARMS[@]}"; do
66
+ [ -f "$BASE/configs/stage1/training/KERMANY/${arm}_decXL.yaml" ] || missing=1
67
+ [ -f "$BASE/configs/stage2/training/KERMANY/DiTDH-XL_${arm}.yaml" ] || missing=1
68
+ done
69
+ if [ "$missing" = "0" ]; then
70
+ echo "[$(ts)] Kermany FM configs already exist; skip regenerate"
71
+ else
72
+ python "$PIPE/make_kermany_fm_configs.py" --base "$BASE" --stage1-epochs 30 --stage2-epochs 50 --arms "${ARMS[@]}"
73
+ fi
74
+ }
75
+
76
+ stage1_one() {
77
+ local arm=$1 gpu=$2 port=$3
78
+ local ngpu=$(echo "$gpu" | tr ',' '\n' | wc -l)
79
+ env_on
80
+ if [ -f "$RESULTS/stage1/stage1_${arm}/checkpoints/ep-last.pt" ]; then
81
+ echo "[$(ts)] [stage1] skip existing $arm"
82
+ return
83
+ fi
84
+ echo "[$(ts)] [stage1] $arm gpus=$gpu"
85
+ CUDA_VISIBLE_DEVICES=$gpu EXPERIMENT_NAME=stage1_$arm torchrun --nproc_per_node="$ngpu" --master_port="$port" \
86
+ src/train_stage1.py \
87
+ --config "$BASE/configs/stage1/training/KERMANY/${arm}_decXL.yaml" \
88
+ --data-path "$DATA" --results-dir "$RESULTS/stage1" --image-size 256 --precision bf16 \
89
+ >"$LOGS/stage1_${arm}.log" 2>&1
90
+ }
91
+
92
+ stat_one() {
93
+ local arm=$1 gpu=$2 port=$3
94
+ local ngpu=$(echo "$gpu" | tr ',' '\n' | wc -l)
95
+ env_on
96
+ if [ -f "$RESULTS/stats/$arm/normalization_stats.pt" ]; then
97
+ echo "[$(ts)] [stat] skip existing $arm"
98
+ return
99
+ fi
100
+ echo "[$(ts)] [stat] $arm gpus=$gpu"
101
+ local stat_cfg="$DOWN/cfgs/stat_${arm}.yaml"
102
+ python - <<PY
103
+ import yaml
104
+ from pathlib import Path
105
+ src = Path("$BASE/configs/stage2/training/KERMANY/DiTDH-XL_${arm}.yaml")
106
+ cfg = yaml.safe_load(src.read_text())
107
+ cfg["stage_1"]["params"]["normalization_stat_path"] = None
108
+ Path("$stat_cfg").parent.mkdir(parents=True, exist_ok=True)
109
+ Path("$stat_cfg").write_text(yaml.safe_dump(cfg, sort_keys=False))
110
+ PY
111
+ SAVE_FOLDER=$arm CUDA_VISIBLE_DEVICES=$gpu torchrun --nproc_per_node="$ngpu" --master_port="$port" \
112
+ src/calculate_stat.py \
113
+ --config "$stat_cfg" \
114
+ --data-path "$DATA" --sample-dir "$RESULTS/stats" --image-size 256 \
115
+ --per-proc-batch-size 64 --num-workers 8 --precision bf16 \
116
+ >"$LOGS/stat_${arm}.log" 2>&1
117
+ }
118
+
119
+ stage2_one() {
120
+ local arm=$1 gpu=$2 port=$3
121
+ local ngpu=$(echo "$gpu" | tr ',' '\n' | wc -l)
122
+ env_on
123
+ if [ -f "$RESULTS/stage2/stage2_${arm}/checkpoints/ep-last.pt" ]; then
124
+ echo "[$(ts)] [stage2] skip existing $arm"
125
+ return
126
+ fi
127
+ echo "[$(ts)] [stage2] $arm gpus=$gpu"
128
+ CUDA_VISIBLE_DEVICES=$gpu EXPERIMENT_NAME=stage2_$arm torchrun --nproc_per_node="$ngpu" --master_port="$port" \
129
+ src/train.py \
130
+ --config "$BASE/configs/stage2/training/KERMANY/DiTDH-XL_${arm}.yaml" \
131
+ --data-path "$DATA" --results-dir "$RESULTS/stage2" --image-size 256 --precision bf16 --compile \
132
+ >"$LOGS/stage2_${arm}.log" 2>&1
133
+ }
134
+
135
+ sample_eval_downstream_one() {
136
+ local arm=$1 gpu=$2
137
+ env_on
138
+ local scfg=$DOWN/cfgs/sample_${arm}.yaml
139
+ local sdir=$SYNROOT/$arm
140
+ local csv=$sdir/synth.csv
141
+ if [ ! -f "$csv" ] || [ "$(tail -n +2 "$csv" 2>/dev/null | wc -l)" -lt 8000 ]; then
142
+ python - <<PY
143
+ import yaml
144
+ from pathlib import Path
145
+ src = Path("$BASE/configs/stage2/training/KERMANY/DiTDH-XL_${arm}.yaml")
146
+ cfg = yaml.safe_load(src.read_text())
147
+ cfg["stage_2"]["ckpt"] = "$RESULTS/stage2/stage2_${arm}/checkpoints/ep-last.pt"
148
+ Path("$scfg").parent.mkdir(parents=True, exist_ok=True)
149
+ Path("$scfg").write_text(yaml.safe_dump(cfg, sort_keys=False))
150
+ PY
151
+ echo "[$(ts)] [sample] $arm gpu=$gpu"
152
+ CUDA_VISIBLE_DEVICES=$gpu PYTHONPATH="$BASE/src" python "$PIPE/kermany_fm_sample_conditional.py" \
153
+ --config "$scfg" --output-dir "$sdir" --num-per-class 2000 --cfg-scales 1.0 \
154
+ --batch-size 50 --precision bf16 >"$LOGS/sample_${arm}.log" 2>&1
155
+ /root/miniconda3/envs/rae_v2/bin/python "$PIPE/build_kermany_synth_csv.py" "$sdir" 1.0 "$csv" >>"$LOGS/sample_${arm}.log" 2>&1
156
+ else
157
+ echo "[$(ts)] [sample] skip existing $arm csv=$csv"
158
+ fi
159
+
160
+ if [ ! -f "$EVAL/${arm}.json" ]; then
161
+ echo "[$(ts)] [quality] $arm gpu=$gpu"
162
+ CUDA_VISIBLE_DEVICES=$gpu python src/evaluate_quality_v2.py \
163
+ --gen-dir "$sdir" --real-dir "$REAL" --output "$EVAL/${arm}.json" --batch-size 64 \
164
+ >"$LOGS/eval_${arm}.log" 2>&1 || echo "[$(ts)] [quality] $arm failed; continuing to downstream"
165
+ fi
166
+
167
+ local i=0
168
+ for dose in "${DOSES[@]}"; do
169
+ for seed in "${SEEDS[@]}"; do
170
+ local out="$JSONS/C-${arm}_d${dose}_s${seed}.json"
171
+ if [ -f "$out" ]; then
172
+ echo "[$(ts)] [downstream] skip existing $out"
173
+ continue
174
+ fi
175
+ local dgpu=$((i % 8))
176
+ local log="$DOWN/logs/C-${arm}_d${dose}_s${seed}.log"
177
+ /root/miniconda3/envs/rae_v2/bin/python "$PIPE/kermany_dose_rn50.py" \
178
+ --dose "$dose" --regime C --seed "$seed" --device "cuda:$dgpu" \
179
+ --synth-csv "$csv" --epochs 15 --out-json "$out" >"$log" 2>&1 &
180
+ echo "[$(ts)] [downstream] launched $arm d=$dose seed=$seed gpu=$dgpu pid=$!"
181
+ i=$((i + 1))
182
+ while [ "$(jobs -rp | wc -l)" -ge 8 ]; do sleep 10; done
183
+ sleep 2
184
+ done
185
+ done
186
+ for seed in "${SEEDS[@]}"; do
187
+ local out="$JSONS/C-${arm}_full_s${seed}.json"
188
+ if [ -f "$out" ]; then
189
+ echo "[$(ts)] [downstream] skip existing $out"
190
+ continue
191
+ fi
192
+ local dgpu=$((i % 8))
193
+ local log="$DOWN/logs/C-${arm}_full_s${seed}.log"
194
+ /root/miniconda3/envs/rae_v2/bin/python "$PIPE/kermany_dose_rn50.py" \
195
+ --dose 1.0 --regime C --seed "$seed" --device "cuda:$dgpu" \
196
+ --real-cap-per-class 0 --synth-csv "$csv" --epochs 15 --out-json "$out" >"$log" 2>&1 &
197
+ echo "[$(ts)] [downstream] launched $arm full seed=$seed gpu=$dgpu pid=$!"
198
+ i=$((i + 1))
199
+ while [ "$(jobs -rp | wc -l)" -ge 8 ]; do sleep 10; done
200
+ sleep 2
201
+ done
202
+ local fail=0
203
+ for pid in $(jobs -rp); do wait "$pid" || fail=$((fail + 1)); done
204
+ echo "[$(ts)] [downstream] $arm fail=$fail"
205
+ }
206
+
207
+ run_wave() {
208
+ local -a wave=("$@")
209
+ echo "[$(ts)] running wave: ${wave[*]}"
210
+ for idx in "${!wave[@]}"; do
211
+ stage1_one "${wave[$idx]}" "${GPUS[$idx]}" "$((PORTS[$idx] + 0))" &
212
+ done
213
+ wait
214
+ for idx in "${!wave[@]}"; do
215
+ stat_one "${wave[$idx]}" "${GPUS[$idx]}" "$((PORTS[$idx] + 100))" &
216
+ done
217
+ wait
218
+ for idx in "${!wave[@]}"; do
219
+ stage2_one "${wave[$idx]}" "${GPUS[$idx]}" "$((PORTS[$idx] + 200))" &
220
+ done
221
+ wait
222
+ for idx in "${!wave[@]}"; do
223
+ sample_eval_downstream_one "${wave[$idx]}" "$idx"
224
+ done
225
+ }
226
+
227
+ echo "[$(ts)] KERMANY_FM_PIPELINE_START arms=${ARMS[*]}"
228
+ wait_for_sdvae
229
+ prepare
230
+ run_wave "${CORE_ARMS[@]}"
231
+ if [ "$RUN_EXTRA_ARMS" = "1" ]; then
232
+ run_wave "${EXTRA_ARMS[@]}"
233
+ else
234
+ echo "[$(ts)] skip extra arms: ${EXTRA_ARMS[*]}"
235
+ fi
236
+ /root/miniconda3/envs/rae_v2/bin/python "$PIPE/aggregate_kermany.py" --arms "${ARMS[@]}" sdvae --metric macro >"$DOWN/SUMMARY_all_macro.txt"
237
+ /root/miniconda3/envs/rae_v2/bin/python "$PIPE/aggregate_kermany.py" --arms "${ARMS[@]}" sdvae --metric disease_mean >"$DOWN/SUMMARY_all_disease_mean.txt"
238
+ echo "KERMANY_FM_PIPELINE_DONE $(date)" > "$DOWN/FM_PIPELINE_DONE"
239
+ echo "[$(ts)] KERMANY_FM_PIPELINE_DONE"