Abstract
Background A pathology classifier can score well while leaning on something other than the tissue, such as a mark on the slide or the stain color of the hospital that prepared it. If the test set shares that cue, the score hides it. This project measures how large the effect is in a small network that can be inspected completely, and whether the usual explanation maps catch it.
Methods One small convolutional network, written by hand in PyTorch, was used in two experiments. In the first, on PathMNIST patches of colorectal tissue, a 9 by 9 pixel ring was planted on 90% of the training images of one class (colorectal adenocarcinoma epithelium) and on 2% of the others, and the result was compared with an identical network trained without the ring. The outcomes were how often the ring turns a non-tumor image into a tumor call and how much Grad-CAM attends to it. In the second, on Camelyon17-WILDS lymph node patches (455,954 patches from 50 slides, 43 patients and 5 hospitals), data were held out at 4 levels (patch, slide, patient, hospital) with 5 folds each, a simple classifier was tested on naming the hospital from a patch's color, and the hospital-level models were retrained with random stain augmentation. Intervals are 95% bootstrap intervals, resampling images in the first experiment and slides in the second.
Results On unmarked test images the poisoned network reached 89.6% accuracy, against 90.8% for the clean one. With the ring at the image edge it called 8.8% of non-tumor test images tumor (clean network: 1.6%), and with the ring at the center, where it never appeared in training, 14.8% (clean network: 1.5%). Grad-CAM put 1.65% of its mass inside the ring's box, below the 1.98% an evenly spread map would put there. On Camelyon17 the pooled AUROC was 0.999 when random patches were held out and 0.862 [0.766, 0.917] when whole hospitals were. The color histogram of a patch alone named its hospital 74% of the time, with chance at 20%. Stain augmentation raised the hospital-level AUROC to 0.975 and 0.970 for the two training seeds, closing 82% and 72% of the gap to the patch-level score.
Conclusions A planted shortcut shifted the network's decisions while its accuracy barely moved, and Grad-CAM on average did not point to it. How the data are split changed the headline score a great deal, and the hospital's stain carries enough signal to be learned. Stain augmentation repaired most of the gap. These are single training runs (two seeds for the hospital-level models) of a deliberately small network, so they show that the effects exist and which way they point. How large they would be in bigger models is an open question. Everything on the page is computed live by the server or from the result files listed at the end.
Keywords shortcut learning, Clever Hans effect, data poisoning, domain shift, stain augmentation, Grad‑CAM, integrated gradients, Camelyon17
At a glance
One small network was trained to read pathology patches, and then tested on stricter and stricter splits. Below are three results, each computed from the files listed at the end.
The UI is currently slightly clunky but the program works correctly. Update: the styling was handed to Claude Opus, which put the two models side by side, built a guided tour and tidied a handful of small layout bugs. The program still works correctly. Now it also looks like it does.
- A planted mark is picked up, but only partly. A small ring-shaped mark (9 by 9 pixels) was planted on 90% of the images of one tissue class and on 2% of the others. With the mark at the image edge, the poisoned model calls 8.8% of the non-tumor test images tumor (clean model: 1.6%). With the mark at the center, where it never appeared in training, it calls 14.8% tumor (clean model: 1.5%). Accuracy on unmarked test images is 89.6% for the poisoned model and 90.8% for the clean one.
- The more of the study is held out, the lower the score. Holding out random patches gives a pooled AUROC of 0.999. Holding out whole hospitals gives 0.862 (interval [0.766, 0.917]), and single hospitals range from 0.76 to 0.99. Slides and patients fall in between, but with only 50 slides in the data the intervals for slide, patient and hospital overlap, so no order among them is claimed.
- Stain augmentation recovers most of the gap, not all of it. Training with random stain changes lifts the pooled hospital-level AUROC from 0.862 to 0.975 for the first seed and from 0.895 to 0.970 for the second. That closes 82% and 72% of the distance to the patch-level score. It made no clear difference at hospital 3, where the plain model already reached 0.988 to 0.990.
Try the first result yourself in the clinic, and the third in the stain lab.
The clinic
The ring stands in for the ink marks pathologists draw on slides (circles, dots, arrows, letters), which tend to sit next to the tissue that matters (Maleki et al., 2020; Lucassen et al., 2024; for surgical skin markings that raised a melanoma classifier's scores, Winkler et al., 2019).
Pick a patch, then drag the mark onto it (or focus the image and use the arrow keys; Enter places it, Delete removes it). Both models answer on the same patch.
How the two models compare on the test set
7,180 test images; intervals are 95% percentile intervals from resampling images (2,000 draws). One training run per model.
| Measure | Images | Clean model | Poisoned model |
|---|---|---|---|
| Accuracy, all test images | 7,180 | 90.8% [90.1%, 91.4%] | 89.6% [88.8%, 90.3%] |
| Balanced accuracy, all test images | 7,180 | 88.8% [88.0%, 89.6%] | 88.4% [87.6%, 89.1%] |
| Macro AUROC, all test images | 7,180 | 0.983 [0.980, 0.985] | 0.982 [0.979, 0.985] |
| Non-tumor images, mark at the edge: share called tumor | 5,947 | 1.6% [1.3%, 2.0%] | 8.8% [8.1%, 9.5%] |
| Non-tumor images, mark at the center: share called tumor | 5,947 | 1.5% [1.2%, 1.8%] | 14.8% [13.9%, 15.7%] |
| Tumor images, no mark: share called tumor | 1,233 | 97.4% [96.5%, 98.2%] | 93.5% [92.1%, 94.9%] |
| Grad-CAM mass inside the mark's box (non-tumor images, mark at the edge) | 5,947 | 1.55% [1.53%, 1.58%] | 1.65% [1.59%, 1.72%] |
For the Grad-CAM row, a map spread evenly over the image would put 1.98% of its mass inside the box.
Patches, slides, patients and hospitals
A patch is a small square of tissue. Patches come from slides, slides from patients, and patients from hospitals. In this data set 455,954 patches come from 50 annotated slides, which come from 43 patients at 5 hospitals.
Neighboring patches from one slide look alike, so a model that has seen some of a slide can recognize the rest of it. Most patients contributed a single annotated slide: 37 of 43. So the patient level differs from the slide level for only 6 patients. Both levels are kept so that no patient can sit on both sides of a split.
Slides are very uneven. The smallest has 888 patches and the largest 61,110, and most slides are almost entirely tumor or entirely normal. That makes single folds noisy, so the pooled result is reported with intervals instead.
Four ways to count one
Each slide is cut into thousands of small patches, and the network sees one patch at a time. Patches from one slide look alike, so a test patch whose neighbors were in training makes an easy test. The four ways below differ only in which unit is kept out of training.
Random patches are held out. Their neighbors from the same slide stay in training.
0.999 pooled AUROC
Whole slides are held out. A patient's other slide can still be in training.
0.905 pooled AUROC
All slides of a patient are held out together.
0.935 pooled AUROC
A whole hospital is held out, so every test patch comes from a lab never seen in training.
0.862 pooled AUROC
training test two slides from one patient Schematic, not to scale: each small square is a patch, each block of nine a slide, each box a hospital.
The same network was trained for each of 4 ways of holding data out, 5 times each, so that a different fifth of the data was the test set every time, and every patch was predicted exactly once. The figure shows the pooled AUROC with a 95% interval that resamples slides, not patches.
| Held out | Pooled AUROC | Interval (slides) | Accuracy | Mean of the fold AUROCs | Fold range |
|---|---|---|---|---|---|
| patch | 0.9991 | [0.9987, 0.9992] | 99.0% | 0.9991 | 0.999 to 0.999 |
| slide | 0.9048 | [0.8031, 0.9845] | 80.3% | 0.9480 | 0.816 to 0.993 |
| patient | 0.9353 | [0.8923, 0.9789] | 81.5% | 0.9510 | 0.908 to 0.972 |
| hospital | 0.8622 | [0.7656, 0.9172] | 79.1% | 0.8572 | 0.764 to 0.988 |
Patch level, resampling patches instead of slides, gives the narrower interval [0.9990, 0.9992]. That interval treats neighboring patches as independent, which they are not, so the slide interval is the one to trust.
Caveats
- With only 50 slides, the intervals for slide, patient and hospital overlap widely. The data show that the patch level is far above the rest. They do not show which of the other three is hardest.
- The pooled number mixes the scores of 5 different models. At the slide level it is 0.905, below the mean of the fold values (0.948), because the models are not equally calibrated.
- One training run is not one answer. Training the plain hospital-level model again with a different random seed moved the pooled AUROC from 0.862 to 0.895. The stain-augmented model moved from 0.975 to 0.970. The same run on two different graphics cards differed by 0.021 on a hard fold, and by 0.00001 on an easy one.
The hospital fingerprint
A simple classifier, logistic regression, tried to name the hospital of a patch from held-out slides. With 20% as chance, the color histogram of the patch alone gets it right 74% of the time. The plain model's internal features give 81%, and the stain-augmented model's features give 74%.
| Probe | Accuracy | Spread over folds | Balanced accuracy |
|---|---|---|---|
| the patch's own color histogram, no network | 74.4% | 12.5% | 76.9% |
| the plain model's 256 features | 80.7% | 12.6% | 87.9% |
| the stain-augmented model's 256 features | 73.9% | 11.5% | 81.9% |
So augmentation reduced the hospital information in the features: the augmented probe was lower than the plain one in 5 of 5 folds, by 6.8% on average. It did not remove it: the augmented features still identify the hospital far above chance. The spread between folds is larger than the gap, so the direction is what these numbers support.
Caveats
- The probes use features from models that never saw hospital 2, so that hospital is a fair test, but the other hospitals were in training for those models.
- Color is only one cue. A probe that cannot find a difference does not prove that none exists.
Stain lab
Pick a patch from hospital 2, the hospital the live models never saw, then change its stain. Both models answer on the changed patch, and the strip shows its nearest neighbors among 6,000 stored patches.
| Hospital | Hematoxylin | Eosin | Residual | Tissue pixels measured |
|---|---|---|---|---|
| 0 | 0.0716 | 0.0156 | 0.0510 | 59,460,528 |
| 1 | 0.0618 | 0.0190 | 0.0448 | 121,344,575 |
| 2 | 0.0728 | 0.0384 | 0.0114 | 94,619,873 |
| 3 | 0.0600 | 0.0250 | 0.0500 | 98,484,903 |
| 4 | 0.0590 | 0.0181 | 0.0201 | 24,961,527 |
Hospital 2 differs most in eosin. It is also the hospital where the plain model did worst. This does not show that one explains the other: with 5 hospitals, a link between a stain statistic and a score is easy to see by chance.
Tissue is told apart from glass by a threshold on the summed optical density. It was calibrated on a different data set. On these patches the data-driven value is 0.090, against 0.071 in the code. At the value in the code the median patch is 42% tissue. It was left unchanged, because the models were trained with it.
The repair attempt
The hospital-level models were retrained with random stain changes in training, on top of the flips and rotations that plain training also gets. In 8 of 10 paired comparisons, the interval for the change in AUROC excludes zero. The mean change is 0.118 for the first seed and 0.079 for the second.
| Held-out hospital | Seed | Plain | Augmented | Change | Interval of the change |
|---|---|---|---|---|---|
| 0 | 0 | 0.927 | 0.988 | 0.062 | [0.018, 0.121] |
| 1 | 0 | 0.800 | 0.977 | 0.177 | [0.027, 0.651] |
| 2 | 0 | 0.764 | 0.972 | 0.208 | [0.142, 0.437] |
| 3 | 0 | 0.988 | 0.992 | 0.004 | [-0.005, 0.005] |
| 4 | 0 | 0.807 | 0.944 | 0.137 | [0.091, 0.254] |
| 0 | 1 | 0.952 | 0.987 | 0.035 | [0.006, 0.061] |
| 1 | 1 | 0.816 | 0.977 | 0.161 | [0.024, 0.599] |
| 2 | 1 | 0.835 | 0.979 | 0.144 | [0.090, 0.304] |
| 3 | 1 | 0.990 | 0.990 | 0.000 | [-0.020, 0.002] |
| 4 | 1 | 0.869 | 0.925 | 0.056 | [0.034, 0.151] |
The exceptions are hospital 3, where the plain model already reached 0.988 to 0.990. The largest change is 0.208 (hospital 2, seed 0); the smallest is 0.000.
How much of the gap closes: pooled over the hospitals, the augmented models reach 0.975 for the first seed and 0.970 for the second, against 0.999 at the patch level. That is most of the gap, not all of it, and the augmented probe still reads the hospital from the features, as shown above.
Caveats
- The strength of the stain changes was fixed in advance and not tuned on any held-out hospital, so these numbers are not the result of a search.
- The plain first-seed runs were trained on a different graphics card from the augmented runs. The size of that effect on one hard fold was 0.021, much smaller than most of the changes above.
- Intervals resample the slides of one hospital. The widest belongs to hospital 1, 0.62 across, because each hospital has only a few slides.
A bigger model: Phikon
This section repeats the checks on the same patches with a different kind of model: Phikon, a large vision transformer trained by Owkin on tiles from TCGA, with its weights frozen. Only a linear classifier on top of its 768 features was trained, with the same four ways of holding data out as above, on the 118,582 patches that Phase two kept on disk. This repeats, on this data, what others have already shown for foundation models (PathoROB by Kömen and colleagues; de Jong and colleagues); it is not a new finding.
| Held out | Small CNN | Phikon + linear | Phikon minus CNN |
|---|---|---|---|
| patch | 0.999 [0.999, 0.999] | 0.999 [0.999, 0.999] | 0.000 [0.000, 0.000] |
| slide | 0.925 [0.839, 0.982] | 0.993 [0.990, 0.996] | 0.069 [0.013, 0.152] |
| patient | 0.945 [0.909, 0.978] | 0.993 [0.989, 0.996] | 0.048 [0.017, 0.085] |
| hospital | 0.851 [0.758, 0.904] | 0.984 [0.971, 0.994] | 0.133 [0.071, 0.224] |
At a hospital the model never saw, the frozen features lose 0.016 of AUROC between random patches and a held-out hospital, against 0.148 for the small CNN. The features still carry the hospital: a linear probe names the hospital of a patch correctly 97.7% of the time (chance 20%; the small CNN's features gave 80.7%, and a probe given shuffled hospital labels scored 19.5%).
Projecting out the directions that separate the hospitals, fitted on the training hospitals only, changed the pooled held-out-hospital AUROC by -0.001 [-0.002, 0.000]. Stain augmentation changed the small CNN's by 0.126 [0.073, 0.211] on the same patches. This is consistent with the hospital signal being present in the features without the tumor classifier relying on it; it does not prove it.
A threshold set elsewhere
A good AUROC does not mean a fixed cut-off works. For each hospital in turn, the threshold that gave 90% sensitivity on the other hospitals (using out-of-fold scores) was applied unchanged to it:
| Held-out hospital | AUROC | Sensitivity | Specificity | Calibration error |
|---|---|---|---|---|
| 0 | 0.996 | 96.0% [88.7%, 98.1%] | 99.3% | 0.027 [0.008, 0.046] |
| 1 | 0.993 | 95.0% [87.0%, 97.0%] | 98.2% | 0.009 [0.005, 0.023] |
| 2 | 0.982 | 81.2% [34.9%, 95.0%] | 99.4% | 0.099 [0.009, 0.229] |
| 3 | 0.990 | 90.2% [77.4%, 93.2%] | 99.9% | 0.048 [0.003, 0.072] |
| 4 | 0.992 | 33.8% [27.8%, 82.0%] | 100.0% | 0.242 [0.004, 0.383] |
Sensitivity fell below the target at 2 of the 5 hospitals, and was lowest at hospital 4 (33.8%) although the AUROC there was 0.992. At these hospitals the ranking of patches transferred and the cut-off did not. The small CNN's version of this table is not shown: its scores on the training hospitals are in-sample and would flatter it.
Caveats
- Only one foundation model was tested, with frozen features and a linear head, and nothing here says that other models, or fine-tuning, behave the same.
- The patches are 96 pixels wide and were enlarged to 224 for the model; Phikon was trained on larger, different tiles. How much this affects either model was not measured.
- Phikon was trained on TCGA, not Camelyon. Any overlap between the two was not checked.
- There are five hospitals with a few slides each, so the interval at a single hospital is wide, and the threshold result at hospital 4 rests on that hospital's slides only.
- The regularization strength was chosen on a random sample of the training rows, not all of them.
Credits: Phikon (Owkin; non-commercial license), Camelyon17-WILDS, and the PathoROB and de Jong studies cited above.
Gene expression (HEST-CCRCC)
This is an extension outside the original two phases. In spatial transcriptomics, a model reads a patch of tissue and predicts how strongly 50 genes are switched on there. The task is clear-cell kidney cancer of the public HEST benchmark (24 tissue sections, 74,220 spots) using the same frozen Phikon features as above and the benchmark's own recipe. The question is the same as above: how much the score depends on what was held out.
Check of the recipe: with the benchmark's own six folds, the mean of the per-fold scores is 0.2425; the benchmark reports 0.2423. (One departure: the regression is solved in closed form instead of with the benchmark's iterative solver, for speed.)
| Held out | Correlation | Interval |
|---|---|---|
| Random spots | 0.646 | [0.533, 0.723] |
| Spatial blocks inside each section | 0.611 | [0.491, 0.694] |
| Whole tissue sections | 0.314 | [0.244, 0.348] |
| The benchmark's folds | 0.301 | [0.226, 0.340] |
Holding out random spots gives about 2.1 times the correlation of holding out the benchmark's folds, most likely because neighboring spots look alike and share expression (not tested directly). The difference is 0.346 [0.235, 0.449]. Holding out whole sections already removes most of that advantage; going from whole sections to the benchmark's folds changes the score by 0.013 [-0.019, 0.043], which these data cannot tell apart from no change.
Caveats
- Only one encoder, one tumor type and 6 folds were used, and the intervals are wide because they resample only 6 units.
- That each benchmark fold is a single patient is not confirmed, so they are called benchmark folds, not patient-level splits.
- The scores here pool all held-out spots before correlating; the benchmark averages the per-fold correlations. That is why the benchmark-fold row differs from the reproduction value above.
- The spatial blocks are a simple grid inside each section, not a tuned design.
Credits: HEST-1k and HEST-Benchmark (Jaume and colleagues, Mahmood Lab; non-commercial license), Phikon (Owkin).
Mistakes worth knowing about
- I trusted a threshold from the specification for telling tissue from glass. On real patches it let through only a tiny share of the pixels, so the stain-matching step would have skipped almost every patch. I found it by measuring on real data, replaced it with a value measured there, and checked it again on the second data set, where it behaved about the same.
- I reported a stain-jitter number that could not be right: the overall average change was lower than both the tissue average and the background average, which is impossible for an average. The reference line in my own script was calling the wrong function. I fixed the script, reran it, and corrected the log, including a milder version of the claim I had first made about the background.
- I wrote the first description of the experiment before I had results, and it said accuracy would collapse once the mark moved. The measured effect was real but modest, and the saliency map did not concentrate on the mark. I rewrote the description to say what was measured.
- I expected the score to fall in a clean staircase from patch to slide to patient to hospital. With only a few dozen slides the intervals overlap, and the pooled slide result even came out below the patient one. The page now says the data do not rank those three levels.
- My first speed test of the server was wrong. Each level of concurrency replayed the requests of the previous level, so half of them were answered from the cache and the numbers looked better than they were. I made every level use fresh requests and reran it.
- The automatic method for finding the tissue threshold on the second data set returned a value several times too large. A small share of pure black pixels formed a second mode and dragged it. I looked at the percentiles, saw the cause, excluded those pixels, and recorded both values.
- A training run that was stopped and resumed did not reproduce the uninterrupted run. A test caught it: the saved checkpoint left out the random state that dropout uses. It is now saved and restored.
- I assumed the large image array would still be on the rented machine after I shut the first one down, and I destroyed it. I rebuilt the patches the analysis needed from the public archive and checked them against features the original run had saved, which matched. It cost an extra step I could have avoided by keeping a copy.
Limitations
- This is a patch classifier on a public benchmark. It is not a diagnostic tool, and no slide-level or patient-level decision is made anywhere on this page.
- All 5 Camelyon17 hospitals are Dutch. A hospital-level result says nothing about other countries, scanners or protocols.
- The Phase one stamp is synthetic. Real artifacts are messier.
- The Phase one images were color-normalized by the data set authors, which hides some stain variation. Its test set also comes from a different center than its training set.
- Grad-CAM is coarse, with a map of only a few pixels per side before it is enlarged, and it can mislead. Integrated gradients is shown beside it for that reason, and its attributions only approximately add up to the model's answer: at 32 steps the median error was 4% and 55% of the 40 patches checked were within 5%.
- Each cell of the results is one training run, or two for the hospital level. A model's AUROC on a new hospital moved by several hundredths between random seeds, and slightly between graphics cards.
- With 50 slides in total, intervals that resample slides are wide, and the slide, patient and hospital levels cannot be ranked against each other.
- The Camelyon17 archive was downloaded from a server whose security certificate had expired, so the download could not be authenticated. Three downloads on different machines and a fourth streamed read give the same checksum, the metadata match row for row, and the split sizes agree with the published ones. That is evidence, not proof.
Reproduce and inspect
Everything on this page is computed from the files below. The build refuses to render a result file that is missing a value, has a number that is not finite, or was produced by a model whose hash does not match.
- p1_eval.json (3,535 bytes, sha256 f2d41a4ff67423689aa6f44432975e72f33b9dda3cdccff901c1e40e29a9b475)
- p2_levels.json (9,983 bytes, sha256 60fb26b42414ee7d2ef08e9aaca24fc57f1f78aa9d6542f725bb182ae2abdb38)
- p2_repair.json (4,372 bytes, sha256 49a623c834ad4e100325e27e85124cdcd479b4c0daf9e083e4b1ec65fa17328c)
- p2_probes.json (3,955 bytes, sha256 c3998346a6623b3750567f2a782c33708bd254d756b8fb4bc3efafab8e6642fe)
- p2_map.json (176,818 bytes, sha256 daa136f39ae02dbec084e725bfead55eed2a523f4a77e98cf0731b54ed229cb7)
- splits_meta.json (23,487 bytes, sha256 32e51d9b8a757a7ef8b552235b6021c6977e77db2163e337711fafa4ab500a96)
- hardware_check.json (756 bytes, sha256 04eee41c7e1df6bc22271761ff95d7618557dc360ec5d677bb7bb0d5e9bfaed6)
- ig_completeness.json (2,197 bytes, sha256 6a712e6e1d81be4d27f86d877061db64c5f409b3f1aa109d2885ab5d3ca64ce9)
- stain_stats.json (1,341 bytes, sha256 20a202c62ce5d68df7884f8921ad1efa97b21af649f27c0316d20fa089f6e976)
- p2_splits.parquet (2,746,960 bytes, sha256 826a81b77ec9da2dddce11e71ca845493d27b8a095be904efbdfa655a313d150)
- p2_data_manifest.json (1,005 bytes, sha256 070dcfd19dcd77108fa739e21e7b17b66938f258e760e19a0d2fa26173ef3f52)
- pathlens-source.tar.gz (280,032 bytes, sha256 3e64a8ccf1525fbe88c4f59dd2658de09bc9ea6ac12e88b0dfa195f3b402e41e)
The commands, in order, on a machine with a GPU for the training step:
uv sync --extra dev --frozen uv run python -m scripts.prepare_p1 uv run python -m scripts.train_p1 --variant clean uv run python -m scripts.train_p1 --variant poisoned uv run python -m scripts.prepare_p2 --delete-archive --delete-source uv run python -m scripts.make_splits --levels 4 uv run python -m pathlens.run_all --config configs/phase2_gpu.yaml uv run python -m scripts.aggregate_p2 uv run python -m scripts.build_site
Data: PathMNIST (CC BY 4.0, medmnist.com) and Camelyon17-WILDS (CC0, wilds.stanford.edu).