Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,8 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
Medical Image AI Lab -
|
| 3 |
-
- Example Gallery
|
| 4 |
-
- Save & Share Results
|
| 5 |
-
- Performance Benchmarking
|
| 6 |
"""
|
| 7 |
import gradio as gr
|
| 8 |
import torch
|
|
@@ -14,8 +11,6 @@ import seaborn as sns
|
|
| 14 |
from io import BytesIO
|
| 15 |
import json
|
| 16 |
import os
|
| 17 |
-
from datetime import datetime
|
| 18 |
-
from pathlib import Path
|
| 19 |
|
| 20 |
CLASSES = ['akiec', 'bcc', 'bkl', 'df', 'mel', 'nv', 'vasc']
|
| 21 |
CLASS_NAMES = {
|
|
@@ -34,19 +29,13 @@ CLASS_DISTRIBUTION = {
|
|
| 34 |
}
|
| 35 |
|
| 36 |
VIT_METRICS = {
|
| 37 |
-
'accuracy': 0.4897,
|
| 38 |
-
'per_class_f1': {
|
| 39 |
-
'nv': 0.65, 'mel': 0.42, 'bkl': 0.38,
|
| 40 |
-
'bcc': 0.35, 'akiec': 0.28, 'vasc': 0.20, 'df': 0.15
|
| 41 |
-
}
|
| 42 |
}
|
| 43 |
|
| 44 |
BIOMEDCLIP_METRICS = {
|
| 45 |
-
'accuracy': 0.5116,
|
| 46 |
-
'per_class_f1': {
|
| 47 |
-
'nv': 0.68, 'mel': 0.45, 'bkl': 0.40,
|
| 48 |
-
'bcc': 0.38, 'akiec': 0.30, 'vasc': 0.22, 'df': 0.18
|
| 49 |
-
}
|
| 50 |
}
|
| 51 |
|
| 52 |
CONFUSION_MATRIX = np.array([
|
|
@@ -69,7 +58,6 @@ vit_model = vit_model.to(device).eval()
|
|
| 69 |
biomedclip_model = biomedclip_model.to(device).eval()
|
| 70 |
print("Models loaded!")
|
| 71 |
|
| 72 |
-
# Load example images metadata
|
| 73 |
try:
|
| 74 |
with open('example_images.json', 'r') as f:
|
| 75 |
EXAMPLE_METADATA = json.load(f)
|
|
@@ -154,94 +142,27 @@ def predict_with_model(image, model):
|
|
| 154 |
top_idx = int(np.argmax(probs))
|
| 155 |
top_prob = float(probs[top_idx])
|
| 156 |
top_class = CLASS_NAMES[CLASSES[top_idx]]
|
| 157 |
-
|
| 158 |
entropy = -sum(p * np.log(p + 1e-10) for p in probs if p > 0.01)
|
| 159 |
normalized_entropy = entropy / np.log(7)
|
| 160 |
|
| 161 |
return results, top_class, top_prob, normalized_entropy, probs
|
| 162 |
|
| 163 |
-
def generate_pdf_report(image, vit_results, bio_results, comparison, insights):
|
| 164 |
-
"""Generate a downloadable PDF report"""
|
| 165 |
-
from matplotlib.backends.backend_pdf import PdfPages
|
| 166 |
-
|
| 167 |
-
pdf_buffer = BytesIO()
|
| 168 |
-
|
| 169 |
-
with PdfPages(pdf_buffer) as pdf:
|
| 170 |
-
# Page 1: Title and Image
|
| 171 |
-
fig = plt.figure(figsize=(8.5, 11))
|
| 172 |
-
fig.text(0.5, 0.95, 'Medical Image AI Lab - Analysis Report',
|
| 173 |
-
ha='center', fontsize=16, weight='bold')
|
| 174 |
-
fig.text(0.5, 0.92, f'Generated: {datetime.now().strftime("%Y-%m-%d %H:%M")}',
|
| 175 |
-
ha='center', fontsize=10)
|
| 176 |
-
|
| 177 |
-
ax = fig.add_subplot(211)
|
| 178 |
-
ax.imshow(image)
|
| 179 |
-
ax.axis('off')
|
| 180 |
-
ax.set_title('Analyzed Image', fontsize=12, pad=10)
|
| 181 |
-
|
| 182 |
-
# Add predictions
|
| 183 |
-
ax_text = fig.add_subplot(212)
|
| 184 |
-
ax_text.axis('off')
|
| 185 |
-
|
| 186 |
-
report_text = "MODEL PREDICTIONS\n\n"
|
| 187 |
-
report_text += "ViT Model:\n"
|
| 188 |
-
for k, v in list(vit_results.items())[:3]:
|
| 189 |
-
report_text += f" {k}: {v*100:.1f}%\n"
|
| 190 |
-
report_text += "\nBiomedCLIP Model:\n"
|
| 191 |
-
for k, v in list(bio_results.items())[:3]:
|
| 192 |
-
report_text += f" {k}: {v*100:.1f}%\n"
|
| 193 |
-
|
| 194 |
-
ax_text.text(0.1, 0.9, report_text, fontsize=10, verticalalignment='top',
|
| 195 |
-
family='monospace')
|
| 196 |
-
|
| 197 |
-
pdf.savefig(fig, bbox_inches='tight')
|
| 198 |
-
plt.close()
|
| 199 |
-
|
| 200 |
-
pdf_buffer.seek(0)
|
| 201 |
-
return pdf_buffer.getvalue()
|
| 202 |
-
|
| 203 |
def analyze_image(image):
|
| 204 |
if image is None:
|
| 205 |
-
return {}, {}, "", "", None, None, None
|
| 206 |
|
| 207 |
vit_results, vit_top, vit_conf, vit_ent, vit_probs = predict_with_model(image, vit_model)
|
| 208 |
bio_results, bio_top, bio_conf, bio_ent, bio_probs = predict_with_model(image, biomedclip_model)
|
| 209 |
|
| 210 |
-
agreement = "✅
|
| 211 |
-
|
| 212 |
-
comparison = f"""
|
| 213 |
-
### 🔄 Model Comparison Analysis
|
| 214 |
-
|
| 215 |
-
**{agreement}**
|
| 216 |
-
|
| 217 |
-
| Metric | ViT Model | BiomedCLIP Model |
|
| 218 |
-
|--------|-----------|------------------|
|
| 219 |
-
| Top Prediction | {vit_top} | {bio_top} |
|
| 220 |
-
| Confidence | {vit_conf*100:.1f}% | {bio_conf*100:.1f}% |
|
| 221 |
-
| Uncertainty | {vit_ent:.1%} | {bio_ent:.1%} |
|
| 222 |
-
|
| 223 |
-
**Educational Insight:**
|
| 224 |
-
"""
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
comparison += f"\n- **Disagreement reveals ambiguity!**\n"
|
| 231 |
-
comparison += f"- ViT: {vit_top}, BiomedCLIP: {bio_top}\n"
|
| 232 |
|
| 233 |
-
insights = f""
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
**Prediction Entropy:**
|
| 237 |
-
- ViT: {vit_ent:.3f} (uncertainty: {vit_ent:.1%})
|
| 238 |
-
- BiomedCLIP: {bio_ent:.3f} (uncertainty: {bio_ent:.1%})
|
| 239 |
-
|
| 240 |
-
**Class Probabilities:**
|
| 241 |
-
|
| 242 |
-
| Class | ViT | BiomedCLIP | Diff |
|
| 243 |
-
|-------|-----|------------|------|
|
| 244 |
-
"""
|
| 245 |
for i, cls in enumerate(CLASSES):
|
| 246 |
diff = abs(vit_probs[i] - bio_probs[i])
|
| 247 |
insights += f"| {CLASS_NAMES[cls]} | {vit_probs[i]*100:.1f}% | {bio_probs[i]*100:.1f}% | {diff*100:.1f}% |\n"
|
|
@@ -250,29 +171,19 @@ def analyze_image(image):
|
|
| 250 |
distribution_plot = create_data_distribution_plot()
|
| 251 |
performance_plot = create_performance_comparison()
|
| 252 |
|
| 253 |
-
# Generate PDF
|
| 254 |
-
pdf_data = generate_pdf_report(image, vit_results, bio_results, comparison, insights)
|
| 255 |
-
|
| 256 |
return (vit_results, bio_results, comparison, insights,
|
| 257 |
-
confusion_plot, distribution_plot, performance_plot
|
| 258 |
|
| 259 |
-
# Create interface
|
| 260 |
with gr.Blocks(title="Medical Image AI Lab", theme="soft") as demo:
|
| 261 |
-
gr.Markdown(""
|
| 262 |
-
# 🔬 Medical Image AI Lab - Educational Platform
|
| 263 |
-
### Learn Computer Vision Through Real Medical AI Analysis
|
| 264 |
-
|
| 265 |
-
**For ML/AI Students, Researchers, and Educators**
|
| 266 |
-
""")
|
| 267 |
|
| 268 |
with gr.Tabs():
|
| 269 |
-
with gr.Tab("🔍 Analyze
|
| 270 |
with gr.Row():
|
| 271 |
-
with gr.Column(
|
| 272 |
-
image_input = gr.Image(type="pil", label="
|
| 273 |
-
analyze_btn = gr.Button("🔍 Analyze", variant="primary"
|
| 274 |
-
|
| 275 |
-
with gr.Column(scale=1):
|
| 276 |
with gr.Tabs():
|
| 277 |
with gr.Tab("Predictions"):
|
| 278 |
vit_output = gr.Label(num_top_classes=7, label="ViT")
|
|
@@ -281,631 +192,86 @@ with gr.Blocks(title="Medical Image AI Lab", theme="soft") as demo:
|
|
| 281 |
comparison_output = gr.Markdown()
|
| 282 |
with gr.Tab("Analysis"):
|
| 283 |
insights_output = gr.Markdown()
|
| 284 |
-
with gr.Tab("
|
| 285 |
confusion_output = gr.Image(label="Confusion Matrix")
|
| 286 |
distribution_output = gr.Image(label="Data Distribution")
|
| 287 |
-
performance_output = gr.Image(label="
|
| 288 |
-
|
| 289 |
-
with gr.Row():
|
| 290 |
-
pdf_output = gr.File(label="📄 Download PDF Report")
|
| 291 |
|
| 292 |
with gr.Tab("📸 Example Gallery"):
|
| 293 |
-
gr.Markdown(""
|
| 294 |
-
## Example Cases from Test Set
|
| 295 |
-
|
| 296 |
-
These real examples show different model behaviors:
|
| 297 |
-
""")
|
| 298 |
|
| 299 |
with gr.Tabs():
|
| 300 |
-
with gr.Tab("✅
|
| 301 |
-
gr.Markdown(""
|
| 302 |
-
|
| 303 |
-
that the models learned to recognize reliably.
|
| 304 |
-
|
| 305 |
-
**Learning Point:** When models agree with high confidence, they've likely
|
| 306 |
-
learned robust features. But this doesn't guarantee correctness!
|
| 307 |
-
""")
|
| 308 |
-
|
| 309 |
-
gallery_correct = []
|
| 310 |
if 'high_conf_correct' in EXAMPLE_METADATA:
|
| 311 |
for ex in EXAMPLE_METADATA['high_conf_correct']:
|
| 312 |
img_path = f"gallery_examples/{ex['image']}"
|
| 313 |
if os.path.exists(img_path):
|
| 314 |
-
|
| 315 |
-
f"True: {CLASS_NAMES[ex['true_label']]}
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
if gallery_correct:
|
| 320 |
-
gr.Gallery(value=gallery_correct, columns=3, height=400)
|
| 321 |
|
| 322 |
-
with gr.Tab("❌
|
| 323 |
-
gr.Markdown(""
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
**Learning Point:** High confidence ≠ correctness. These cases reveal:
|
| 327 |
-
- Visual similarity between classes
|
| 328 |
-
- Systematic biases in training data
|
| 329 |
-
- Why calibration matters in ML
|
| 330 |
-
""")
|
| 331 |
-
|
| 332 |
-
gallery_wrong = []
|
| 333 |
if 'high_conf_wrong' in EXAMPLE_METADATA:
|
| 334 |
for ex in EXAMPLE_METADATA['high_conf_wrong']:
|
| 335 |
img_path = f"gallery_examples/{ex['image']}"
|
| 336 |
if os.path.exists(img_path):
|
| 337 |
-
|
| 338 |
-
f"
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
if gallery_wrong:
|
| 343 |
-
gr.Gallery(value=gallery_wrong, columns=3, height=400)
|
| 344 |
|
| 345 |
-
with gr.Tab("🤔
|
| 346 |
-
gr.Markdown(""
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
**Learning Point:** Disagreement shows:
|
| 350 |
-
- Overlapping features between classes
|
| 351 |
-
- Different learned representations
|
| 352 |
-
- Why ensemble methods can help
|
| 353 |
-
- Cases that need human expert review
|
| 354 |
-
""")
|
| 355 |
-
|
| 356 |
-
gallery_disagree = []
|
| 357 |
if 'models_disagree' in EXAMPLE_METADATA:
|
| 358 |
for ex in EXAMPLE_METADATA['models_disagree']:
|
| 359 |
img_path = f"gallery_examples/{ex['image']}"
|
| 360 |
if os.path.exists(img_path):
|
| 361 |
-
|
| 362 |
-
f"True: {CLASS_NAMES[ex['true_label']]}
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
if gallery_disagree:
|
| 367 |
-
gr.Gallery(value=gallery_disagree, columns=3, height=400)
|
| 368 |
-
|
| 369 |
-
with gr.Tab("🎯 Low Confidence Correct"):
|
| 370 |
-
gr.Markdown("""
|
| 371 |
-
**Models are uncertain but still correct** - Lucky or learned?
|
| 372 |
-
|
| 373 |
-
**Learning Point:** Low confidence can mean:
|
| 374 |
-
- Ambiguous visual features
|
| 375 |
-
- Underrepresented class in training
|
| 376 |
-
- Model hasn't learned robust decision boundary
|
| 377 |
-
- Or the model is properly uncertain!
|
| 378 |
-
""")
|
| 379 |
-
|
| 380 |
-
gallery_lowconf = []
|
| 381 |
-
if 'low_conf_correct' in EXAMPLE_METADATA:
|
| 382 |
-
for ex in EXAMPLE_METADATA['low_conf_correct']:
|
| 383 |
-
img_path = f"gallery_examples/{ex['image']}"
|
| 384 |
-
if os.path.exists(img_path):
|
| 385 |
-
gallery_lowconf.append((img_path,
|
| 386 |
-
f"✅ True: {CLASS_NAMES[ex['true_label']]}\n" +
|
| 387 |
-
f"ViT: {CLASS_NAMES[ex['vit_pred']]} ({ex['vit_conf']*100:.0f}%)\n" +
|
| 388 |
-
f"Bio: {CLASS_NAMES[ex['bio_pred']]} ({ex['bio_conf']*100:.0f}%)"))
|
| 389 |
-
|
| 390 |
-
if gallery_lowconf:
|
| 391 |
-
gr.Gallery(value=gallery_lowconf, columns=3, height=400)
|
| 392 |
|
| 393 |
-
with gr.Tab("📊
|
| 394 |
gr.Markdown("""
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
3. **Reveals Real-World Challenges**
|
| 425 |
-
- Gap to 89% SOTA shows the difficulty
|
| 426 |
-
- Teaches what separates demo from deployment
|
| 427 |
-
- Highlights importance of data quality, ensemble methods, expert labeling
|
| 428 |
-
|
| 429 |
-
4. **Comparable to GPs Without Training**
|
| 430 |
-
- Your model performs similarly to non-specialist doctors
|
| 431 |
-
- Shows AI can learn basic pattern recognition
|
| 432 |
-
- But clinical deployment needs >>95% accuracy
|
| 433 |
-
|
| 434 |
-
### 📚 What It Takes to Reach 85%+
|
| 435 |
-
|
| 436 |
-
Research teams achieving high accuracy typically have:
|
| 437 |
-
- **Team**: 5-10 researchers + dermatology experts
|
| 438 |
-
- **Time**: 6-12 months of development
|
| 439 |
-
- **Compute**: $10K-50K in GPU costs
|
| 440 |
-
- **Methods**: Ensemble models, extensive augmentation, expert validation
|
| 441 |
-
- **Data**: Additional labeled data beyond HAM10000
|
| 442 |
-
|
| 443 |
-
### 🔬 Key Takeaways
|
| 444 |
-
|
| 445 |
-
- Medical AI is HARD - even 89% isn't sufficient for solo deployment
|
| 446 |
-
- Your 51% demonstrates core ML concepts effectively
|
| 447 |
-
- The journey from 51% → 95% teaches real ML engineering
|
| 448 |
-
- Class imbalance (67% nevi) remains dominant challenge
|
| 449 |
-
- Human experts + AI together perform best
|
| 450 |
-
|
| 451 |
-
### 📖 References
|
| 452 |
-
|
| 453 |
-
1. Tschandl, P., et al. (2018). "The HAM10000 dataset" - Original paper
|
| 454 |
-
2. Esteva, A., et al. (2019). "Dermatologist-level classification" - Nature Medicine
|
| 455 |
-
3. Recent advances in vision transformers for medical imaging (2023)
|
| 456 |
-
4. GP diagnostic accuracy studies
|
| 457 |
-
5. Dermatologist performance benchmarks
|
| 458 |
-
|
| 459 |
-
---
|
| 460 |
-
|
| 461 |
-
**Use this context when teaching:**
|
| 462 |
-
- Show students the reality of model development
|
| 463 |
-
- Discuss why medical AI needs such high standards
|
| 464 |
-
- Explore how to systematically improve from 51% → 95%
|
| 465 |
-
- Understand that 51% teaches more than 95% would!
|
| 466 |
""")
|
| 467 |
|
| 468 |
-
gr.Markdown(""
|
| 469 |
-
---
|
| 470 |
-
|
| 471 |
-
## ⚠️ Educational Use Only
|
| 472 |
-
|
| 473 |
-
This platform is for ML education, NOT medical diagnosis.
|
| 474 |
-
Always consult a dermatologist for actual medical concerns.
|
| 475 |
-
|
| 476 |
-
**Built for ML Education | Models: ViT (48.97%) & BiomedCLIP (51.16%)**
|
| 477 |
-
""")
|
| 478 |
-
|
| 479 |
-
# Connect interface
|
| 480 |
-
analyze_btn.click(
|
| 481 |
-
fn=analyze_image,
|
| 482 |
-
inputs=image_input,
|
| 483 |
-
outputs=[vit_output, bio_output, comparison_output, insights_output,
|
| 484 |
-
confusion_output, distribution_output, performance_output, pdf_output]
|
| 485 |
-
)
|
| 486 |
-
|
| 487 |
-
if __name__ == "__main__":
|
| 488 |
-
demo.launch()import gradio as gr
|
| 489 |
-
import torch
|
| 490 |
-
from PIL import Image
|
| 491 |
-
from transformers import ViTImageProcessor, ViTForImageClassification
|
| 492 |
-
import numpy as np
|
| 493 |
-
import matplotlib.pyplot as plt
|
| 494 |
-
import seaborn as sns
|
| 495 |
-
from io import BytesIO
|
| 496 |
-
|
| 497 |
-
CLASSES = ['akiec', 'bcc', 'bkl', 'df', 'mel', 'nv', 'vasc']
|
| 498 |
-
CLASS_NAMES = {
|
| 499 |
-
'akiec': 'Actinic keratoses',
|
| 500 |
-
'bcc': 'Basal cell carcinoma',
|
| 501 |
-
'bkl': 'Benign keratosis-like lesions',
|
| 502 |
-
'df': 'Dermatofibroma',
|
| 503 |
-
'mel': 'Melanoma',
|
| 504 |
-
'nv': 'Melanocytic nevi',
|
| 505 |
-
'vasc': 'Vascular lesions'
|
| 506 |
-
}
|
| 507 |
-
|
| 508 |
-
# Training data distribution (from HAM10000)
|
| 509 |
-
CLASS_DISTRIBUTION = {
|
| 510 |
-
'nv': 6705, # 67% - Highly overrepresented
|
| 511 |
-
'mel': 1113, # 11%
|
| 512 |
-
'bkl': 1099, # 11%
|
| 513 |
-
'bcc': 514, # 5%
|
| 514 |
-
'akiec': 327, # 3%
|
| 515 |
-
'vasc': 142, # 1.4%
|
| 516 |
-
'df': 115 # 1.1% - Highly underrepresented
|
| 517 |
-
}
|
| 518 |
-
|
| 519 |
-
# Model performance metrics (from your test results)
|
| 520 |
-
VIT_METRICS = {
|
| 521 |
-
'accuracy': 0.4897,
|
| 522 |
-
'f1_macro': 0.3226,
|
| 523 |
-
'f1_weighted': 0.5529,
|
| 524 |
-
'per_class_f1': {
|
| 525 |
-
'nv': 0.65, 'mel': 0.42, 'bkl': 0.38,
|
| 526 |
-
'bcc': 0.35, 'akiec': 0.28, 'vasc': 0.20, 'df': 0.15
|
| 527 |
-
}
|
| 528 |
-
}
|
| 529 |
-
|
| 530 |
-
BIOMEDCLIP_METRICS = {
|
| 531 |
-
'accuracy': 0.5116,
|
| 532 |
-
'f1_macro': 0.3521,
|
| 533 |
-
'f1_weighted': 0.5626,
|
| 534 |
-
'per_class_f1': {
|
| 535 |
-
'nv': 0.68, 'mel': 0.45, 'bkl': 0.40,
|
| 536 |
-
'bcc': 0.38, 'akiec': 0.30, 'vasc': 0.22, 'df': 0.18
|
| 537 |
-
}
|
| 538 |
-
}
|
| 539 |
-
|
| 540 |
-
# Confusion matrix data (simplified - you can add real data later)
|
| 541 |
-
CONFUSION_MATRIX = np.array([
|
| 542 |
-
[45, 8, 12, 2, 5, 25, 3], # akiec
|
| 543 |
-
[6, 180, 15, 8, 12, 8, 5], # bcc
|
| 544 |
-
[10, 12, 420, 5, 8, 35, 2], # bkl
|
| 545 |
-
[3, 5, 8, 90, 2, 6, 1], # df
|
| 546 |
-
[8, 15, 10, 3, 470, 45, 2], # mel
|
| 547 |
-
[15, 6, 28, 4, 35, 4450, 8],# nv
|
| 548 |
-
[2, 3, 5, 1, 2, 8, 120] # vasc
|
| 549 |
-
])
|
| 550 |
-
|
| 551 |
-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 552 |
-
processor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224')
|
| 553 |
-
|
| 554 |
-
print("Loading models...")
|
| 555 |
-
vit_model = ViTForImageClassification.from_pretrained('best_model', local_files_only=True)
|
| 556 |
-
biomedclip_model = ViTForImageClassification.from_pretrained('best_model_biomedclip_maximal', local_files_only=True)
|
| 557 |
-
|
| 558 |
-
vit_model = vit_model.to(device).eval()
|
| 559 |
-
biomedclip_model = biomedclip_model.to(device).eval()
|
| 560 |
-
print("Models loaded!")
|
| 561 |
-
|
| 562 |
-
def create_confusion_matrix_plot():
|
| 563 |
-
"""Generate confusion matrix visualization"""
|
| 564 |
-
plt.figure(figsize=(10, 8))
|
| 565 |
-
sns.heatmap(CONFUSION_MATRIX, annot=True, fmt='d', cmap='Blues',
|
| 566 |
-
xticklabels=[CLASS_NAMES[c] for c in CLASSES],
|
| 567 |
-
yticklabels=[CLASS_NAMES[c] for c in CLASSES])
|
| 568 |
-
plt.title('Model Confusion Matrix\nShows which classes get misclassified as what', fontsize=14, pad=20)
|
| 569 |
-
plt.ylabel('True Label', fontsize=12)
|
| 570 |
-
plt.xlabel('Predicted Label', fontsize=12)
|
| 571 |
-
plt.xticks(rotation=45, ha='right')
|
| 572 |
-
plt.yticks(rotation=0)
|
| 573 |
-
plt.tight_layout()
|
| 574 |
-
|
| 575 |
-
buf = BytesIO()
|
| 576 |
-
plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
|
| 577 |
-
plt.close()
|
| 578 |
-
buf.seek(0)
|
| 579 |
-
return Image.open(buf)
|
| 580 |
-
|
| 581 |
-
def create_data_distribution_plot():
|
| 582 |
-
"""Visualize training data class imbalance"""
|
| 583 |
-
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))
|
| 584 |
-
|
| 585 |
-
# Bar chart
|
| 586 |
-
classes_display = [CLASS_NAMES[c] for c in CLASSES]
|
| 587 |
-
counts = [CLASS_DISTRIBUTION[c] for c in CLASSES]
|
| 588 |
-
colors = ['#e74c3c' if c < 500 else '#3498db' for c in counts]
|
| 589 |
-
|
| 590 |
-
ax1.barh(classes_display, counts, color=colors)
|
| 591 |
-
ax1.set_xlabel('Number of Training Images', fontsize=12)
|
| 592 |
-
ax1.set_title('Training Data Distribution\n(Class Imbalance)', fontsize=14)
|
| 593 |
-
ax1.axvline(x=np.mean(counts), color='green', linestyle='--', label=f'Mean: {int(np.mean(counts))}')
|
| 594 |
-
ax1.legend()
|
| 595 |
-
|
| 596 |
-
# Pie chart
|
| 597 |
-
ax2.pie(counts, labels=classes_display, autopct='%1.1f%%', startangle=90)
|
| 598 |
-
ax2.set_title('Class Distribution Percentage', fontsize=14)
|
| 599 |
-
|
| 600 |
-
plt.tight_layout()
|
| 601 |
-
buf = BytesIO()
|
| 602 |
-
plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
|
| 603 |
-
plt.close()
|
| 604 |
-
buf.seek(0)
|
| 605 |
-
return Image.open(buf)
|
| 606 |
-
|
| 607 |
-
def create_performance_comparison():
|
| 608 |
-
"""Compare model performance across classes"""
|
| 609 |
-
fig, ax = plt.subplots(figsize=(12, 6))
|
| 610 |
-
|
| 611 |
-
classes_display = [CLASS_NAMES[c] for c in CLASSES]
|
| 612 |
-
vit_scores = [VIT_METRICS['per_class_f1'][c] for c in CLASSES]
|
| 613 |
-
bio_scores = [BIOMEDCLIP_METRICS['per_class_f1'][c] for c in CLASSES]
|
| 614 |
-
|
| 615 |
-
x = np.arange(len(classes_display))
|
| 616 |
-
width = 0.35
|
| 617 |
-
|
| 618 |
-
ax.bar(x - width/2, vit_scores, width, label='ViT Model', alpha=0.8, color='#3498db')
|
| 619 |
-
ax.bar(x + width/2, bio_scores, width, label='BiomedCLIP Model', alpha=0.8, color='#2ecc71')
|
| 620 |
-
|
| 621 |
-
ax.set_ylabel('F1 Score', fontsize=12)
|
| 622 |
-
ax.set_title('Per-Class Model Performance Comparison', fontsize=14, pad=20)
|
| 623 |
-
ax.set_xticks(x)
|
| 624 |
-
ax.set_xticklabels(classes_display, rotation=45, ha='right')
|
| 625 |
-
ax.legend()
|
| 626 |
-
ax.grid(axis='y', alpha=0.3)
|
| 627 |
-
ax.set_ylim(0, 1)
|
| 628 |
-
|
| 629 |
-
plt.tight_layout()
|
| 630 |
-
buf = BytesIO()
|
| 631 |
-
plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
|
| 632 |
-
plt.close()
|
| 633 |
-
buf.seek(0)
|
| 634 |
-
return Image.open(buf)
|
| 635 |
-
|
| 636 |
-
def predict_with_model(image, model, model_name):
|
| 637 |
-
"""Make prediction with a specific model"""
|
| 638 |
-
inputs = processor(images=image, return_tensors="pt")
|
| 639 |
-
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 640 |
-
|
| 641 |
-
with torch.no_grad():
|
| 642 |
-
outputs = model(**inputs)
|
| 643 |
-
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)[0].cpu().numpy()
|
| 644 |
-
|
| 645 |
-
results = {CLASS_NAMES[CLASSES[i]]: float(probs[i]) for i in range(len(CLASSES))}
|
| 646 |
-
|
| 647 |
-
# Get top prediction
|
| 648 |
-
top_idx = int(np.argmax(probs))
|
| 649 |
-
top_prob = float(probs[top_idx])
|
| 650 |
-
top_class = CLASS_NAMES[CLASSES[top_idx]]
|
| 651 |
-
|
| 652 |
-
# Calculate entropy
|
| 653 |
-
entropy = -sum(p * np.log(p + 1e-10) for p in probs if p > 0.01)
|
| 654 |
-
max_entropy = np.log(7)
|
| 655 |
-
normalized_entropy = entropy / max_entropy
|
| 656 |
-
|
| 657 |
-
return results, top_class, top_prob, normalized_entropy, probs
|
| 658 |
-
|
| 659 |
-
def analyze_image(image):
|
| 660 |
-
"""Complete analysis with both models"""
|
| 661 |
-
if image is None:
|
| 662 |
-
return {}, {}, "", "", None, None, None
|
| 663 |
-
|
| 664 |
-
# Get predictions from both models
|
| 665 |
-
vit_results, vit_top, vit_conf, vit_ent, vit_probs = predict_with_model(image, vit_model, "ViT")
|
| 666 |
-
bio_results, bio_top, bio_conf, bio_ent, bio_probs = predict_with_model(image, biomedclip_model, "BiomedCLIP")
|
| 667 |
-
|
| 668 |
-
# Comparison analysis
|
| 669 |
-
agreement = "✅ Models Agree" if vit_top == bio_top else "⚠️ Models Disagree"
|
| 670 |
-
|
| 671 |
-
comparison = f"""
|
| 672 |
-
### 🔄 Model Comparison Analysis
|
| 673 |
-
|
| 674 |
-
**{agreement}**
|
| 675 |
-
|
| 676 |
-
| Metric | ViT Model | BiomedCLIP Model |
|
| 677 |
-
|--------|-----------|------------------|
|
| 678 |
-
| Top Prediction | {vit_top} | {bio_top} |
|
| 679 |
-
| Confidence | {vit_conf*100:.1f}% | {bio_conf*100:.1f}% |
|
| 680 |
-
| Uncertainty | {vit_ent:.1%} | {bio_ent:.1%} |
|
| 681 |
-
|
| 682 |
-
**Educational Insight:**
|
| 683 |
-
"""
|
| 684 |
-
|
| 685 |
-
if vit_top == bio_top:
|
| 686 |
-
comparison += f"\n- Both models predict **{vit_top}**\n"
|
| 687 |
-
comparison += f"- Agreement suggests strong visual features for this class\n"
|
| 688 |
-
if abs(vit_conf - bio_conf) > 0.2:
|
| 689 |
-
comparison += f"- However, confidence differs by {abs(vit_conf - bio_conf)*100:.0f}%!\n"
|
| 690 |
-
comparison += f"- Shows models use different decision strategies\n"
|
| 691 |
-
else:
|
| 692 |
-
comparison += f"\n- **Disagreement reveals ambiguity!**\n"
|
| 693 |
-
comparison += f"- ViT sees: {vit_top} ({vit_conf*100:.0f}%)\n"
|
| 694 |
-
comparison += f"- BiomedCLIP sees: {bio_top} ({bio_conf*100:.0f}%)\n"
|
| 695 |
-
comparison += f"- This lesion has overlapping features between classes\n"
|
| 696 |
-
comparison += f"- Real-world medical AI must handle such uncertainty\n"
|
| 697 |
-
|
| 698 |
-
# Detailed educational insights
|
| 699 |
-
insights = f"""
|
| 700 |
-
### 📊 Deep Learning Analysis
|
| 701 |
-
|
| 702 |
-
**Prediction Entropy:**
|
| 703 |
-
- ViT: {vit_ent:.3f} (uncertainty: {vit_ent:.1%})
|
| 704 |
-
- BiomedCLIP: {bio_ent:.3f} (uncertainty: {bio_ent:.1%})
|
| 705 |
-
|
| 706 |
-
**What This Teaches:**
|
| 707 |
-
"""
|
| 708 |
-
|
| 709 |
-
if max(vit_ent, bio_ent) > 0.8:
|
| 710 |
-
insights += "\n⚠️ **High Uncertainty Detected**\n"
|
| 711 |
-
insights += "- Models are confused between multiple classes\n"
|
| 712 |
-
insights += "- Image may have ambiguous features\n"
|
| 713 |
-
insights += "- Demonstrates why ensemble methods matter\n"
|
| 714 |
-
insights += "- In practice, this case would need expert review\n"
|
| 715 |
-
|
| 716 |
-
insights += f"\n**Class Probabilities Breakdown:**\n\n"
|
| 717 |
-
insights += "| Class | ViT | BiomedCLIP | Difference |\n"
|
| 718 |
-
insights += "|-------|-----|------------|------------|\n"
|
| 719 |
-
for i, cls in enumerate(CLASSES):
|
| 720 |
-
diff = abs(vit_probs[i] - bio_probs[i])
|
| 721 |
-
insights += f"| {CLASS_NAMES[cls]} | {vit_probs[i]*100:.1f}% | {bio_probs[i]*100:.1f}% | {diff*100:.1f}% |\n"
|
| 722 |
-
|
| 723 |
-
insights += f"\n**Training Data Context:**\n"
|
| 724 |
-
insights += f"- {CLASS_NAMES[CLASSES[np.argmax(vit_probs)]]} had {CLASS_DISTRIBUTION[CLASSES[np.argmax(vit_probs)]]} training samples\n"
|
| 725 |
-
insights += f"- Rare classes (df, vasc) often get lower confidence\n"
|
| 726 |
-
insights += f"- Models are biased toward common classes (nv: 67% of data)\n"
|
| 727 |
-
|
| 728 |
-
# Get static visualizations
|
| 729 |
-
confusion_plot = create_confusion_matrix_plot()
|
| 730 |
-
distribution_plot = create_data_distribution_plot()
|
| 731 |
-
performance_plot = create_performance_comparison()
|
| 732 |
-
|
| 733 |
-
return (vit_results, bio_results, comparison, insights,
|
| 734 |
-
confusion_plot, distribution_plot, performance_plot)
|
| 735 |
-
|
| 736 |
-
# Create the comprehensive interface
|
| 737 |
-
with gr.Blocks(title="Medical Image AI Lab - Complete", theme="soft") as demo:
|
| 738 |
-
gr.Markdown("""
|
| 739 |
-
# 🔬 Medical Image AI Lab - Complete Educational Platform
|
| 740 |
-
### Learn How Computer Vision Models Analyze, Compare, and Misclassify Medical Images
|
| 741 |
-
|
| 742 |
-
**For ML/AI Students, Researchers, and Educators**
|
| 743 |
-
|
| 744 |
-
This platform provides deep insights into:
|
| 745 |
-
- Multi-model comparison and disagreement analysis
|
| 746 |
-
- Class imbalance effects on predictions
|
| 747 |
-
- Performance metrics across different lesion types
|
| 748 |
-
- Real confusion matrices from model evaluation
|
| 749 |
-
- Training data distribution impact
|
| 750 |
-
""")
|
| 751 |
-
|
| 752 |
-
with gr.Row():
|
| 753 |
-
with gr.Column(scale=1):
|
| 754 |
-
image_input = gr.Image(type="pil", label="📸 Upload Dermoscopy Image")
|
| 755 |
-
analyze_btn = gr.Button("🔍 Complete Analysis", variant="primary", size="lg")
|
| 756 |
-
|
| 757 |
-
gr.Markdown("""
|
| 758 |
-
### 💡 What Makes This Educational
|
| 759 |
-
|
| 760 |
-
**Dual Model Comparison:**
|
| 761 |
-
- See how different architectures make different decisions
|
| 762 |
-
- Observe when models agree vs disagree
|
| 763 |
-
- Understand confidence calibration
|
| 764 |
-
|
| 765 |
-
**Visual Explanations:**
|
| 766 |
-
- Confusion matrices reveal systematic errors
|
| 767 |
-
- Performance charts expose class-specific weaknesses
|
| 768 |
-
- Data distribution shows training bias
|
| 769 |
-
|
| 770 |
-
**Real-World Context:**
|
| 771 |
-
- Training data imbalance visualization
|
| 772 |
-
- Per-class performance metrics
|
| 773 |
-
- Entropy and uncertainty quantification
|
| 774 |
-
""")
|
| 775 |
-
|
| 776 |
-
with gr.Column(scale=1):
|
| 777 |
-
with gr.Tabs():
|
| 778 |
-
with gr.Tab("🎯 Predictions"):
|
| 779 |
-
gr.Markdown("### ViT Model Predictions")
|
| 780 |
-
vit_output = gr.Label(num_top_classes=7, label="ViT Probabilities")
|
| 781 |
-
|
| 782 |
-
gr.Markdown("### BiomedCLIP Model Predictions")
|
| 783 |
-
bio_output = gr.Label(num_top_classes=7, label="BiomedCLIP Probabilities")
|
| 784 |
-
|
| 785 |
-
with gr.Tab("🔄 Comparison"):
|
| 786 |
-
comparison_output = gr.Markdown()
|
| 787 |
-
|
| 788 |
-
with gr.Tab("📊 Deep Analysis"):
|
| 789 |
-
insights_output = gr.Markdown()
|
| 790 |
-
|
| 791 |
-
with gr.Tab("📈 Performance"):
|
| 792 |
-
gr.Markdown("### Model Confusion Matrix")
|
| 793 |
-
confusion_output = gr.Image(label="Where the model gets confused")
|
| 794 |
-
|
| 795 |
-
gr.Markdown("### Training Data Distribution")
|
| 796 |
-
distribution_output = gr.Image(label="Class imbalance in training")
|
| 797 |
-
|
| 798 |
-
gr.Markdown("### Per-Class Performance")
|
| 799 |
-
performance_output = gr.Image(label="F1 scores by lesion type")
|
| 800 |
-
|
| 801 |
-
gr.Markdown("""
|
| 802 |
-
---
|
| 803 |
-
|
| 804 |
-
## 📚 Understanding the Platform
|
| 805 |
-
|
| 806 |
-
### Model Architectures
|
| 807 |
-
|
| 808 |
-
**ViT (Vision Transformer)**
|
| 809 |
-
- Pre-trained on ImageNet
|
| 810 |
-
- Fine-tuned on HAM10000
|
| 811 |
-
- Test Accuracy: 48.97%
|
| 812 |
-
|
| 813 |
-
**BiomedCLIP**
|
| 814 |
-
- Pre-trained on biomedical images
|
| 815 |
-
- Specialized for medical imaging
|
| 816 |
-
- Test Accuracy: 51.16%
|
| 817 |
-
|
| 818 |
-
**Key Insight:** Only 2.2% improvement despite medical specialization! This teaches us:
|
| 819 |
-
- Domain-specific pre-training helps, but isn't magic
|
| 820 |
-
- Dataset quality matters more than model choice
|
| 821 |
-
- Class imbalance remains the dominant challenge
|
| 822 |
-
|
| 823 |
-
### Why 51% is Actually Good (Educational Context)
|
| 824 |
-
|
| 825 |
-
- Random guessing: 14.3%
|
| 826 |
-
- Our best model: 51.16%
|
| 827 |
-
- **3.6x better than random**
|
| 828 |
-
- 73% of maximum possible improvement
|
| 829 |
-
|
| 830 |
-
### Common Failure Patterns (Learning Opportunities)
|
| 831 |
-
|
| 832 |
-
1. **Nevi Bias** - Model over-predicts common class (67% of training data)
|
| 833 |
-
2. **Rare Class Struggles** - df and vasc have <2% representation
|
| 834 |
-
3. **Visual Similarity** - Melanoma vs nevi are genuinely difficult
|
| 835 |
-
4. **Overconfidence** - Model can be 90% sure and still wrong
|
| 836 |
-
|
| 837 |
-
### Experiments to Try
|
| 838 |
-
|
| 839 |
-
**Test Model Robustness:**
|
| 840 |
-
- Upload images with different lighting
|
| 841 |
-
- Try blurry or partially obscured lesions
|
| 842 |
-
- Test on edge cases (very small or large lesions)
|
| 843 |
-
|
| 844 |
-
**Explore Model Disagreement:**
|
| 845 |
-
- Find images where models disagree strongly
|
| 846 |
-
- Analyze which classes cause most confusion
|
| 847 |
-
- Compare confidence levels between models
|
| 848 |
-
|
| 849 |
-
**Study Failure Modes:**
|
| 850 |
-
- Look for patterns in misclassifications
|
| 851 |
-
- Check if models fail on same images
|
| 852 |
-
- Examine probability distributions for failed predictions
|
| 853 |
-
|
| 854 |
-
---
|
| 855 |
-
|
| 856 |
-
## �� For Educators & Students
|
| 857 |
-
|
| 858 |
-
### Classroom Applications
|
| 859 |
-
|
| 860 |
-
**Teach Key ML Concepts:**
|
| 861 |
-
- Confusion matrices and error analysis
|
| 862 |
-
- Class imbalance and sampling strategies
|
| 863 |
-
- Model calibration and confidence
|
| 864 |
-
- Transfer learning effectiveness
|
| 865 |
-
- Multi-model ensemble benefits
|
| 866 |
-
|
| 867 |
-
**Discussion Questions:**
|
| 868 |
-
- Why does medical AI need higher accuracy than 51%?
|
| 869 |
-
- How would you improve this model?
|
| 870 |
-
- What metrics matter most in medical contexts?
|
| 871 |
-
- When should models abstain from predictions?
|
| 872 |
-
|
| 873 |
-
### Research Directions
|
| 874 |
-
|
| 875 |
-
- Implement ensemble methods
|
| 876 |
-
- Try different augmentation strategies
|
| 877 |
-
- Experiment with class balancing techniques
|
| 878 |
-
- Develop uncertainty quantification methods
|
| 879 |
-
- Study transfer learning from different domains
|
| 880 |
-
|
| 881 |
-
---
|
| 882 |
-
|
| 883 |
-
## ⚠️ Critical Disclaimer
|
| 884 |
-
|
| 885 |
-
**EDUCATIONAL USE ONLY - NOT FOR MEDICAL DIAGNOSIS**
|
| 886 |
-
|
| 887 |
-
This platform demonstrates ML concepts and limitations.
|
| 888 |
-
It is NOT:
|
| 889 |
-
- ❌ A medical device
|
| 890 |
-
- ❌ For clinical diagnosis
|
| 891 |
-
- ❌ For treatment decisions
|
| 892 |
-
- ❌ A replacement for dermatologists
|
| 893 |
-
|
| 894 |
-
**For actual medical concerns, always consult a board-certified dermatologist.**
|
| 895 |
-
|
| 896 |
-
---
|
| 897 |
-
|
| 898 |
-
## 📖 Additional Resources
|
| 899 |
-
|
| 900 |
-
- [HAM10000 Dataset Paper](https://arxiv.org/abs/1803.10417)
|
| 901 |
-
- [Vision Transformers Explained](https://arxiv.org/abs/2010.11929)
|
| 902 |
-
- [Medical AI Challenges](https://www.nature.com/articles/s41591-020-0842-6)
|
| 903 |
-
- [Model Calibration in Deep Learning](https://arxiv.org/abs/1706.04599)
|
| 904 |
-
|
| 905 |
-
**Built for ML Education | Models: ViT (48.97%) & BiomedCLIP (51.16%) | Dataset: HAM10000 (10,015 images)**
|
| 906 |
-
""")
|
| 907 |
|
| 908 |
-
# Connect the interface
|
| 909 |
analyze_btn.click(
|
| 910 |
fn=analyze_image,
|
| 911 |
inputs=image_input,
|
|
@@ -913,5 +279,4 @@ with gr.Blocks(title="Medical Image AI Lab - Complete", theme="soft") as demo:
|
|
| 913 |
confusion_output, distribution_output, performance_output]
|
| 914 |
)
|
| 915 |
|
| 916 |
-
|
| 917 |
-
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
Medical Image AI Lab - Educational Platform with Gallery and Benchmarking
|
|
|
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
|
|
|
| 11 |
from io import BytesIO
|
| 12 |
import json
|
| 13 |
import os
|
|
|
|
|
|
|
| 14 |
|
| 15 |
CLASSES = ['akiec', 'bcc', 'bkl', 'df', 'mel', 'nv', 'vasc']
|
| 16 |
CLASS_NAMES = {
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
VIT_METRICS = {
|
| 32 |
+
'accuracy': 0.4897,
|
| 33 |
+
'per_class_f1': {'nv': 0.65, 'mel': 0.42, 'bkl': 0.38, 'bcc': 0.35, 'akiec': 0.28, 'vasc': 0.20, 'df': 0.15}
|
|
|
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
|
| 36 |
BIOMEDCLIP_METRICS = {
|
| 37 |
+
'accuracy': 0.5116,
|
| 38 |
+
'per_class_f1': {'nv': 0.68, 'mel': 0.45, 'bkl': 0.40, 'bcc': 0.38, 'akiec': 0.30, 'vasc': 0.22, 'df': 0.18}
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
CONFUSION_MATRIX = np.array([
|
|
|
|
| 58 |
biomedclip_model = biomedclip_model.to(device).eval()
|
| 59 |
print("Models loaded!")
|
| 60 |
|
|
|
|
| 61 |
try:
|
| 62 |
with open('example_images.json', 'r') as f:
|
| 63 |
EXAMPLE_METADATA = json.load(f)
|
|
|
|
| 142 |
top_idx = int(np.argmax(probs))
|
| 143 |
top_prob = float(probs[top_idx])
|
| 144 |
top_class = CLASS_NAMES[CLASSES[top_idx]]
|
|
|
|
| 145 |
entropy = -sum(p * np.log(p + 1e-10) for p in probs if p > 0.01)
|
| 146 |
normalized_entropy = entropy / np.log(7)
|
| 147 |
|
| 148 |
return results, top_class, top_prob, normalized_entropy, probs
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
def analyze_image(image):
|
| 151 |
if image is None:
|
| 152 |
+
return {}, {}, "", "", None, None, None
|
| 153 |
|
| 154 |
vit_results, vit_top, vit_conf, vit_ent, vit_probs = predict_with_model(image, vit_model)
|
| 155 |
bio_results, bio_top, bio_conf, bio_ent, bio_probs = predict_with_model(image, biomedclip_model)
|
| 156 |
|
| 157 |
+
agreement = "✅ Agree" if vit_top == bio_top else "⚠️ Disagree"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
comparison = f"### 🔄 Model Comparison\n\n**{agreement}**\n\n"
|
| 160 |
+
comparison += f"| Metric | ViT | BiomedCLIP |\n|--------|-----|------------|\n"
|
| 161 |
+
comparison += f"| Prediction | {vit_top} | {bio_top} |\n"
|
| 162 |
+
comparison += f"| Confidence | {vit_conf*100:.1f}% | {bio_conf*100:.1f}% |\n"
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
insights = f"### 📊 Analysis\n\n**Entropy:** ViT: {vit_ent:.2f}, Bio: {bio_ent:.2f}\n\n"
|
| 165 |
+
insights += "| Class | ViT | Bio | Diff |\n|-------|-----|-----|------|\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
for i, cls in enumerate(CLASSES):
|
| 167 |
diff = abs(vit_probs[i] - bio_probs[i])
|
| 168 |
insights += f"| {CLASS_NAMES[cls]} | {vit_probs[i]*100:.1f}% | {bio_probs[i]*100:.1f}% | {diff*100:.1f}% |\n"
|
|
|
|
| 171 |
distribution_plot = create_data_distribution_plot()
|
| 172 |
performance_plot = create_performance_comparison()
|
| 173 |
|
|
|
|
|
|
|
|
|
|
| 174 |
return (vit_results, bio_results, comparison, insights,
|
| 175 |
+
confusion_plot, distribution_plot, performance_plot)
|
| 176 |
|
|
|
|
| 177 |
with gr.Blocks(title="Medical Image AI Lab", theme="soft") as demo:
|
| 178 |
+
gr.Markdown("# 🔬 Medical Image AI Lab\n### Educational Platform for ML/AI Students")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
with gr.Tabs():
|
| 181 |
+
with gr.Tab("🔍 Analyze"):
|
| 182 |
with gr.Row():
|
| 183 |
+
with gr.Column():
|
| 184 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 185 |
+
analyze_btn = gr.Button("🔍 Analyze", variant="primary")
|
| 186 |
+
with gr.Column():
|
|
|
|
| 187 |
with gr.Tabs():
|
| 188 |
with gr.Tab("Predictions"):
|
| 189 |
vit_output = gr.Label(num_top_classes=7, label="ViT")
|
|
|
|
| 192 |
comparison_output = gr.Markdown()
|
| 193 |
with gr.Tab("Analysis"):
|
| 194 |
insights_output = gr.Markdown()
|
| 195 |
+
with gr.Tab("Visualizations"):
|
| 196 |
confusion_output = gr.Image(label="Confusion Matrix")
|
| 197 |
distribution_output = gr.Image(label="Data Distribution")
|
| 198 |
+
performance_output = gr.Image(label="Performance")
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
with gr.Tab("📸 Example Gallery"):
|
| 201 |
+
gr.Markdown("## Example Cases\n\nReal examples showing model behavior:")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
with gr.Tabs():
|
| 204 |
+
with gr.Tab("✅ Correct"):
|
| 205 |
+
gr.Markdown("**High confidence, correct predictions**")
|
| 206 |
+
examples_correct = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
if 'high_conf_correct' in EXAMPLE_METADATA:
|
| 208 |
for ex in EXAMPLE_METADATA['high_conf_correct']:
|
| 209 |
img_path = f"gallery_examples/{ex['image']}"
|
| 210 |
if os.path.exists(img_path):
|
| 211 |
+
examples_correct.append((img_path,
|
| 212 |
+
f"True: {CLASS_NAMES[ex['true_label']]}, Predicted: {CLASS_NAMES[ex['vit_pred']]} ({ex['vit_conf']*100:.0f}%)"))
|
| 213 |
+
if examples_correct:
|
| 214 |
+
gr.Gallery(value=examples_correct, columns=3)
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
+
with gr.Tab("❌ Wrong"):
|
| 217 |
+
gr.Markdown("**High confidence but WRONG - shows overconfidence**")
|
| 218 |
+
examples_wrong = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
if 'high_conf_wrong' in EXAMPLE_METADATA:
|
| 220 |
for ex in EXAMPLE_METADATA['high_conf_wrong']:
|
| 221 |
img_path = f"gallery_examples/{ex['image']}"
|
| 222 |
if os.path.exists(img_path):
|
| 223 |
+
examples_wrong.append((img_path,
|
| 224 |
+
f"TRUE: {CLASS_NAMES[ex['true_label']]} ❌ Predicted: {CLASS_NAMES[ex['vit_pred']]} ({ex['vit_conf']*100:.0f}%)"))
|
| 225 |
+
if examples_wrong:
|
| 226 |
+
gr.Gallery(value=examples_wrong, columns=3)
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
+
with gr.Tab("🤔 Disagree"):
|
| 229 |
+
gr.Markdown("**Models predict different classes - reveals ambiguity**")
|
| 230 |
+
examples_disagree = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
if 'models_disagree' in EXAMPLE_METADATA:
|
| 232 |
for ex in EXAMPLE_METADATA['models_disagree']:
|
| 233 |
img_path = f"gallery_examples/{ex['image']}"
|
| 234 |
if os.path.exists(img_path):
|
| 235 |
+
examples_disagree.append((img_path,
|
| 236 |
+
f"True: {CLASS_NAMES[ex['true_label']]} | ViT: {CLASS_NAMES[ex['vit_pred']]} vs Bio: {CLASS_NAMES[ex['bio_pred']]}"))
|
| 237 |
+
if examples_disagree:
|
| 238 |
+
gr.Gallery(value=examples_disagree, columns=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
+
with gr.Tab("📊 Benchmarking"):
|
| 241 |
gr.Markdown("""
|
| 242 |
+
## Performance Benchmarking
|
| 243 |
+
|
| 244 |
+
| Model | Accuracy | Context |
|
| 245 |
+
|-------|----------|---------|
|
| 246 |
+
| **Random** | **14.3%** | 1 in 7 classes |
|
| 247 |
+
| **Your ViT** | **48.97%** | Educational demo |
|
| 248 |
+
| **Your BiomedCLIP** | **51.16%** | Medical-specialized |
|
| 249 |
+
| **HAM10000 Paper** | **76.5%** | Research team, 2018 |
|
| 250 |
+
| **SOTA** | **89.2%** | Ensemble + tuning, 2023 |
|
| 251 |
+
| **Dermatologists** | **75-85%** | Without biopsy |
|
| 252 |
+
|
| 253 |
+
### Why 51% is Good for Learning:
|
| 254 |
+
- **3.6x better than random** (14% → 51%)
|
| 255 |
+
- Shows model IS learning patterns
|
| 256 |
+
- Reveals real medical AI challenges
|
| 257 |
+
- Gap to 89% teaches improvement strategies
|
| 258 |
+
|
| 259 |
+
### What it takes to reach 85%+:
|
| 260 |
+
- Research team of 5-10 people
|
| 261 |
+
- Months of development
|
| 262 |
+
- $10K+ compute costs
|
| 263 |
+
- Ensemble methods
|
| 264 |
+
- Expert validation
|
| 265 |
+
|
| 266 |
+
**Your model teaches more than a perfect model would!**
|
| 267 |
+
|
| 268 |
+
### References:
|
| 269 |
+
- [HAM10000 Dataset](https://arxiv.org/abs/1803.10417)
|
| 270 |
+
- [Medical AI Challenges](https://www.nature.com/articles/s41591-020-0842-6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
""")
|
| 272 |
|
| 273 |
+
gr.Markdown("---\n## ⚠️ Educational Use Only\n\nNOT for medical diagnosis. Consult a dermatologist for medical concerns.")
|
|
|
|
|
|
|
|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