Skip to content

Numerical inaccuracies with the random_walker #2360

Closed

Description

@hugues-talbot

Description

The random walker segmentation code is very useful but behaves sometimes very strangely with respect to numerical results.

See following code (sorry this is slow):

result:

Should be between 0 and 1: [0.000000 ; 1.103701]

Way to reproduce

import numpy as np
from skimage.filters import gaussian
from skimage.segmentation import random_walker

np.random.seed(42)
blob3d = np.random.randn(101,101,101) # 3D image of random noise
blob3ds = gaussian(blob3d,sigma=10.) # makes a Gaussian random field
p05=np.percentile(blob3ds,5.) # Normalize the GRF between 0 and 1
p95=np.percentile(blob3ds,95.)
blob3ds = (blob3ds-p05)/(p95-p05)
blob3ds = np.clip(blob3ds,0,1)

markers3d = np.zeros((101,101,101)) 
markers3d[45:55,45:55,45:55] = 1  # central box
markers3d[:,:,0]=markers3d[:,0,:]=markers3d[0,:,:] = 2 # border of the box
markers3d[100,:,:]=markers3d[:,100,:] = markers3d[:,:,100] = 2 # border of the box cont.

grow3d = random_walker(blob3ds, markers3d, beta=5e4, mode='cg_mg', tol=1e-5, return_full_prob=True)

print("Should be between 0 and 1: [%f ; %f]" % (np.min(grow3d[1]), np.max(grow3d[1])))

Activity

  1. jni commented on Oct 31, 2016

    @jni
    Member

    @JDWarner any ideas about this? Pretty funny to see a probability of 1.1. =D

  2. jmetz commented on Oct 31, 2016

    @jmetz
    Contributor

    This code doesn't work for me... I get

    Traceback (most recent call last):
      File "issue_2360.py", line 18, in <module>
        grow3d = random_walker(blob3ds, markers3d, beta=5e4, mode='mg_cg', tol=1e-5, return_full_prob=True)
      File ".../skimage/segmentation/random_walker_segmentation.py", line 459, in random_walker
        for Xline in X])
    UnboundLocalError: local variable 'X' referenced before assignment
    

    This should probably be added as a bug also?!?

  3. jni commented on Oct 31, 2016

    @jni
    Member

    @jmetz LOL #2361 #2362

    The correct mode is 'cg_mg'.

  4. jmetz commented on Nov 1, 2016

    @jmetz
    Contributor

    Thanks for that @jni - I got it to work and can confirm a similar result using the "cg" method also:

    Method cg
    Should be between 0 and 1: [0.000000 ; 1.256141]
    Method cg_mg
    Should be between 0 and 1: [0.000000 ; 1.103701]
    

    I also generated output images (maximum projections along the first dimension as the data is 3d) for the two probabilities, for each method:

    figure_1
    figure_2
    figure_3
    figure_4

    So a couple of things;

    a) This isn't due to cg_mg only as it also occurs in cg
    b) It only seems to be affecting the second probability image

    Don't know the algorithm well enough to fix it yet but will keep digging.

  5. hugues-talbot commented on Aug 23, 2017

    @hugues-talbot
    Author

    Looks like a convergence problem, try with tol=1e-7

  6. emmanuelle commented on Sep 2, 2017

    @emmanuelle
    Member

    @hugues-talbot sorry I didn't see your issue earlier, I must have been busy when you opened it ;-).

    Yes, the problem seems related to a numerical convergence problem, since decreasing tol solves it, and decreasing the value of beta also solves it. tol is passed to the linear solver (scipy.linalg.cg for cg mode, with a multigrid ruge_stuben_solver preconditioner for cg_mg). It would be possible to test whether probabilities are between [0, 1] (with some tolerance) and issue a warning suggesting to change tol or beta if it is not. How would that be?

  7. hugues-talbot commented on Sep 3, 2017

    @hugues-talbot
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions