def filter_fn( self, source_raw: "ndarray[Any, dtype[float32]]" ) -> "ndarray[Any, dtype[float32]]": return fastfilters.structureTensorEigenvalues( source_raw, innerScale=self.innerScale, outerScale=self.outerScale, window_size=self.window_size)
def test_parallel_filter_structure_tensor(self): from ilastik.workflows.carving.carvingTools import parallel_filter name = 'structureTensorEigenvalues' shape = 3 * (128,) x = numpy.random.rand(*shape).astype('float32') sigma = 1.6 outer_scale = 2 * sigma res = parallel_filter(name, x, sigma, outer_scale=outer_scale, max_workers=4) exp = fastfilters.structureTensorEigenvalues(x, sigma, outer_scale) assert numpy.allclose(res, exp)
def test_vigra_compare(): a = np.random.randn(1000000).reshape(1000,1000).astype(np.float32)[:,:900] a = np.ascontiguousarray(a) sigmas = [1.0, 5.0, 10.0] for order in [0,1,2]: for sigma in sigmas: res_ff = ff.gaussianDerivative(a, sigma, order, window_size=3.5) res_vigra = vigra.filters.gaussianDerivative(a, sigma, [order,order], window_size=3.5) print("gaussian ", order, sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: ", order, sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: res_ff = ff.hessianOfGaussianEigenvalues(a, sigma, window_size=3.5) res_vigra = vigra.filters.hessianOfGaussianEigenvalues(a, sigma, window_size=3.5) print("HOG", sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6) or np.any(np.isnan(np.abs(res_ff - res_vigra))): raise Exception("FAIL: HOG", sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: res_ff = ff.gaussianGradientMagnitude(a, sigma, window_size=3.5) res_vigra = vigra.filters.gaussianGradientMagnitude(a, sigma, window_size=3.5) print("gradmag2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: gradmag2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: res_ff = ff.laplacianOfGaussian(a, sigma, window_size=3.5) res_vigra = vigra.filters.laplacianOfGaussian(a, sigma, window_size=3.5) print("laplacian2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: laplacian2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: for sigma2 in sigmas: res_ff = ff.structureTensorEigenvalues(a, sigma2, sigma, window_size=3.5) res_vigra = vigra.filters.structureTensorEigenvalues(a, sigma, sigma2, window_size=3.5) print("ST", sigma, sigma2, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6) or np.any(np.isnan(np.abs(res_ff - res_vigra))): raise Exception("FAIL: ST", sigma, sigma2, np.max(np.abs(res_ff - res_vigra)))
def test_vigra_compare3d(): a = np.random.randn(1000000).reshape(100,100,100).astype(np.float32)[:,:90,:80] a = np.ascontiguousarray(a) sigmas = [1.0, 5.0, 10.0] for order in [0,1,2]: for sigma in sigmas: res_ff = ff.gaussianDerivative(a, sigma, order) res_vigra = vigra.filters.gaussianDerivative(a, sigma, [order,order,order]) print("gaussian ", order, sigma, np.max(np.abs(res_ff - res_vigra)), np.sum(np.abs(res_ff-res_vigra))/np.size(res_vigra)) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: ", order, sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: res_ff = ff.gaussianGradientMagnitude(a, sigma) res_vigra = vigra.filters.gaussianGradientMagnitude(a, sigma) print("gradmag3d ", order, sigma, np.max(np.abs(res_ff - res_vigra)), np.sum(np.abs(res_ff-res_vigra))/np.size(res_vigra)) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: gradmag3d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: res_ff = ff.laplacianOfGaussian(a, sigma) res_vigra = vigra.filters.laplacianOfGaussian(a, sigma) print("laplacian3d ", order, sigma, np.max(np.abs(res_ff - res_vigra)), np.sum(np.abs(res_ff-res_vigra))/np.size(res_vigra)) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: laplacian3d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: res_ff = ff.hessianOfGaussianEigenvalues(a, sigma) res_vigra = vigra.filters.hessianOfGaussianEigenvalues(a, sigma) print("HOG", sigma, np.max(np.abs(res_ff - res_vigra)), np.sum(np.abs(res_ff-res_vigra))/np.size(res_vigra), np.sum(np.abs(res_ff-res_vigra))/np.size(res_vigra)) if np.sum(np.abs(res_ff-res_vigra))/np.size(res_vigra) > 1e-6 or np.any(np.isnan(np.abs(res_ff - res_vigra))): raise Exception("FAIL: HOG", sigma, np.max(np.abs(res_ff - res_vigra)), np.sum(np.abs(res_ff-res_vigra))/np.size(res_vigra)) for sigma in sigmas: for sigma2 in sigmas: res_ff = ff.structureTensorEigenvalues(a, sigma2, sigma) res_vigra = vigra.filters.structureTensorEigenvalues(a, sigma, sigma2) print("ST", sigma, sigma2, np.max(np.abs(res_ff - res_vigra)), np.sum(np.abs(res_ff-res_vigra))/np.size(res_vigra)) if np.sum(np.abs(res_ff-res_vigra))/np.size(res_vigra) > 1e-5 or np.any(np.isnan(np.abs(res_ff - res_vigra))): raise Exception("FAIL: ST", sigma, sigma2, np.max(np.abs(res_ff - res_vigra)), np.sum(np.abs(res_ff-res_vigra))/np.size(res_vigra))
def __call__(self, dataIn, slicing, featureArray): fIndex = 0 dataIn = numpy.require(dataIn,'float32').squeeze() dataWithChannel = extractChannels(dataIn, self.usedChannels) slicingEv = slicing + [slice(0,2)] for c in range(dataWithChannel.shape[2]): data = dataWithChannel[:,:,c] # pre-smoothed sigmaPre = self.sigmas[0]/2.0 preS = fastfilters.gaussianSmoothing(data, sigmaPre) for sigma in self.sigmas: neededScale = getScale(target=sigma, presmoothed=sigmaPre) preS = fastfilters.gaussianSmoothing(preS, neededScale) sigmaPre = sigma featureArray[:,:,fIndex] = preS[slicing] fIndex += 1 featureArray[:,:,fIndex] = fastfilters.laplacianOfGaussian(preS, neededScale)[slicing] fIndex += 1 featureArray[:,:,fIndex] = fastfilters.gaussianGradientMagnitude(preS, neededScale)[slicing] fIndex += 1 featureArray[:,:,fIndex:fIndex+2] = fastfilters.hessianOfGaussianEigenvalues(preS, neededScale)[slicingEv] fIndex += 2 #print("array shape",featureArray[:,:,:,fIndex:fIndex+3].shape) feat = fastfilters.structureTensorEigenvalues(preS, float(sigma)*0.3, float(sigma)*0.7)[slicingEv] #print("feat shape",feat.shape) featureArray[:,:,fIndex:fIndex+2] = feat fIndex += 2 assert fIndex == self.n_features
for sigma in sigmas: res_ff = ff.gaussianGradientMagnitude(a, sigma) res_vigra = vigra.filters.gaussianGradientMagnitude(a, sigma) print("gradmag2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: gradmag2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: res_ff = ff.laplacianOfGaussian(a, sigma) res_vigra = vigra.filters.laplacianOfGaussian(a, sigma) print("laplacian2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: laplacian2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: for sigma2 in sigmas: res_ff = ff.structureTensorEigenvalues(a, sigma2, sigma) res_vigra = vigra.filters.structureTensorEigenvalues(a, sigma, sigma2) print("ST", sigma, sigma2, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6) or np.any(np.isnan(np.abs(res_ff - res_vigra))): raise Exception("FAIL: ST", sigma, sigma2, np.max(np.abs(res_ff - res_vigra))) with open(sys.argv[1], 'w') as f: f.write('')
def filter_fn(self, source_raw: numpy.ndarray) -> numpy.ndarray: return fastfilters.structureTensorEigenvalues( source_raw, innerScale=self.innerScale, outerScale=self.outerScale, window_size=self.window_size)
res_ff = ff.gaussianGradientMagnitude(a, sigma) res_vigra = vigra.filters.gaussianGradientMagnitude(a, sigma) print("gradmag2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: gradmag2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: res_ff = ff.laplacianOfGaussian(a, sigma) res_vigra = vigra.filters.laplacianOfGaussian(a, sigma) print("laplacian2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: laplacian2d ", order, sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: for sigma2 in sigmas: res_ff = ff.structureTensorEigenvalues(a, sigma2, sigma) res_vigra = vigra.filters.structureTensorEigenvalues(a, sigma, sigma2) print("ST", sigma, sigma2, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6) or np.any( np.isnan(np.abs(res_ff - res_vigra))): raise Exception("FAIL: ST", sigma, sigma2, np.max(np.abs(res_ff - res_vigra))) with open(sys.argv[1], 'w') as f: f.write('')
with Timer() as tff: resff = ff.hessianOfGaussianEigenvalues(a, sigma) fact = tvigra.delta / tff.delta print("Timing HOG 2D with sigma = %f: vigra = %f, ff = %f --> speedup: %f" % (sigma, tvigra.delta, tff.delta, fact)) for sigma in [1,2,3,4,5,6,7,8,9,10]: sigma2 = 2*sigma with Timer() as tvigra: resvigra = vigra.filters.structureTensorEigenvalues(a, sigma, sigma2) with Timer() as tff: resff = ff.structureTensorEigenvalues(a, sigma, sigma2) fact = tvigra.delta / tff.delta print("Timing ST 2D with sigma = %f: vigra = %f, ff = %f --> speedup: %f" % (sigma, tvigra.delta, tff.delta, fact)) a = np.zeros((100,100,100)).astype(np.float32) for order in [0,1,2]: for sigma in [1,2,3,4,5]: with Timer() as tvigra: resvigra = vigra.filters.gaussianDerivative(a, sigma, [order, order, order]) with Timer() as tff: resff = ff.gaussianDerivative(a, sigma, [order, order, order])