#5772·MONAI

Gradients of the existing bilateral filter layers (normal and PHL)

Author: faebstn96Created Dec 19, 2022Updated Aug 23, 2026
LabelsContribution wantedFeature request

Describe the bug The backward operations of the existing bilateral filter layers (normal and PHL) do not calculate the correct derivative of the filter prediction with respect to the filter input. Currently, they use the exact same filter forward function as the backward operation, which is not the correct derivative as shown in here. This issue is related to #1840 and may be partly fixed by pull-request #5757.

As discussed with @wyli we should think about how to treat the existing bilateral filter implementations (normal and PHL). I believe that their forward operations are fast and valid operators which can be valuable for users. However, their provided gradient is not correct I think. The normal bilateral filter implementation could be replaced by the #5757 version in the medium term as their forward operations are almost identical.

To Reproduce Steps to reproduce the behavior:

  1. Install BUILD_MONAI=1 pip install --no-build-isolation git+https://github.com/Project-MONAI/MONAI#egg=monai
  2. Run the gradcheck
import torch
from monai.networks.layers import BilateralFilter


tensor_in = (torch.randn(2, 1, 10, 10, 10, dtype=torch.double, requires_grad=True))
filter = BilateralFilter.apply
gradcheck(filter, (tensor_in, 1.1, 0.5), eps=1e-6, atol=1e-5)

Expected behavior Gradcheck passes.

Current behavior Gradcheck fails as visible in the screenshot. gradcheck