def add_lattice_layer(gm, pixel_unaries, pixel_regularizer=None, offset=0): """ Adds a lattice (pixel grid) layer to a GM where all edges share the same regularizer offset is the variable index of the first new variable. Make this the count of all variables already added Parameters: - pixel_unaries - a 3D array of shape (width, height, n_labels). - pixel_regularizer (optional) - a pairwise opengm function e.g. opengm.PottsFunction([2,2],0.0,beta) """ n_labels = pixel_unaries.shape[-1] n_pixels = pixel_unaries.shape[0] * pixel_unaries.shape[1] edges = opengm.secondOrderGridVis(pixel_unaries.shape[0], pixel_unaries.shape[1]) unaries = pixel_unaries.reshape((n_pixels, n_labels)) gm = add_layer(gm, unaries, edges, pairwise=pixel_regularizer, offset=offset) return gm
import numpy import opengm import time dimx = 1000 dimy = 1000 numVar = dimx * dimy numLabels = 2 numberOfStates = numpy.ones(numVar, dtype=opengm.index_type) * numLabels vis2Order = opengm.secondOrderGridVis(dimx, dimy) numFac = len(vis2Order) randf = numpy.random.rand(numFac, numLabels, numLabels).astype(numpy.float64) print randf.shape print "numVar", numVar, "numFac", numFac print "# METHOD A" with opengm.Timer(): gm = opengm.graphicalModel(numberOfStates, operator="adder", reserveNumFactorsPerVariable=4) gm.reserveFunctions(numFac, "explicit") fids = gm.addFunctions(randf) gm.addFactors(fids, vis2Order) print "# METHOD B" with opengm.Timer(): # (reserve reserveNumFactorsPerVariable does not make sense if we not "finalize" factors directely)
def denoiseModel(img, norm=2, weight=1.0, truncate=None, numLabels=256, neighbourhood=4, inpaintPixels=None, randInpaitStartingPoint=False): """ this function is used to set up a graphical model similar to **Denoising and inpainting problems:** from `Mrf- Benchmark <http://vision.middlebury.edu/MRF/results/ >`_ Args : img : a grayscale image in the range [0,256) norm : used norm for unaries and 2-order functions (default : 2) weight : weight of 2-order functions (default : 1.0) truncate : Truncate second order function at an given value (defaut : None) numLabels : number of labels for each variable in the graphical model, set this to a lower number to speed up inference (default : 255) neighbourhood : neighbourhood for the second order functions, so far only 4 is allowed (default : 4) inpaintPixels : a tuple of x and y coordinates where no unaries are added randInpaitStartingPoint : use a random starting point for all pixels without unaries (default : False) """ shape = img.shape if (img.ndim != 2): raise RuntimeError("image must be gray") if neighbourhood != 4: raise RuntimeError( "A neighbourhood other than 4 is not yet implemented") # normalize and flatten image iMin = numpy.min(img) iMax = numpy.max(img) imgNorm = ((img[:, :] - iMin) / (iMax - iMin)) * float(numLabels) imgFlat = imgNorm.reshape(-1).astype(numpy.uint64) # Set up Grapical Model: numVar = int(img.size) gm = opengm.gm([numLabels] * numVar, operator='adder') gm.reserveFunctions(numLabels, 'explicit') numberOfPairwiseFactors = shape[0] * (shape[1] - 1) + shape[1] * (shape[0] - 1) gm.reserveFactors(numVar - len(inpaintPixels[0]) + numberOfPairwiseFactors) # Set up unaries: # - create a range of all possible labels allPossiblePixelValues = numpy.arange(numLabels) pixelValueRep = numpy.repeat(allPossiblePixelValues[:, numpy.newaxis], numLabels, 1) # - repeat [0,1,2,3,...,253,254,255] numVar times labelRange = numpy.arange(numLabels, dtype=opengm.value_type) labelRange = numpy.repeat(labelRange[numpy.newaxis, :], numLabels, 0) unaries = numpy.abs(pixelValueRep - labelRange)**norm # - add unaries to the graphical model fids = gm.addFunctions(unaries.astype(opengm.value_type)) # add unary factors to graphical model if (inpaintPixels is None): for l in xrange(numLabels): whereL = numpy.where(imgFlat == l) gm.addFactors(fids[l], whereL[0].astype(opengm.index_type)) else: # get vis of inpaint pixels ipX = inpaintPixels[0] ipY = inpaintPixels[1] ipVi = ipX * shape[1] + ipY for l in xrange(numLabels): whereL = numpy.where(imgFlat == l) notInInpaint = numpy.setdiff1d(whereL[0], ipVi) gm.addFactors(fids[l], notInInpaint.astype(opengm.index_type)) # add ONE second order function f = opengm.differenceFunction(shape=[numLabels, numLabels], norm=2, weight=weight) fid = gm.addFunction(f) vis2Order = opengm.secondOrderGridVis(shape[0], shape[1], True) # add all second order factors gm.addFactors(fid, vis2Order) # create a starting point startingPoint = imgFlat.copy() if randInpaitStartingPoint: startingPointRandom = numpy.random.randint( 0, numLabels, size=numVar).astype(opengm.index_type) ipVi = inpaintPixels[0] * shape[1] + inpaintPixels[1] for x in ipVi: startingPoint[x] = startingPointRandom[x] startingPoint[startingPoint == numLabels] = numLabels - 1 return gm, startingPoint.astype(opengm.index_type)
def denoiseModel( img, norm = 2, weight = 1.0, truncate = None, numLabels = 256, neighbourhood = 4, inpaintPixels = None, randInpaitStartingPoint = False ): """ this function is used to set up a graphical model similar to **Denoising and inpainting problems:** from `Mrf- Benchmark <http://vision.middlebury.edu/MRF/results/ >`_ Args : img : a grayscale image in the range [0,256) norm : used norm for unaries and 2-order functions (default : 2) weight : weight of 2-order functions (default : 1.0) truncate : Truncate second order function at an given value (defaut : None) numLabels : number of labels for each variable in the graphical model, set this to a lower number to speed up inference (default : 255) neighbourhood : neighbourhood for the second order functions, so far only 4 is allowed (default : 4) inpaintPixels : a tuple of x and y coordinates where no unaries are added randInpaitStartingPoint : use a random starting point for all pixels without unaries (default : False) """ shape = img.shape if(img.ndim!=2): raise RuntimeError("image must be gray") if neighbourhood != 4 : raise RuntimeError("A neighbourhood other than 4 is not yet implemented") # normalize and flatten image iMin = numpy.min(img) iMax = numpy.max(img) imgNorm = ((img[:,:]-iMin)/(iMax-iMin))*float(numLabels) imgFlat = imgNorm.reshape(-1).astype(numpy.uint64) # Set up Grapical Model: numVar = int(img.size) gm = opengm.gm([numLabels]*numVar,operator='adder') gm.reserveFunctions(numLabels,'explicit') numberOfPairwiseFactors=shape[0]*(shape[1]-1) + shape[1]*(shape[0]-1) gm.reserveFactors(numVar-len(inpaintPixels[0]) + numberOfPairwiseFactors ) # Set up unaries: # - create a range of all possible labels allPossiblePixelValues=numpy.arange(numLabels) pixelValueRep = numpy.repeat(allPossiblePixelValues[:,numpy.newaxis],numLabels,1) # - repeat [0,1,2,3,...,253,254,255] numVar times labelRange = numpy.arange(numLabels,dtype=opengm.value_type) labelRange = numpy.repeat(labelRange[numpy.newaxis,:], numLabels, 0) unaries = numpy.abs(pixelValueRep - labelRange)**norm # - add unaries to the graphical model fids=gm.addFunctions(unaries.astype(opengm.value_type)) # add unary factors to graphical model if(inpaintPixels is None): for l in xrange(numLabels): whereL=numpy.where(imgFlat==l) gm.addFactors(fids[l],whereL[0].astype(opengm.index_type)) else: # get vis of inpaint pixels ipX = inpaintPixels[0] ipY = inpaintPixels[1] ipVi = ipX*shape[1] + ipY for l in xrange(numLabels): whereL=numpy.where(imgFlat==l) notInInpaint=numpy.setdiff1d(whereL[0],ipVi) gm.addFactors(fids[l],notInInpaint.astype(opengm.index_type)) # add ONE second order function f=opengm.differenceFunction(shape=[numLabels,numLabels],norm=2,weight=weight) fid=gm.addFunction(f) vis2Order=opengm.secondOrderGridVis(shape[0],shape[1],True) # add all second order factors gm.addFactors(fid,vis2Order) # create a starting point startingPoint = imgFlat.copy() if randInpaitStartingPoint : startingPointRandom = numpy.random.randint(0,numLabels,size=numVar).astype(opengm.index_type) ipVi = inpaintPixels[0]*shape[1] + inpaintPixels[1] for x in ipVi: startingPoint[x]=startingPointRandom[x] startingPoint[startingPoint==numLabels]=numLabels-1 return gm,startingPoint.astype(opengm.index_type)
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")
def segment_overlap_graph(pixel_unaries, segment_map, segment_unaries, pixel_regularizer=None, segment_regularizer=None, inter_layer_regularizer=None): """ greates a graphical model comprised of two layers. - The first layer is a pixel lattice - The second layer is a region adjacency graph over segments - Connections exist between pixels where they are overlapped by a segment Parameters: - pixel_unaries - a 3D array of shape (width, height, n_labels). - segment_map - a 2d array of shape (width, height). Each element >= 0 is a segment id that maps the corresponding pixel to that id. -1 represents no segment and no corresponding node will be added - segment_unaries - a 2d arry of shape (n_segments, n_labels) - pixel_regularizer (optional) - a pairwise opengm function e.g. opengm.PottsFunction([2,2],0.0,beta) or list of opengm functions of length n_pixels - segment_regularizer (optional) - a pairwise opengm function, same requirements as pixel_regularizer - inter_layer_regularizer (optional) - a pairwise opengm function, same requirements as pixel_regularizer """ _check_valid_segment_map(segment_map) _check_compatible_segment_map_unaries(segment_map, segment_unaries) # calculate how many variables and factors will be required n_pixels = pixel_unaries.shape[0] * pixel_unaries.shape[1] n_segments = segment_unaries.shape[0] n_variables = n_pixels + n_segments n_labels_pixels = pixel_unaries.shape[-1] n_labels_segments = segment_unaries.shape[-1] # calculate the region adjacency graph for the segments rag_edges = segment_map_to_rag_edges(segment_map) rag_edges += n_pixels #segment indices start at n_pixels remember! n_pixel_edges = (pixel_unaries.shape[0] - 1) * pixel_unaries.shape[1] + ( pixel_unaries.shape[1] - 1) * pixel_unaries.shape[0] n_segment_edges = rag_edges.shape[0] #check this is right n_inter_edges = n_pixels n_edges = n_pixel_edges + n_segment_edges + n_inter_edges # allocate space for the model and all its variables gm = opengm.graphicalModel([n_labels_pixels] * n_pixels + [n_labels_segments] * n_segments) gm.reserveFunctions( n_variables + 3, 'explicit') # the unary functions plus the 3 types of regularizer gm.reserveFactors(n_variables + n_edges) # add unary functions and factors fids = gm.addFunctions(pixel_unaries.reshape([n_pixels, n_labels_pixels])) gm.addFactors(fids, np.arange(n_pixels), finalize=False) fids = gm.addFunctions(segment_unaries) gm.addFactors(fids, n_pixels + np.arange(n_segments), finalize=False) ## add pairwise functions # pixel lattice if pixel_regularizer is not None: fid = gm.addFunction(pixel_regularizer) vis = opengm.secondOrderGridVis(pixel_unaries.shape[0], pixel_unaries.shape[1]) gm.addFactors(fid, vis, finalize=False) # segment rag if segment_regularizer is not None: fid = gm.addFunction(segment_regularizer) gm.addFactors(fid, np.sort(rag_edges, axis=1), finalize=False) # inter-layer if inter_layer_regularizer is not None: fid = gm.addFunction(inter_layer_regularizer) vis = np.dstack([ np.arange(n_pixels).reshape(pixel_unaries.shape[:2]), segment_map ]).reshape((-1, 2)) vis = _remove_rows_with_negative(vis) vis[:, 1] += n_pixels gm.addFactors(fid, vis, finalize=False) gm.finalize() return gm
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])