Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,10 @@ from sklearn.metrics import (
|
|
| 6 |
accuracy_score, precision_score, recall_score, f1_score,
|
| 7 |
confusion_matrix, ConfusionMatrixDisplay
|
| 8 |
)
|
| 9 |
-
|
| 10 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
MODEL_ID = "Thamer/resnet-fine_tuned"
|
| 13 |
clf = pipeline("image-classification", model=MODEL_ID)
|
|
@@ -75,27 +77,40 @@ def reset_cm():
|
|
| 75 |
|
| 76 |
def load_silpa_safe():
|
| 77 |
"""
|
| 78 |
-
SilpaCS/Alzheimer has a broken
|
| 79 |
-
|
|
|
|
| 80 |
"""
|
| 81 |
-
|
| 82 |
-
"
|
| 83 |
-
|
| 84 |
-
split="train"
|
| 85 |
)
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
def run_full_evaluation(progress=gr.Progress()):
|
| 89 |
-
"""
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
falah = load_dataset("Falah/Alzheimer_MRI", split="test")
|
| 92 |
falah_label_names = falah.features["label"].names
|
| 93 |
|
| 94 |
-
progress(0.05, desc="
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
|
|
|
| 97 |
y_true, y_pred = [], []
|
| 98 |
-
total = len(falah) + len(silpa)
|
| 99 |
i = 0
|
| 100 |
|
| 101 |
# --- Falah test split ---
|
|
@@ -107,15 +122,20 @@ def run_full_evaluation(progress=gr.Progress()):
|
|
| 107 |
y_pred.append(top)
|
| 108 |
i += 1
|
| 109 |
|
| 110 |
-
# --- SilpaCS (
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
progress(1.0, desc="Done!")
|
| 121 |
|
|
@@ -124,10 +144,18 @@ def run_full_evaluation(progress=gr.Progress()):
|
|
| 124 |
rec = recall_score(y_true, y_pred, average="macro", zero_division=0)
|
| 125 |
f1 = f1_score(y_true, y_pred, average="macro", zero_division=0)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
metrics_md = f"""
|
| 128 |
-
## Evaluation Results — ResNet-34
|
| 129 |
|
| 130 |
-
*
|
| 131 |
|
| 132 |
| Metric | Score |
|
| 133 |
|-----------|------------|
|
|
@@ -184,9 +212,12 @@ with gr.Blocks(title="Alzheimer's MRI Classification (4-class) — Demo") as dem
|
|
| 184 |
with gr.Tab("Full Evaluation (7,680 images)"):
|
| 185 |
gr.Markdown("""
|
| 186 |
### Combined Evaluation — Falah/Alzheimer_MRI test split + SilpaCS/Alzheimer
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
| 190 |
""")
|
| 191 |
|
| 192 |
eval_btn = gr.Button("Run Full Evaluation", variant="primary")
|
|
|
|
| 6 |
accuracy_score, precision_score, recall_score, f1_score,
|
| 7 |
confusion_matrix, ConfusionMatrixDisplay
|
| 8 |
)
|
|
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import requests
|
| 12 |
+
from io import BytesIO
|
| 13 |
|
| 14 |
MODEL_ID = "Thamer/resnet-fine_tuned"
|
| 15 |
clf = pipeline("image-classification", model=MODEL_ID)
|
|
|
|
| 77 |
|
| 78 |
def load_silpa_safe():
|
| 79 |
"""
|
| 80 |
+
SilpaCS/Alzheimer has a broken dataset builder on HuggingFace.
|
| 81 |
+
Fetch the raw auto-converted Parquet file directly via HTTP instead,
|
| 82 |
+
bypassing the datasets library entirely for this source.
|
| 83 |
"""
|
| 84 |
+
url = (
|
| 85 |
+
"https://huggingface.co/datasets/SilpaCS/Alzheimer"
|
| 86 |
+
"/resolve/refs%2Fconvert%2Fparquet/default/train/0000.parquet"
|
|
|
|
| 87 |
)
|
| 88 |
+
response = requests.get(url, timeout=120)
|
| 89 |
+
response.raise_for_status()
|
| 90 |
+
df = pd.read_parquet(BytesIO(response.content))
|
| 91 |
+
return df
|
| 92 |
|
| 93 |
def run_full_evaluation(progress=gr.Progress()):
|
| 94 |
+
"""
|
| 95 |
+
Evaluate on:
|
| 96 |
+
- Falah/Alzheimer_MRI test split (1,280 images) — clean held-out set
|
| 97 |
+
- SilpaCS/Alzheimer train split (6,400 images) — independent source
|
| 98 |
+
Total: 7,680 images
|
| 99 |
+
"""
|
| 100 |
+
progress(0, desc="Loading Falah/Alzheimer_MRI test split...")
|
| 101 |
falah = load_dataset("Falah/Alzheimer_MRI", split="test")
|
| 102 |
falah_label_names = falah.features["label"].names
|
| 103 |
|
| 104 |
+
progress(0.05, desc="Fetching SilpaCS/Alzheimer via Parquet...")
|
| 105 |
+
try:
|
| 106 |
+
silpa_df = load_silpa_safe()
|
| 107 |
+
except Exception as e:
|
| 108 |
+
# If SilpaCS fails, fall back to Falah-only evaluation
|
| 109 |
+
silpa_df = None
|
| 110 |
+
print(f"Warning: SilpaCS failed to load ({e}), running Falah-only evaluation.")
|
| 111 |
|
| 112 |
+
total = len(falah) + (len(silpa_df) if silpa_df is not None else 0)
|
| 113 |
y_true, y_pred = [], []
|
|
|
|
| 114 |
i = 0
|
| 115 |
|
| 116 |
# --- Falah test split ---
|
|
|
|
| 122 |
y_pred.append(top)
|
| 123 |
i += 1
|
| 124 |
|
| 125 |
+
# --- SilpaCS (raw Parquet DataFrame) ---
|
| 126 |
+
if silpa_df is not None:
|
| 127 |
+
for _, row in silpa_df.iterrows():
|
| 128 |
+
progress(i / total, desc=f"Evaluating image {i+1}/{total}...")
|
| 129 |
+
try:
|
| 130 |
+
img_bytes = row["image"]["bytes"]
|
| 131 |
+
img = Image.open(BytesIO(img_bytes)).convert("RGB")
|
| 132 |
+
top = _get_top_label(clf(img))
|
| 133 |
+
raw = row["label"]
|
| 134 |
+
y_true.append(SILPA_LABEL_MAP.get(raw, raw))
|
| 135 |
+
y_pred.append(top)
|
| 136 |
+
except Exception:
|
| 137 |
+
pass # skip any malformed rows silently
|
| 138 |
+
i += 1
|
| 139 |
|
| 140 |
progress(1.0, desc="Done!")
|
| 141 |
|
|
|
|
| 144 |
rec = recall_score(y_true, y_pred, average="macro", zero_division=0)
|
| 145 |
f1 = f1_score(y_true, y_pred, average="macro", zero_division=0)
|
| 146 |
|
| 147 |
+
n_falah = len(falah)
|
| 148 |
+
n_silpa = len(silpa_df) if silpa_df is not None else 0
|
| 149 |
+
source_note = (
|
| 150 |
+
f"Falah/Alzheimer_MRI test split ({n_falah} images) + SilpaCS/Alzheimer ({n_silpa} images)"
|
| 151 |
+
if n_silpa > 0
|
| 152 |
+
else f"Falah/Alzheimer_MRI test split only ({n_falah} images) — SilpaCS failed to load"
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
metrics_md = f"""
|
| 156 |
+
## Evaluation Results — ResNet-34
|
| 157 |
|
| 158 |
+
*{source_note}*
|
| 159 |
|
| 160 |
| Metric | Score |
|
| 161 |
|-----------|------------|
|
|
|
|
| 212 |
with gr.Tab("Full Evaluation (7,680 images)"):
|
| 213 |
gr.Markdown("""
|
| 214 |
### Combined Evaluation — Falah/Alzheimer_MRI test split + SilpaCS/Alzheimer
|
| 215 |
+
|
| 216 |
+
Evaluates across **7,680 total MRI images** from two independent sources:
|
| 217 |
+
- **Falah/Alzheimer_MRI** (1,280 images) — the held-out test split of the model's training dataset
|
| 218 |
+
- **SilpaCS/Alzheimer** (6,400 images) — a fully independent dataset not used during training
|
| 219 |
+
|
| 220 |
+
⚠️ This will take **several minutes** to complete.
|
| 221 |
""")
|
| 222 |
|
| 223 |
eval_btn = gr.Button("Run Full Evaluation", variant="primary")
|