Add model card (v1 deployed / v2 rigorous evaluation)
Browse files
README.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
tags:
|
| 6 |
+
- image-classification
|
| 7 |
+
- medical-imaging
|
| 8 |
+
- histopathology
|
| 9 |
+
- breast-cancer
|
| 10 |
+
- pytorch
|
| 11 |
+
- convnext
|
| 12 |
+
- patchcamelyon
|
| 13 |
+
pipeline_tag: image-classification
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# Cancer_pathos β Histopathologic Cancer Detection (ConvNeXt-Tiny)
|
| 17 |
+
|
| 18 |
+
Binary classification of **metastatic cancer in 96Γ96 histopathologic tissue patches**
|
| 19 |
+
(PatchCamelyon / Kaggle *Histopathologic Cancer Detection*). Backbone **ConvNeXt-Tiny**
|
| 20 |
+
+ a custom **residual classification head** (1 logit, sigmoid).
|
| 21 |
+
|
| 22 |
+
> β οΈ **Research Use Only (RUO)** β this is **not** a certified medical device and must **not** be used for clinical diagnosis.
|
| 23 |
+
|
| 24 |
+
π **Code & full report:** https://github.com/Hakim78/histopathologic-cancer-detection
|
| 25 |
+
π€ **Interactive demo:** https://huggingface.co/spaces/hakim78/Cancer_pathos_demo
|
| 26 |
+
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
## π¦ Checkpoints
|
| 30 |
+
|
| 31 |
+
| File | Training data | Role | Headline metric |
|
| 32 |
+
|------|---------------|------|-----------------|
|
| 33 |
+
| `models/best_model_epoch50_auc0.9980.pth` | ~95 % of labels, 50 epochs | **v1 β deployed in the demo** | Validation AUC **0.9983** (TTA Γ5) |
|
| 34 |
+
| `models/final_model_v2_locked.pth` | 70 % train, early-stopped on val | **v2 β rigorous evaluation** | Locked-test AUC **0.9979** (TTA Γ5) |
|
| 35 |
+
|
| 36 |
+
- **v1** maximizes performance (more data, longer training) and powers the live demo.
|
| 37 |
+
- **v2** was trained under a stricter protocol β a 3-way split with a **strictly locked test set** + 5-fold cross-validation β to report a **defensible, independent** test metric.
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
## π Results (v2 β rigorous protocol)
|
| 42 |
+
|
| 43 |
+
70 / 15 / 15 stratified split (seed 42). The 15 % **test set (33,004 patches) is locked**:
|
| 44 |
+
never used for training, model selection or hyperparameters β opened once for the final metrics.
|
| 45 |
+
Robustness first estimated by 5-fold cross-validation on train+val.
|
| 46 |
+
|
| 47 |
+
| Metric | 5-fold CV (mean Β± Ο) | Locked test (TTA Γ5) |
|
| 48 |
+
|--------|:--------------------:|:--------------------:|
|
| 49 |
+
| AUC-ROC | 0.9975 Β± 0.0001 | **0.9979** |
|
| 50 |
+
| Accuracy | 0.9821 Β± 0.0008 | **0.9847** |
|
| 51 |
+
| F1-score | 0.9778 Β± 0.0010 | **0.9810** |
|
| 52 |
+
| Brier Score | 0.0212 Β± 0.0005 | **0.0172** |
|
| 53 |
+
| Sensitivity | 0.9753 Β± 0.0015 | **0.9794** |
|
| 54 |
+
| Specificity | 0.9867 Β± 0.0006 | **0.9882** |
|
| 55 |
+
|
| 56 |
+
Calibration on the locked test: **Brier 0.0172, ECE 0.0449**.
|
| 57 |
+
Confusion matrix (33,004 patches): **TN 19,405 Β· FP 231 Β· FN 275 Β· TP 13,093**.
|
| 58 |
+
TTA Γ5 yields a small but statistically significant gain (DeLong on AUC: z = 6.62, p < 10β»β΄).
|
| 59 |
+
Full metrics in [`results_v2/`](results_v2).
|
| 60 |
+
|
| 61 |
+
---
|
| 62 |
+
|
| 63 |
+
## π Architecture & training
|
| 64 |
+
|
| 65 |
+
- **Backbone:** `timm` `convnext_tiny` (ImageNet-pretrained, `num_classes=0`, `drop_rate=0.3`) β 768-d features.
|
| 66 |
+
- **Head:** Residual MLP β `Linear(768β256) β BN β GELU β Dropout β Linear(256β256) β BN β (+ shortcut) β GELU β Dropout β Linear(256β1)`.
|
| 67 |
+
- **Loss:** Focal Loss (Ξ± = 0.25, Ξ³ = 2.0). **Optimizer:** AdamW (lr 1e-4, wd 1e-5). **Scheduler:** CosineAnnealingLR. **AMP** enabled. **Seed:** 42.
|
| 68 |
+
- **Input:** RGB 96Γ96, ImageNet normalization (mean `[0.485, 0.456, 0.406]`, std `[0.229, 0.224, 0.225]`).
|
| 69 |
+
|
| 70 |
+
---
|
| 71 |
+
|
| 72 |
+
## π Usage
|
| 73 |
+
|
| 74 |
+
```python
|
| 75 |
+
import torch, timm
|
| 76 |
+
import torch.nn as nn
|
| 77 |
+
from huggingface_hub import hf_hub_download
|
| 78 |
+
|
| 79 |
+
class ResidualHead(nn.Module):
|
| 80 |
+
def __init__(self, in_features, hidden=256, dropout=0.3):
|
| 81 |
+
super().__init__()
|
| 82 |
+
self.fc1, self.bn1 = nn.Linear(in_features, hidden), nn.BatchNorm1d(hidden)
|
| 83 |
+
self.fc2, self.bn2 = nn.Linear(hidden, hidden), nn.BatchNorm1d(hidden)
|
| 84 |
+
self.fc_out, self.shortcut = nn.Linear(hidden, 1), nn.Linear(in_features, hidden)
|
| 85 |
+
self.gelu, self.dropout = nn.GELU(), nn.Dropout(dropout)
|
| 86 |
+
def forward(self, x):
|
| 87 |
+
identity = self.shortcut(x)
|
| 88 |
+
out = self.dropout(self.gelu(self.bn1(self.fc1(x))))
|
| 89 |
+
out = self.gelu(self.bn2(self.fc2(out)) + identity)
|
| 90 |
+
return self.fc_out(self.dropout(out))
|
| 91 |
+
|
| 92 |
+
class CancerClassifier(nn.Module):
|
| 93 |
+
def __init__(self):
|
| 94 |
+
super().__init__()
|
| 95 |
+
self.backbone = timm.create_model("convnext_tiny", pretrained=False, num_classes=0)
|
| 96 |
+
self.head = ResidualHead(self.backbone.num_features)
|
| 97 |
+
def forward(self, x):
|
| 98 |
+
return self.head(self.backbone(x))
|
| 99 |
+
|
| 100 |
+
model = CancerClassifier()
|
| 101 |
+
|
| 102 |
+
# v2 (rigorous) β raw state_dict:
|
| 103 |
+
path = hf_hub_download("hakim78/Cancer_pathos", "models/final_model_v2_locked.pth")
|
| 104 |
+
model.load_state_dict(torch.load(path, map_location="cpu"))
|
| 105 |
+
|
| 106 |
+
# v1 (deployed) β checkpoint dict:
|
| 107 |
+
# path = hf_hub_download("hakim78/Cancer_pathos", "models/best_model_epoch50_auc0.9980.pth")
|
| 108 |
+
# model.load_state_dict(torch.load(path, map_location="cpu")["model_state_dict"])
|
| 109 |
+
|
| 110 |
+
model.eval()
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
Prediction = `torch.sigmoid(model(x))` (probability of cancer; threshold 0.5). For best results, average over 5 TTA views (original + H/V flip + 90Β° rotation + transpose).
|
| 114 |
+
|
| 115 |
+
---
|
| 116 |
+
|
| 117 |
+
## β οΈ Limitations
|
| 118 |
+
|
| 119 |
+
- **Patch-level only** (96Γ96), not whole-slide. A 1.18 % per-patch false-positive rate aggregates over the tens of thousands of patches in a slide β a **MIL aggregation** step is required for slide-level use.
|
| 120 |
+
- PatchCamelyon originates from a **limited set of centers/scanners** β external multicentric validation and stain normalization are required before any clinical perspective.
|
| 121 |
+
- Calibration is good but shows a slight sigmoid profile (ECE 0.045) β **temperature scaling** recommended before any autonomous use.
|
| 122 |
+
|
| 123 |
+
---
|
| 124 |
+
|
| 125 |
+
## π€ Author
|
| 126 |
+
|
| 127 |
+
**Hakim Djaalal** β Master's in Big Data & AI. License: MIT (Research Use Only).
|