You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!')
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)
Hello,
I am not able to solve this issue. but Greyscale channel will help to solve
this problem for data verification but In training that cause problem of
model input dim.
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:
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:
importosimportnumpyasnpfromPILimportImagelabel_dir=r"D:\Hamza Farooq\T_Net\T_Net\Data_nnUnet\nnUNet_raw\Dataset876_Hamzabenchmark\labelsTr"forfinos.listdir(label_dir):
ifnotf.lower().endswith(".png"):
continuep=os.path.join(label_dir, f)
arr=np.array(Image.open(p))
ifarr.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.ifarr.shape[-1] >=3andnp.array_equal(arr[..., 0], arr[..., 1]) andnp.array_equal(arr[..., 0], arr[..., 2]):
arr=arr[..., 0]
else:
raiseRuntimeError(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:
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."
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.
@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 :)
Activity
[-]2D RGB png dataset bechmarking[/-][+] RGB png dataset bechmarking[/+]HamzaFarooq013 commentedon Feb 14, 2024
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)
Ismatse commentedon Feb 19, 2024
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.
HamzaFarooq013 commentedon Feb 20, 2024
Karol-G commentedon May 18, 2026
Hey,
Is this issue still relevant? Otherwise, I will close it in 14 days.
Best,
Karol
HamzaFarooq013 commentedon May 18, 2026
Karol-G commentedon May 18, 2026
@claude lets get some feedback from you
github-actions commentedon May 18, 2026
🤖 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
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 callsread_images((seg_fname,))(line 61–62).read_images, if the PNG hasndim == 3, it is treated as RGB(A) and transposed to(c, 1, H, W)withc=3orc=4(lines 40–47).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_integrityonly checks that the seg's spatial shape matches the image (segmentation.shape[1:]vsimages.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 yourverify_dataset_integritypasses 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/(andlabelsTs/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:After resaving, delete
nnUNet_preprocessed/Dataset876_Hamzabenchmark/and rerunnnUNetv2_plan_and_preprocessso the preprocessed.npy/.b2ndfiles are regenerated from the corrected labels.Proposed library-side fix
This is a recurring footgun for users of
NaturalImage2DIO. The cleanest fix is to makeread_segfail fast (or auto-collapse) when given a multi-channel label file, and to mirror the check inverify_dataset_integrity: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."nnunetv2/experiment_planning/verify_dataset_integrity.py::check_cases, afterrw.read_seg(label_file), assertsegmentation.shape[0] == 1and print a similarly clear error if not — so the failure happens duringverify_dataset_integrityinstead 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-fixto trigger the fix‑PR workflow. Otherwise happy to refine.Karol-G commentedon May 18, 2026
@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 :)
Karol-G commentedon Jun 22, 2026
I assume this is fixed. Will close the issue now due to inactivity. Feel free to open it again if the issue still persists.
Reject multi-channel segmentations early