Skip to content

5867 add tjbf layer#5876

Merged
wyli merged 10 commits intoProject-MONAI:devfrom
faebstn96:5867-add_tjbf_layer
Jan 20, 2023
Merged

5867 add tjbf layer#5876
wyli merged 10 commits intoProject-MONAI:devfrom
faebstn96:5867-add_tjbf_layer

Conversation

@faebstn96
Copy link
Copy Markdown
Contributor

Fixes #5867.

Description

I integrated the trainable joint bilateral filter layer (TJBF) in the MONAI repository as a new PyTorch filter layer. The TJBF contains an analytical gradient derivation toward its filter parameters, its guidance image, and its noisy input image which enables gradient-based optimization within the PyTorch graph. See here for more details on the gradient derivation. Unit tests were added that check the filter output as well as the gradient computations.

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
Copy link
Copy Markdown
Contributor

wyli commented Jan 20, 2023

/black

Signed-off-by: monai-bot <[email protected]>
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, do you think the component names should have _3d in them? all the logic seems to be coupled with three spatial dimensions.

e.g.

TrainableJointBilateralFilterForward(
torch::Tensor inputTensor,
torch::Tensor guidanceTensor,
float sigma_x,
float sigma_y,
float sigma_z,
float colorSigma) {

sigma x: trainable standard deviation of the spatial filter kernel in x direction.
sigma y: trainable standard deviation of the spatial filter kernel in y direction.
sigma z: trainable standard deviation of the spatial filter kernel in z direction.

@faebstn96
Copy link
Copy Markdown
Contributor Author

Do you mean renaming them to, e.g., sigma_3d_x etc? If you think that makes the code more readable I can do that.

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Jan 20, 2023

I think in general the question is, as a user looking at this set of components

_C, _ = optional_import("monai._C")
__all__ = ["BilateralFilter", "PHLFilter", "TrainableBilateralFilter", "TrainableJointBilateralFilter"]

the underlying concepts are very similar but the input argument styles are different...

ideally the signatures should be unified, but maybe that's out of the scope of this PR. so I'm thinking alternatively if we could rename the new components as BilateralFilter3D and JointBilateralFilter3D (do the same renaming for the c++ functions), and in the docstring clarify the implementation differences among the components in file monai/networks/layers/filtering.py

@faebstn96
Copy link
Copy Markdown
Contributor Author

So, the inputs to the TrainableBilateralFilter and TrainableJointBilateralFilter can also be [B, C, X] or [B, C, X, Y] tensors as there is some unsqueezing done:

# C++ extension so far only supports 5-dim inputs.
if len_input == 3:
input_tensor = input_tensor.unsqueeze(3).unsqueeze(4)
elif len_input == 4:
input_tensor = input_tensor.unsqueeze(4)

However, you are right, the underlying C++/Cuda code operates on three dimensions.
Though, I just recognized we should maybe make the spatial sigma parameters a tuple input like sigma_spatial=(sigma_x, sigma_y, sigma_z) and then check during layer initialization if len(sigma_spatial) equals the spatial dimensions of the input image/guidance tensors, right? This would make the layer flexible for 1D, 2D, and 3D inputs.

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Jan 20, 2023

agreed, if it's renamed to spatial_sigma, then it's consistent with the existing BilateralFilter

@faebstn96
Copy link
Copy Markdown
Contributor Author

Great, I just refactored the spatial_sigma initializations accordingly.

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, I'll merge this once the tests are ok (I'll push a workaround for the premerge test issue #5859)

monai-bot and others added 3 commits January 20, 2023 16:33
Signed-off-by: monai-bot <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Jan 20, 2023

/build

@wyli wyli enabled auto-merge (squash) January 20, 2023 16:59
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Jan 20, 2023

/build

@wyli wyli merged commit 2d0e021 into Project-MONAI:dev Jan 20, 2023
@faebstn96 faebstn96 deleted the 5867-add_tjbf_layer branch January 31, 2023 09:50
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.

Fully differentiable and trainable Joint Bilateral Filter (TJBF) layer

3 participants