Skip to content

fix: address issue #1948#3036

Open
github-actions[bot] wants to merge 1 commit into
masterfrom
fix/issue-1948-26030392909-1
Open

fix: address issue #1948#3036
github-actions[bot] wants to merge 1 commit into
masterfrom
fix/issue-1948-26030392909-1

Conversation

@ghost
Copy link
Copy Markdown
Contributor

@ghost ghost commented May 18, 2026

Fixes #1948

What was broken

When NaturalImage2DIO was used for 2D PNG datasets, label files saved as
RGB (3-channel) PNGs were silently loaded as (3, 1, H, W) instead of
(1, 1, H, W). This passed verify_dataset_integrity (which only
compared the spatial dimensions of images and segmentations and never
checked that the segmentation itself was single-channel) and only
exploded later during training inside the data loader with the cryptic
error:

could not broadcast input array from shape (3,301,301) into shape (1,301,301)

raised at nnunetv2/training/dataloading/data_loader_2d.py:92 when the
seg array was padded into the pre-allocated single-channel seg_all
buffer.

What this fixes

Two minimal, targeted changes make the failure happen early with a clear
message instead of crashing mid-training:

  1. nnunetv2/imageio/natural_image_reader_writer.pyNaturalImage2DIO.read_seg
    now checks the channel dimension of the loaded segmentation and
    raises RuntimeError with a message that explains the cause and the
    fix (re-save labels as single-channel grayscale images with integer
    class indices) when a multi-channel label file is encountered.

  2. nnunetv2/experiment_planning/verify_dataset_integrity.pycheck_cases
    additionally asserts segmentation.shape[0] == 1 and prints a clear
    error pointing at the offending label file, so verify_dataset_integrity
    now actually catches this dataset issue instead of giving a false
    "Done" and letting training crash later.

No behavioral change for users with correctly formatted single-channel
labels.

How to verify

  1. Build a tiny 2D PNG dataset of the kind that triggers the bug
    (any 2D dataset using NaturalImage2DIO) and save one of the label
    PNGs as RGB (e.g. with Pillow's mode="RGB").

  2. Run nnUNetv2_plan_and_preprocess -d <ID> --verify_dataset_integrity.
    With the fix, verify_dataset_integrity fails immediately with a
    message like:

    Error: Segmentation must be a single-channel integer label map but has 3 channels. ...
    Seg file: <path>
    

    instead of printing verify_dataset_integrity Done. and proceeding
    into preprocessing/training before crashing inside the data loader.

  3. Re-save the label as a single-channel mode="L" PNG and confirm the
    pipeline runs through preprocessing as before — no regression for
    well-formed datasets.

@FabianIsensee FabianIsensee self-assigned this May 18, 2026
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.

RGB png dataset bechmarking

1 participant