#!/usr/bin/env bash # Resumable end-to-end pipeline. Re-run any time -- each step checks if its # output already exists and skips (or resumes) accordingly. # # Force-rerun a single step: # FORCE_DOWNLOAD=1 bash scripts/run_all.sh # ignore cached zips and re-download # FORCE_PREPROCESS=1 bash scripts/run_all.sh # reprocess every case # FORCE_STAGE1=1 bash scripts/run_all.sh # retrain Stage 1 from scratch # FORCE_STAGE2=1 bash scripts/run_all.sh # retrain Stage 2 from scratch # FORCE_INFER=1 bash scripts/run_all.sh # re-export meshes # FORCE_ALL=1 bash scripts/run_all.sh # wipe and restart everything set -e export HF_ENDPOINT=${HF_ENDPOINT:-https://hf-mirror.com} CFG=configs/default.yaml # Read paths from the YAML so the script honors whatever you put in there. RAW=$(python -c "import yaml; print(yaml.safe_load(open('$CFG'))['paths']['raw_dir'])") PROC=$(python -c "import yaml; print(yaml.safe_load(open('$CFG'))['paths']['proc_dir'])") OUT=$(python -c "import yaml; print(yaml.safe_load(open('$CFG'))['paths']['out_dir'])") if [[ "${FORCE_ALL:-0}" == "1" ]]; then echo "[run_all] FORCE_ALL set -> wiping $PROC and $OUT" rm -rf "$PROC" "$OUT" fi mkdir -p "$RAW" "$PROC" "$OUT" # ----------- 1. download + discover ----------- MANIFEST="$RAW/manifest.json" HAS_CASES=$(find "$RAW" -maxdepth 3 -name "image_from_dicom.nii.gz" -o -name "*.dcm" 2>/dev/null | head -1) if [[ "${FORCE_DOWNLOAD:-0}" == "1" || -z "$HAS_CASES" ]]; then echo "[1/6] downloading + extracting from HuggingFace" python -m toothcanal.download --config $CFG else echo "[1/6] raw data already present -- skipping download, just refreshing manifest" python -m toothcanal.download --config $CFG --skip_download fi # ----------- 2. preprocess (per-case skip) ----------- echo "[2/6] preprocess (already-processed cases will be skipped)" if [[ "${FORCE_PREPROCESS:-0}" == "1" ]]; then python -m toothcanal.preprocess --config $CFG --force else python -m toothcanal.preprocess --config $CFG fi N_PROC=$(ls "$PROC"/*.npz 2>/dev/null | wc -l) echo "[run_all] $N_PROC processed cases available." if [[ "$N_PROC" -lt 5 ]]; then echo "[run_all] ERROR: fewer than 5 processed cases -- something is wrong." exit 1 fi # Free space: raw/ is no longer needed after preprocess. Keep _zips so re-runs # don't re-download. Only cleanup if user opts in via CLEANUP_RAW=1. if [[ "${CLEANUP_RAW:-0}" == "1" ]]; then echo "[run_all] CLEANUP_RAW=1 -> removing $RAW case folders (keeping _zips cache)" find "$RAW" -mindepth 1 -maxdepth 1 -type d ! -name "_zips" -exec rm -rf {} + df -h "$(dirname $RAW)" | tail -1 fi # ----------- 3. Stage 1 ----------- if [[ "${FORCE_STAGE1:-0}" == "1" ]]; then rm -f "$OUT/stage1.pt" fi if [[ -f "$OUT/stage1.pt" ]]; then # check if we still need more epochs EP=$(python -c "import torch; print(torch.load('$OUT/stage1.pt', map_location='cpu', weights_only=False).get('epoch', 0))" 2>/dev/null || echo 0) MAX=$(python -c "import yaml; print(yaml.safe_load(open('$CFG'))['stage1']['max_epochs'])") if [[ "$EP" -ge "$MAX" ]]; then echo "[3/6] Stage 1 already trained ($EP/$MAX epochs) -- skipping" else echo "[3/6] Stage 1 resuming from epoch $EP/$MAX" python -m toothcanal.train_stage1 --config $CFG --resume fi else echo "[3/6] Stage 1 - coarse segmentation (training from scratch)" python -m toothcanal.train_stage1 --config $CFG fi # ----------- 4. Stage 2 ----------- if [[ "${FORCE_STAGE2:-0}" == "1" ]]; then rm -f "$OUT/stage2.pt" fi if [[ -f "$OUT/stage2.pt" ]]; then EP=$(python -c "import torch; print(torch.load('$OUT/stage2.pt', map_location='cpu', weights_only=False).get('epoch', 0))" 2>/dev/null || echo 0) MAX=$(python -c "import yaml; print(yaml.safe_load(open('$CFG'))['stage2']['max_epochs'])") if [[ "$EP" -ge "$MAX" ]]; then echo "[4/6] Stage 2 already trained ($EP/$MAX epochs) -- skipping" else echo "[4/6] Stage 2 resuming from epoch $EP/$MAX" python -m toothcanal.train_stage2 --config $CFG --resume fi else echo "[4/6] Stage 2 - implicit dual-SDF (training from scratch)" python -m toothcanal.train_stage2 --config $CFG fi # ----------- 5. inference ----------- N_STL=$(ls "$OUT/meshes"/*_tooth.stl 2>/dev/null | wc -l) if [[ "${FORCE_INFER:-0}" == "1" || "$N_STL" -lt 1 ]]; then echo "[5/6] inference -> per-tooth STL meshes" python -m toothcanal.infer --config $CFG else echo "[5/6] $N_STL tooth meshes already exported -- skipping (FORCE_INFER=1 to redo)" fi # ----------- 6. evaluate (Oracle + Predicted) + exports + visualize ----------- echo "[6/6] evaluate (Oracle ROI = Stage-2 upper bound) ..." python -m toothcanal.evaluate --config $CFG --roi_source oracle --tag oracle echo "[6/6] evaluate (Predicted ROI = full system, no GT) ..." python -m toothcanal.evaluate --config $CFG --roi_source predicted --tag predicted || \ echo "[run_all] predicted-ROI eval skipped (needs stage1.pt)" python -m toothcanal.export_gt --config $CFG python -m toothcanal.export_nifti --config $CFG python -m toothcanal.visualize --config $CFG --all echo echo "============================================================" echo "DONE. Outputs:" echo " $OUT/meshes/ : per-tooth STL files" echo " $OUT/viz/*.glb,.png : combined tooth+canal 3D preview" echo " $OUT/eval_metrics.csv : surface metrics on held-out cases" echo "============================================================"