BBillot / SynthSeg

Contrast-agnostic segmentation of MRI scans
Apache License 2.0
358 stars 92 forks source link

ValueError: Error when checking input: expected labels_input to have 4 dimensions, but got array with shape (1, 216, 256, 8, 1) #79

Closed whdong-whu closed 7 months ago

whdong-whu commented 8 months ago

First of all, thank you for your excellent work on this project. I've been following the tutorials and encountered an error in the first step of the 1-generation_visualisation process, specifically when generating images from a file with a .nii.gz suffix. The shape of the data obtained after reading the file with nib.load is (232, 256, 10).

Upon debugging, I believe the issue might lie in the model_inputs.py file, particularly around line 104: list_label_maps.append(utils.add_axis(lab, axis=[0, -1])). When I tried changing it to list_label_maps.append(utils.add_axis(lab, axis=[0])), the error changed to ValueError: Error when checking input: expected labels_input to have shape (232, 256, 1) but got array with shape (240, 256, 10). Could you please explain why an additional dimension is added at both the beginning and the end of the axis? Also, I'm wondering if there's something wrong with the shape of my input data or if I need to modify something on my end to accommodate it.

I would greatly appreciate any guidance or suggestions you could provide on this matter.

Thank you for your time and assistance.

Best regards,

BBillot commented 8 months ago

Hi, thanks for the interest in our tool :)

dimensions are added so that the data is compatible with keras formatting (batch, H, W, D, channels).

Your case is an edge case. Input data are often 3D with shape (H, W, D), but sometimes inputs already have a channel dimension (H, W, D, channels), where channels could be 1 or more. So I needed a mechanism to detect when the last dimension is a spatial dimension or a channel dimension, and I came up with this rule of thumb, saying that if the last dimension is less or equal to 10, then this is a channel dimension. Thus, in your case, the code thinks your data is 2D + channel dimension.

I believe you can fix this by changing max_channels to 11 or more in: https://github.com/BBillot/SynthSeg/blob/13a2fb9d494a8cc38bb3e46ed2ad91ae93cc8737/ext/lab2im/utils.py#L558 https://github.com/BBillot/SynthSeg/blob/13a2fb9d494a8cc38bb3e46ed2ad91ae93cc8737/ext/lab2im/utils.py#L113 https://github.com/BBillot/SynthSeg/blob/13a2fb9d494a8cc38bb3e46ed2ad91ae93cc8737/ext/lab2im/utils.py#L163

With that being said, I don;t know what label maps you're using, but having a spatial dimension with 10 either means you are at super low resolution, or you;re very tightly cropped around a specific area, but in both cases, I'm not sure this is very good for SynthSeg

Hope this helps, Benjamin