Skip to content

2511 Add EnsureType transform#2522

Merged
Nic-Ma merged 17 commits intoProject-MONAI:devfrom
Nic-Ma:2511-add-ensure-tensor
Jul 7, 2021
Merged

2511 Add EnsureType transform#2522
Nic-Ma merged 17 commits intoProject-MONAI:devfrom
Nic-Ma:2511-add-ensure-tensor

Conversation

@Nic-Ma
Copy link
Copy Markdown
Contributor

@Nic-Ma Nic-Ma commented Jul 5, 2021

Fixes #2511 .

Description

This PR added the EnsureType and EnsureTyped transforms.

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.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 6, 2021

/black

@Nic-Ma Nic-Ma changed the title [WIP] 2511 Add EnsureTensor transform 2511 Add EnsureTensor transform Jul 6, 2021
@Nic-Ma Nic-Ma force-pushed the 2511-add-ensure-tensor branch from 23b7cf9 to 4ceb468 Compare July 6, 2021 06:09
@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 6, 2021

/black

@Nic-Ma Nic-Ma marked this pull request as ready for review July 6, 2021 06:10
@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 6, 2021

/integration-test

@Nic-Ma Nic-Ma requested review from ericspod, rijobro and wyli July 6, 2021 06:12
@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 6, 2021

/black

wyli
wyli previously approved these changes Jul 6, 2021
Copy link
Copy Markdown
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.

Thanks!

@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 6, 2021

Hi @wyli ,

Thanks for your review.
BTW, there is one logic slightly different from ToTensor transform: if passing a list like: [1, 2, 3, 4], EnsureTensor will convert it to [torch.Tensor(1), torch.Tensor(2), torch.Tensor(3), torch.Tensor(4)], while ToTensor will convert it to torch.Tensor([1, 2, 3, 4]). I prefer to the behavior of EnsureTensor because users may also pass [[1], [2, 3], 4] that can't convert to 1 Tensor array.

Thanks.

@Nic-Ma Nic-Ma enabled auto-merge (squash) July 6, 2021 15:12
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Jul 6, 2021

Hi @wyli ,

Thanks for your review.
BTW, there is one logic slightly different from ToTensor transform: if passing a list like: [1, 2, 3, 4], EnsureTensor will convert it to [torch.Tensor(1), torch.Tensor(2), torch.Tensor(3), torch.Tensor(4)], while ToTensor will convert it to torch.Tensor([1, 2, 3, 4]). I prefer to the behavior of EnsureTensor because users may also pass [[1], [2, 3], 4] that can't convert to 1 Tensor array.

Thanks.

sure I feel the transform shouldn't change the data structure, but does it mean we shouldn't deprecate ToTensor?

@Nic-Ma Nic-Ma disabled auto-merge July 6, 2021 15:49
@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 6, 2021

Hi @wyli ,

That's a good point, yes, I think we should keep ToTensor just like the AddChannel and EnsureChannelFirst.
Let me update the PR.

Thanks.

@rijobro
Copy link
Copy Markdown
Contributor

rijobro commented Jul 6, 2021

I think we could have more general solutions. We have LoadImage instead of LoadNifti, so we could also have ToType instead of ToTensor. This would take the output type as argument (Numpy, Tensor) etc. and could also have the option to do recursively or not. This would solve your problem, whilst not creating a confusing amount of classes that might leave users unsure as to which they should be using. What do you think?

@wyli wyli dismissed their stale review July 6, 2021 17:00

perhaps have a generic EnsureType? may need some discussions here

@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 6, 2021

Hi @rijobro ,

I think the reason we add this EnsureTensor transform is because almost every MONAI program needs to use ToTensor transform in preprocessing and postprocessing now, but ToNumpy is only used in very few cases.
So we want to enhance the transform to convert data to Tensor.

Thanks.

@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 6, 2021

Hi @wyli @rijobro @ericspod ,

So all you guys prefer to EnsureType(dtype: str) now?
If so, I will try to update the PR ASAP and write the decollate document based on it for v0.6 release tomorrow.

Thanks in advance.

@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 6, 2021

Or maybe we can have both ToType and EnsureTensor transforms?
For most cases, users just need to put EnsureTensor in their pre-transforms for collate and post-transforms after decollate.

Thanks.

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Jul 6, 2021

Hi @wyli @rijobro @ericspod ,

So all you guys prefer to EnsureType(dtype: str) now?
If so, I will try to update the PR ASAP and write the decollate document based on it for v0.6 release tomorrow.

Thanks in advance.

I think we could have a EnsureType(dtype='tensor') API? with an initial and default support for 'tensor'. this should be minor change for the current PR, we can expand it if time permits...

@ericspod
Copy link
Copy Markdown
Member

ericspod commented Jul 6, 2021

An EnsureType transform and dict version makes sense. We could do a simple version for now that just substitutes in for ToTensor but think about defining it later to convert to and from Pytorch, Numpy, and native Python types as the case allows.

We might want to keep ToTensor as an alias to this since so much code relies on it and it's a common idiom Torchvision and other libraries that follow that pattern.

@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 7, 2021

Hi @wyli @ericspod ,

Plan sounds good, let me try to update this PR today.

Thanks.

@Nic-Ma Nic-Ma changed the title 2511 Add EnsureTensor transform 2511 Add EnsureType transform Jul 7, 2021
@Nic-Ma Nic-Ma requested review from rijobro and wyli July 7, 2021 03:20
@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 7, 2021

Hi @wyli @rijobro @ericspod ,

Thanks for your great suggestions.
I updated the PR according to your comments.
Could you please help review it again?

Thanks in advance.

@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 7, 2021

/black

@Nic-Ma Nic-Ma enabled auto-merge (squash) July 7, 2021 11:33
@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented Jul 7, 2021

Hi @wyli ,

Seems the GPU tests are running for 3 hours, is there any issue or expected behavior?

Thanks.

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Jul 7, 2021

I think it's because I pushed the 0.6.0rc2 tag as requested by Sachi, this has triggered full tests...

@Nic-Ma Nic-Ma merged commit e9a4d33 into Project-MONAI:dev Jul 7, 2021
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.

Add EnsureTensor transform

5 participants