Skip to content

fix: keep fractional intensities of float valued input images - #641

Open
harshpatel wants to merge 2 commits into
wasserth:masterfrom
harshpatel:fix/keep-float-intensities
Open

harshpatel wants to merge 2 commits into
wasserth:masterfrom
harshpatel:fix/keep-float-intensities

Conversation

@harshpatel

@harshpatel harshpatel commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem

nnUNet_predict_image casts the cropped and resampled input image to int32 before it is passed to nnU-Net. That loses nothing on CT in Hounsfield units, but MR images are often float valued with a small range (normalised to [0, 1], bias corrected, quantitative maps like ADC). Such an image keeps a few grey levels or none, and the result is an empty or degraded segmentation without any warning.

Measured on tests/reference_files/example_mr_sm.nii.gz with -ta total_mr on master (GPU). The image is rescaled linearly and saved as float32, and each run is compared with the run on the original image:

input before after
rescaled to [0, 1] 0 of 23 structures found 23 of 23, mean Dice 0.974
rescaled to [0, 1], --roi_subset liver spleen kidney_left kidney_right 0 of 4 4 of 4, mean Dice 0.9997
rescaled to [0, 1], --fast (upsampled to 1.5 mm first, so that it gets resampled) 0 of 22 22 of 22, mean Dice 0.937
rescaled to [0, 3] 23 of 23, mean Dice 0.728 23 of 23, mean Dice 0.974

With -ho the [0, 1] image already segments well today (22 of 22 structures, mean Dice 0.985 against the original image with -ho), because that path skips the resampling and its cast. So the models handle such images, and only the cast is in the way. The 0.985 also shows how far a linear rescale alone moves the result on this small image.

This is probably what happened in #455, where total_mr --roi_subset on a [0, 1] image returned mostly empty masks.

Fix

The working copy keeps float32 when the image has fractional values and stays int32 otherwise, in both places that cast it (crop and resampling). The check is a few lines where the image is loaded: one slice-by-slice pass over the image takes about 0.3 s for 512 x 512 x 900.

Whole-number images are passed as int32 as before. On the original int16 test image, the output before and after the change differs in 4 voxels, the same as two runs of the unchanged code on the same GPU.

Checks

  • The GPU numbers above were measured with the first commit. The second commit only makes the code smaller: the same images get float32 as in the first commit.
  • Without a GPU, with prediction mocked: float [0, 1], float [0, 1] with a crop, and int16 with a fractional scl_slope reach nnU-Net with their intensities preserved (all three fail on master). int16 and float images with only whole numbers are still passed as int32.
  • ruff and the existing no-GPU tests pass.

The totalseg_v3 branch has the same two casts. I can open the same change there if useful.

nnUNet_predict_image cast the cropped and resampled input image to int32.
That loses nothing on CT in Hounsfield units, but MR images are often float
valued with a small range (normalised to [0, 1], bias corrected, quantitative
maps). A [0, 1] image became all zeros, and total_mr returned an empty
segmentation without a warning (see wasserth#455).

The working copy now keeps float32 when the image has fractional values.
Integer valued images are passed as int32 as before.
@harshpatel
harshpatel force-pushed the fix/keep-float-intensities branch from 4afbd07 to 7e0a3a1 Compare November 11, 2026 20:06
@harshpatel

Copy link
Copy Markdown
Contributor Author

Thank you for merging the other three PRs and for the float32 change in the statistics.

I simplified this PR the same way: removed the test file and inlined the helper function where the image is loaded. The diff is now +9 -3 in nnunet.py.

Images with only whole-number values (CT in Hounsfield units, most MR) are still passed as int32, so their results do not change. Only images with fractional intensities stay float32. The check takes about 0.3 s on a 512 x 512 x 900 image.

If you prefer to always use float32 here, as in the statistics, I can switch to that two-line change. I kept int32 because resampled CT values would otherwise no longer be truncated, so CT results could differ slightly from today.

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