You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Numerical inaccuracies with the random_walker #2360
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])))
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
@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?
Activity
jni commentedon Oct 31, 2016
@JDWarner any ideas about this? Pretty funny to see a probability of 1.1. =D
jmetz commentedon Oct 31, 2016
This code doesn't work for me... I get
This should probably be added as a bug also?!?
jni commentedon Oct 31, 2016
@jmetz LOL #2361 #2362
The correct mode is
'cg_mg'.jmetz commentedon Nov 1, 2016
Thanks for that @jni - I got it to work and can confirm a similar result using the
"cg"method also:I also generated output images (maximum projections along the first dimension as the data is 3d) for the two probabilities, for each method:
So a couple of things;
a) This isn't due to
cg_mgonly as it also occurs incgb) 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.
hugues-talbot commentedon Aug 23, 2017
Looks like a convergence problem, try with tol=1e-7
emmanuelle commentedon Sep 2, 2017
@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.
tolis passed to the linear solver (scipy.linalg.cgforcgmode, with a multigrid ruge_stuben_solver preconditioner forcg_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?hugues-talbot commentedon Sep 3, 2017