sct-pipeline / contrast-agnostic-softseg-spinalcord

Contrast-agnostic spinal cord segmentation project with softseg
MIT License
4 stars 3 forks source link

Shifted predictions #113

Open jcohenadad opened 3 months ago

jcohenadad commented 3 months ago

Some predictions are shifted in the axial plane, see notably: https://github.com/valosekj/dcm-brno/issues/19

valosekj commented 3 months ago

The shift in the top slices is also presented for v2.3. v2.3 vs v2.4 details: https://github.com/valosekj/dcm-brno/issues/19#issuecomment-2241085876

naga-karthik commented 3 months ago

Relevant issue https://github.com/ivadomed/canproco/issues/102

jcohenadad commented 3 months ago

One possible cause: the resampling of some highly anisotropic data to 1mm iso. For example, the DWI data from BRNO: sub-1860B6472B_ses-1860B_acq-ZOOMit_dir-AP_dwi_crop_crop_moco_dwi_mean.nii.gz

We observe a clear shift in the predictions: image

Interesting observation: when applying the resampling, there is an apparent shift close to the interface between two axial slices: bad

whereas when the resampling happens in the middle of the slice, there is no apparent shift: good

[!Note] The type of interpolation (linear vs. spline) does not make any difference

Next steps:

jcohenadad commented 3 months ago

When applying the model on the resampled data, the shift is still visible, at the same location, ie: towards the top slices:

image

Maybe this is related to an 'edge' effect (eg: padding that would somehow shift the prediction of the top slices?)

naga-karthik commented 3 months ago

I found something really interesting when trying the different padding options available during inference.

Model: contrast-agnostic v2.4 used via SCT's master branch

Padding options tested:

What I changed in the code -- these two lines were replaced with:

        ResizeWithPadOrCropd(keys=["image"], spatial_size=crop_size, mode='edge'), 
        DivisiblePadd(keys=["image"], k=2 ** 5, mode='edge'),
zero-padding -- shift still exists ![ezgif com-animated-gif-maker](https://github.com/user-attachments/assets/b6fb60b3-6553-4b86-912d-5022d3faea31)
symmetric (mirror) padding -- shift still exists and also undersegments ![ezgif com-animated-gif-maker-2](https://github.com/user-attachments/assets/55b8e2b7-a885-4e79-906f-69fbafc42f2c)
edge-padding -- shifting resolved! the seg also seems to be centred ![ezgif com-animated-gif-maker-3](https://github.com/user-attachments/assets/47a5b4c7-68df-41b1-84f9-abe9566bf257)

This is also consistent with my earlier investigations on dcm-zurich and sci-colorado datasets (slides). I think I should set edge padding as default on the SCT's inference script for monai models

EDIT

Also tested on 1mm iso resampled images and it seems that edge padding is able to segment without any shifts

edge-padding on 1mm resampled image ![ezgif com-animated-gif-maker-4](https://github.com/user-attachments/assets/884480cb-2435-43b9-8211-aa39a1498f75)
valosekj commented 3 months ago

Nice! 👍🏻

edge-padding -- shifting resolved! the seg also seems to be centred

Do you have a hunch why python monai/run_inference_single_image.py --pad-mode edge did not help in the case of https://github.com/valosekj/dcm-brno/issues/19#issuecomment-2243265925?

naga-karthik commented 3 months ago

Do you have a hunch why python monai/run_inference_single_image.py --pad-mode edge did not help

I was surprised about this too then I realized that I wasn't applying the padding at the right step. In the run_inference_single_image.py script, the padding is applied only at this line:

DivisiblePadd(keys=["image"], k=2**5, mode=pad_mode),

But turns out that you would have apply the padding in both these lines:

ResizeWithPadOrCropd(keys=["image"], spatial_size=crop_size, mode=pad_mode),
DivisiblePadd(keys=["image"], k=2**5, mode=pad_mode),

in order to have the desired effect! I have fixed that bug in this commit in my new branch.

naga-karthik commented 3 months ago

The changes required on the SCT side can be accessed if you checkout to my branch https://github.com/spinalcordtoolbox/spinalcordtoolbox/tree/nk/set-edge-pad-default

(will open a PR once it is tested on all dcm-brno subjects and after we see consistent improvement fromedge padding over zero padding

valosekj commented 3 months ago

Thank you for the fix, Naga! I tested the updated edge padding on dcm-brno DWI images, and the shift is much better!

See comparison on 5 subjects (original padding vs updated edge padding):

Kapture 2024-07-25 at 11 57 15

@naga-karthik, maybe you can open a PR and merge the fixed padding to the SCT master branch?