Spaces:
Sleeping
Sleeping
Serve the 5-fold CNN ensemble instead of one checkpoint
Browse files- src/drdetect/serve/demo.py +16 -4
src/drdetect/serve/demo.py
CHANGED
|
@@ -60,7 +60,7 @@ def _format_summary(result: PredictionResult) -> str:
|
|
| 60 |
|
| 61 |
|
| 62 |
def build_interface(
|
| 63 |
-
checkpoint: str | Path,
|
| 64 |
*,
|
| 65 |
backbone: str = "efficientnet_b0",
|
| 66 |
loss_name: str = "ce",
|
|
@@ -72,6 +72,12 @@ def build_interface(
|
|
| 72 |
on HF Spaces' free ZeroGPU tier, where `torch.cuda.is_available()`
|
| 73 |
reports True outside an actual `@spaces.GPU` grant, so auto-detection
|
| 74 |
would pick "cuda" and crash the moment a tensor touches it (see app.py).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
"""
|
| 76 |
import gradio as gr
|
| 77 |
import torch
|
|
@@ -82,15 +88,21 @@ def build_interface(
|
|
| 82 |
if torch.backends.mps.is_available()
|
| 83 |
else ("cuda" if torch.cuda.is_available() else "cpu")
|
| 84 |
)
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
def infer(image: np.ndarray, force: bool):
|
| 89 |
if image is None:
|
| 90 |
return None, "Upload a fundus photo to begin."
|
| 91 |
result = run_pipeline(
|
| 92 |
image,
|
| 93 |
-
|
| 94 |
loss_name=loss_name,
|
| 95 |
size=size,
|
| 96 |
device=device,
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
def build_interface(
|
| 63 |
+
checkpoint: str | Path | list[str | Path],
|
| 64 |
*,
|
| 65 |
backbone: str = "efficientnet_b0",
|
| 66 |
loss_name: str = "ce",
|
|
|
|
| 72 |
on HF Spaces' free ZeroGPU tier, where `torch.cuda.is_available()`
|
| 73 |
reports True outside an actual `@spaces.GPU` grant, so auto-detection
|
| 74 |
would pick "cuda" and crash the moment a tensor touches it (see app.py).
|
| 75 |
+
|
| 76 |
+
`checkpoint` accepts a list to run as a cross-fold ensemble (raw outputs
|
| 77 |
+
and Grad-CAMs averaged in `run_pipeline`) instead of one model -- how the
|
| 78 |
+
live Space serves the 5-fold CNN ensemble that beat the single shipped
|
| 79 |
+
checkpoint on the DDR external test set (referable AUC 0.899 vs 0.891,
|
| 80 |
+
DeLong p=0.038).
|
| 81 |
"""
|
| 82 |
import gradio as gr
|
| 83 |
import torch
|
|
|
|
| 88 |
if torch.backends.mps.is_available()
|
| 89 |
else ("cuda" if torch.cuda.is_available() else "cpu")
|
| 90 |
)
|
| 91 |
+
checkpoints = [checkpoint] if isinstance(checkpoint, str | Path) else list(checkpoint)
|
| 92 |
+
models = [
|
| 93 |
+
load_grader(c, backbone=backbone, loss_name=loss_name, device=device) for c in checkpoints
|
| 94 |
+
]
|
| 95 |
+
# Only meaningful for a fitted sidecar next to this exact file (see
|
| 96 |
+
# load_temperature); none of the 5-fold CV checkpoints have one, and it's
|
| 97 |
+
# unused entirely for regression loss (the deployed config) either way.
|
| 98 |
+
temperature = load_temperature(checkpoints[0])
|
| 99 |
|
| 100 |
def infer(image: np.ndarray, force: bool):
|
| 101 |
if image is None:
|
| 102 |
return None, "Upload a fundus photo to begin."
|
| 103 |
result = run_pipeline(
|
| 104 |
image,
|
| 105 |
+
models,
|
| 106 |
loss_name=loss_name,
|
| 107 |
size=size,
|
| 108 |
device=device,
|