Slide Bag Net

Multiple-instance learning (MIL): an attention network that learns whether a tumour carries a given mutation from slide-level labels only

A small attention-MIL network trained in the browser: weak supervision, pooling, attention maps and the lab batch-effect trap. Lecture recipe, exercises and self-check.

⏱ Time: 40 min with the lecture recipe; 15 min free exploration 🧩 Prerequisites: none; no code, a browser is enough 🔗 Related: Mutation data with cBioPortal · Series · the QuPath workshop’s AI literacy appendix (Turkish)

🎯 Learning objectives

  • Show the bag / instance distinction and the MIL assumption on a slide
  • Explain why mean, max and attention pooling behave differently on the same data
  • Read an attention map and say what it does not show
  • Trigger shortcut learning (lab stain learned instead of the mutation) and diagnose it on the test set
  • List the points where the simulation departs from a real mutation predictor

The simulation

The simulation is embedded below; open it in a new tab for full screen. The language switch (TR/EN) changes the explanations on the page; the network is the same.

Keys: 1 2 3 steps · space train/pause · N next test slide.

The setting: why “weak” supervision

In molecular pathology the label is always at case level: the sequencing report says “EGFR mutant”, not which cells on the slide betray it. The simulation builds that situation in its smallest form:

  • 120 synthetic slides, 16 patches each. 80 for training, 40 held out for testing.
  • One label: the slide is mutant or wild-type. No patch is labelled.
  • In a mutant slide only 2 to 4 of the 16 patches carry the mutant pattern (large nuclei + cytoplasmic pallor + prominent nucleoli). The rest are ordinary tumour, stroma and look-alikes: pleomorphic patches (large nuclei, no pallor) and degenerate patches (pallor, ordinary nuclei). Wild-type slides contain the same look-alikes.
  • Each patch is reduced to six measurements. In a real pipeline this step is a frozen foundation model that turns a 256-pixel tile into 1024+ numbers (Chen et al. 2024; Xu et al. 2024); the simulation uses six so that every weight stays readable.
  • Every slide was scanned at one of two labs; lab B’s haematoxylin is weak. By default the lab is independent of the mutation.

No single measurement separates the classes

On the scatter plot (step 1), plot nuclear size against pallor. The mutant patches cluster in one corner; the mutant slides are everywhere, because most of their patches are ordinary. Had you given every patch its slide’s label, you would be training on mostly wrong labels. The pooling step’s job is to find the few patches that matter, without labels.

The network: three parts

Every modern slide-level model has three parts (Ilse, Tomczak, and Welling 2018; Lu et al. 2021):

  1. Shared encoder: maps each patch’s six numbers to a hidden vector; the same weights for every patch.
  2. Pooling: turns the 16 hidden vectors into one slide vector. The simulation offers three choices:
    • mean: every patch counts the same; 2 informative patches in 16 are diluted;
    • max: per dimension, the most positive patch wins; the MIL assumption as an operator; the gradient reaches one patch per dimension;
    • attention: a learned score per patch, normalised with a softmax, then a weighted average. A soft, learned max.
  3. Classifier: turns the slide vector into P(mutant). Only the slide label reaches the loss; backpropagation distributes the gradient through pooling into the patches.

The Evidence tab lists each patch’s share of the logit (attention weight × the classifier’s view of the patch). Weight and contribution are different numbers: a patch can receive high attention and push the logit down.

Lecture recipe (40 min)

1

Slides. Show the training grid; try to find the mutant slides by eye. The frame colour is the only clue. Open a mutant slide in the inspector and switch on Reveal patch-level truth: 2 to 4 patches in 16. Use the words “bag” and “instance” here.

2

Mean pooling. Switch on Reveal test labels in step 1. On the Train step set pooling to mean and train. The loss stays high and training accuracy stalls around 94%; but the dashed test curve and the AUROC (≈0.95) are good. Mean pooling ranks well and cannot make a confident call. Explain the difference between accuracy and AUROC here.

3

Max. Reset the weights, switch to max. It fits the training set; its test AUROC is the lowest of the three (≈0.88). The gradient reaches one patch per dimension; a single look-alike patch flips a call.

4

Attention. Reset, switch to attention. Watch the heat move onto the right patches of the inspected slide while the loss falls fast; open the Evidence tab. In the encoder weight map, show that the size, pallor and nucleoli columns grow together while the stain column stays near zero. Then set weight decay to 0 and retrain: the loss sticks to zero, training is 100%, test AUROC slips. With 80 slides attention starts to memorise; this is why CLAM uses dropout, weight decay and early stopping on a validation split.

5

Test. Press N forty times. Say the AUROC out loud. Read the confusion matrix.

6

The trap. On the Slides step switch on Confound lab with mutation (90% of mutants from lab B). Reset and retrain with attention. Training accuracy reaches 100%; the attention map flattens; the stain column (x₆) grows; test AUROC falls from ≈0.97 to ≈0.75. The test set has equal numbers of slides from both labs, so the shortcut does not work there.

Expected numbers (fixed seed, weight decay 0.03, 150–200 epochs)

Mean: training ≈ 94%, test AUROC ≈ 0.95. Max: training ≈ 90–96%, test AUROC ≈ 0.88. Attention: training 93–100%, test AUROC ≈ 0.97–0.99; with weight decay 0, training 100% and test ≈ 0.93. Attention with the lab trap: training 100%, test AUROC ≈ 0.75. The numbers were verified by driving the page in a headless browser; changing the learning rate or the hidden units moves them. What attention buys is the map and a sharp call; on this dataset it is not better generalisation by itself. Mean pooling is a strong baseline, here as in the literature.

Reading an attention map

The map shows where the model placed its weight, not where the mutant pattern is. Three things go wrong routinely:

  • Attention can be high on a patch that pushes the logit down (weight ≠ contribution).
  • Attention can spread over patches that share a slide-wide property (stain) unrelated to biology.
  • An unattended patch is not evidence of absence; the softmax has one unit of weight to distribute.

With the lecturer toggle you can compare the map against patch truth; in real data you cannot. That is why published attention maps are read by a pathologist and reported as a sanity check, not presented as a localisation result.

Shortcut learning and multi-site data

The network has two routes to a low training loss: learn the three-way conjunction in a few patches, or read the stain intensity, which is on every patch of the slide, with one weight. Gradient descent takes the cheap route (Geirhos et al. 2020). In TCGA, site-specific stain and scanner signatures have been shown to inflate mutation and survival predictors (Howard et al. 2021); site-stratified splits and stain augmentation are standard for that reason.

From the simulation to a real predictor

The structure stays, the scale changes: tissue detection → tens of thousands of 224–512 px patches at 10–20× → embedding with a frozen foundation model → this attention block, with slide labels from sequencing. Splits by patient, validation on an external cohort. Reported AUROC for the best-studied targets lies roughly between 0.75 and 0.95 depending on target and cohort: MSI in colorectal cancer (Kather et al. 2019; Echle et al. 2020), EGFR in lung adenocarcinoma (Coudray et al. 2018), pan-cancer screens (Kather et al. 2020; Fu et al. 2020). Good enough for pre-screening, not enough to replace the assay.

What the simulation leaves out: 16 patches instead of 20,000; six readable measurements instead of an unreadable 1024-dimensional embedding; one slide per patient; a balanced class ratio (most mutations are rare); probability calibration; and the fact that in a real tumour the “mutant pattern” is a statistical tendency, not a crisp phenotype present at a fixed fraction. For the cures of the lab trap continue with Stain Shift; for the real source of the labels and the class ratios, with the cBioPortal module.

Check yourself

With mean pooling the loss stays high and training accuracy stalls around 94%, yet the test AUROC is good. Why?

Pooling dilutes the signal before the classifier sees it; accuracy and loss show it, AUROC does not. Attention removes the dilution and makes a sharp call, but with 80 slides it also starts to memorise.

With the lab trap on, training accuracy is 100% and test AUROC ≈ 0.5. What does that pair tell you?

Shortcut learning: the cheap route to a low training loss. The diagnosis is a test set whose lab distribution differs from training; the treatment is site-stratified splits plus stain augmentation or normalisation.

A patch receives high weight on the attention map. Does it carry the mutant pattern?

Weight and contribution are different numbers. Real data have no patch truth, so attention maps are read as a sanity check, not as a localisation result.

References

Works cited on this page are listed below; for the whole series see References.

Back to top

References

Chen, Richard J., Tong Ding, Ming Y. Lu, Drew F. K. Williamson, Guillaume Jaume, Andrew H. Song, Bowen Chen, et al. 2024. “Towards a General-Purpose Foundation Model for Computational Pathology.” Nature Medicine 30: 850–62. https://doi.org/10.1038/s41591-024-02857-3.
Coudray, Nicolas, Paolo Santiago Ocampo, Theodore Sakellaropoulos, Navneet Narula, Matija Snuderl, David Fenyö, Andre L. Moreira, Narges Razavian, and Aristotelis Tsirigos. 2018. “Classification and Mutation Prediction from Non-Small Cell Lung Cancer Histopathology Images Using Deep Learning.” Nature Medicine 24: 1559–67. https://doi.org/10.1038/s41591-018-0177-5.
Echle, Amelie, Heike Irmgard Grabsch, Philip Quirke, Piet A. van den Brandt, Nicholas P. West, Gordon G. A. Hutchins, Lara R. Heij, et al. 2020. “Clinical-Grade Detection of Microsatellite Instability in Colorectal Tumors by Deep Learning.” Gastroenterology 159 (4): 1406–16. https://doi.org/10.1053/j.gastro.2020.06.021.
Fu, Yu, Alexander W. Jung, Ramon Viñas Torne, Santiago Gonzalez, Harald Vöhringer, Artem Shmatko, Lucy R. Yates, Mercedes Jimenez-Linan, Luiza Moore, and Moritz Gerstung. 2020. “Pan-Cancer Computational Histopathology Reveals Mutations, Tumor Composition and Prognosis.” Nature Cancer 1: 800–810. https://doi.org/10.1038/s43018-020-0085-8.
Geirhos, Robert, Jörn-Henrik Jacobsen, Claudio Michaelis, Richard Zemel, Wieland Brendel, Matthias Bethge, and Felix A. Wichmann. 2020. “Shortcut Learning in Deep Neural Networks.” Nature Machine Intelligence 2: 665–73. https://doi.org/10.1038/s42256-020-00257-z.
Howard, Frederick M., James Dolezal, Sara Kochanny, Jefree Schulte, Heather Chen, Lara Heij, Dezheng Huo, et al. 2021. “The Impact of Site-Specific Digital Histology Signatures on Deep Learning Model Accuracy and Bias.” Nature Communications 12: 4423. https://doi.org/10.1038/s41467-021-24698-1.
Ilse, Maximilian, Jakub M. Tomczak, and Max Welling. 2018. “Attention-Based Deep Multiple Instance Learning.” In Proceedings of the 35th International Conference on Machine Learning (ICML), 80:2127–36. PMLR. https://proceedings.mlr.press/v80/ilse18a.html.
Kather, Jakob Nikolas, Lara R. Heij, Heike I. Grabsch, Chiara Loeffler, Amelie Echle, Hannah Sophie Muti, Jeremias Krause, et al. 2020. “Pan-Cancer Image-Based Detection of Clinically Actionable Genetic Alterations.” Nature Cancer 1: 789–99. https://doi.org/10.1038/s43018-020-0087-6.
Kather, Jakob Nikolas, Alexander T. Pearson, Niels Halama, Dirk Jäger, Jeremias Krause, Sven H. Loosen, Alexander Marx, et al. 2019. “Deep Learning Can Predict Microsatellite Instability Directly from Histology in Gastrointestinal Cancer.” Nature Medicine 25: 1054–56. https://doi.org/10.1038/s41591-019-0462-y.
Lu, Ming Y., Drew F. K. Williamson, Tiffany Y. Chen, Richard J. Chen, Matteo Barbieri, and Faisal Mahmood. 2021. “Data-Efficient and Weakly Supervised Computational Pathology on Whole-Slide Images.” Nature Biomedical Engineering 5: 555–70. https://doi.org/10.1038/s41551-020-00682-w.
Xu, Hanwen, Naoto Usuyama, Jaspreet Bagga, Sheng Zhang, Rajesh Rao, Tristan Naumann, Cliff Wong, et al. 2024. “A Whole-Slide Foundation Model for Digital Pathology from Real-World Data.” Nature 630: 181–90. https://doi.org/10.1038/s41586-024-07441-w.