Skip to content

2145 Extract invert logic from TransformInverter handler#2165

Merged
Nic-Ma merged 14 commits intoProject-MONAI:devfrom
Nic-Ma:2145-invert-transform
May 10, 2021
Merged

2145 Extract invert logic from TransformInverter handler#2165
Nic-Ma merged 14 commits intoProject-MONAI:devfrom
Nic-Ma:2145-invert-transform

Conversation

@Nic-Ma
Copy link
Copy Markdown
Contributor

@Nic-Ma Nic-Ma commented May 10, 2021

Fixes #2145 .

Description

This PR extracted the main logic of TransformInverter to a post transform. So non-ignite users can freely use this feature in the regular PyTorch program.
It also moved the BatchInverseTransform from "monai/data/" to "monai/transforms/" as it's a transform.

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 May 10, 2021

/black

@Nic-Ma Nic-Ma requested review from ericspod, rijobro and wyli May 10, 2021 07:08
monai-bot and others added 4 commits May 10, 2021 07:10
@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented May 10, 2021

/black

@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented May 10, 2021

Hi @rijobro ,

I slightly changed the _BatchInverseDataset to avoid circle-import, I didn't figure out why the TTA unit tests failed...
If adding "self.XXX = transform" in the init of _BatchInverseDataset, the unit tests can pass (XXX can be any word).
Could you please help take a look? Maybe this is some potential logic in your TTA module that I ignored?

Thanks in advance.

@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented May 10, 2021

Paste the error log:

======================================================================
ERROR: test_single_transform (tests.test_testtimeaugmentation.TestTestTimeAugmentation)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/MONAI/MONAI/monai/data/utils.py", line 264, in list_data_collate
    ret[k] = default_collate([d[k] for d in data])
  File "/opt/hostedtoolcache/Python/3.8.10/x64/lib/python3.8/site-packages/torch/utils/data/_utils/collate.py", line 55, in default_collate
    return torch.stack(batch, 0, out=out)
TypeError: expected Tensor as element 1 in argument 0, but got numpy.ndarray

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/work/MONAI/MONAI/tests/test_testtimeaugmentation.py", line 147, in test_single_transform
    tta(self.get_data(1, (20, 20)))
  File "/home/runner/work/MONAI/MONAI/monai/data/test_time_augmentation.py", line 183, in __call__
    inv_batch = inverter(inferred_dict)
  File "/home/runner/work/MONAI/MONAI/monai/transforms/inverse_batch_transform.py", line 92, in __call__
    return first(inv_loader)
  File "/home/runner/work/MONAI/MONAI/monai/utils/misc.py", line 71, in first
    for i in iterable:
  File "/opt/hostedtoolcache/Python/3.8.10/x64/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 517, in __next__
    data = self._next_data()
  File "/opt/hostedtoolcache/Python/3.8.10/x64/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 557, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "/opt/hostedtoolcache/Python/3.8.10/x64/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "/home/runner/work/MONAI/MONAI/monai/data/utils.py", line 288, in list_data_collate
    raise TypeError(re_str)
TypeError: expected Tensor as element 1 in argument 0, but got numpy.ndarray
Collate error on the key 'label' of dictionary data.

MONAI hint: if your transforms intentionally create mixtures of torch Tensor and numpy ndarray, creating your `DataLoader` with `collate_fn=pad_list_data_collate` might solve this problem (check its documentation).

Signed-off-by: Nic Ma <[email protected]>
@rijobro
Copy link
Copy Markdown
Contributor

rijobro commented May 10, 2021

I just tried the current state of your PR and test_testtimeaugmentation.py passes. Have you solved the problem? Or do I need to change something to be able to reproduce the problem?

@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented May 10, 2021

Hi @rijobro ,

I think the root cause is that: the 2 failed unit tests of TTA missed ToTensorD() transform, so it means that we invert a Tensor data in a random transform, if do_transform, it will convert to numpy array, if not, still be Tensor, so sometimes can't collate a batch.
About my previous finding: as it's random behavior, adding self.transform = transform may affect the random result and if I change to prob=0.2, it also failed.
Now I just changed prob=1.0 in the TTA unit tests.
In the long term, I think maybe we can change the inverse logic to convert Tensor -> numpy no matter do_transform is True or not, or we change the TTA logic to convert Tensor -> numpy before inverse, now it's numpy -> Tensor.
what do you think?

Thanks.

@rijobro
Copy link
Copy Markdown
Contributor

rijobro commented May 10, 2021

I see. Perhaps best to implement both, since the inverse of ToTensor is ToNumpy, so that makes sense. Also inverse transforms will always need to be preceded by a ToTensor logic.

@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented May 10, 2021

Hi @rijobro ,

Thanks for your help, yes, ToTensorD is supposed to be used in all MONAI transform chains, so the test failure is not a regular use case.
But any way, we can enhance the numpy and Tensor logic to make it more clear later.
This PR mainly decoupled invert transform with ignite, users can use it as post transform in regular program.
Could you please help review it?

Thanks in advance.

Copy link
Copy Markdown
Contributor

@rijobro rijobro left a comment

Choose a reason for hiding this comment

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

sounds good, thanks for this

Signed-off-by: Nic Ma <[email protected]>
@Nic-Ma
Copy link
Copy Markdown
Contributor Author

Nic-Ma commented May 10, 2021

Hi @vfdev-5 ,

In this PR, I temporarily combined state.batch and state.output to construct a unique dict for the dict-based transform which is decoupled from ignite, just as we discussed in the ignite ticket before.
Could you please double confirm whether this PR has any potential issues?

Thanks.

@vfdev-5
Copy link
Copy Markdown
Member

vfdev-5 commented May 10, 2021

@Nic-Ma I left a small comment in the code. Otherwise, I do not see any other issues with this approach.

Thanks for pinging!

@Nic-Ma Nic-Ma force-pushed the 2145-invert-transform branch from 01cbfb2 to a211d78 Compare May 10, 2021 13:09
@Nic-Ma Nic-Ma enabled auto-merge (squash) May 10, 2021 13:10
@Nic-Ma Nic-Ma merged commit 3bd0245 into Project-MONAI:dev May 10, 2021
yanielc pushed a commit to yanielc/MONAI that referenced this pull request May 10, 2021
…ONAI#2165)

* [DLMED] add Invertd transform

Signed-off-by: Nic Ma <[email protected]>

* [MONAI] python code formatting

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

* [DLMED] fix doc build

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] skip min test

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] fix flake8 issue

Signed-off-by: Nic Ma <[email protected]>

* [MONAI] python code formatting

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

* [DLMED] fix CI test

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] enhance docs

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] enhance doc-string

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] update according to comments

Signed-off-by: Nic Ma <[email protected]>

Co-authored-by: monai-bot <[email protected]>
Signed-off-by: Yaniel Cabrera <[email protected]>
yanielc pushed a commit to yanielc/MONAI that referenced this pull request May 13, 2021
…ONAI#2165)

* [DLMED] add Invertd transform

Signed-off-by: Nic Ma <[email protected]>

* [MONAI] python code formatting

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

* [DLMED] fix doc build

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] skip min test

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] fix flake8 issue

Signed-off-by: Nic Ma <[email protected]>

* [MONAI] python code formatting

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

* [DLMED] fix CI test

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] enhance docs

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] enhance doc-string

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] update according to comments

Signed-off-by: Nic Ma <[email protected]>

Co-authored-by: monai-bot <[email protected]>
Signed-off-by: Yaniel Cabrera <[email protected]>
yanielc pushed a commit to yanielc/MONAI that referenced this pull request May 13, 2021
…ONAI#2165)

* [DLMED] add Invertd transform

Signed-off-by: Nic Ma <[email protected]>

* [MONAI] python code formatting

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

* [DLMED] fix doc build

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] skip min test

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] fix flake8 issue

Signed-off-by: Nic Ma <[email protected]>

* [MONAI] python code formatting

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

* [DLMED] fix CI test

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] enhance docs

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] enhance doc-string

Signed-off-by: Nic Ma <[email protected]>

* [DLMED] update according to comments

Signed-off-by: Nic Ma <[email protected]>

Co-authored-by: monai-bot <[email protected]>
Signed-off-by: Yaniel Cabrera <[email protected]>
@Nic-Ma Nic-Ma deleted the 2145-invert-transform branch July 2, 2021 23:36
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.

extract main logic of TransformInverter into utils

4 participants