nnUNet icon indicating copy to clipboard operation
nnUNet copied to clipboard

OverflowError: cannot convert float infinity to integer - nnUNet 2D

Open AliceSantilli opened this issue 7 months ago • 3 comments

Hi there,

I've been using nnunet v2 for many 3D imaging projects but wanted to use it on an Xray project I have with 2D images + segmentations.

I've converted my images and segmentations to nifti files (nii.gz). I've tried both 3D (1, w, h), (w, h, 1) and one 2D (w,h) versions of the raw images saved and both give this exact error.

It passes by the dataset integrity portion but there seems to be a "divide by 0" issue in the experiment planning script.

This is my dataset JSON { "channel_names": { "0": "XR" }, "labels": { "background": 0, "FemoralHead_Anterior": 1, "FemoralHead_Posterior": 2, "Sacrum": 3, "L5": 4, "L4": 5, "L3": 6, "L2": 7, "L1": 8, "T12": 9, "T11": 10, "T10": 11, "T9": 12, "T8": 13, "T7": 14, "T6": 15, "T5": 16, "T4": 17, "T3": 18, "T2": 19, "T1": 20, "C7": 21 }, "numTraining": 48, "file_ending": ".nii.gz", "overwrite_image_reader_writer": "NibabelIOWithReorient" }

Here is the traceback :

`nnUNetv2_plan_and_preprocess -d 501 -c 2d --verify_dataset_integrity --verbose Fingerprint extraction... Dataset501_SagittalSpine Using <class 'nnunetv2.imageio.nibabel_reader_writer.NibabelIOWithReorient'> reader/writer

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

Experiment planning...

############################ INFO: You are using the old nnU-Net default planner. We have updated our recommendations. Please consider using those instead! Read more here: https://github.com/MIC-DKFZ/nnUNet/blob/master/documentation/resenc_presets.md ############################

Attempting to find 3d_lowres config. Current spacing: [1.03 1.03 1.03]. Current patch size: (256, 256, 1). Current median shape: [1.50485437e+03 1.96213592e+03 9.70873786e-01] Attempting to find 3d_lowres config. Current spacing: [1.0609 1.0609 1.0609]. Current patch size: (256, 256, 1). Current median shape: [1.46102366e+03 1.90498633e+03 9.42595909e-01] Attempting to find 3d_lowres config. Current spacing: [1.092727 1.092727 1.092727]. Current patch size: (256, 256, 1). Current median shape: [1.41846957e+03 1.84950129e+03 9.15141659e-01] Attempting to find 3d_lowres config. Current spacing: [1.12550881 1.12550881 1.12550881]. Current patch size: (256, 256, 1). Current median shape: [1.37715492e+03 1.79563232e+03 8.88487048e-01] Attempting to find 3d_lowres config. Current spacing: [1.15927407 1.15927407 1.15927407]. Current patch size: (256, 256, 1). Current median shape: [1.33704362e+03 1.74333235e+03 8.62608784e-01] Attempting to find 3d_lowres config. Current spacing: [1.1940523 1.1940523 1.1940523]. Current patch size: (256, 256, 1). Current median shape: [1.29810060e+03 1.69255568e+03 8.37484257e-01] Attempting to find 3d_lowres config. Current spacing: [1.22987387 1.22987387 1.22987387]. Current patch size: (256, 256, 1). Current median shape: [1.26029184e+03 1.64325794e+03 8.13091511e-01] Attempting to find 3d_lowres config. Current spacing: [1.26677008 1.26677008 1.26677008]. Current patch size: (256, 256, 1). Current median shape: [1.22358431e+03 1.59539606e+03 7.89409234e-01] Attempting to find 3d_lowres config. Current spacing: [1.30477318 1.30477318 1.30477318]. Current patch size: (256, 256, 1). Current median shape: [1.18794594e+03 1.54892822e+03 7.66416732e-01] Attempting to find 3d_lowres config. Current spacing: [1.34391638 1.34391638 1.34391638]. Current patch size: (256, 256, 1). Current median shape: [1.15334557e+03 1.50381380e+03 7.44093915e-01] Attempting to find 3d_lowres config. Current spacing: [1.38423387 1.38423387 1.38423387]. Current patch size: (256, 256, 1). Current median shape: [1.11975298e+03 1.46001340e+03 7.22421277e-01] Attempting to find 3d_lowres config. Current spacing: [1.42576089 1.42576089 1.42576089]. Current patch size: (256, 256, 1). Current median shape: [1.08713881e+03 1.41748874e+03 7.01379880e-01] Attempting to find 3d_lowres config. Current spacing: [1.46853371 1.46853371 1.46853371]. Current patch size: (256, 256, 1). Current median shape: [1.05547458e+03 1.37620266e+03 6.80951340e-01] Attempting to find 3d_lowres config. Current spacing: [1.51258972 1.51258972 1.51258972]. Current patch size: (256, 256, 1). Current median shape: [1.02473260e+03 1.33611909e+03 6.61117806e-01] Attempting to find 3d_lowres config. Current spacing: [1.55796742 1.55796742 1.55796742]. Current patch size: (256, 256, 1). Current median shape: [9.94886018e+02 1.29720300e+03 6.41861947e-01] Attempting to find 3d_lowres config. Current spacing: [1.60470644 1.60470644 1.60470644]. Current patch size: (256, 256, 1). Current median shape: [9.65908756e+02 1.25942038e+03 6.23166939e-01] Attempting to find 3d_lowres config. Current spacing: [1.65284763 1.65284763 1.65284763]. Current patch size: (256, 256, 1). Current median shape: [9.37775491e+02 1.22273824e+03 6.05016446e-01] Attempting to find 3d_lowres config. Current spacing: [1.70243306 1.70243306 1.70243306]. Current patch size: (256, 256, 1). Current median shape: [9.10461642e+02 1.18712450e+03 5.87394608e-01] Attempting to find 3d_lowres config. Current spacing: [1.75350605 1.75350605 1.75350605]. Current patch size: (256, 256, 1). Current median shape: [8.83943342e+02 1.15254806e+03 5.70286027e-01] Attempting to find 3d_lowres config. Current spacing: [1.80611123 1.80611123 1.80611123]. Current patch size: (256, 256, 1). Current median shape: [8.58197419e+02 1.11897870e+03 5.53675754e-01] Attempting to find 3d_lowres config. Current spacing: [1.86029457 1.86029457 1.86029457]. Current patch size: (256, 256, 1). Current median shape: [8.33201378e+02 1.08638709e+03 5.37549276e-01] Attempting to find 3d_lowres config. Current spacing: [1.91610341 1.91610341 1.91610341]. Current patch size: (256, 256, 1). Current median shape: [8.08933376e+02 1.05474474e+03 5.21892501e-01] Attempting to find 3d_lowres config. Current spacing: [1.97358651 1.97358651 1.97358651]. Current patch size: (256, 256, 1). Current median shape: [7.85372210e+02 1.02402402e+03 5.06691748e-01] /opt/conda/lib/python3.11/site-packages/nnunetv2/experiment_planning/experiment_planners/default_experiment_planner.py:372: RuntimeWarning: divide by zero encountered in divide batch_size = round((reference / estimate) * ref_bs) Traceback (most recent call last): File "/opt/conda/bin/nnUNetv2_plan_and_preprocess", line 8, in sys.exit(plan_and_preprocess_entry()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/conda/lib/python3.11/site-packages/nnunetv2/experiment_planning/plan_and_preprocess_entrypoints.py", line 184, in plan_and_preprocess_entry plans_identifier = plan_experiments(args.d, args.pl, args.gpu_memory_target, args.preprocessor_name, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/conda/lib/python3.11/site-packages/nnunetv2/experiment_planning/plan_and_preprocess_api.py", line 94, in plan_experiments _, plans_identifier = plan_experiment_dataset(d, experiment_planner, gpu_memory_target_in_gb, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/conda/lib/python3.11/site-packages/nnunetv2/experiment_planning/plan_and_preprocess_api.py", line 72, in plan_experiment_dataset ret = planner.plan_experiment() ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/conda/lib/python3.11/site-packages/nnunetv2/experiment_planning/experiment_planners/default_experiment_planner.py", line 460, in plan_experiment plan_3d_lowres = self.get_plans_for_configuration(lowres_spacing, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/conda/lib/python3.11/site-packages/nnunetv2/experiment_planning/experiment_planners/default_experiment_planner.py", line 372, in get_plans_for_configuration batch_size = round((reference / estimate) * ref_bs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OverflowError: cannot convert float infinity to integer `

AliceSantilli avatar May 15 '25 20:05 AliceSantilli

Hi @Boboshinidie,

the official documentation for preprocessing guidelines for nnU-Net can be found here: https://github.com/MIC-DKFZ/nnUNet/blob/master/documentation/dataset_format.md

Further, we provide an example for the 2d use-case here: https://github.com/MIC-DKFZ/nnUNet/blob/master/nnunetv2/dataset_conversion/Dataset120_RoadSegmentation.py

Best regards, Carsten

sten2lu avatar May 20 '25 13:05 sten2lu

Hi Carsten,

Thanks for the links! I restarted from scratch and followed the above example to double check myself and I still get the exact same error as above.

When I force it (in the code itself) to ignore the 3d_lowres and 3d_fullres planning it completes the planning phase and provides the 2D plan below and then fails in the preprocessing phase at a different error.

2D U-Net configuration: {'data_identifier': 'nnUNetPlans_2d', 'preprocessor_name': 'DefaultPreprocessor', 'batch_size': 516, 'patch_size': (2048, 1), 'median_image_size_in_voxels': array([2.021e+03, 1.000e+00]), 'spacing': array([1., 1.]), 'normalization_schemes': ['ZScoreNormalization', 'ZScoreNormalization', 'ZScoreNormalization'], 'use_mask_for_norm': [False, False, False], '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}, 'architecture': {'network_class_name': 'dynamic_network_architectures.architectures.unet.PlainConvUNet', 'arch_kwargs': {'n_stages': 9, 'features_per_stage': (32, 64, 128, 256, 512, 512, 512, 512, 512), 'conv_op': 'torch.nn.modules.conv.Conv2d', 'kernel_sizes': ((3, 3), (3, 3), (3, 3), (3, 3), (3, 3), (3, 3), (3, 3), (3, 3), (3, 3)), 'strides': ((1, 1), (2, 1), (2, 1), (2, 1), (2, 1), (2, 1), (2, 1), (2, 1), (2, 1)), 'n_conv_per_stage': (2, 2, 2, 2, 2, 2, 2, 2, 2), 'n_conv_per_stage_decoder': (2, 2, 2, 2, 2, 2, 2, 2), 'conv_bias': True, 'norm_op': 'torch.nn.modules.instancenorm.InstanceNorm2d', 'norm_op_kwargs': {'eps': 1e-05, 'affine': True}, 'dropout_op': None, 'dropout_op_kwargs': None, 'nonlin': 'torch.nn.LeakyReLU', 'nonlin_kwargs': {'inplace': True}}, '_kw_requires_import': ('conv_op', 'norm_op', 'dropout_op', 'nonlin')}, 'batch_dice': True}

Does this plan seem right to you? The patch_size seems off on the second dimension, as if it's using the wrong channel to get to this number.

AliceSantilli avatar May 20 '25 18:05 AliceSantilli

Hi @AliceSantilli, Could you provide me a small sample of your dataset so that I can run some tests on it myself?

Best regards, Carsten

sten2lu avatar May 21 '25 09:05 sten2lu