Skip to content

Adding a median filter for 2/3D images#5307

Merged
wyli merged 12 commits intoProject-MONAI:devfrom
dzenanz:medianFilter
Oct 12, 2022
Merged

Adding a median filter for 2/3D images#5307
wyli merged 12 commits intoProject-MONAI:devfrom
dzenanz:medianFilter

Conversation

@dzenanz
Copy link
Copy Markdown
Contributor

@dzenanz dzenanz commented Oct 10, 2022

Co-authored-by: @ebrahimebrahim. Closes #5264.

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.

@dzenanz
Copy link
Copy Markdown
Contributor Author

dzenanz commented Oct 10, 2022

One test fails, I don't know why.

@dzenanz dzenanz marked this pull request as draft October 10, 2022 14:44
@dzenanz
Copy link
Copy Markdown
Contributor Author

dzenanz commented Oct 11, 2022

@wyli and @ebrahimebrahim can you provide some feedback? Or take over the PR?

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Oct 11, 2022

sure, I'll try to finalize this one..

@ebrahimebrahim
Copy link
Copy Markdown
Contributor

I think it looks good; thanks for working on this!

Some useful features, which could potentially be put off to future work:

  • be generic to spatial dimensionality. there is no reason it has to focus on 3D images only
  • add padding_mode options similar to those supported by other monai filters. torch.nn.functional.conv3d only does zero or "same" padding I think, so torch.nn.functional.pad may need to get involved to provide other options such as padding by a nonzero constant

ERROR: test_value_15 (__main__.TestMedianSmoothd)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
    return func(*(a + p.args), **p.kwargs)
  File "/home/dzenan/MONAI/tests/test_median_smoothd.py", line 72, in test_value
    result = MedianSmoothd(**argments)(image)
  File "/home/dzenan/MONAI/monai/transforms/intensity/dictionary.py", line 1020, in __call__
    d[key] = self.converter(d[key])
  File "/home/dzenan/MONAI/monai/transforms/intensity/array.py", line 1165, in __call__
    out_t: torch.Tensor = median_filter_instance(img_t.unsqueeze(0)).squeeze(0)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/dzenan/MONAI/monai/networks/layers/simplelayers.py", line 519, in forward
    x = median_filter(x, self.window, self.kernel)
  File "/home/dzenan/MONAI/monai/networks/layers/simplelayers.py", line 475, in median_filter
    features: torch.Tensor = F.conv3d(in_tensor.reshape(c, 1, *sshape), kernel, padding="same", stride=1)
RuntimeError: Expected 4-dimensional input for 4-dimensional weight[3, 1, 1, 3], but got 5-dimensional input of size [1, 1, 1, 3, 3] instead

----------------------------------------------------------------------
Ran 20 tests in 0.828s
@dzenanz
Copy link
Copy Markdown
Contributor Author

dzenanz commented Oct 11, 2022

I am stuck again. Judging by debugger's inspection, we are passing both 4D tensor and 4D kernel for 2D test case to F.conv3d() on line 475.

@dzenanz
Copy link
Copy Markdown
Contributor Author

dzenanz commented Oct 11, 2022

What we really want is pad with 'replicate' mode. Otherwise zeroes creep in all around the edges. With the small test images that is super-obvious, but it might be hard to debug with realistic-sized images later on, especially for other people.

@wyli If you can take a crack at this modification, that would be great. Otherwise I could give it a shot later, perhaps in a new PR.

Signed-off-by: Wenqi Li <[email protected]>
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Oct 11, 2022

What we really want is pad with 'replicate' mode. Otherwise zeroes creep in all around the edges. With the small test images that is super-obvious, but it might be hard to debug with realistic-sized images later on, especially for other people.

@wyli If you can take a crack at this modification, that would be great. Otherwise I could give it a shot later, perhaps in a new PR.

sure, I'll stop here with constant padding 0 for a basic implementation...

Signed-off-by: Wenqi Li <[email protected]>
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Oct 11, 2022

added an example here Project-MONAI/DocImages#16

@wyli wyli marked this pull request as ready for review October 11, 2022 22:05
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Oct 11, 2022

/build

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Oct 12, 2022

/build

@wyli wyli enabled auto-merge (squash) October 12, 2022 11:16
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.

looks good to me, padding mode and separable approximations are left for future PRs.

@wyli wyli changed the title Adding a median filter for 3D images Adding a median filter for 2/3D images Oct 12, 2022
@wyli wyli merged commit 9731823 into Project-MONAI:dev Oct 12, 2022
@dzenanz dzenanz deleted the medianFilter branch October 13, 2022 19:03
wyli added a commit that referenced this pull request Oct 14, 2022
Ref #5264. Follow-up to #5307. This is a more normal, intuitive variant
of median filtering.

Signed-off-by: Dženan Zukić <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Co-authored-by: Wenqi Li <[email protected]>
Co-authored-by: Wenqi Li <[email protected]>
bhashemian pushed a commit to JHancox/MONAI that referenced this pull request Oct 20, 2022
Ref Project-MONAI#5264. Follow-up to Project-MONAI#5307. This is a more normal, intuitive variant
of median filtering.

Signed-off-by: Dženan Zukić <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Co-authored-by: Wenqi Li <[email protected]>
Co-authored-by: Wenqi Li <[email protected]>
Signed-off-by: Behrooz <[email protected]>
wyli pushed a commit that referenced this pull request Oct 24, 2022
Co-authored-by: @ebrahimebrahim. Closes #5264.

<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] 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).
- [x] 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`.
- [x] In-line docstrings updated.
- [x] Documentation updated, tested `make html` command in the `docs/`
folder.
wyli added a commit that referenced this pull request Oct 24, 2022
Ref #5264. Follow-up to #5307. This is a more normal, intuitive variant
of median filtering.

Signed-off-by: Dženan Zukić <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Co-authored-by: Wenqi Li <[email protected]>
Co-authored-by: Wenqi Li <[email protected]>
wyli pushed a commit that referenced this pull request Oct 24, 2022
Co-authored-by: @ebrahimebrahim. Closes #5264.

<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] 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).
- [x] 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`.
- [x] In-line docstrings updated.
- [x] Documentation updated, tested `make html` command in the `docs/`
folder.
wyli added a commit that referenced this pull request Oct 24, 2022
Ref #5264. Follow-up to #5307. This is a more normal, intuitive variant
of median filtering.

Signed-off-by: Dženan Zukić <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Co-authored-by: Wenqi Li <[email protected]>
Co-authored-by: Wenqi Li <[email protected]>
KumoLiu pushed a commit that referenced this pull request Nov 2, 2022
Co-authored-by: @ebrahimebrahim. Closes #5264.

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] 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).
- [x] 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`.
- [x] In-line docstrings updated.
- [x] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: KumoLiu <[email protected]>
KumoLiu pushed a commit that referenced this pull request Nov 2, 2022
Ref #5264. Follow-up to #5307. This is a more normal, intuitive variant
of median filtering.

Signed-off-by: Dženan Zukić <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Co-authored-by: Wenqi Li <[email protected]>
Co-authored-by: Wenqi Li <[email protected]>
Signed-off-by: KumoLiu <[email protected]>
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.

Add median image filter as a transform

4 participants