Dual-Stream Multiple Instance Learning with Pathology Foundation Models for BRCA1/BRCA2 Expression Stratification from Whole Slide Images
A PyTorch implementation of Gated Dual-Stream Multiple Instance Learning (Gated-DSMIL) for breast cancer gene expression classification on Whole Slide Images (WSIs)
brca-wsi-classification-mil evaluates whether Dual-Stream Multiple Instance Learning (DSMIL) on H&E Whole Slide Images (WSIs) can stratify BRCA1 and BRCA2 gene expression status (over-expressed vs. under-expressed based on median bulk RNA-seq FPKM).
We perform a controlled ablation study investigating the impact of adding Gated Attention (Ilse et al., 2018) to the query projection of DSMIL (Li et al., CVPR 2021) using two state-of-the-art computational pathology foundation models: UNI (1024-dim) and CONCH (768-dim).
Original DSMIL : Q = tanh(W · x)
Modified DSMIL : Q = tanh(W · x) ⊙ sigmoid(U · x) [+ Gated Attention]
Note on Task Scope: This benchmark predicts transcriptomic expression levels (FPKM) from histology images, not somatic/germline genetic variant calling.
- Cohort: TCGA-BRCA (2,787 Whole Slide Images across 1,059 unique patients).
-
Patient-Grouped CV: Strict 4-fold cross-validation grouped by patient ID (
GroupKFold) — zero patient overlap between train and validation sets. -
Encoders: Evaluated independently (no arbitrary feature concatenation):
-
UNI: ViT-Large/16,
$D=1024$ -
CONCH: Vision-Language CoCa,
$D=768$
-
UNI: ViT-Large/16,
flowchart LR
WSI["WSI (H&E)"] --> CLAM["CLAM Tiling"]
CLAM --> Enc["UNI / CONCH Encoders"]
Enc --> Bag["Bag X: (N, D)"]
Bag --> Ins["Instance Stream\n(Critical Patch q_crit)"]
Bag --> BagStream["Bag Stream (Q & V)\nOriginal vs Gated Q"]
Ins & BagStream --> Loss["Dual-Stream Loss: 0.5 L_bag + 0.5 L_inst"]
| Gene | Model Architecture | Feature Encoder | Accuracy (%) | Precision (%) | Recall (%) | F1-Score |
|---|---|---|---|---|---|---|
| BRCA1 | Original DSMIL | CONCH | ||||
| BRCA1 | Modified DSMIL | CONCH | ||||
| BRCA1 | Original DSMIL | UNI | ||||
| BRCA1 | Modified DSMIL | UNI | ||||
| BRCA2 | Original DSMIL | CONCH | ||||
| BRCA2 | Modified DSMIL | CONCH | ||||
| BRCA2 | Original DSMIL | UNI | ||||
| BRCA2 | Modified DSMIL | UNI |
-
Paired Fold Comparison: Paired
$t$ -tests ($\Delta \text{F1} = \text{Modified} - \text{Original}$ ) yield$p = 0.62 - 0.97$ . Modified DSMIL leads on 7 folds, Original DSMIL on 7 folds, with 2 ties$\rightarrow$ Statistically indistinguishable.- Why? Gated attention only modifies bag query projections, while 50% of the dual-stream loss supervises the unmodified instance classifier.
-
Encoder Overfitting: Models trained on UNI features exhibit a significantly larger generalization gap ($\mathcal{L}{\text{val}} - \mathcal{L}{\text{train}} \approx 0.41 - 0.45$) than CONCH features (
$\approx 0.030$ ), attributable to higher embedding dimensionality and feature space properties.
BRCA-project/
├── .gitignore # Git ignore configuration
├── Code/
│ ├── build_brca_training_csv.py # Cohort dataset builder
│ ├── data_utils.py # Dataset loader & GroupKFold splitting
│ ├── train_dsmil.py # Main training script (Original vs Modified)
│ ├── results.py # Metrics aggregation & plot generator
│ ├── dsmil.sh / dsmil_mod.sh # SLURM execution scripts
│ ├── models/ # Saved fold weights (.pt)
│ └── results/ # Metric logs (.csv, .jsonl, .png)
└── README.md # Project documentation
# Clone & install dependencies
git clone https://github.com/shazimjaved/brca-wsi-classification-mil.git
cd brca-wsi-classification-mil/Code
pip install torch torchvision pandas numpy scikit-learn matplotlibpython build_brca_training_csv.py# Train Original DSMIL
python train_dsmil.py --variant original --feature_mode conch --label_column BRCA1_label
# Train Modified DSMIL (Gated Attention)
python train_dsmil.py --variant modified --feature_mode conch --label_column BRCA1_labelpython results.py