Skip to content

RGB png dataset bechmarking #1948

Closed
Closed

Description

@HamzaFarooq013

Hi,
I am trying to bechmark nnUnet on my own 2D ( RGB .png) dataset but got errors in (verify_dataset_integrity). Can you please share some configuration setting for normal .png datset for binary class for benchmarking on nnUnet. (Thanks in advance)

my settings for Json:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
overwrite_json_file = True #make it True if you want to overwrite the dataset.json file in Dataset_folder
json_file_exist = False

if os.path.exists(os.path.join(task_folder_name,'dataset.json')):
print('dataset.json already exist!')
json_file_exist = True

if json_file_exist==False or overwrite_json_file:

json_dict = OrderedDict()
json_dict['name'] = dataset_name
json_dict['description'] = "multi_lables_uniclass"
json_dict['tensorImageSize'] = "3D"
json_dict['reference'] = "ABC"
json_dict['licence'] = "ABC"
json_dict['release'] = "0.1"

#you may mention more than one modality
json_dict['channel_names'] = {
   "0": "R",
   "1": "G",
   "3": "B"
}

# set expected file ending
json_dict["file_ending"] = ".png"

#label names should be mentioned for all the labels in the dataset
json_dict['labels'] = {
    "background": 0,
    "white matter": 1
}

train_ids = os.listdir(train_label_dir)
test_ids = os.listdir(test_dir)
json_dict['numTraining'] = len(train_ids)
json_dict['numTest'] = len(test_ids)


with open(os.path.join(task_folder_name,"dataset.json"), 'w') as f:
    json.dump(json_dict, f, indent=4, sort_keys=True)

if os.path.exists(os.path.join(task_folder_name,'dataset.json')):
    if json_file_exist==False:
        print('dataset.json created!')
    else:
        print('dataset.json overwritten!')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

!nnUNetv2_plan_and_preprocess -d 876 -c 2d_fullres --verify_dataset_integrity

Error: Unexpected number of modalities.
Expected: 3.
Got: 1.

Activity

  1. changed the title [-]2D RGB png dataset bechmarking[/-] [+] RGB png dataset bechmarking[/+] on Feb 14, 2024
  2. HamzaFarooq013 commented on Feb 14, 2024

    @HamzaFarooq013
    Author

    I have kind of solved that issue but why this shape error is happend?

    As data is verified but still dim error

    Fingerprint extraction...
    Dataset876_Hamzabenchmark
    Using <class 'nnunetv2.imageio.natural_image_reager_writer.NaturalImage2DIO'> as reader/writer

    ####################
    verify_dataset_integrity Done.
    If you didn't see any error messages then your dataset is most likely OK!
    ####################

    Experiment planning...
    2D U-Net configuration:
    {'data_identifier': 'nnUNetPlans_2d', 'preprocessor_name': 'DefaultPreprocessor', 'batch_size': 49, 'patch_size': array([256, 256]), 'median_image_size_in_voxels': array([256., 256.]), 'spacing': array([1., 1.]), 'normalization_schemes': ['ZScoreNormalization'], 'use_mask_for_norm': [False], 'UNet_class_name': 'PlainConvUNet', 'UNet_base_num_features': 32, 'n_conv_per_stage_encoder': (2, 2, 2, 2, 2, 2, 2), 'n_conv_per_stage_decoder': (2, 2, 2, 2, 2, 2), 'num_pool_per_axis': [6, 6], 'pool_op_kernel_sizes': [[1, 1], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2]], 'conv_kernel_sizes': [[3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3]], 'unet_max_num_features': 512, 'resampling_fn_data': 'resample_data_or_seg_to_shape', 'resampling_fn_seg': 'resample_data_or_seg_to_shape', 'resampling_fn_data_kwargs': {'is_seg': False, 'order': 3, 'order_z': 0, 'force_separate_z': None}, 'resampling_fn_seg_kwargs': {'is_seg': True, 'order': 1, 'order_z': 0, 'force_separate_z': None}, 'resampling_fn_probabilities': 'resample_data_or_seg_to_shape', 'resampling_fn_probabilities_kwargs': {'is_seg': False, 'order': 1, 'order_z': 0, 'force_separate_z': None}, 'batch_dice': True}

    Using <class 'nnunetv2.imageio.natural_image_reager_writer.NaturalImage2DIO'> as reader/writer
    Plans were saved to d:\Hamza Farooq\T_Net\T_Net\Data_nnUnet\nnUNet_preprocessed\Dataset876_Hamzabenchmark\nnUNetPlans.json
    Preprocessing...
    Preprocessing dataset Dataset876_Hamzabenchmark
    Configuration: 2d...
    Configuration: 3d_fullres...
    INFO: Configuration 3d_fullres not found in plans file nnUNetPlans.json of dataset Dataset876_Hamzabenchmark. Skipping.
    Configuration: 3d_lowres...
    INFO: Configuration 3d_lowres not found in plans file nnUNetPlans.json of dataset Dataset876_Hamzabenchmark. Skipping.

    #########################################################################

    Exception in background worker 0:
    could not broadcast input array from shape (3,301,301) into shape (1,301,301)
    using pin_memory on device 0
    Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
    Traceback (most recent call last):
    File "d:\Hamza Farooq.venv\lib\site-packages\batchgenerators\dataloading\nondet_multi_threaded_augmenter.py", line 53, in producer
    item = next(data_loader)
    File "d:\Hamza Farooq.venv\lib\site-packages\batchgenerators\dataloading\data_loader.py", line 126, in next
    return self.generate_train_batch()
    File "d:\Hamza Farooq.venv\lib\site-packages\nnunetv2\training\dataloading\data_loader_2d.py", line 92, in generate_train_batch
    seg_all[j] = np.pad(seg, ((0, 0), *padding), 'constant', constant_values=-1)
    ValueError: could not broadcast input array from shape (3,301,301) into shape (1,301,301)

  3. Ismatse commented on Feb 19, 2024

    @Ismatse

    Hi, @HamzaFarooq013
    Have you been able to solve that problem? I have kind of the same shape issue while training and I have no idea why.

  4. HamzaFarooq013 commented on Feb 20, 2024

    @HamzaFarooq013
    Author
  5. assigned and unassigned on May 8, 2026
  6. Karol-G commented on May 18, 2026

    @Karol-G
    Contributor

    Hey,

    Is this issue still relevant? Otherwise, I will close it in 14 days.

    Best,
    Karol

  7. HamzaFarooq013 commented on May 18, 2026

    @HamzaFarooq013
    Author
  8. Karol-G commented on May 18, 2026

    @Karol-G
    Contributor

    @claude lets get some feedback from you

  9. github-actions commented on May 18, 2026

    @github-actions
    Contributor

    🤖 nnU-Net Assistant

    Thanks for confirming this is still relevant, @HamzaFarooq013. After analyzing your traceback and the relevant source, the root cause is clear.

    Root cause

    The broadcast error

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

    is raised when the data loader tries to pad the segmentation. The (3, 301, 301) shape means your label PNG is being read as an RGB image (3 color channels) rather than a single‑channel grayscale label map.

    Walking through the read path in nnunetv2/imageio/natural_image_reader_writer.py:

    • NaturalImage2DIO.read_seg(seg_fname) simply calls read_images((seg_fname,)) (line 61–62).
    • Inside read_images, if the PNG has ndim == 3, it is treated as RGB(A) and transposed to (c, 1, H, W) with c=3 or c=4 (lines 40–47).
    • A grayscale image (ndim == 2) becomes (1, 1, H, W) (lines 48–50).

    So an RGB label file ends up with 3 channels in the segmentation array. Downstream, verify_dataset_integrity only checks that the seg's spatial shape matches the image (segmentation.shape[1:] vs images.shape[1:] — verify_dataset_integrity.py:62–68) and that images have the expected number of modalities — it never checks that the segmentation itself is single-channel. That is why your verify_dataset_integrity passes but training later crashes during batch assembly.

    How this almost certainly happened

    Your label PNGs were saved as RGB (typical when masks are exported from tools like Photoshop, GIMP, matplotlib, or after viewing a single-channel mask as a heatmap before saving). Even if the three channels look identical, the file is encoded with 3 channels and skimage reads it as RGB.

    Fix (on your side, immediate)

    Re-save every file in labelsTr/ (and labelsTs/ if applicable) as a single-channel PNG with integer class indices {0, 1, ..., N} — not RGB and not a color-mapped image. A quick batch script:

    import os
    import numpy as np
    from PIL import Image
    
    label_dir = r"D:\Hamza Farooq\T_Net\T_Net\Data_nnUnet\nnUNet_raw\Dataset876_Hamzabenchmark\labelsTr"
    for f in os.listdir(label_dir):
        if not f.lower().endswith(".png"):
            continue
        p = os.path.join(label_dir, f)
        arr = np.array(Image.open(p))
        if arr.ndim == 3:
            # if the three channels are identical (e.g. masks erroneously saved as RGB),
            # collapse to one channel. Otherwise you need to map colors to class IDs yourself.
            if arr.shape[-1] >= 3 and np.array_equal(arr[..., 0], arr[..., 1]) and np.array_equal(arr[..., 0], arr[..., 2]):
                arr = arr[..., 0]
            else:
                raise RuntimeError(f"{f}: channels differ — you must map colors to class indices manually")
        Image.fromarray(arr.astype(np.uint8), mode="L").save(p)

    After resaving, delete nnUNet_preprocessed/Dataset876_Hamzabenchmark/ and rerun nnUNetv2_plan_and_preprocess so the preprocessed .npy/.b2nd files are regenerated from the corrected labels.

    Proposed library-side fix

    This is a recurring footgun for users of NaturalImage2DIO. The cleanest fix is to make read_seg fail fast (or auto-collapse) when given a multi-channel label file, and to mirror the check in verify_dataset_integrity:

    1. In nnunetv2/imageio/natural_image_reader_writer.py::NaturalImage2DIO.read_seg, raise a clear error if the loaded segmentation has more than one channel, with a message like "Segmentation file X has C channels; nnU-Net expects single-channel integer label maps. Re-save as grayscale."
    2. In nnunetv2/experiment_planning/verify_dataset_integrity.py::check_cases, after rw.read_seg(label_file), assert segmentation.shape[0] == 1 and print a similarly clear error if not — so the failure happens during verify_dataset_integrity instead of mid-training.

    @Karol-G — the diagnosis is solid and the fix above is localized (two files, ~10 lines). If you agree with the approach, please apply ready-for-fix to trigger the fix‑PR workflow. Otherwise happy to refine.

  10. Karol-G commented on May 18, 2026

    @Karol-G
    Contributor

    @HamzaFarooq013 Is claude right? From the log it also seems to me that your segmentations/labels have 3 channels. They should only have one however. If this is the case then this is an easy fix :)

  11. added
    has-fix-prMaintainer or agent has created a PR for this issue
    and removed on May 18, 2026
  12. Karol-G commented on Jun 22, 2026

    @Karol-G
    Contributor

    I assume this is fixed. Will close the issue now due to inactivity. Feel free to open it again if the issue still persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghas-fix-prMaintainer or agent has created a PR for this issueneeds-maintainerRequires maintainer attention

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions