File size: 5,455 Bytes
6fc9d4d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | ---
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).
|