refactor: extract the nnU-Net transform tail shared by five trainers - #64
Open
Hendrik-code wants to merge 1 commit into
Open
refactor: extract the nnU-Net transform tail shared by five trainers#64Hendrik-code wants to merge 1 commit into
Hendrik-code wants to merge 1 commit into
Conversation
Five get_training_transforms methods across nnUNetTrainerDAExt.py and nnUNetTrainerTest.py ended with a character-identical copy of the same nnU-Net sequence: intensity masking, the -1 label removal, the two cascade transforms, region conversion and deep-supervision downsampling. That is trainers/utils.py::nnunet_tail_transforms now. -255 lines, +134. The four full copies differed in exactly one thing: whether the DownsampleSegForDSTransform block was live or commented out. The GPU trainers carry it commented, because with GPU augmentations the mask is still being deformed after this point and the multi-scale targets have to be built from the augmented mask in train_step. That is expressed by passing deep_supervision_scales=None rather than by a comment. get_validation_transforms is deliberately left alone. Its cascade branch adds only MoveSegAsOneHotToDataTransform, without the two RandomTransform wrappers, so it is a different sequence rather than a sixth copy of this one. Forcing it through the helper would need a flag that changes that branch, which costs more than it saves. Verified by enumerating the transform list every trainer builds across 160 argument combinations -- dummy 2D on/off, mask, cascade, regions, deep supervision -- and comparing the type and repr of each entry before and after. All 160 are identical once function memory addresses are normalised out. That check is local-only and not repeatable in CI: nnunetv2 is an optional extra and `pip install -e ".[dev]"` does not pull it, so nothing under trainers/ is imported by the test suite at all. It is the reason this is a separate commit from the gpu/contrast.py extraction, which CI does cover. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Stacked on #63.
Five
get_training_transformsmethods acrossnnUNetTrainerDAExt.pyandnnUNetTrainerTest.pyended with a character-identical copy of the same nnU-Net sequence — intensity masking, the-1label removal, the two cascade transforms, region conversion, deep-supervision downsampling. That'strainers/utils.py::nnunet_tail_transformsnow. −255 / +134.The one thing the copies differed in
Whether the
DownsampleSegForDSTransformblock was live or commented out:The GPU trainers carry it commented because with GPU augmentations the mask is still being deformed after this point, so the multi-scale targets have to be built from the augmented mask in
train_step. That's now expressed by passingdeep_supervision_scales=Nonerather than by a comment.What is deliberately left alone
get_validation_transformsis not folded in. Its cascade branch adds onlyMoveSegAsOneHotToDataTransform, without the twoRandomTransformwrappers — so it's a different sequence, not a sixth copy. Forcing it through the helper would need a flag that changes that branch, costing more than it saves.How I know it changes nothing
I enumerated the transform list every trainer builds across 160 argument combinations — dummy-2D on/off ×
use_mask_for_norm× cascade × regions × deep supervision, for all five trainers — and compared the type andreprof every entry before and after:(The only raw differences were
<function ... at 0x...>addresses.)That check is local-only and cannot run in CI.
nnunetv2is an optional extra andpip install -e ".[dev]"does not pull it, so nothing undertrainers/is imported by the test suite at all — CI will go green here without exercising a single line of this diff. That is exactly why this is a separate PR from #63, which CI does cover.Full suite: 105 passed, 200 subtests (unchanged — none of them touch the trainers).
ruffandmypyclean.