ivadomed: convert_file_to_nifti bug for ADS SEM test data
Discussed in https://github.com/ivadomed/ivadomed/discussions/1310
<div type='discussions-op-text'>Originally posted by chennyyuee January 25, 2024 Dear ivadomed team,
I’m trying to use ivadomed Two-class segmentation to train a model for use in AxonDeepSeg. But when I run ivadomed -c config_microscopy.json
, using the downloaded tutorial dataset.
I got the following error:
File "C:\Users\ct630\ivadomed_env\lib\site-packages\ivadomed\loader\segmentation_pair.py", line 297, in read_file
img = self.convert_file_to_nifti(filename, extension, is_gt)
File "C:\Users\ct630\ivadomed_env\lib\site-packages\ivadomed\loader\segmentation_pair.py", line 360, in convert_file_to_nifti
_img = np.sum(_img * (.299, .587, .114, 0), axis=-1)
ValueError: operands could not be broadcast together with shapes (744,1154,2) (4,)
Is there a way to solve this problem ?
Many thanks! chenyue
</div>This user’s error is reproducible on my end.
It relates to a part of code that was added (possibly by @kanishk16?) in PR #1297,
I ran the debugger using the ADS SEM training data, and found the following for the case where there is an error:
(Pdb) _img.ndim
2
(Pdb) _img.ndim
2
(Pdb) colorspace_idx
2
(Pdb) props.is_batch
False
(Pdb) _img.shape[colorspace_idx]
*** IndexError: tuple index out of range
(Pdb) _img.shape
(954, 1280)
Looking at imageio’s documentation, it’s not clear what props.is_batch
is being used for here in relation to setting colorspace_idx
, @kanishk16 do you recall? The case is_batch=True
I think would mean it’s a list/tuple of images, so what does that have to do with the colorspace?
If I understand the logic, for this image (954, 1280), the image is binary so _img.ndim <= colorspace_idx: # binary or gray
should be triggered but isn’t being, because props.is_batch
is false and then _img.ndim = colorspace_idx
. So we possibly need a different colorspace_idx
condition, and/or a different if/else condition.
Lastly, to avoid this in the future, we need to add test images & tests for each cases of this is/else chain, as this issue was missed due to the lack of test coverage of these lines:
About this issue
- Original URL
- State: open
- Created 5 months ago
- Reactions: 1
- Comments: 16 (16 by maintainers)
^from the user’s issue; maybe the only problem here is the missing case of greyscale images with an alpha channel, that one if definitely not covered in the current code, and it’s an odd edgecase
I’ll test it out locally
Since this block was inside
except
just raised an error in thetry
to execute this block of code to remove my doubt regarding any potential issue with the img 😅Absolutely 🚀
(Just a small heads-up: I got the ping on the previous issue, and have been reading through the replies here, and wanted to try my hand at proposing a solution to handle this issue. I hope that’s okay!)
Playing around I set this,
Which worked fine, up until one file ([data_axondeepseg_sem/sub-rat6/micr/sub-rat6_sample-data15_SEM.png] (https://github.com/axondeepseg/data_axondeepseg_sem/blob/master/sub-rat6/micr/sub-rat6_sample-data15_SEM.png)), which has a quirk in that it’s an a grayscale image but with an alpha channel, meaning it’s not covered by one of the above conditions (or the previous ones):
So we need a better way to handle the alpha cases