Repository navigation
fix: keep fractional intensities of float valued input images - #641
Open
harshpatel wants to merge 2 commits into
Open
harshpatel wants to merge 2 commits into
harshpatel wants to merge 2 commits into
Conversation
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
force-pushed
the
fix/keep-float-intensities
branch
from
November 11, 2026 20:06
4afbd07 to
7e0a3a1
Compare
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 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
nnUNet_predict_imagecasts 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.gzwith-ta total_mron master (GPU). The image is rescaled linearly and saved as float32, and each run is compared with the run on the original image:--roi_subset liver spleen kidney_left kidney_right--fast(upsampled to 1.5 mm first, so that it gets resampled)With
-hothe [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_subseton 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
scl_slopereach nnU-Net with their intensities preserved (all three fail on master). int16 and float images with only whole numbers are still passed as int32.ruffand the existing no-GPU tests pass.The
totalseg_v3branch has the same two casts. I can open the same change there if useful.