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

Meta tensor channel #4222

Merged
merged 34 commits into from
May 11, 2022
Merged

Conversation

rijobro
Copy link
Contributor

@rijobro rijobro commented May 4, 2022

Description

Channel-based transforms to use MetaTensor.

Status

Ready

Types of changes

  • 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).
  • New tests added to cover the changes.

rijobro added 2 commits May 4, 2022 10:35
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
@rijobro rijobro requested review from wyli and Nic-Ma May 4, 2022 09:57
Signed-off-by: Richard Brown <[email protected]>
@rijobro rijobro mentioned this pull request May 4, 2022
6 tasks
rijobro added 2 commits May 4, 2022 12:08
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
@Nic-Ma
Copy link
Contributor

Nic-Ma commented May 4, 2022

Hi @rijobro ,

Thanks for your quick update for MetaTensor support.
I feel maybe we don't need to remove the NdarrayOrTensor support for the current stage? It doesn't block the development of MetaTensor in transforms so far. And we already completed NdarrayOrTensor support for all transforms.
If really necessary to deprecate NdarrayOrTensor, we can remove it later?
@wyli @ericspod What do you think?

Thanks.

@rijobro
Copy link
Contributor Author

rijobro commented May 4, 2022

It's true that MetaTensor is a subclass of torch.Tensor, which is a sub-part of NdarrayOrTensor, so we could leave it. But the proposed changes mean that the input to our transforms will become torch.Tensor, and so I don't think it's bad that these PRs start implementing that. It enables us to remove some # type: ignore, and should improve readability.

I don't see the advantage of leaving them as NdarrayOrTensor, let me know your opinions on that one. Thanks

monai/data/image_reader.py Outdated Show resolved Hide resolved
Signed-off-by: Richard Brown <[email protected]>
@wyli
Copy link
Contributor

wyli commented May 5, 2022

I think I agree with @Nic-Ma, perhaps removing NdarrayOrTensor (and removing the numpy support) could be done in a separate dedicated PR? our previous discussion here #3805 should be revisited...)

@rijobro
Copy link
Contributor Author

rijobro commented May 5, 2022

Ok, how about this:

  1. If a transform needs metadata (e.g., EnsureChannelFirstd), I will switch this to torch.Tensor, since it now can't be used with a np.ndarray as the metadata is no longer present,
  2. If no metadata is required (e.g.,AddChanneld), I will leave this as NdarrayOrTensor.

Does that sounds reasonable?

@wyli
Copy link
Contributor

wyli commented May 5, 2022

sounds good to me, what do you think @Nic-Ma ?

@Nic-Ma
Copy link
Contributor

Nic-Ma commented May 5, 2022

Hi @rijobro ,

Plan sounds good to me!
Thanks for the discussion.

rijobro added 4 commits May 6, 2022 15:57
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
Copy link
Contributor

@Nic-Ma Nic-Ma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me except for some minor comments inline.

Thanks.

monai/data/image_reader.py Show resolved Hide resolved
monai/data/image_reader.py Show resolved Hide resolved
@rijobro rijobro force-pushed the MetaTensor_channel branch from 504c9a5 to 392106c Compare May 10, 2022 11:15
@rijobro rijobro force-pushed the MetaTensor_channel branch from 392106c to c8240d2 Compare May 10, 2022 11:27
rijobro added 2 commits May 10, 2022 15:35
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
@Nic-Ma
Copy link
Contributor

Nic-Ma commented May 11, 2022

Hi @rijobro ,

As this PR is going to merge, could you please help add the doc-string for args of update_meta:
https://github.com/Project-MONAI/MONAI/blob/feature/MetaTensor/monai/data/meta_tensor.py#L117
I got confused before checking the code details.

Thanks in advance.

@rijobro
Copy link
Contributor Author

rijobro commented May 11, 2022

Hi @rijobro ,
As this PR is going to merge, could you please help add the doc-string for args of update_meta:
https://github.com/Project-MONAI/MONAI/blob/feature/MetaTensor/monai/data/meta_tensor.py#L117
I got confused before checking the code details.
Thanks in advance.

Sure @Nic-Ma, will do.

rijobro added 2 commits May 11, 2022 11:59
Signed-off-by: Richard Brown <[email protected]>
Signed-off-by: Richard Brown <[email protected]>
@rijobro
Copy link
Contributor Author

rijobro commented May 11, 2022

@drbeh I needed to change this line from self.filter.to(prob_map) to self.filter.to(prob_map.device). Does this seem correct to you? Was the original code just to make sure the filter was on the same device as prob_map?

Signed-off-by: Richard Brown <[email protected]>
@bhashemian
Copy link
Member

bhashemian commented May 11, 2022

@drbeh I needed to change this line from self.filter.to(prob_map) to self.filter.to(prob_map.device). Does this seem correct to you? Was the original code just to make sure the filter was on the same device as prob_map?

@rijobro you are right but self.filter.to(prob_map) is equivalent of self.filter.to(prob_map.device).to(prob_map.dtype) and I'm not sure if we need the dtype here. There is no harm to keep it as it but I can double check if you really want to change it.

@wyli
Copy link
Contributor

wyli commented May 11, 2022

/black

monai-bot and others added 3 commits May 11, 2022 21:23
Signed-off-by: Wenqi Li <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Copy link
Contributor

@wyli wyli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly tested, merging it to trigger full integration tests.

@wyli wyli merged commit c022ba2 into Project-MONAI:feature/MetaTensor May 11, 2022
wyli added a commit that referenced this pull request May 11, 2022
* MetaTensor channel transforms

Signed-off-by: Richard Brown <[email protected]>

* fixes

Signed-off-by: Richard Brown <[email protected]>

* typo

Signed-off-by: Richard Brown <[email protected]>

* fixes

Signed-off-by: Richard Brown <[email protected]>

* fix

Signed-off-by: Richard Brown <[email protected]>

* remove deepcopy

Signed-off-by: Richard Brown <[email protected]>

* fixes

Signed-off-by: Richard Brown <[email protected]>

* fix

Signed-off-by: Richard Brown <[email protected]>

* fix

Signed-off-by: Richard Brown <[email protected]>

* Merge remote-tracking branch 'MONAI/dev' into MetaTensor_channel

Signed-off-by: Richard Brown <[email protected]>

* metatensor convert helper

Signed-off-by: Richard Brown <[email protected]>

* autofix

Signed-off-by: Richard Brown <[email protected]>

* Merge branch 'feature/MetaTensor' into MetaTensor_channel

Signed-off-by: Richard Brown <[email protected]>

* Revert "Merge remote-tracking branch 'MONAI/dev' into MetaTensor_channel"

This reverts commit 6a5f888.

Signed-off-by: Richard Brown <[email protected]>

* fix

Signed-off-by: Richard Brown <[email protected]>

* FIXME

Signed-off-by: Richard Brown <[email protected]>

* update_meta docstring

Signed-off-by: Richard Brown <[email protected]>

* fix test

Signed-off-by: Richard Brown <[email protected]>

* fix

Signed-off-by: Richard Brown <[email protected]>

* fix str

Signed-off-by: Richard Brown <[email protected]>

* format, update str

Signed-off-by: Wenqi Li <[email protected]>

* fixes comp. torch.solve for metatensor

Signed-off-by: Wenqi Li <[email protected]>

* fixes inverse collation

Signed-off-by: Wenqi Li <[email protected]>

* fixes resample to match

Signed-off-by: Wenqi Li <[email protected]>

* fixes resample to matchd

Signed-off-by: Wenqi Li <[email protected]>

* fixes integration bundle run

Signed-off-by: Wenqi Li <[email protected]>

* fixes image dataset test

Signed-off-by: Wenqi Li <[email protected]>

* [MONAI] python code formatting

Signed-off-by: monai-bot <[email protected]>

* fixes integration

Signed-off-by: Wenqi Li <[email protected]>

* fixes mypy

Signed-off-by: Wenqi Li <[email protected]>

Co-authored-by: Wenqi Li <[email protected]>
Co-authored-by: Wenqi Li <[email protected]>
Co-authored-by: monai-bot <[email protected]>
@rijobro rijobro deleted the MetaTensor_channel branch May 19, 2022 11:08
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.

5 participants