Skip to content

3697 add spatial resample#3701

Merged
wyli merged 44 commits intoProject-MONAI:devfrom
wyli:add-spatial-resample
Feb 3, 2022
Merged

3697 add spatial resample#3701
wyli merged 44 commits intoProject-MONAI:devfrom
wyli:add-spatial-resample

Conversation

@wyli
Copy link
Copy Markdown
Contributor

@wyli wyli commented Jan 23, 2022

Fixes #3697

Description

adds a resample transform based on the affine matrices
this is provided with an option using monai's csrc backend

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 --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

@wyli wyli requested review from Nic-Ma, ericspod and rijobro January 23, 2022 17:45
@wyli
Copy link
Copy Markdown
Contributor Author

wyli commented Jan 23, 2022

this is non-breaking. I'll add more tests after addressing any major comments @Project-MONAI/core-reviewers

(.cu and .cpp were updated to remove some duplicated assignments.)

@Nic-Ma
Copy link
Copy Markdown
Contributor

Nic-Ma commented Jan 24, 2022

Hi @wyli ,

Thanks for the quick implementation.
I think the overall design is aligned with our discussion in the ImageWriter PR.
@ericspod Do you have any other concerns to go this way?

Thanks in advance.

@wyli wyli force-pushed the add-spatial-resample branch from f8c116c to 9b332c5 Compare January 25, 2022 10:13
@wyli
Copy link
Copy Markdown
Contributor Author

wyli commented Jan 25, 2022

/black

@wyli wyli force-pushed the add-spatial-resample branch from 0d7d7c3 to bace1fc Compare January 25, 2022 13:56
@ericspod
Copy link
Copy Markdown
Member

One issue is that the purpose of the various transforms is getting less clear, if I want to do an affine transform on an image do I use Affine, SpatialResample, AffineGrid (the docstring for this isn't clear about it's purpose though the arguments for __call__ show it won't work on images), or Resample. SpatialResample uses Affine and AffineTransform so it's clearer looking at the code to use this, but then why have Affine?

A more general issue that I encountered with Rand*DElastic is that the coordinate space for grid_pull (ours) and grid_sample (Pytorch) are different and so conversion has to be done in Resample here and below. I think in my profiling this was a significant cause of slowness with Rand*DElastic that was avoided in the way RandSmoothDeform created it's coord field with meshgrid and used the smooth field. I realize there are functional differences between RandSmoothDeform and Rand*DElastic but eliminating the need to manipulate coordinate fields, and keeping as much object creation in the constructor as possible, improved performance significantly with visually similar results. SpatialResample will have the same issue of performance since it ultimately uses Resample. It's a bit out of scope here but I had to mention it somewhere!

@wyli
Copy link
Copy Markdown
Contributor Author

wyli commented Jan 26, 2022

Sure for the first concern I'm revising the docstring to clarify the usage.

For the Resample coordinates issue,

  • pytorch uses [-1, 1] and axes (k, j, i), channel last
  • ours csrc backend uses [0, size-1] and axes [i, j, k]
  • for the grid computations in the data augmentations it seems natural to use [-(size-1)/2, (size-1)/2] (for example we often rotate an image around its centre instead of [0,0]).

These describe the same thing with different conventions, should be converted properly somewhere before calling the backend resampling API. previously it's done in the resampler. Now I added a norm_coords: bool in Resample to make the normalisation optional, it should speedup things a little bit, and the user should take care of the convention if norm_coords=False
in the future we may define a default convention and further optimise it...

@wyli wyli force-pushed the add-spatial-resample branch from bace1fc to 080f803 Compare January 26, 2022 12:40
wyli added 2 commits January 26, 2022 14:24
Signed-off-by: Wenqi Li <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
@wyli wyli force-pushed the add-spatial-resample branch from 5525b9d to 0dc88af Compare January 26, 2022 14:55
Signed-off-by: Wenqi Li <[email protected]>
@wyli wyli force-pushed the add-spatial-resample branch from b34c883 to 9dea4f7 Compare January 31, 2022 01:21
Copy link
Copy Markdown
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.

Hi @wyli ,

Thanks for your quick update.
Could you please help confirm and unify all the default value of dtype arg?

Thanks.

@Nic-Ma Nic-Ma requested review from ericspod and removed request for ericspod February 1, 2022 14:07
wyli added 4 commits February 1, 2022 15:11
This reverts commit e532490.

Signed-off-by: Wenqi Li <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
@wyli wyli requested a review from Nic-Ma February 2, 2022 08:25
@Nic-Ma
Copy link
Copy Markdown
Contributor

Nic-Ma commented Feb 2, 2022

Thanks for the quick update, this PR overall looks good to me now, put minor comments inline.
I didn't check carefully about the affine computation and C++ resampling logic as I am not very familiar with that.
@ericspod @rijobro Could you please help double confirm this PR as you put some comments before?
If you guys don't have any other comments or concerns, I will approve this big PR soon.

Thanks in advance.

@wyli
Copy link
Copy Markdown
Contributor Author

wyli commented Feb 2, 2022

/integration-test

@wyli
Copy link
Copy Markdown
Contributor Author

wyli commented Feb 2, 2022

/integration-test

@wyli wyli requested a review from Nic-Ma February 2, 2022 22:38
wyli added 2 commits February 2, 2022 23:27
Signed-off-by: Wenqi Li <[email protected]>
Copy link
Copy Markdown
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.

Looks good to me now.
@ericspod @rijobro do you guys have any other concerns or comments?

Thanks.

@wyli
Copy link
Copy Markdown
Contributor Author

wyli commented Feb 3, 2022

@wyli
Copy link
Copy Markdown
Contributor Author

wyli commented Feb 3, 2022

/build

1 similar comment
@wyli
Copy link
Copy Markdown
Contributor Author

wyli commented Feb 3, 2022

/build

@wyli wyli merged commit 5cce0af into Project-MONAI:dev Feb 3, 2022
@wyli wyli deleted the add-spatial-resample branch February 3, 2022 10:56
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.

decouple the resampling step from image writer

4 participants