Skip to content

feat: z-score the SynthSeg synthesis on request with zscore_output - #146

Open
Hendrik-code wants to merge 1 commit into
mainfrom
hm/synthseg-zscore-output
Open

Hendrik-code wants to merge 1 commit into
mainfrom
hm/synthseg-zscore-output

Conversation

@Hendrik-code

Copy link
Copy Markdown
Collaborator

What

RandomSynthSegGPU takes zscore_output (default False). When set, each synthesised sample is standardised over its patch before it replaces the image:

"RandomSynthSegGPU": { "p": 1.0, "zscore_output": true, ... }

Why

With SynthSeg's default normalise=True the synthesis is min-max scaled to [0, 1], while the real patches beside it are z-scored. Measured on the 128³ training patches of four SpineGAN datasets (two cases each, five draws): the image std after SynthSeg is 0.16-0.20, against 0.86-0.91 for the Dixon in-phase and fat inputs.

Every later transform that works in absolute units then acts on the synthetic samples at several times its strength on the real ones. The visible case is RandomGaussianNoiseGPU, whose std is drawn from U(0, std): at the paper config's std: 1.0, the noise std exceeds the image's in about 80% of firings on a SynthSeg sample. In the measured sample, 16 of 20 noise firings after SynthSeg left the label boundaries indistinguishable from the background gradient (mean |∇I| on label boundaries ÷ mean |∇I| over the slice fell from 1.8-2.8 to 1.0-1.3). That affects the published SynthSeg (post) configuration (transform_params_paper-synthseg.json), where noise is on at p: 0.1 and SynthSeg at p: 1.0.

Behaviour

  • Draws no random numbers: the same seed gives the same synthesis, up to the rescale (tested).
  • Default False: shipped configs and their seeded outputs are unchanged.
  • all_augmentations.json regenerated (smauglab matrix --write).

Tests

unit_tests/test_synthseg_zscore_output.py:

  • the default still returns [0, 1];
  • with the option set, every sample has mean 0 and std 1;
  • the output is exactly the standardised default synthesis;
  • the registry accepts the key.

Full suite: 477 passed, 13 skipped. mypy smauglab/ clean, pre-commit passes.

🤖 Generated with Claude Code

With SynthSeg's default `normalise=True` the synthesis is min-max scaled to
[0, 1]. The real training patches it is mixed with are z-scored, so the
synthetic samples reach every later transform at about a fifth of the spread
of a real one: measured 0.16-0.20 against 0.86-0.91 on two Dixon datasets.
Transforms that work in absolute units then act on them at several times
their strength on real samples. RandomGaussianNoiseGPU draws its std from
U(0, std), and with the paper config's std 1.0 the synthesis was buried under
noise in 16 of 20 firings in a measured sample (label boundaries no longer
stood out from the background gradient).

`zscore_output=True` standardises each synthesised sample over its patch
before it replaces the image. It draws no random numbers, so the same seed
gives the same synthesis up to that rescale. Default False keeps the [0, 1]
output and every shipped config's seeded result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Hendrik-code

Copy link
Copy Markdown
Collaborator Author

Results: Dataset803 and Dataset804, fold 0

Z-scoring the synthesis improves the SynthSeg config on Dataset804 (+0.019 in-domain, +0.014 out-of-domain) and changes nothing measurable on Dataset803. It does not bring SynthSeg up to the paper config: in-domain stays about 0.03 below it on both datasets.

Setup. nnUNetTrainerDAExtGPU, 3d_fullres, fold 0, 1000 epochs, one run per arm. Dataset803 is trained on SpineGAN Dixon in-phase MRI and Dataset804 on SpineGAN CT; out-of-domain is every other modality and sequence in the test split (803: 1077 scans out, 24 in; 804: 462 out, 639 in). Metric is global binary Dice, averaged over vertebra, IVD and spinal canal per scan. Verdicts are TOST equivalence at ±0.01 (α = 0.05) on paired per-scan deltas. For scale, three fold-0 runs of the paper config span 0.0043 (803) and 0.0079 (804) out-of-domain in cohort means.

arm config code GPU
z-scored transform_params_paper-synthseg.json + "zscore_output": true 42e2823 (this branch) A40
paper-synthseg transform_params_paper-synthseg.json 41ac414b (older)
paper (replicate) transform_params_one-sequence-to-segment-them-all.json 7169464 A40
paper (published run) same b0110743 A40

The two SynthSeg arms differ only in zscore_output. paper-synthseg is on an older checkout with no fold-0 replicate, but nothing between it and this branch changes SynthSeg in this trainer.

Z-scored minus paper-synthseg, paired mean and 95% CI:

in-domain out-of-domain
Dataset803 −0.002 [−0.004, +0.001] +0.007 [+0.006, +0.008]
Dataset804 +0.019 [+0.018, +0.020] +0.014 [+0.013, +0.016]

Dataset803 is equivalent on both scopes (TOST p < 0.001). Dataset804 is better on both: the whole interval clears +0.01. Three Dataset803 runs of the SynthSeg config (folds 0 to 2) spread up to 0.007 in cohort means, so the 804 gain is beyond run-to-run noise.

Where the 804 gain comes from: the CT and SPIDER cohorts (myelom-ct +0.019, spider-T1w +0.020, spider-T2w +0.015, spinegan-ct +0.009). The three Dixon cohorts (24 scans each) move −0.007 to +0.007. Out-of-domain, all three classes gain: vertebra +0.016, IVD +0.017, spinal canal +0.010.

Against the paper config, paired mean:

803 in 803 out 804 in 804 out
z-scored − paper (replicate) −0.034 −0.008 −0.027 −0.009
z-scored − paper (published) −0.034 −0.008 −0.027 −0.006
paper-synthseg − paper (replicate) −0.032 −0.015 −0.046 −0.024

Cohort means, unweighted across cohorts:

arm 803 in 803 out 804 in 804 out
paper (replicate) 0.901 0.844 0.903 0.781
paper (published) 0.901 0.844 0.903 0.777
paper-synthseg 0.869 0.830 0.858 0.773
z-scored 0.867 0.832 0.872 0.781

In training. About 74 s/epoch on an A40, no non-finite losses in either run. In the pre-flight, SynthSeg fired in 20 of 20 batches.

Bottom line: worth merging, and worth turning on in the SynthSeg configs. It removes a scale mismatch at no cost and gains on the CT-trained model. On its own, it does not make SynthSeg competitive with the paper config in-domain.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant