MIC-DKFZ / nnUNet

Apache License 2.0
5.82k stars 1.74k forks source link

Error: Unexpected number of modalities. #2017

Closed linamedicalimaging closed 6 months ago

linamedicalimaging commented 7 months ago

Hi, FabianIsensee, I have a question again.

I've been training nnUNet with my own dataset. I modified the script Dataset120_RoadSegmentation.py. Here are the modifications I made: **import multiprocessing import shutil from multiprocessing import Pool

from batchgenerators.utilities.file_and_folder_operations import *

from nnunetv2.dataset_conversion.generate_dataset_json import generate_dataset_json from nnunetv2.paths import nnUNet_raw from skimage import io from acvl_utils.morphology.morphology_helper import generic_filter_components from scipy.ndimage import binary_fill_holes

def load_and_convert_case(input_image: str, input_seg: str, output_image: str, output_seg: str, min_component_size: int = 50): seg = io.imread(input_seg) seg[seg == 255] = 1 image = io.imread(input_image) image = image.sum(2) mask = image == (3 * 255)

the dataset has large white areas where road segmentations can exist but no image information is available.

# Remove the road label in these areas
mask = generic_filter_components(mask, filter_fn=lambda ids, sizes: [i for j, i in enumerate(ids) if
                                                                     sizes[j] > min_component_size])
mask = binary_fill_holes(mask)
seg[mask] = 0
io.imsave(output_seg, seg, check_contrast=False)
shutil.copy(input_image, output_image)

if name == "main": source = '/home/lina/nnunet_ACDC_heart/Dataset_raw' dataset_name = 'Dataset300_ACDCHeartSegmentation' imagestr = join(nnUNet_raw, dataset_name, 'imagesTr') imagests = join(nnUNet_raw, dataset_name, 'imagesTs') labelstr = join(nnUNet_raw, dataset_name, 'labelsTr') labelsts = join(nnUNet_raw, dataset_name, 'labelsTs') maybe_mkdir_p(imagestr) maybe_mkdir_p(imagests) maybe_mkdir_p(labelstr) maybe_mkdir_p(labelsts)

train_source = join(source, 'train')
test_source = join(source, 'test')

with multiprocessing.get_context("spawn").Pool(8) as p:
    valid_ids = subfiles(join(train_source, 'labels'), join=False, suffix='png')
    num_train = len(valid_ids)
    r = []
    for v in valid_ids:
        r.append(
            p.starmap_async(
                load_and_convert_case,
                ((
                     join(train_source, 'images', v),
                     join(train_source, 'labels', v),
                     join(imagestr, v[:-4] + '_0000.png'),
                     join(labelstr, v),
                     50
                 ),)
            )
        )

    valid_ids = subfiles(join(test_source, 'labels'), join=False, suffix='png')
    for v in valid_ids:
        r.append(
            p.starmap_async(
                load_and_convert_case,
                ((
                     join(test_source, 'images', v),
                     join(test_source, 'labels', v),
                     join(imagests, v[:-4] + '_0000.png'),
                     join(labelsts, v),
                     50
                 ),)
            )
        )
    _ = [i.get() for i in r]

generate_dataset_json(join(nnUNet_raw, dataset_name), {0: 'R', 1: 'G', 2: 'B'}, {'background': 0, 'Heart': 1},
                      num_train, '.png', dataset_name='Dataset300_ACDCHeartSegmentation')**

After modifying, I ran the script “Dataset120_RoadSegmentation.py” and there were no errors. However, I noticed that the images in labelsTr were all white (instead of black). I didn't pay much attention to it and continued to run “nnUNetv2_plan_and_preprocess -d 300 --verify_dataset_integrity” in CMD. It showed that all data in imagesTr had issues: Error: Unexpected number of modalities.

I really don't know where the problem lies. I have previously trained nnUNet with my own data (.tif) quite smoothly. This time the data is in ".png" format.

Thank you for taking the time to read my question. I look forward to your reply.

Lina

linamedicalimaging commented 7 months ago

I'm sorry, I realized that I forgot to post the error message from the CMD. Here's an addition of the error message I received (it's too long, so I've only included a part): (nnunet_ACDC_Heart) lina@UP-CS-GPU01:~/nnunet_ACDC_heart/nnUNet/nnunetv2/dataset_conversion$ nnUNetv2_plan_and_preprocess -d 300 --verify_dataset_integrity Fingerprint extraction... Dataset300_ACDCHeartSegmentation Using <class 'nnunetv2.imageio.natural_image_reader_writer.NaturalImage2DIO'> as reader/writer Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_124_0000.png']

Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_149_0000.png'] Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_174_0000.png']

Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_248_0000.png'] Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_1_0000.png']

Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_222_0000.png']

Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_199_0000.png']

Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_272_0000.png']

Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_125_0000.png'] ...... ...... ...... Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_826_0000.png']

Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_827_0000.png']

Error: Unexpected number of modalities. Expected: 3. Got: 4. Images: ['/home/lina/nnunet_ACDC_heart/nnUNet/DATASET/nnUNet_raw/Dataset300_ACDCHeartSegmentation/imagesTr/image_828_0000.png']

Traceback (most recent call last): File "/home/lina/anaconda3/envs/nnunet_ACDC_Heart/bin/nnUNetv2_plan_and_preprocess", line 8, in sys.exit(plan_and_preprocess_entry()) File "/home/lina/nnunet_ACDC_heart/nnUNet/nnunetv2/experiment_planning/plan_and_preprocess_entrypoints.py", line 182, in plan_and_preprocess_entry extract_fingerprints(args.d, args.fpe, args.npfp, args.verify_dataset_integrity, args.clean, args.verbose) File "/home/lina/nnunet_ACDC_heart/nnUNet/nnunetv2/experiment_planning/plan_and_preprocess_api.py", line 46, in extract_fingerprints extract_fingerprint_dataset(d, fingerprint_extractor_class, num_processes, check_dataset_integrity, clean, File "/home/lina/nnunet_ACDC_heart/nnUNet/nnunetv2/experiment_planning/plan_and_preprocess_api.py", line 29, in extract_fingerprint_dataset verify_dataset_integrity(join(nnUNet_raw, dataset_name), num_processes) File "/home/lina/nnunet_ACDC_heart/nnUNet/nnunetv2/experiment_planning/verify_dataset_integrity.py", line 220, in verify_dataset_integrity raise RuntimeError( RuntimeError: Some images have errors. Please check text output above to see which one(s) and what's going on.

Thank you again!!!

GregorKoehler commented 6 months ago

Hi @linamedicalimaging , the error comes from this line: https://github.com/MIC-DKFZ/nnUNet/blob/3808231601265aed98d70de4218fc845e185959d/nnunetv2/experiment_planning/verify_dataset_integrity.py#L84

Did you maybe not specify the correct image reader class for png?

GregorKoehler commented 6 months ago

Closing this issue for now, as it was stale for roughly a week. Please feel free to re-open if you're still facing this issue!

felixkrones commented 5 months ago

Dear @GregorKoehler:

I am having the same issue. I think it is a problem with the image channels that they are confused with the modalities (maybe somewhere is an ordering wrong (c,1,x,y) vs (1,c,x,y)?

<> Error: Unexpected number of modalities. Expected: 1. Got: 3. Images: ['../Dataset900_Signals/imagesTr/image_01_0000.png']

<>

felixkrones commented 5 months ago

I think I found the problem, the dimensions were ordered (c,1,x,y) but expected was (1,c,x,y). I created a PR on it: #2158