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

1198 efficient differentiable bilateral filter #1375

Merged
merged 57 commits into from
Dec 18, 2020
Merged

1198 efficient differentiable bilateral filter #1375

merged 57 commits into from
Dec 18, 2020

Conversation

charliebudd
Copy link
Collaborator

@charliebudd charliebudd commented Dec 16, 2020

Fixes #1198 .

Description

Adds two bilateral filter algorithms to the c++ extention module. Each have both a cpu and a cuda implementation.
One works by a bruteforce kernel, the other using a more refined approximate solution presented here...https://graphics.stanford.edu/papers/permutohedral/
The desired implementation is chosen via a boolean flag at the python interface level.

There are improvements which could be made but this is long overdue and already a very large PR.
There are also two issues I'm aware of but both are imperceptible at normal scales...

  • The approximate cuda implementation shows some small variablity in its output.
  • Both the approximate cpu and approximate cuda implementations show slight asymetry in the filtered output (This may be an artifact of the approximation method)

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

@charliebudd charliebudd changed the title 1198 efficient differentiable bilateral filter [WIP]1198 efficient differentiable bilateral filter Dec 16, 2020
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
… some data to process

Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
…ode currently untested due to windows build issues, implementation from https://github.com/SamuelJoutard/Permutohedral_attention_module

Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
…s caused on windows

Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
@wyli
Copy link
Contributor

wyli commented Dec 18, 2020

/black

Signed-off-by: charliebudd <charles.budd@kcl.ac.uk>
@charliebudd charliebudd changed the title [WIP]1198 efficient differentiable bilateral filter 1198 efficient differentiable bilateral filter Dec 18, 2020
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.

thanks @charliebudd this is very nice, would be great to have a short notebook demo

@charliebudd charliebudd merged commit 70be9ab into Project-MONAI:master Dec 18, 2020
@charliebudd charliebudd deleted the 1198-efficient-differentiable-bilateral-filter branch December 18, 2020 14:24
@wyli wyli mentioned this pull request Dec 18, 2020
@charliebudd charliebudd restored the 1198-efficient-differentiable-bilateral-filter branch December 18, 2020 15:00
@charliebudd charliebudd deleted the 1198-efficient-differentiable-bilateral-filter branch December 18, 2020 15:18
@faebstn96
Copy link
Contributor

Hi @charliebudd ,
Thanks for your implementation. Unfortunately, I am failing to get the layer running. When trying to call the forward pass of the bilateral filter with
filter_layer = monai.networks.layers.BilateralFilter(spatial_sigma=5, color_sigma=0.5, fast_approx=True)
filter_layer.apply(tensor_in) # tensor_in: arbitrary 3,4 or 5 dim torch tensor

I get problems with the save_for_backward function, as it does not accept the filter parameters (I think save_for_backward is actually made for storing the input tensor and not for the parameters):
TypeError: save_for_backward can only save variables, but argument 0 is of type int

Wouldn't it be easier to implement the bilateral filter class as a nn.Module instead of torch.autograd.Function (like, e.g., the Gaussian filter)? Then, one could store the parameters in an instance variable.

Thanks for your help!

@faebstn96
Copy link
Contributor

I just recognized, you can just assign the parameters directly to ctx instead of using save_for_backward. E.g., like
ctx.spatial_sigma = spatial_sigma

Then the layer at least does not throw an error.

@charliebudd
Copy link
Collaborator Author

charliebudd commented Mar 29, 2021

Indeed, thanks for pointing out this out. I have made the changes and have drafted a PR #1888. Sorry for the error.

@faebstn96
Copy link
Contributor

Thank you so much!

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.

Efficient, differentiable bilateral filter
3 participants