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

BUG: Fix traceback in restored transform #1766

Merged

Conversation

ThomasKierski
Copy link
Contributor

Using transforms from MONAI and MONAILabel can result in metadata containing torch.tensors and torch.Sizes. This change fixes a traceback in the restored transform resulting from an incompatibility between torch datatypes and numpy.any().

Signed-off-by: Thomas Kierski thomas.kierski@revvity.com

Using transforms from MONAI and MONAILabel can result in metadata containing torch.tensors and torch.Sizes. This change fixes a traceback in the restored transform resulting from an incompatibility between torch datatypes and numpy.any().
Signed-off-by: Thomas Kierski <thomas.kierski@revvity.com>
@ThomasKierski
Copy link
Contributor Author

A snippet to reproduce the transform pipeline that resulted in this traceback is below:

    pre_transforms = Compose([
        LoadImaged(keys=("image", "label"), reader="ITKReader"),
        EnsureChannelFirstd(keys=("image", "label")),
        NormalizeLabelsInDatasetd(keys="label", label_names=label_names),
        Orientationd(keys=["image", "label"], axcodes="RAS"),
        ScaleIntensityRanged(keys=("image"), a_min=-1000, a_max=3500, b_min=0.0, b_max=1.0, clip=True),
        Resized(keys=("image", "label"), spatial_size=3*[128], mode=("area", "nearest")),
    ])

    post_transforms = Compose([
        Activationsd(keys=("pred"), softmax=True),
        AsDiscreted(
            keys=("pred", "label"),
            argmax=(True, False),
            to_onehot=num_labels,
        ),
        SplitPredsLabeld(keys="pred"),
    ])

    data = {"image": "/path/to/image.mha", "label":"/path/to/label.seg.nrrd"}
    data = pre_transforms(data)
    data["pred"] = model(data["image"])
    restorex = Restored(keys=["pred"], ref_image="image")
    restored_prediction = restorex(data)

The traceback in question:

TypeError Traceback (most recent call last)
Cell In[10], line 81
79 data = monai.data.decollate_batch(data)
80 for n in range(len(data)):
---> 81 data[n] = restorex(data[n])
82 data = monai.data.list_data_collate(data)
83 # write_sample_to_disk(data, os.path.join(outpath,os.path.basename(data["label"].meta["filename_or_obj"][0])))

File /usr/local/lib/python3.10/dist-packages/monailabel/transform/post.py:132, in Restored.call(self, data)
129 spatial_size = spatial_shape[-len(current_size) :]
131 # Undo Spacing
--> 132 if np.any(np.not_equal(current_size, spatial_size)):
133 resizer = Resize(spatial_size=spatial_size, mode=self.mode[idx])
134 result = resizer(result, mode=self.mode[idx], align_corners=self.align_corners[idx])

File <array_function internals>:200, in any(*args, **kwargs)

File /usr/local/lib/python3.10/dist-packages/numpy/core/fromnumeric.py:2423, in any(a, axis, out, keepdims, where)
2333 @array_function_dispatch(_any_dispatcher)
2334 def any(a, axis=None, out=None, keepdims=np._NoValue, *, where=np._NoValue):
2335 """
2336 Test whether any array element along a given axis evaluates to True.
2337
(...)
2421
2422 """
-> 2423 return _wrapreduction(a, np.logical_or, 'any', axis, None, out,
2424 keepdims=keepdims, where=where)

File /usr/local/lib/python3.10/dist-packages/numpy/core/fromnumeric.py:84, in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
82 return reduction(axis=axis, dtype=dtype, out=out, **passkwargs)
83 else:
---> 84 return reduction(axis=axis, out=out, **passkwargs)
86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

TypeError: any() received an invalid combination of arguments - got (out=NoneType, axis=NoneType, ), but expected one of:

  • ()
  • (tuple of ints dim = None, bool keepdim = False)
    didn't match because some of the keywords were incorrect: out, axis
  • (int dim, bool keepdim = False)
    didn't match because some of the keywords were incorrect: out, axis
  • (name dim, bool keepdim = False)
    didn't match because some of the keywords were incorrect: out, axis

@SachidanandAlle SachidanandAlle enabled auto-merge (squash) October 26, 2024 08:38
@SachidanandAlle SachidanandAlle merged commit e8e1440 into Project-MONAI:main Oct 26, 2024
19 checks passed
KumoLiu pushed a commit that referenced this pull request Oct 29, 2024
Using transforms from MONAI and MONAILabel can result in metadata containing torch.tensors and torch.Sizes. This change fixes a traceback in the restored transform resulting from an incompatibility between torch datatypes and numpy.any().
Signed-off-by: Thomas Kierski <thomas.kierski@revvity.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
SachidanandAlle added a commit that referenced this pull request Nov 21, 2024
* fix ignite-info issue

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* update monai version

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* revert version change

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* temp fix

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* minor fix

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* minor fix

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* update to 3.9 and drop 3.8

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* [pre-commit.ci] pre-commit autoupdate (#1716)

updates:
- [github.com/pre-commit/pre-commit-hooks: v4.5.0 → v4.6.0](pre-commit/pre-commit-hooks@v4.5.0...v4.6.0)
- [github.com/psf/black: 24.3.0 → 24.4.2](psf/black@24.3.0...24.4.2)
- [github.com/psf/black: 24.3.0 → 24.4.2](psf/black@24.3.0...24.4.2)
- [github.com/PyCQA/flake8: 7.0.0 → 7.1.0](PyCQA/flake8@7.0.0...7.1.0)
- [github.com/pre-commit/mirrors-mypy: v1.9.0 → v1.10.1](pre-commit/mirrors-mypy@v1.9.0...v1.10.1)
- [github.com/asottile/pyupgrade: v3.15.2 → v3.16.0](asottile/pyupgrade@v3.15.2...v3.16.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: SACHIDANAND ALLE <sachidanand.alle@gmail.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* Bump urllib3 from 2.2.1 to 2.2.2 (#1753)

Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.1 to 2.2.2.
- [Release notes](https://github.com/urllib3/urllib3/releases)
- [Changelog](https://github.com/urllib3/urllib3/blob/main/CHANGES.rst)
- [Commits](urllib3/urllib3@2.2.1...2.2.2)

---
updated-dependencies:
- dependency-name: urllib3
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* Bump requests from 2.31.0 to 2.32.2 (#1754)

Bumps [requests](https://github.com/psf/requests) from 2.31.0 to 2.32.2.
- [Release notes](https://github.com/psf/requests/releases)
- [Changelog](https://github.com/psf/requests/blob/main/HISTORY.md)
- [Commits](psf/requests@v2.31.0...v2.32.2)

---
updated-dependencies:
- dependency-name: requests
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* ENH: Allow newer version of dependencies (#1748)

Pinning basic packages such as "requests" to a specific patch level release such "2.31.0" is too strict. New releases and especially new patch releases allow to use latest security updates of various packages.

Signed-off-by: James Butler <james.butler@revvity.com>
Co-authored-by: SACHIDANAND ALLE <sachidanand.alle@gmail.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* Release 0.8.4 checks and updates (#1757)

* Release 0.8.4 checks and updates

Signed-off-by: tangy5 <yuchengt@nvidia.com>

* Trigger monai container update

Signed-off-by: tangy5 <yuchengt@nvidia.com>

* Drop python 3.8

Signed-off-by: tangy5 <yuchengt@nvidia.com>

* Drop python 3.8

Signed-off-by: tangy5 <yuchengt@nvidia.com>

---------

Signed-off-by: tangy5 <yuchengt@nvidia.com>
Co-authored-by: tangy5 <yuchengt@nvidia.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* Add MITK as supported platform (#1759)

Signed-off-by: Ashis Ravindran <ashis.ravindran@dkfz-heidelberg.de>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* Add MITK to installation docs (#1761)

* Add MITK as supported platform

Signed-off-by: Ashis Ravindran <ashis.ravindran@dkfz-heidelberg.de>

* Update link to MONAI Label repo

Signed-off-by: Ashis Ravindran <ashis.ravindran@dkfz-heidelberg.de>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Signed-off-by: Ashis Ravindran <ashis.ravindran@dkfz-heidelberg.de>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* fix(mode): no need to initialize toolbar service (#1763)

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* BUG: Fix traceback in restored transform (#1766)

Using transforms from MONAI and MONAILabel can result in metadata containing torch.tensors and torch.Sizes. This change fixes a traceback in the restored transform resulting from an incompatibility between torch datatypes and numpy.any().
Signed-off-by: Thomas Kierski <thomas.kierski@revvity.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* Revert "update to 3.9 and drop 3.8"

This reverts commit 8ced475.

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* revert

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* revert

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

* revert

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>

---------

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: James Butler <james.butler@revvity.com>
Signed-off-by: tangy5 <yuchengt@nvidia.com>
Signed-off-by: Ashis Ravindran <ashis.ravindran@dkfz-heidelberg.de>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: SACHIDANAND ALLE <sachidanand.alle@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: James Butler <jamesobutler@users.noreply.github.com>
Co-authored-by: tangy5 <58751975+tangy5@users.noreply.github.com>
Co-authored-by: tangy5 <yuchengt@nvidia.com>
Co-authored-by: Ashis Ravindran <33703127+ASHISRAVINDRAN@users.noreply.github.com>
Co-authored-by: Alireza <ar.sedghi@gmail.com>
Co-authored-by: Thomas Kierski <54414492+ThomasKierski@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants