Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ MR-RATE/
│
├── dino-pretraining/ # Self-supervised native 3-D DINOv3 on coregistered MRI
│
├── report-generation-fora/ # Report writer on the MIL classifier (diagnostic plan + Gemma-4-31B, SFT and DPO)
│
└── README.md
```

Expand All @@ -36,13 +38,17 @@ Each folder includes its own `README.md` detailing configuration, dependencies,
| --------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Data Preprocessing** | End-to-end pipeline converting raw DICOM exports into clean, anonymized, and spatially standardized NIfTI volumes. Covers DICOM-to-NIfTI conversion, PACS metadata filtering, modality classification, binary brain segmentation & defacing, co-registration to a shared T1w reference, atlas registration to MNI152 space, and multi-label anatomical segmentations. Also includes the radiology report preprocessing pipeline (anonymization, translation, structuring, QC) and LLM-based pathology classification producing binary labels for 37 SNOMED CT-grounded pathologies. Includes standalone scripts for downloading and merging all MR-RATE Hugging Face repositories. |
| **Contrastive Pretraining** | Contrastive vision-language model that aligns multi-sequence MRI volumes and radiology reports using VL-CABS loss. Uses a VJEPA2 (ViT-G) image encoder with LoRA fine-tuning and a BiomedVLP-CXR-BERT text encoder. Supports four multi-volume fusion modes (`early`, `mid_cnn`, `late`, `late_attn`) and three image spaces (`native_space`, `coreg_space`, `atlas_space`). Enables zero-shot brain MRI pathology classification at inference time. |
| **Report Generation (MIL plan writer)** | Brain MRI Findings generation following the FORA recipe. The frozen 74-diagnosis MIL classifier reads every sequence of a study and gives a calibrated diagnostic plan with evidence descriptions (distribution, brain-region sites, sequences) and focus tokens for each planned finding. A Gemma-4-31B-it writer with LoRA reads the MIL tokens of the whole study and the plan, and is trained with SFT and then offline DPO on rule-based preference pairs. |
| **3-D DINOv3 Pretraining** | Self-supervised DINO+iBOT+KoLeo training on coregistered multi-sequence MRI, with physical 3-D RoPE, exact per-sequence coverage, study-balanced weighting, FSDP2/FP8 production launchers, and synthetic end-to-end tests. |

## Workflow Summary

For the Dong MR DINO checkpoints, full multi-sequence MIL (online/offline) and
streaming contrastive VLM are documented in
[`dino-pretraining/MIL_VLM.md`](dino-pretraining/MIL_VLM.md).
[`dino-pretraining/MIL_VLM.md`](dino-pretraining/MIL_VLM.md). The MIL ablations (training-data size,
cross-sequence attention, modalities as input channels) are described in
[`dino-pretraining/ABLATIONS.md`](dino-pretraining/ABLATIONS.md). Report generation on top of the MIL classifier is
described in [`report-generation-fora/README.md`](report-generation-fora/README.md).

1. **Download preprocessed data** directly from [MR-RATE on Hugging Face](https://huggingface.co/datasets/Forithmus/MR-RATE) (see `data-preprocessing/` for preprocessing details)
2. **Train the contrastive model** on (multi-sequence MRI, radiology report) pairs using `contrastive-pretraining/`
Expand Down
84 changes: 84 additions & 0 deletions dino-pretraining/ABLATIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# MIL ablations

Three ablations of the MR-RATE MIL classifier (ViT-H+ DINO backbone, 74 labels). All use the
resumable multi-node trainer `dfw_mil.mil_fast` and the launcher `scripts/dfw/train_array.sbatch`,
with the baseline settings (head lr 0.002, last-block lr 1e-5, 10 epochs, one study per rank per
step, validation every epoch on the full validation split).

Set `CODE` to the `dino-pretraining` folder of this checkout. `mr_dino` and `dfw_mil` are both
imported from it.

## 1. Training-data size

`--train-fraction f` keeps a fraction `f` of the training patients. Patients are ordered once by a
hash of their id and `--subset-seed`, and a fraction keeps the first `ceil(f * n)` of them, so the
20% subset is contained in the 40% subset and so on, and all studies of a patient stay together.
Validation and test are never subsetted. The feature normalization is computed on the same subset.

```bash
for f in 0.2 0.4 0.6 0.8; do TRAIN_FRACTION=$f sbatch scripts/dfw/train_array.sbatch; done
```

Runs go to `runs/mil_frac<f>`. The 100% point is the baseline run.

## 2. Cross-sequence attention

`--fusion cross_sequence` inserts `dfw_mil.fusion.CrossSequenceAttention` between the trainable
last block and the MIL head. All sequences of a study share the atlas grid, so at every token
position the tokens of the sequences covering it (T1, T2, FLAIR, SWI and further acquisitions)
attend to each other, with a learned embedding of each token's sequence kind. Every sequence
token stays in the bag. The output projections start at zero, so the untrained model equals the
baseline. The module is deterministic, which keeps the two-pass MIL gradient exact, and the last
block is recomputed per sequence in the backward pass (`--fusion-recompute`) to bound memory.

```bash
FUSION=cross_sequence NORMALIZATION=<baseline run>/normalization.pt sbatch scripts/dfw/train_array.sbatch
```

Options: `--fusion-depth` (1), `--fusion-heads` (8), `--fusion-mlp-ratio` (2), `FUSION_LR` (1e-4).
The baseline normalization can be reused because the backbone and training set are unchanged.

## 3. Modalities as input channels

The backbone takes T1, T2, FLAIR and SWI as the channels of one input, plus one availability
plane per modality (8 input channels, `mr_dino.channels`). Missing modalities are zero with
availability 0. Contrast-enhanced T1 uses the T1 channel. Other sequences are not used.

**DINO pretraining** (`--input-channels modality`, raw NIfTI input only). One item per study.
Each item draws one acquisition per modality. Each view keeps a random nonempty subset of the
available modalities, the full set with probability `--full-modality-probability` (0.5). The two
global views share their subset unless, with `--cross-sequence-probability`, the second draws its
own, and iBOT/cross-crop twin tokens are paired only between views with the same subset. Add
`--input-channels modality` to the trainer arguments of each stage (pretrain, gram, highres) and
keep every other setting of the baseline recipe.

**Export** the final teacher as a release:

```bash
python -m mr_dino.export_release --checkpoint <highres run>/checkpoints/step_00005000 --output <release dir>
```

The release records `in_chans: 8` and its channel layout in `config.json`.

**MIL** with that release. Each study is a bag of modality stacks. Stack `k` uses acquisition
`k mod n_m` of every modality `m`, so every acquisition enters the bag at least once.

```bash
RELEASE=<release dir> RUN=<new run dir> sbatch scripts/dfw/train_array.sbatch
```

## Smoke test

`SMOKE=1` trains 2 epochs on 96 training and 24 validation studies, checkpoints after 3 steps and
resumes in the same job:

```bash
SMOKE=1 sbatch --array=1 --time=01:30:00 scripts/dfw/train_array.sbatch
SMOKE=1 FUSION=cross_sequence sbatch --array=1 --time=01:30:00 scripts/dfw/train_array.sbatch
```

## Tests

```bash
python -m pytest tests/test_ablations.py
```
13 changes: 13 additions & 0 deletions dino-pretraining/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ nohup scripts/probe_watch.sh $R/highres 2500 5000 &
`train_32n_7b.sbatch` and `train_32n_hplus.sbatch` are the old 7B-continuation recipe
(32-node DDP/FSDP) and are kept for reference only.

## MIL ablations

[`ABLATIONS.md`](ABLATIONS.md) describes three ablations of the 74-label MIL classifier, all run with the resumable
multi-node trainer `dfw_mil.mil_fast` and `scripts/dfw/train_array.sbatch`.

* **Training-data size.** `--train-fraction` trains on nested patient-level subsets (20, 40, 60, 80 %) with the
same validation set.
* **Cross-sequence attention.** `--fusion cross_sequence` lets the tokens of all sequences at the same atlas position
attend to each other before the MIL head. It starts as the identity, so training begins from the baseline.
* **Modalities as input channels.** `--input-channels modality` pretrains DINO on T1, T2, FLAIR and SWI as the
channels of one input (plus availability planes), `mr_dino.export_release` exports the backbone, and the MIL uses
it directly.

## Validation probe (checkpoint scoring)

This is the MR DINO protocol: frozen features, MIL on the validation split only, and
Expand Down
2 changes: 2 additions & 0 deletions dino-pretraining/dfw_mil/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"""Resumable online MR-RATE MIL (multi-node) and its ablations: training-data fraction,
cross-sequence attention and modality-channel backbones."""
243 changes: 243 additions & 0 deletions dino-pretraining/dfw_mil/fast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
"""Online MRI study features for MIL, with the outputs of ``mr_dino.vision.online_study_factory``.

* The frozen prefix (all blocks but the last) runs once per study, tiles batched, and its
tokens stay on the GPU so both MIL passes reuse them instead of recomputing.
* Volumes for upcoming studies are read and resampled in background threads.

Three input layouts:

* ``cached_study_factory``: every sequence is a one-channel input and every token of every
sequence enters the bag (the MR-RATE MIL).
* the same function with a modality-channel release (``config["input_channels"]``): each
input stacks one acquisition per modality plus availability planes (``mr_dino.channels``).
Stack ``k`` uses acquisition ``k mod n_m`` of each modality, so every acquisition is seen.
* ``cross_sequence_factory``: per tile position, the tokens of all sequences at each atlas
position, for ``fusion.CrossSequenceAttention``.
"""
from __future__ import annotations

import json
import os
import time
from concurrent.futures import ThreadPoolExecutor

import torch
import torch.distributed as dist
import torch.nn.functional as F
from torch.utils.checkpoint import checkpoint

from mr_dino.channels import acquisition_stacks, modality_groups, stack_channels
from mr_dino.data import raw_volume_with_support
from mr_dino.geometry import normalized_positions, sequence_kind

RANK = int(os.environ.get("RANK", 0))
TIMING_LOG = os.environ.get("MIL_TIMING_LOG")


def emit(**row) -> None:
if not TIMING_LOG:
return
row["rank"] = RANK
with open(f"{TIMING_LOG}.rank{RANK}", "a") as handle:
handle.write(json.dumps(row) + "\n")


def modality_layout(metadata) -> bool:
layout = metadata["config"].get("input_channels")
if layout is None:
return False
if layout.get("layout") != "modality":
raise ValueError(f"Unknown input channel layout {layout}")
return True


def load_study(study, metadata):
"""One ``(image, support)`` per sequence path; ``None`` for paths a modality release ignores."""
args = metadata["args"]
start = time.perf_counter()
used = set(range(len(study["image_paths"])))
if modality_layout(metadata):
used = {i for group in modality_groups(study["image_paths"]) for i in group}
volumes = [raw_volume_with_support(source, tuple(args["target_shape"]), tuple(args["target_spacing"]),
args["posterior_shift_mm"]) if i in used else None
for i, source in enumerate(study["image_paths"])]
emit(event="load", uid=study["study_uid"], sequences=len(used), seconds=time.perf_counter() - start)
return volumes


class Prefetcher:
"""Loads items ``i+1 .. i+depth`` while item ``i`` is being used; ``None`` entries are skipped."""

def __init__(self, keys, load, depth=2):
self.keys, self.load, self.depth = keys, load, depth
self.pool = ThreadPoolExecutor(max_workers=max(1, depth))
self.futures = {}

def _submit(self, i):
if 0 <= i < len(self.keys) and self.keys[i] is not None and i not in self.futures:
self.futures[i] = self.pool.submit(self.load, self.keys[i])

def get(self, i):
for j in range(i, i + self.depth + 1):
self._submit(j)
start = time.perf_counter()
out = self.futures.pop(i).result()
emit(event="load_wait", seconds=time.perf_counter() - start)
return out

def close(self):
self.pool.shutdown(wait=False, cancel_futures=True)


def _padded(image, support, tile):
"""Pad ``[C, D, H, W]`` image and ``[D, H, W]`` support to whole tiles."""
pads = []
for n, t in reversed(list(zip(image.shape[-3:], tile))):
pads.extend((0, (-n) % t))
return F.pad(image, pads, value=0), F.pad(support.float(), pads, value=0)


def _tile_starts(shape, tile):
for z in range(0, shape[0], tile[0]):
for y in range(0, shape[1], tile[1]):
for x in range(0, shape[2], tile[2]):
yield z, y, x


def _inputs(study, volumes, metadata):
"""``(image [C, D, H, W], support [D, H, W], kind)`` per backbone input."""
if not modality_layout(metadata):
for sequence, (volume, support) in enumerate(volumes):
yield volume[None], support, sequence_kind(study["image_paths"][sequence])
return
for stack in acquisition_stacks(modality_groups(study["image_paths"])):
images = [None if i is None else volumes[i][0] for i in stack]
support = torch.zeros_like(next(volumes[i][1] for i in stack if i is not None))
for i in stack:
if i is not None:
support |= volumes[i][1]
yield stack_channels(images), support, -1


def _encode(backbone, crops, device, tile_batch):
"""Frozen prefix tokens of each crop ``[C, d, h, w]``, kept on the GPU."""
out = []
with torch.no_grad(), torch.autocast(device.type, dtype=torch.bfloat16):
for i in range(0, len(crops), tile_batch):
crop = torch.stack(crops[i:i + tile_batch]).to(device, non_blocking=True)
tokens, grid = backbone.prepare_tokens_with_masks(crop)
rope = tuple(r.to(device=tokens.device, dtype=tokens.dtype)
for r in backbone.rope_embed(D=grid[0], H=grid[1], W=grid[2]))
for block in backbone.blocks[:-1]:
tokens = block(tokens, rope)
out.extend((tokens[j:j + 1], grid) for j in range(len(tokens)))
return out


def cached_study_factory(study, volumes, backbone, adapter, metadata, device, tile_batch=4):
tile = tuple(metadata["config"]["training_crops"]["global"])
patch = tuple(metadata["config"]["backbone"]["patch_size"])
in_chans = int(metadata["config"]["backbone"].get("in_chans", 1))
tiles = []
for image, support, kind in _inputs(study, volumes, metadata):
if image.shape[0] != in_chans:
raise ValueError(f"Backbone takes {in_chans} channels, input has {image.shape[0]}")
image, valid = _padded(image, support, tile)
global_grid = tuple(n // p for n, p in zip(image.shape[-3:], patch))
for z, y, x in _tile_starts(image.shape[-3:], tile):
sl = (slice(z, z + tile[0]), slice(y, y + tile[1]), slice(x, x + tile[2]))
keep = F.max_pool3d(valid[sl][None, None], patch, patch).flatten() > 0
if not bool(keep.any()):
continue
start = tuple(a // p for a, p in zip((z, y, x), patch))
tiles.append((image[(slice(None),) + sl], keep, start, global_grid, kind))
prefix = []

def factory():
if not prefix:
prefix.extend(_encode(backbone, [t[0] for t in tiles], device, tile_batch))
for (_, keep, start, global_grid, kind), (tokens, grid) in zip(tiles, prefix):
keep = keep.to(device)
with torch.autocast(device.type, dtype=torch.bfloat16):
features = adapter(tokens, grid)[0, keep]
coordinates = normalized_positions(start, grid, global_grid, device)[keep]
yield features.float(), None, coordinates, kind

return factory


def cross_sequence_factory(study, volumes, backbone, adapter, metadata, device, tile_batch=4, recompute=True):
"""Per tile position: tokens ``[N, S, D]`` of the ``S`` sequences that cover it, ``valid [N, S]``,
sequence kinds ``[S]`` and coordinates ``[N, 3]`` of the ``N`` positions covered by any sequence.

``recompute`` checkpoints the trainable last block per sequence, so the backward pass holds
one sequence's activations at a time.
"""
if modality_layout(metadata):
raise ValueError("Cross-sequence attention takes one-channel sequence inputs")
tile = tuple(metadata["config"]["training_crops"]["global"])
patch = tuple(metadata["config"]["backbone"]["patch_size"])
padded = []
for sequence, (volume, support) in enumerate(volumes):
image, valid = _padded(volume[None], support, tile)
padded.append((image, valid, sequence_kind(study["image_paths"][sequence])))
shapes = {tuple(image.shape[-3:]) for image, _, _ in padded}
if len(shapes) != 1:
raise ValueError(f"Sequences of {study['study_uid']} are not on one grid: {sorted(shapes)}")
shape = shapes.pop()
global_grid = tuple(n // p for n, p in zip(shape, patch))
positions, crops = [], []
for z, y, x in _tile_starts(shape, tile):
sl = (slice(z, z + tile[0]), slice(y, y + tile[1]), slice(x, x + tile[2]))
members = []
for image, valid, kind in padded:
keep = F.max_pool3d(valid[sl][None, None], patch, patch).flatten() > 0
if bool(keep.any()):
members.append((len(crops), keep, kind))
crops.append(image[(slice(None),) + sl])
if members:
positions.append((tuple(a // p for a, p in zip((z, y, x), patch)), members))
prefix = []

def last_block(tokens, grid):
with torch.autocast(device.type, dtype=torch.bfloat16):
return adapter(tokens, grid)[0].float()

def factory():
if not prefix:
prefix.extend(_encode(backbone, crops, device, tile_batch))
for start, members in positions:
grid = prefix[members[0][0]][1]
outputs = []
for index, _, _ in members:
tokens = prefix[index][0]
if recompute and torch.is_grad_enabled():
outputs.append(checkpoint(last_block, tokens, grid, use_reentrant=False))
else:
outputs.append(last_block(tokens, grid))
tokens = torch.stack(outputs, 1)
valid = torch.stack([keep for _, keep, _ in members], 1).to(device)
covered = valid.any(1)
kinds = torch.tensor([kind for _, _, kind in members], device=device)
coordinates = normalized_positions(start, grid, global_grid, device)[covered]
yield tokens[covered], valid[covered], kinds, coordinates

return factory


class Deadline:
"""Collective stop decision so every rank checkpoints and exits at the same step."""

def __init__(self, device, margin_seconds=0.0):
epoch = float(os.environ.get("MIL_DEADLINE_EPOCH", "0") or 0)
self.at = epoch - margin_seconds if epoch else None
self.device = device

def remaining(self):
return float("inf") if self.at is None else self.at - time.time()

def reached(self, reserve_seconds=0.0):
flag = torch.tensor(float(self.remaining() < reserve_seconds), device=self.device)
if dist.is_initialized():
dist.all_reduce(flag, op=dist.ReduceOp.MAX)
return bool(flag.item())
Loading
Loading