File size: 5,559 Bytes
08764e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | #!/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 "============================================================"
|