Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why the data shape are different through transform "spacingd. #5935

Closed
wyli opened this issue Feb 3, 2023 Discussed in #5934 · 3 comments · Fixed by #5950
Closed

Why the data shape are different through transform "spacingd. #5935

wyli opened this issue Feb 3, 2023 Discussed in #5934 · 3 comments · Fixed by #5950
Labels
bug Something isn't working

Comments

@wyli
Copy link
Contributor

wyli commented Feb 3, 2023

Discussed in #5934

Originally posted by xxsxxsxxs666 February 3, 2023

1675428782920

val_transforms = Compose(
    [
        LoadImaged(keys=["image", "label"]),
        EnsureChannelFirstd(keys=["image", "label"]),
        ScaleIntensityRanged(
            keys=["image"], a_min=-57, a_max=400,
            b_min=0.0, b_max=1.0, clip=True,
        ),
        CropForegroundd(keys=["image", "label"], source_key="image"),  # default: value>0
        Orientationd(keys=["image", "label"], axcodes="RAI"),
        Spacingd(keys=["image", "label"], pixdim=(
            0.35, 0.35, 0.5), mode=("bilinear", "nearest")),  # Now only Sequential_str, How to add spline
    ]
)

The shape and metadata are the same between image and label
image

this problem only happen when I use cachedataset. Nothing went wrong when I use normal dataset.

@wyli wyli added the bug Something isn't working label Feb 3, 2023
@wyli
Copy link
Contributor Author

wyli commented Feb 6, 2023

thanks @xxsxxsxxs666 for reporting the issue, while I think the shape inconsistency could be addressed by using the EnsureSameShaped transform

class EnsureSameShaped(MapTransform):
"""
Checks if segmentation label images (in keys) have the same spatial shape as the main image (in source_key),
and raise an error if the shapes are significantly different.
If the shapes are only slightly different (within an allowed_shape_difference in each dim), then resize the label using
nearest interpolation. This transform is designed to correct datasets with slight label shape mismatches.
Generally image and segmentation label must have the same spatial shape, however some public datasets are having slight
shape mismatches, which will cause potential crashes when calculating loss or metric functions.

I'm still not sure why this only happens with cacachedatset. Could you please share that test data if possible? I'm trying to reproduce the issue.

@xxsxxsxxs666
Copy link

xxsxxsxxs666 commented Feb 7, 2023 via email

wyli added a commit to wyli/MONAI that referenced this issue Feb 7, 2023
Signed-off-by: Wenqi Li <wenqil@nvidia.com>
@wyli
Copy link
Contributor Author

wyli commented Feb 7, 2023

thanks for the details @xxsxxsxxs666, I'm able to reproduce the issue and submitting a pull request #5950 to fix it.

The issue was with one of the images pixdim 0.346909 which was close to the target 0.35, the spacing transform correctly skipped the resampling but there's a buggy side-effect of assigning 0.346909 to self.pixdim. the assignment was not thread-safe and when used with the cachedataset with 4 workers, it showed some random behaviours, but your script narrowed it down, thanks!

@wyli wyli closed this as completed in #5950 Feb 8, 2023
wyli added a commit that referenced this issue Feb 8, 2023
Signed-off-by: Wenqi Li <wenqil@nvidia.com>

Fixes #5935

### Description
assigning `out_d[idx]` in the `__call__` shouldn't change `self.pixdim`
in Spacing

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [x] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: Wenqi Li <wenqil@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants