Skip to content

Respect requested scale ranges below one - #144

Merged
FabianIsensee merged 1 commit into
MIC-DKFZ:masterfrom
jdotc1:fix/scale-ranges-137
Oct 29, 2026
Merged

FabianIsensee merged 1 commit into
MIC-DKFZ:masterfrom
jdotc1:fix/scale-ranges-137

Conversation

@jdotc1

@jdotc1 jdotc1 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #137.

Correct scale sampling when the entire requested interval is below one. For example, (0.7, 0.9) must not produce factors above 0.9. The change covers isotropic and per-axis scaling in both augment_spatial and augment_spatial_2.

Preserves the existing random branch selection and draw count, the split around one, and the identity branch for intervals ending at one. Invalid or reversed intervals are outside this correction.

Validation recorded for the approved revision:

  • The final four-file suite passes 40 tests and 146 subcases.
  • The original implementation reproduces 59 failing bound checks.
  • An independent 224-case seeded comparison preserves all complete random states and all 160 unaffected outputs and random-call traces; all 64 affected cases change as intended.
  • Regression coverage includes both functions, 2D/3D, isotropic and per-axis behavior, isotropic fallback, constant bounds, random-state preservation, and the upper-bound-of-one identity branch.

Both original and patched suites emit the same existing warning in the empty-input segmentation-resize test.

@jdotc1

jdotc1 commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for considering this contribution. When you have a chance, I would appreciate a review of this PR. I’m happy to clarify anything that would be helpful, and I appreciate your time.

@FabianIsensee
FabianIsensee merged commit fbd5704 into MIC-DKFZ:master Oct 29, 2026
@FabianIsensee

Copy link
Copy Markdown
Member

thanks!

FabianIsensee added a commit that referenced this pull request Oct 29, 2026
augment_contrast and augment_gamma had the sampling that PR #144 fixed for the spatial scale (issue #137): half of
the draws came from (range[0], 1), the other half from uniform(max(range[0], 1), range[1]). For a range entirely
below one, such as (0.7, 0.9), the first exceeded the upper bound and the second ran from 1 down to range[1], so no
draw stayed inside the range. Both halves now draw from the range when it ends below one.

The number of draws is unchanged, and for ranges that reach one (all of nnU-Net's) the results are identical:
outputs and RNG state compared bit for bit on 200 seeds, 7 such ranges, both per_channel settings (8400 arrays);
the same comparison finds the change for (0.7, 0.9). The new range test fails on the old code; the draw count test
passes on both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
FabianIsensee added a commit that referenced this pull request Oct 29, 2026
The scale, contrast and gamma sampling flips a coin between the part of the range below one and the part above it,
so that (0.5, 2) shrinks as often as it enlarges. For a range ending at one, such as (0.7, 1), the upper part is the
single value 1, so half of all draws were exactly 1, i.e. no augmentation. Such a range is now sampled uniformly, as
ranges ending below one already are.

The sampling was copied eight times (augment_spatial, augment_spatial_2, augment_contrast and augment_gamma, each
with and without per-axis or per-channel factors). It is now sample_factor_around_one in augmentations/utils.py.

The number of draws is unchanged. Outputs and RNG state are bit-identical to the previous commit for every tested
range that does not end at one: (0.75, 1.25), (0.7, 1.5), (0.5, 2), (1.1, 1.5), (1, 1), (1, 2), (0.85, 1.25) and
(0.7, 0.9), over contrast, gamma and both spatial functions in 2D and 3D (9720 arrays). (0.7, 1) differs as intended.
PR #144's test that pinned the old behaviour for (0.7, 1) now requires a draw below one; it and the extended range
tests fail on the old code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
FabianIsensee added a commit that referenced this pull request Oct 29, 2026
Augmenters (MultiThreadedAugmenter, NonDetMultiThreadedAugmenter):
- With seeds=None, all forked workers drew the same torch random numbers, so transforms that draw from torch (all of
  batchgeneratorsv2, hence nnU-Net's training augmentation) repeated the same augmentation sequence in every worker.
  Workers now reseed torch from the OS.
- With seeds given, workers also seed torch (from a seed derived from the given one) and random (PR #139). numpy
  integer seeds work.
- Workers restore the default SIGTERM handler (PR #127) and stop when their parent is killed, also under fork when
  the parent forked other processes after them.
- next() raises instead of waiting forever when the results thread fails; shutting down with torch tensor batches no
  longer prints tracebacks from the results thread.
- Logging goes through named module loggers instead of the root logger (PR #140).

Augmentations:
- Scale, contrast and gamma ranges that end at or below one are sampled from the range (PR #144, issue #137).
- Random crops can draw the last valid origin (PR #143, issue #142, and the random_crop_* helpers in utils.py).
- ChannelTranslation keeps the batch dimension (PR #145) and the channel order, can shift by +max_shift and accepts
  max_shift 0.
- ResizeTransform and ZoomTransform return their results for list and tuple inputs (PR #146).

Packaging: unittest2 and future are no longer required, numpy>=1.17; requirements.txt and the Makefile are gone.
The CIFAR-10 example runs again.

Seeded runs draw differently for random crops, channel translation, torch-based transforms under the augmenters and
ranges ending at one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

code segment about scale

2 participants