Skip to content

Include the last valid random crop origin - #143

Merged
FabianIsensee merged 1 commit into
MIC-DKFZ:masterfrom
ktz03:fix/random-crop-upper-bound
Oct 29, 2026
Merged

FabianIsensee merged 1 commit into
MIC-DKFZ:masterfrom
ktz03:fix/random-crop-upper-bound

Conversation

@ktz03

@ktz03 ktz03 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #142.

Include the last valid origin when sampling a random crop. NumPy's upper bound is exclusive, so the previous bound prevented crops from reaching the final row, column or slice at the requested margin.

Add deterministic 2D/3D regressions for zero, positive and negative margins, checking image and segmentation alignment. Keep the existing single-origin and oversized-crop fallbacks.

Validation on Windows, Python 3.12.14 and NumPy 2.5.4:

  • pytest tests --ignore=tests/test_DataLoader.py --ignore=tests/test_multithreaded_augmenter.py -q: original code with the same tests gives 6 failures and 81 passes; the fix gives 87 passes. Both runs report the same existing scikit-image empty-input warning.
  • Public random_crop calls reproduce the missing boundary in all six 2D/3D and margin cases; the fix reaches each legal endpoint and keeps image/segmentation crops aligned.
  • Source and wheel builds pass. The two multiprocessing test modules, Linux, real datasets and model training were not tested.

Random draws can change for a fixed seed because the sampling interval now includes the previously omitted endpoint.

@FabianIsensee
FabianIsensee merged commit e62b12d 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
…ils.py

random_crop_2D_image, random_crop_3D_image and their _batched variants drew each origin with
np.random.randint(0, size - crop_size), whose upper bound is exclusive, so a crop could never end at the image's
last voxel. This is the bug fixed for random_crop in PR #143 (issue #142). Seeded runs crop differently.

The new tests fail on the old code for all four functions.

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.

random_crop excludes the last valid spatial origin

2 participants