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

Clip output in denoise_tv_bregman #1943

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Conversation

jni
Copy link
Member

@jni jni commented Feb 14, 2016

This is pretty simple: always clip the output of denoise_tv_bregman in order to prevent invalid output due to numerical errors. Fixes #1939. However, the tests are failing in 2.7 and 3.5. Anyone got any idea why???

@ahojnnes
Copy link
Member

I don't think this is the right solution, the clipping should be optional. There is nothing in the TV denoising equation that guarantees that the input range is not altered.

@ahojnnes
Copy link
Member

Optional in the sense that there should be a parameter that would default to true.

@@ -134,7 +134,7 @@ def denoise_tv_bregman(image, weight, max_iter=100, eps=1e-3, isotropic=True):

Returns
-------
u : ndarray
out : ndarray
Denoised image.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a reason this variable is called u. It refers to a variable in the equation further above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's very clear... I would suggest that we keep out or denoised (both are used in the file) and replace u in the equation with that name. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jni Sounds good!

@jni
Copy link
Member Author

jni commented Feb 14, 2016

@ahojnnes sure, I can make it optional, but it still doesn't help me figure out why np.clip isn't doing its job in the tests! Any ideas?

@jni
Copy link
Member Author

jni commented Feb 14, 2016

Or why there's no Travis???? =O

@emmanuelle
Copy link
Member

@jni this is so weird... Hope Travis will reappear on your next commit with clipping being optional :-)

@jni jni changed the title [WIP] Clip output in denoise_tv_bregman [MRG] Clip output in denoise_tv_bregman Feb 22, 2016
@jni
Copy link
Member Author

jni commented Feb 22, 2016

@ahojnnes (pinging you because you seemed most familiar with this algorithm, correct me if wrong) so it looks like the failures are due to sometimes producing nan output for the test input. I added a print(np.max(out)) statement before the assertion, and then ran the tests three times in a row. Nothing changed between the runs at all:

(skimtest)
nuneziglesiasj@aradyne Mon Feb 22 16:23
 ~/projects/scikit-image/skimage/restoration/tests (denoise-clip)$ nosetests test_denoise:test_denoise_tv_bregman_approx_errors
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
(skimtest)
nuneziglesiasj@aradyne Mon Feb 22 16:24
 ~/projects/scikit-image/skimage/restoration/tests (denoise-clip)$ nosetests test_denoise:test_denoise_tv_bregman_approx_errors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
(skimtest)
nuneziglesiasj@aradyne Mon Feb 22 16:24
 ~/projects/scikit-image/skimage/restoration/tests (denoise-clip)$ nosetests test_denoise:test_denoise_tv_bregman_approx_errors
F
======================================================================
FAIL: Ensure output is valid image when valid input image is given.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/nuneziglesiasj/anaconda/envs/skimtest/lib/python3.5/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/Volumes/Projects/scikit-image/skimage/restoration/tests/test_denoise.py", line 172, in test_denoise_tv_bregman_approx_errors
    assert np.max(out) <= 1
AssertionError:
-------------------- >> begin captured stdout << ---------------------
nan

... ??? Any ideas?

@ahojnnes
Copy link
Member

ahojnnes commented Feb 22, 2016 via email

@ahojnnes
Copy link
Member

Hm... there is no randomness in the algorithm. The only reason I can image is uninitialized memory, e.g., here https://github.com/jni/scikit-image/blob/denoise-clip/skimage/restoration/_denoise_cy.pyx#L173 ?

@jni
Copy link
Member Author

jni commented Feb 22, 2016

@ahojnnes that was a good catch! But it doesn't explain it — each of those variables is later initialised before use. Unless I'm missing something... Which obviously I am... =\

@ahojnnes
Copy link
Member

ahojnnes commented Aug 26, 2016

@jni Let's finally solve this nasty issue. I think, I found the problem: there is an out of bounds access and the loop should be for r in range(1, rows): / for c in range(1, cols): and not rows + 1 / cols + 1. Can you try?

@jni
Copy link
Member Author

jni commented Aug 27, 2016

@ahojnnes 😱 😱 😱 Well-spotted! And it seems to have fixed it!

@ahojnnes
Copy link
Member

@jni Hm. The tests still seem to fail in all environments.

@jni
Copy link
Member Author

jni commented Aug 27, 2016

Hmm worked on my laptop. Will investigate soon, can't right now...

@ahojnnes
Copy link
Member

@jni Gentle ping :-)

@jni
Copy link
Member Author

jni commented Aug 30, 2016

I'm on holiday till the 5th, thought I could catch a few mins for this but now looking unlikely... Will revisit next week.

@ahojnnes ahojnnes changed the title [MRG] Clip output in denoise_tv_bregman [MRG+1] Clip output in denoise_tv_bregman Sep 4, 2016
@ahojnnes
Copy link
Member

ahojnnes commented Sep 4, 2016

LGTM, if the test case is fixed.

@soupault soupault added the ⏩ type: Enhancement Improve existing features label Sep 4, 2016
@jni
Copy link
Member Author

jni commented Sep 5, 2016

@ahojnnes oh wow, LOL, just saw the test failure, I wonder if the out of range access was causing most of the range problems anyway! Working on this...

@ahojnnes
Copy link
Member

ahojnnes commented Sep 9, 2016

@jni Ping :-)

@jni
Copy link
Member Author

jni commented Sep 9, 2016

@ahojnnes I've had this tab open for the past few days. I'm not sure how to proceed now that the original "failure" passes... I was going to try to find another failing case, but that might be hard, so it's in the back burner... Thoughts?

@soupault soupault changed the title [MRG+1] Clip output in denoise_tv_bregman Clip output in denoise_tv_bregman Oct 25, 2016
@alexdesiqueira
Copy link
Member

@jni how are we on this one? :)

@pep8speaks
Copy link

pep8speaks commented May 25, 2019

Hello @jni! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2019-05-28 23:19:56 UTC

@jni
Copy link
Member Author

jni commented May 25, 2019

@alexandrejaguar thanks for the ping and all the other triaging you've been doing, it's great! Let's see how this rebase turns out... =)

@alexdesiqueira
Copy link
Member

@jni glad I could help!

@@ -111,6 +110,7 @@ def _denoise_tv_bregman(np_floats[:, :, ::1] image, np_floats weight,
shape_ext = (rows2, cols2, dims)

cdef:
np_floats[:, :, ::1] cu = u
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is causing the CI failures: u is not declared and cu is not used afterward...
Simply removing this line may solve the problem.

@rfezzani
Copy link
Member

@jni Is this requirement (clipping denoise_tv_bregman output) still up to date?

@grlee77
Copy link
Contributor

grlee77 commented Feb 20, 2020

@jni Is this requirement (clipping denoise_tv_bregman output) still up to date?

I think it should only clip by default if the input is not floating point to make it consistent with denoise_wavelet. That function used to always clip the output, but was modified to only do so for non-float inputs since img_as_float no longer rescales to [0, 1] (or [-1, 1]):

    # floating-point inputs are not rescaled, so don't clip their output.
    clip_output = image.dtype.kind != 'f'

If you want to have an explicit clip argument, then I would set the default to None and then set to True only for non-float inputs, as done above.

Base automatically changed from master to main February 18, 2021 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⏩ type: Enhancement Improve existing features
Projects
None yet
8 participants