Skip to content

Preserve the batch dimension in ChannelTranslation - #145

Merged
FabianIsensee merged 1 commit into
MIC-DKFZ:masterfrom
Don-Yin:fix-channel-translation-batch
Oct 29, 2026
Merged

FabianIsensee merged 1 commit into
MIC-DKFZ:masterfrom
Don-Yin:fix-channel-translation-batch

Conversation

@Don-Yin

@Don-Yin Don-Yin commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

augment_channel_translation() returns a complete array, but ChannelTranslation selects [0]. This drops the batch dimension and every sample after the first: a (2, 3, 5, 5) input becomes (3, 5, 5); 3D batches lose the same dimension.

I changed the wrapper to keep the returned array and added seeded 2D/3D regression tests against the low-level function, including a custom data key and pass-through segmentation/labels.

Evidence

Measured output dimensions and retained samples before and after the fix

Measured on synthetic two-sample batches. The right panel divides the returned element count by the input size per sample; the fixed outputs also match the full low-level results element-for-element.

Validation

Python 3.12.0, NumPy 2.4.2:

  • python -m pytest tests/test_channel_translation.py -q -p no:cacheprovider: 2 failed before, 2 passed after.
  • python -m unittest tests.test_channel_translation -v: 2 passed.
  • python -m pytest tests --ignore=tests/test_DataLoader.py --ignore=tests/test_multithreaded_augmenter.py -q -p no:cacheprovider: 79 passed.
  • git diff --check: passed.

The two multiprocessing modules were not run. The suite reports one existing scikit-image divide-by-zero warning for the empty-input segmentation resize test, also present in the unchanged spatial suite.

Keep the full array returned by augment_channel_translation instead of
selecting the first sample. Add seeded 2D and 3D regression tests for
shape, values, custom data keys, and unchanged pass-through fields.

Signed-off-by: Don Yin <[email protected]>
@FabianIsensee
FabianIsensee merged commit 68f06fc 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
Both __call__ methods were the same 35 lines apart from the function they applied to each sample. That loop and
the choice of return container are now _augment_per_sample. ChannelTranslation stores the result directly instead
of through ret_val, a name that in this file otherwise means a (data, seg) tuple and led to the [0] fixed in
PR #145. No change in behaviour; tests/test_resize_transforms.py covers ndarray, list and tuple batches, with and
without seg and concatenate_list.

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.

2 participants