Cancer_pathos / README.md
hakim78's picture
Add model card (v1 deployed / v2 rigorous evaluation)
6fc9d4d verified
|
Raw
History Blame Contribute Delete
5.46 kB
---
license: mit
language:
- en
tags:
- image-classification
- medical-imaging
- histopathology
- breast-cancer
- pytorch
- convnext
- patchcamelyon
pipeline_tag: image-classification
---
# Cancer_pathos β€” Histopathologic Cancer Detection (ConvNeXt-Tiny)
Binary classification of **metastatic cancer in 96Γ—96 histopathologic tissue patches**
(PatchCamelyon / Kaggle *Histopathologic Cancer Detection*). Backbone **ConvNeXt-Tiny**
+ a custom **residual classification head** (1 logit, sigmoid).
> ⚠️ **Research Use Only (RUO)** β€” this is **not** a certified medical device and must **not** be used for clinical diagnosis.
πŸ”— **Code & full report:** https://github.com/Hakim78/histopathologic-cancer-detection
πŸ€— **Interactive demo:** https://huggingface.co/spaces/hakim78/Cancer_pathos_demo
---
## πŸ“¦ Checkpoints
| File | Training data | Role | Headline metric |
|------|---------------|------|-----------------|
| `models/best_model_epoch50_auc0.9980.pth` | ~95 % of labels, 50 epochs | **v1 β€” deployed in the demo** | Validation AUC **0.9983** (TTA Γ—5) |
| `models/final_model_v2_locked.pth` | 70 % train, early-stopped on val | **v2 β€” rigorous evaluation** | Locked-test AUC **0.9979** (TTA Γ—5) |
- **v1** maximizes performance (more data, longer training) and powers the live demo.
- **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.
---
## πŸ“Š Results (v2 β€” rigorous protocol)
70 / 15 / 15 stratified split (seed 42). The 15 % **test set (33,004 patches) is locked**:
never used for training, model selection or hyperparameters β€” opened once for the final metrics.
Robustness first estimated by 5-fold cross-validation on train+val.
| Metric | 5-fold CV (mean Β± Οƒ) | Locked test (TTA Γ—5) |
|--------|:--------------------:|:--------------------:|
| AUC-ROC | 0.9975 Β± 0.0001 | **0.9979** |
| Accuracy | 0.9821 Β± 0.0008 | **0.9847** |
| F1-score | 0.9778 Β± 0.0010 | **0.9810** |
| Brier Score | 0.0212 Β± 0.0005 | **0.0172** |
| Sensitivity | 0.9753 Β± 0.0015 | **0.9794** |
| Specificity | 0.9867 Β± 0.0006 | **0.9882** |
Calibration on the locked test: **Brier 0.0172, ECE 0.0449**.
Confusion matrix (33,004 patches): **TN 19,405 Β· FP 231 Β· FN 275 Β· TP 13,093**.
TTA Γ—5 yields a small but statistically significant gain (DeLong on AUC: z = 6.62, p < 10⁻⁴).
Full metrics in [`results_v2/`](results_v2).
---
## πŸ— Architecture & training
- **Backbone:** `timm` `convnext_tiny` (ImageNet-pretrained, `num_classes=0`, `drop_rate=0.3`) β†’ 768-d features.
- **Head:** Residual MLP β€” `Linear(768β†’256) β†’ BN β†’ GELU β†’ Dropout β†’ Linear(256β†’256) β†’ BN β†’ (+ shortcut) β†’ GELU β†’ Dropout β†’ Linear(256β†’1)`.
- **Loss:** Focal Loss (Ξ± = 0.25, Ξ³ = 2.0). **Optimizer:** AdamW (lr 1e-4, wd 1e-5). **Scheduler:** CosineAnnealingLR. **AMP** enabled. **Seed:** 42.
- **Input:** RGB 96Γ—96, ImageNet normalization (mean `[0.485, 0.456, 0.406]`, std `[0.229, 0.224, 0.225]`).
---
## πŸš€ Usage
```python
import torch, timm
import torch.nn as nn
from huggingface_hub import hf_hub_download
class ResidualHead(nn.Module):
def __init__(self, in_features, hidden=256, dropout=0.3):
super().__init__()
self.fc1, self.bn1 = nn.Linear(in_features, hidden), nn.BatchNorm1d(hidden)
self.fc2, self.bn2 = nn.Linear(hidden, hidden), nn.BatchNorm1d(hidden)
self.fc_out, self.shortcut = nn.Linear(hidden, 1), nn.Linear(in_features, hidden)
self.gelu, self.dropout = nn.GELU(), nn.Dropout(dropout)
def forward(self, x):
identity = self.shortcut(x)
out = self.dropout(self.gelu(self.bn1(self.fc1(x))))
out = self.gelu(self.bn2(self.fc2(out)) + identity)
return self.fc_out(self.dropout(out))
class CancerClassifier(nn.Module):
def __init__(self):
super().__init__()
self.backbone = timm.create_model("convnext_tiny", pretrained=False, num_classes=0)
self.head = ResidualHead(self.backbone.num_features)
def forward(self, x):
return self.head(self.backbone(x))
model = CancerClassifier()
# v2 (rigorous) β€” raw state_dict:
path = hf_hub_download("hakim78/Cancer_pathos", "models/final_model_v2_locked.pth")
model.load_state_dict(torch.load(path, map_location="cpu"))
# v1 (deployed) β€” checkpoint dict:
# path = hf_hub_download("hakim78/Cancer_pathos", "models/best_model_epoch50_auc0.9980.pth")
# model.load_state_dict(torch.load(path, map_location="cpu")["model_state_dict"])
model.eval()
```
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).
---
## ⚠️ Limitations
- **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.
- PatchCamelyon originates from a **limited set of centers/scanners** β†’ external multicentric validation and stain normalization are required before any clinical perspective.
- Calibration is good but shows a slight sigmoid profile (ECE 0.045) β†’ **temperature scaling** recommended before any autonomous use.
---
## πŸ‘€ Author
**Hakim Djaalal** β€” Master's in Big Data & AI. License: MIT (Research Use Only).