def generate_mc_grid(dimx, dimy, operator="adder"): labels=dimx*dimy nos = numpy.ones(labels, dtype=numpy.uint64) * labels gm = opengm.gm(nos, operator, 0) for y in range(dimy): for x in range(dimx): if x + 1 < dimx: vis = [x + y * dimx, x + 1 + y * dimx] assert vis.sort is not None vis.sort l=random.random()*2.0 - 1.0 fr=opengm.pottsFunction([labels,labels],0.0,l) fid2=gm.addFunction(fr) gm.addFactor(fid2, vis) if y + 1 < dimy: vis = [x + y * dimx, x + (y + 1) * dimx] vis.sort() l=random.random()*2.0 - 1.0 fr=opengm.pottsFunction([labels,labels],0.0,l) fid2=gm.addFunction(fr) gm.addFactor(fid2, vis) return gm
def buildGM(img,rag,dataImage,numLabels,boundaryPixels,regionPixels,beta,sigma,verbose=False): print "get region clustering" regionFeatures=numpy.ones([rag.numberOfRegions(),3],dtype=numpy.float64) print "lab type in gm" ,type(img) print "lab type in gm shape" ,img.shape npimg=numpy.ones(img.shape) npimg[:,:,:]=img[:,:,:] print "npimg type in gm shape" ,npimg.shape for r in range(rag.numberOfRegions()): for c in range(3): regionFeatures[r,c]=numpy.mean(npimg[regionPixels[r][:,0],regionPixels[r][:,1]][c]) print "do clustering" code,dists=doClustering(regionFeatures,k=numLabels,steps=100) dists=(dists-dists.min())/(dists.max()-dists.min()) if verbose==True : print "get boundary evidence" boundaryEvidence=numpy.ones(rag.numberOfBoundaries(),dtype=numpy.float64) be=numpy.ones([rag.numberOfBoundaries(),2],dtype=numpy.float64) energy=numpy.ones([rag.numberOfBoundaries(),2],dtype=numpy.float64) for b in range(rag.numberOfBoundaries()): boundaryEvidence[b]=numpy.mean(dataImage[boundaryPixels[b][:,0],boundaryPixels[b][:,1]]) r=rag.adjacentRegions(b) boundaryEvidence=(boundaryEvidence-boundaryEvidence.min())/(boundaryEvidence.max()-boundaryEvidence.min())*(1.0-2.0*epsilon) + epsilon be[:,1]=numpy.exp(-1.0*boundaryEvidence[:]*sigma) be[:,0]=1.0-be[:,1] energy [:,0]= (-1.0*numpy.log( (1)*(1.0-beta) ) ) +be[:,0] energy [:,1]= (-1.0*numpy.log( (1)*(beta) ) ) +be[:,1] if verbose==True : print "build gm" gm=opengm.graphicalModel(numpy.ones(rag.numberOfRegions(),dtype=numpy.uint64)*numLabels) shapePotts=[numLabels,numLabels] print "add unaries" for r in range(rag.numberOfRegions()): f=dists[r,:]*gamma vis=[r] gm.addFactor(gm.addFunction(f),vis) print "add 2.order" for b in range(rag.numberOfBoundaries()): f=opengm.pottsFunction(shapePotts, energy[b,0] ,energy[b,1]) vis=rag.adjacentRegions(b) gm.addFactor(gm.addFunction(f),vis) return gm
if img.shape[2] != 3: print "Image must be RGB" sys.exit(0) T = float(args[6]) beta = float(args[7]) imgFlat = img.reshape([-1, 3]).view(numpy.ndarray) numVar = imgFlat.shape[0] gm = opengm.gm(numpy.ones(numVar, dtype=opengm.label_type) * 2) protoColor = numpy.array([args[3], args[4], args[5]], dtype=opengm.value_type).reshape([3, -1]) protoColor = numpy.repeat(protoColor, numVar, axis=1).swapaxes(0, 1) diffArray = numpy.sum(numpy.abs(imgFlat - protoColor), axis=1) unaries = numpy.ones([numVar, 2], dtype=opengm.value_type) unaries[:, 0] = T unaries[:, 1] = diffArray print diffArray gm.addFactors(gm.addFunctions(unaries), numpy.arange(numVar)) regularizer = opengm.pottsFunction([2, 2], 0.0, beta) gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) fid = gm.addFunction(regularizer) gm.addFactors(fid, gridVariableIndices) opengm.hdf5.saveGraphicalModel(gm, args[2], "gm")
# run some tests on the package shape = (4, 4) n_pixels = shape[0] * shape[1] n_pixel_labels = 2 n_segment_labels = 2 pixels = np.random.random((shape + (n_pixel_labels, ))) segment_map = np.arange(n_pixels).reshape(shape) segment_values = np.random.random( (np.max(segment_map) + 1, n_segment_labels)) t0 = time.time() gm = pixel_lattice_graph( pixels, opengm.pottsFunction([n_pixel_labels, n_pixel_labels], 0.0, 0.5)) t1 = time.time() opengm.visualizeGm(gm) print "graph build in", t1 - t0, "seconds" # test inference is possible inference = opengm.inference.GraphCut(gm=gm) t0 = time.time() inference.infer() t1 = time.time() print "inference completed in", t1 - t0, "seconds" t0 = time.time() gm = segment_adjacency_graph(segment_values,
imgFlat = img.reshape([-1,3]).view(numpy.ndarray) numVar = imgFlat.shape[0] gm = opengm.gm(numpy.ones(numVar,dtype=opengm.label_type)*2) protoColor = numpy.array([args[3],args[4],args[5]],dtype=opengm.value_type).reshape([3,-1]) protoColor = numpy.repeat(protoColor,numVar,axis=1).swapaxes(0,1) diffArray = numpy.sum(numpy.abs(imgFlat - protoColor),axis=1) unaries = numpy.ones([numVar,2],dtype=opengm.value_type) unaries[:,0]=T unaries[:,1]=diffArray print diffArray gm.addFactors(gm.addFunctions(unaries),numpy.arange(numVar)) regularizer=opengm.pottsFunction([2,2],0.0,beta) gridVariableIndices=opengm.secondOrderGridVis(img.shape[0],img.shape[1]) fid=gm.addFunction(regularizer) gm.addFactors(fid,gridVariableIndices) print gm inf=opengm.inference.GraphCut(gm) inf.infer() arg=inf.arg().reshape(img.shape[0:2]) vigra.impex.writeImage(arg,args[2])
def segmentation_example(): import vigra import opengm import sklearn import sklearn.mixture import numpy as np from vigra import graphs import matplotlib as mpl import plottool as pt pt.ensure_pylab_qt4() # load image and convert to LAB img_fpath = str(ut.grab_test_imgpath(str('lena.png'))) img = vigra.impex.readImage(img_fpath) imgLab = vigra.colors.transform_RGB2Lab(img) superpixelDiameter = 15 # super-pixel size slicWeight = 15.0 # SLIC color - spatial weight labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight, superpixelDiameter) labels = vigra.analysis.labelImage(labels) - 1 # get 2D grid graph and RAG gridGraph = graphs.gridGraph(img.shape[0:2]) rag = graphs.regionAdjacencyGraph(gridGraph, labels) # Node Features nodeFeatures = rag.accumulateNodeFeatures(imgLab) nodeFeaturesImg = rag.projectNodeFeaturesToGridGraph(nodeFeatures) nodeFeaturesImg = vigra.taggedView(nodeFeaturesImg, "xyc") nodeFeaturesImgRgb = vigra.colors.transform_Lab2RGB(nodeFeaturesImg) nCluster = 5 g = sklearn.mixture.GMM(n_components=nCluster) g.fit(nodeFeatures[:, :]) clusterProb = g.predict_proba(nodeFeatures) # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Irregular%20Factor%20Graphs.ipynb # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Hard%20and%20Soft%20Constraints.ipynb clusterProbImg = rag.projectNodeFeaturesToGridGraph( clusterProb.astype(np.float32)) clusterProbImg = vigra.taggedView(clusterProbImg, "xyc") ndim_data = clusterProbImg.reshape((-1, nCluster)) pca = sklearn.decomposition.PCA(n_components=3) print(ndim_data.shape) pca.fit(ndim_data) print(ut.repr2(pca.explained_variance_ratio_, precision=2)) oldshape = (clusterProbImg.shape[0:2] + (-1,)) clusterProgImg3 = pca.transform(ndim_data).reshape(oldshape) print(clusterProgImg3.shape) # graphical model with as many variables # as superpixels, each has 3 states gm = opengm.gm(np.ones(rag.nodeNum, dtype=opengm.label_type) * nCluster) # convert probabilites to energies probs = np.clip(clusterProb, 0.00001, 0.99999) costs = -1.0 * np.log(probs) # add ALL unaries AT ONCE fids = gm.addFunctions(costs) gm.addFactors(fids, np.arange(rag.nodeNum)) # add a potts function beta = 40.0 # strength of potts regularizer regularizer = opengm.pottsFunction([nCluster] * 2, 0.0, beta) fid = gm.addFunction(regularizer) # get variable indices of adjacent superpixels # - or "u" and "v" node id's for edges uvIds = rag.uvIds() uvIds = np.sort(uvIds, axis=1) # add all second order factors at once gm.addFactors(fid, uvIds) # get super-pixels with slic on LAB image Inf = opengm.inference.BeliefPropagation parameter = opengm.InfParam(steps=10, damping=0.5, convergenceBound=0.001) inf = Inf(gm, parameter=parameter) class PyCallback(object): def __init__(self,): self.labels = [] def begin(self, inference): print("begin of inference") def end(self, inference): self.labels.append(inference.arg()) def visit(self, inference): gm = inference.gm() labelVector = inference.arg() print("energy %r" % (gm.evaluate(labelVector),)) self.labels.append(labelVector) callback = PyCallback() visitor = inf.pythonVisitor(callback, visitNth=1) inf.infer(visitor) pt.imshow(clusterProgImg3.swapaxes(0, 1)) # plot superpixels cmap = mpl.colors.ListedColormap(np.random.rand(nseg, 3)) pt.imshow(labels.swapaxes(0, 1).squeeze(), cmap=cmap) pt.imshow(nodeFeaturesImgRgb) cmap = mpl.colors.ListedColormap(np.random.rand(nCluster, 3)) for arg in callback.labels: arg = vigra.taggedView(arg, "n") argImg = rag.projectNodeFeaturesToGridGraph(arg.astype(np.uint32)) argImg = vigra.taggedView(argImg, "xy") # plot superpixels pt.imshow(argImg.swapaxes(0, 1).squeeze(), cmap=cmap)
def segmentation_example(): import vigra import opengm import sklearn import sklearn.mixture import numpy as np from vigra import graphs import matplotlib as mpl import plottool as pt pt.ensure_pylab_qt4() # load image and convert to LAB img_fpath = str(ut.grab_test_imgpath(str('lena.png'))) img = vigra.impex.readImage(img_fpath) imgLab = vigra.colors.transform_RGB2Lab(img) superpixelDiameter = 15 # super-pixel size slicWeight = 15.0 # SLIC color - spatial weight labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight, superpixelDiameter) labels = vigra.analysis.labelImage(labels) - 1 # get 2D grid graph and RAG gridGraph = graphs.gridGraph(img.shape[0:2]) rag = graphs.regionAdjacencyGraph(gridGraph, labels) # Node Features nodeFeatures = rag.accumulateNodeFeatures(imgLab) nodeFeaturesImg = rag.projectNodeFeaturesToGridGraph(nodeFeatures) nodeFeaturesImg = vigra.taggedView(nodeFeaturesImg, "xyc") nodeFeaturesImgRgb = vigra.colors.transform_Lab2RGB(nodeFeaturesImg) nCluster = 5 g = sklearn.mixture.GMM(n_components=nCluster) g.fit(nodeFeatures[:, :]) clusterProb = g.predict_proba(nodeFeatures) # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Irregular%20Factor%20Graphs.ipynb # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Hard%20and%20Soft%20Constraints.ipynb clusterProbImg = rag.projectNodeFeaturesToGridGraph( clusterProb.astype(np.float32)) clusterProbImg = vigra.taggedView(clusterProbImg, "xyc") ndim_data = clusterProbImg.reshape((-1, nCluster)) pca = sklearn.decomposition.PCA(n_components=3) print(ndim_data.shape) pca.fit(ndim_data) print(ut.repr2(pca.explained_variance_ratio_, precision=2)) oldshape = (clusterProbImg.shape[0:2] + (-1, )) clusterProgImg3 = pca.transform(ndim_data).reshape(oldshape) print(clusterProgImg3.shape) # graphical model with as many variables # as superpixels, each has 3 states gm = opengm.gm(np.ones(rag.nodeNum, dtype=opengm.label_type) * nCluster) # convert probabilites to energies probs = np.clip(clusterProb, 0.00001, 0.99999) costs = -1.0 * np.log(probs) # add ALL unaries AT ONCE fids = gm.addFunctions(costs) gm.addFactors(fids, np.arange(rag.nodeNum)) # add a potts function beta = 40.0 # strength of potts regularizer regularizer = opengm.pottsFunction([nCluster] * 2, 0.0, beta) fid = gm.addFunction(regularizer) # get variable indices of adjacent superpixels # - or "u" and "v" node id's for edges uvIds = rag.uvIds() uvIds = np.sort(uvIds, axis=1) # add all second order factors at once gm.addFactors(fid, uvIds) # get super-pixels with slic on LAB image Inf = opengm.inference.BeliefPropagation parameter = opengm.InfParam(steps=10, damping=0.5, convergenceBound=0.001) inf = Inf(gm, parameter=parameter) class PyCallback(object): def __init__(self, ): self.labels = [] def begin(self, inference): print("begin of inference") def end(self, inference): self.labels.append(inference.arg()) def visit(self, inference): gm = inference.gm() labelVector = inference.arg() print("energy %r" % (gm.evaluate(labelVector), )) self.labels.append(labelVector) callback = PyCallback() visitor = inf.pythonVisitor(callback, visitNth=1) inf.infer(visitor) pt.imshow(clusterProgImg3.swapaxes(0, 1)) # plot superpixels cmap = mpl.colors.ListedColormap(np.random.rand(nseg, 3)) pt.imshow(labels.swapaxes(0, 1).squeeze(), cmap=cmap) pt.imshow(nodeFeaturesImgRgb) cmap = mpl.colors.ListedColormap(np.random.rand(nCluster, 3)) for arg in callback.labels: arg = vigra.taggedView(arg, "n") argImg = rag.projectNodeFeaturesToGridGraph(arg.astype(np.uint32)) argImg = vigra.taggedView(argImg, "xy") # plot superpixels pt.imshow(argImg.swapaxes(0, 1).squeeze(), cmap=cmap)