File size: 13,548 Bytes
f93c1b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# PathQA β€” an AI second-reader QA safety-net for pathology labs

**Positioning:** quality assurance / workflow, **not** diagnosis. PathQA never
issues a diagnosis and never changes a pathologist's sign-out. It runs quietly
behind the sign-out and does exactly one thing: when it *confidently disagrees*
with the pathologist, it raises a hand β€” "case N, worth a second look, here's
why." A human decides. The pathologist stays in control.

This repo is the **v1 build**, in two stages exactly as scoped:

1. **Patch-classifier demo** on public data (PatchCamelyon) β€” proves the model +
   the QA logic end-to-end.
2. **Train-on-your-data** version β€” the model comes to the lab's own labeled
   slides, which is what fixes domain shift, and nothing leaves the machine.

---

## Why this design (the three product requirements)

**1. Trains on the lab's own labeled slides β†’ solves domain shift + keeps data private.**
Off-the-shelf histopathology models degrade badly across stains, scanners and
tissue mixes. `train_on_your_data.py` fine-tunes on a folder of the lab's own
patches, so the model learns *that* lab's distribution. All I/O is local β€” the
model comes to the data.

**2. Flags only disagreements β†’ a human re-checks.**
The QA engine (`pathqa/qa.py`) compares the model's read to the pathologist's
label and flags a case only when the model **disagrees** *and* is **confident**
(`confidence β‰₯ tau`). Everything else passes untouched. Output is a prioritized
re-check worklist, sorted by confidence β€” the queue a pathologist actually opens.

**3. Assist, not replace.**
No case is ever auto-relabeled. The only output is a "recommend re-review" flag
with a plain-language reason. Every artifact carries that reminder.

Two extra design choices that matter for a *QA* tool specifically:

- **Calibration.** A QA threshold is only meaningful if "0.9 confidence" behaves
  like 0.9. Raw softmax is over-confident, so I apply **temperature scaling**
  (Guo et al., 2017) on the validation split. In the smoke test this dropped
  validation loss from 0.265 β†’ 0.231.
- **Workload budget.** Instead of a fixed `tau`, a lab can say "flag the top 5%
  most-confident disagreements" (`--budget 0.05`) so the QA queue fits the time
  the lab actually has. The engine backs out the effective `tau` for you.

---

## Layout

```
pathqa/
  pathqa-app / .bat       # ONE-COMMAND LAUNCHER (venv + deps + open browser)
  pathqa/
    model.py     # ResNet18/34 binary tumor/normal head + TemperatureScaler
    data.py      # 3 sources: PatchCamelyon | your ImageFolder | synthetic (tests)
    engine.py    # train / evaluate (AUROC, ECE) / temperature fitting
    qa.py        # SecondReader β€” patch-level disagreement + flagging engine
    wsi.py       # whole-slide tiling + tissue detection
    slide.py     # slide aggregation (top-K), slide-level QA, heatmap
    gradcam.py   # Grad-CAM explainer (why the model flagged it)
    cli.py       # `pathqa serve|demo|slide-demo|train` dispatcher
  backend/       # stdlib HTTP server + stateful QA session
  frontend/      # single-file React UI (Patch QA + Whole-slide QA tabs)
  demo.py                 # patch QA demo (stage 1)
  slide_demo.py           # whole-slide QA demo
  train_on_your_data.py   # fine-tune on the lab's own labeled patches (stage 2)
  train_pcam.py           # real PatchCamelyon training + AUROC/ECE
  requirements.txt
```

## Install

```bash
pip install -r requirements.txt   # torch, torchvision, h5py
```

## Run β€” Stage 1: patch-classifier demo

No download, verifies the whole pipeline in ~1 minute on CPU:

```bash
python demo.py --synthetic --epochs 1 --no-pretrained
```

Real public data (downloads PatchCamelyon on first run; use a GPU or Colab):

```bash
python demo.py --pcam --subset 20000 --epochs 3 --budget 0.05
```

What it does: loads patches β†’ fine-tunes ResNet18 β†’ temperature-calibrates β†’
simulates a sign-out queue with a small injected human-error rate β†’ runs the QA
second reader β†’ prints a report and writes `qa_out/worklist.csv` +
`qa_out/qa_report.json`.

Sample output:

```
PathQA second-reader report
  cases reviewed        : 128
  model/pathologist disagreements: 17 (13.3%)
  flagged for re-check  : 6 (4.7%) at confidence >= 0.85
  -> 122 cases pass QA untouched; pathologist reviews only the 6 flagged.
[QA effectiveness] of 17 simulated sign-out errors, the second reader flagged 6 for re-check.
```

## Run β€” Stage 2: train on your data

Lay your labeled patches out as:

```
my_lab_data/
  train/normal/*.png   train/tumor/*.png
  val/normal/*.png     val/tumor/*.png
```

```bash
python train_on_your_data.py --data my_lab_data --epochs 5 --out model.pt
```

Saves the fine-tuned weights **and** the fitted temperature, so the QA engine
gets calibrated confidences out of the box. Everything stays on the machine.

---

## Run β€” the local app (one command)

A local, on-prem app so a pathologist (non-coder) can use it. The backend is
**Python stdlib only** (no framework to install, nothing phones home β€” chosen
for an air-gapped lab), and the frontend is a single-file React UI it serves.

```bash
./pathqa-app            # macOS/Linux β€” makes a venv, installs deps, opens the browser
pathqa-app.bat          # Windows

./pathqa-app demo       # patch QA demo (headless)
./pathqa-app slide-demo # whole-slide QA demo (headless)
./pathqa-app train --help
```

The launcher creates a `.venv`, installs `requirements.txt`, then starts the
server and opens `http://127.0.0.1:8000` (localhost only). Under the hood it
calls `pathqa/cli.py`; you can also run the pieces directly:

```bash
PYTHONPATH=. python backend/server.py --open
```

Out of the box it fine-tunes a small demo model on synthetic patches so there's
a working worklist immediately. To serve a model you trained on your own data:

```bash
PATHQA_MODEL=model.pt PYTHONPATH=. python backend/server.py
```

What the UI does: shows the flagged re-check queue (patch thumbnail, sign-out vs
model read, confidence), a live Ο„ slider to tune the queue length, and two
actions per case β€” **Confirm sign-out** or **Amend (model was right)**. Every
decision is appended to `audit_log.jsonl` with model read, confidence and a note
β€” the governance trail. No sign-out is ever changed automatically.

API (stable; maps 1:1 onto FastAPI path operations if you later swap frameworks):
`GET /api/summary`, `GET /api/worklist`, `GET /api/patch/{id}.png`,
`POST /api/decision`, `POST /api/settings`.

Air-gapped frontend: run `frontend/vendor_offline.sh` once to pull React+Babel
locally, then point the three `<script>` tags in `index.html` at `./vendor/...`.

## Whole-slide QA (patch β†’ slide)

A real sign-out is per-slide, not per-patch. The whole-slide layer tiles a slide,
scores every tissue tile, and aggregates into one slide-level decision:

- `pathqa/wsi.py` β€” tiling + tissue/background detection (keeps stained tissue,
  drops glass). Opens large PIL images, true WSI via OpenSlide (`.svs/.ndpi`,
  optional lazy import), or a synthetic slide for the demo.
- `pathqa/slide.py` β€” **top-K mean pooling**: a slide is called tumor if its most
  suspicious regions are tumor (matches metastasis detection β€” one focus is
  enough β€” far better than averaging tumor away across a big slide). Also emits a
  tumor-probability **heatmap** and the slide-level QA flags.

Run the slide demo (synthetic cohort, writes a heatmap per slide + a worklist):

```bash
python slide_demo.py --n-slides 8 --tau 0.7 --outdir slide_out
```

For a real slide, swap `SyntheticSlide` for `open_slide_image("case.svs")` β€” the
rest is unchanged. OpenSlide is only needed for native WSI formats:
`pip install openslide-python` (plus the system OpenSlide library).

In the app, the **Whole-slide QA** tab shows each flagged slide as a heatmap card
(sign-out vs model, slide score, suspicious-tile count) with Confirm / Amend, and
its own Ο„ slider. Slide decisions log to the same `audit_log.jsonl` with
`"level":"slide"`.

## Analyze an image (real model + stain), and the train-better loop

The app's "Analyze image" tab runs an uploaded H&E image through BOTH methods
and combines them:

- the trained CNN (`pathqa/infer.py`) with test-time augmentation, giving a
  calibrated tumour probability plus an uncertainty estimate (so it can say
  "not sure" instead of guessing), and a Grad-CAM overlay (patch) or a tiled
  tumour heatmap with the top suspicious tiles (slide);
- the stain/colour method (dense-nuclei density) as an independent check.

The two are merged into one verdict: if they agree, confidence goes up; if they
disagree, it returns "needs review" rather than a false certainty.

Endpoints: `POST /api/analyze-image` (base64 image in, verdict plus two overlay
images out), `POST /api/feedback` (save a labelled image as training data),
`GET /api/feedback/stats`.

Train-better loop: every image you label in the app is saved under
`data/feedback/{normal,tumor}/`. When you have enough, fine-tune on exactly the
cases your lab sees:

```bash
python finetune_from_feedback.py --base pcam_model.pt --out pcam_model.pt
# prints held-out AUROC + ECE, saves a calibrated checkpoint the app loads
PATHQA_MODEL=pcam_model.pt ./pathqa-app
```

So it gets better the more it is used, and always reports honest metrics before
you adopt a new checkpoint.

## Explainability β€” Grad-CAM

A flag is only actionable if the pathologist can see *why* the model disagreed.
`pathqa/gradcam.py` runs Grad-CAM on the classifier's last conv block and
overlays a heat map on the patch showing which pixels drove the call. In the
app, clicking a flagged patch opens the patch next to its "model attention"
overlay. This is explanation, not diagnosis β€” it never changes a call.

## Real training on PatchCamelyon

`train_pcam.py` trains on real PCam and reports honest held-out metrics β€” AUROC
and Expected Calibration Error (ECE) before/after temperature scaling β€” then
saves an app-ready checkpoint.

```bash
# GPU (full run)
python train_pcam.py --epochs 5 --batch-size 256 --out pcam_model.pt

# quick CPU subset
python train_pcam.py --subset 8000 --epochs 1 --batch-size 64 --out pcam_model.pt

# pipeline check, no download
python train_pcam.py --synthetic --no-pretrained --epochs 1 --out demo_model.pt
```

Colab (free GPU) β€” PCam is ~7 GB so run it there or on a lab GPU box:

```python
!pip -q install torch torchvision h5py
!python train_pcam.py --epochs 5 --batch-size 256 --out pcam_model.pt
```

It prints something like `AUROC: 0.96 Β· ECE: 0.08 β†’ 0.02 (calibrated)`. Then:

```bash
PATHQA_MODEL=pcam_model.pt ./pathqa-app     # the app now uses your real model
```

Note: full PCam training needs a GPU and the ~7 GB download; the synthetic
`--synthetic` path exists to verify the training/metrics/calibration code runs
end-to-end without either. The pipeline was verified this way (AUROC/ECE/
checkpoint all produced correctly); real clinical numbers come from your run.

## Key parameters

| Flag | Meaning |
|---|---|
| `--tau 0.9` | Flag disagreements at/above this confidence. |
| `--budget 0.05` | Instead of `tau`, flag only the top 5% most-confident disagreements (fits a workload). |
| `--path-error-rate` | (demo only) simulated pathologist error rate, to show the safety-net catching misses. |
| `--subset` / `--syn-train` | Cap dataset size for CPU-friendly runs. |

`tau` is the single most important dial. Lower it β†’ catch more true misses but a
longer re-check queue; raise it β†’ shorter queue but you may miss borderline
errors. Tune it against the lab's tolerated false-flag rate on a labeled
holdout, then re-check monthly.

---

## What was verified

Ran end-to-end on CPU (ARM64): training converges, temperature calibration
lowers validation loss, both threshold and workload-budget flagging modes
produce a correct prioritized worklist, and the JSON report round-trips. The
real-data path uses the same code with `--pcam` or `--data`.

## Roadmap (v2+, out of scope for this build)

- **Slide-level, not just patch-level:** tile a whole-slide image, aggregate
  patch scores into a slide decision, and flag at the slide level.
- **Uncertainty on top of calibration:** MC-dropout / deep ensembles to separate
  "confident disagreement" from "model has no idea."
- **Active learning loop:** feed re-checked flags back as new labels so the model
  improves on the lab's hard cases over time.
- **Human factors:** a viewer that jumps to the flagged region with a heatmap
  (Grad-CAM) so re-review takes seconds.
- **Governance:** audit log of every flag + outcome, and drift monitoring on the
  incoming slide distribution.

## Regulatory framing (important, not legal advice)

Framing PathQA as an internal **QA / workflow** tool that only surfaces cases for
human re-review β€” never rendering or altering a diagnosis β€” is a deliberately
conservative posture. Anything that outputs a diagnosis is a different, much
heavier regulatory category. Keep the "assist, not replace, human-in-the-loop"
boundary bright, and involve regulatory/clinical governance before any use on
real patient material. I'm not a lawyer or a regulatory advisor; validate the
intended-use claim with someone who is.

## Limitations

Binary tumor/normal patches only; no whole-slide aggregation yet; PatchCamelyon
is lymph-node metastasis and is not representative of your tissue types β€” the
public-data demo proves the *mechanism*, not clinical performance. Clinical
performance must be established on your own validated data.
```