| #!/usr/bin/env bash |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| set -e |
| export HF_ENDPOINT=${HF_ENDPOINT:-https://hf-mirror.com} |
| CFG=configs/default.yaml |
|
|
| |
| 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" |
|
|
| |
| 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 |
|
|
| |
| 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 |
|
|
| |
| |
| 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 |
|
|
| |
| if [[ "${FORCE_STAGE1:-0}" == "1" ]]; then |
| rm -f "$OUT/stage1.pt" |
| fi |
| if [[ -f "$OUT/stage1.pt" ]]; then |
| |
| 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 |
|
|
| |
| 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 |
|
|
| |
| 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 |
|
|
| |
| 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 "============================================================" |
|
|