def _runAll(self): for runNr in range(20): model = self._buildModel() averaged = self.optimizeModel(model) c = self.updatePExperts(averaged) if c < 0.00001: break vigra.segShow(imgRgb, self.lastNodeLabels + 1) vigra.show()
def allPairsOfShortestPath(graph, weights): pathFinder = vigra.graphs.ShortestPathPathDijkstra(graph) distances = numpy.zeros([graph.nodeNum] * 2) for ni, node in enumerate(graph.nodeIter()): pathFinder.run(weights**1, node) d = pathFinder.distances()**1 distances[ni, :] = d[:] ggf = graph.projectNodeFeaturesToGridGraph(d) ggf = vigra.taggedView(ggf, 'xy') if ni < 1: vigra.imshow(ggf) vigra.show() print distances.min(), distances.max() return distances
def makeRag(raw, showSeg=False): # raw = vigra.gaussianSmoothing(raw,1.0) ew = vigra.filters.hessianOfGaussianEigenvalues(-1.0 * raw, 2.3)[:, :, 0] seg, nseg = vigra.analysis.watershedsNew(ew) # seg, nseg = vigra.analysis.slicSuperpixels(raw,intensityScaling=4.5, seedDistance=20) seg = seg.squeeze() if showSeg: vigra.segShow(raw, seg) vigra.show() # get the rag seg -= 1 assert seg.min() == 0 assert seg.max() == nseg - 1 return nifty.graph.rag.gridRag(seg)
def makeRag(raw, showSeg=False): #raw = vigra.gaussianSmoothing(raw,1.0) ew = vigra.filters.hessianOfGaussianEigenvalues(-1.0 * raw, 2.3)[:, :, 0] seg, nseg = vigra.analysis.watershedsNew(ew) #seg, nseg = vigra.analysis.slicSuperpixels(raw,intensityScaling=4.5, seedDistance=20) seg = seg.squeeze() if showSeg: vigra.segShow(raw, seg) vigra.show() # get the rag seg -= 1 assert seg.min() == 0 assert seg.max() == nseg - 1 return nifty.graph.rag.gridRag(seg)
def optimizeModel(self, model): if self.gtAsCut is None: # generate external proposals # ( we should do this only once) self.gtAsCut = [] for gti in range(self.nExperts): gt2d = self.gts[:, :, gti].astype('uint64') #print gt2d.shape gt1d = model.flattenLabels(gt2d) #print gt1d edgeGt = model.nodeLabelsToEdgeLabels(gt1d) self.gtAsCut.append(edgeGt) # settings for proposal generator settingsProposalGen = agraph.settingsProposalsFusionMovesSubgraphProposals( model) settingsProposalGen.subgraphRadius = 20 # settings for solver itself settings = agraph.settingsFusionMoves(settingsProposalGen) settings.maxNumberOfIterations = 4 settings.nParallelProposals = 50 settings.reduceIterations = 0 settings.seed = 42 for ep in self.gtAsCut: settings.addPropoal(ep) # solver solver = agraph.fusionMoves(model, settings) # solve the damn thing out = solver.run() nodeLabels = model.edgeLabelsToNodeLabels(out) nodeLabels = nodeLabels.astype('uint32') nodeLabels = nodeLabels.reshape(shape, order='F') nodeLabels = vigra.taggedView(nodeLabels, 'xy') self.lastNodeLabels = nodeLabels vigra.segShow(imgRgb, nodeLabels + 1) vigra.show() return out
partial(vigra.filters.gaussianGradientMagnitude, sigma=1.0), partial(vigra.filters.gaussianGradientMagnitude, sigma=2.0), ] dataset, test_set = secondOrderImageDataset(imgs=imgs, gts=gt, numberOfLabels=2, fUnary=fUnary, fBinary=fBinary, addConstFeature=False) learner = learning.subgradientSSVM(dataset, learningRate=0.1, C=100, learningMode='batch', maxIterations=10) learner.learn(infCls=opengm.inference.QpboExternal, parameter=opengm.InfParam()) # predict on test test for (rgbImg, gtImg, gm) in test_set: # infer for test image inf = opengm.inference.QpboExternal(gm) inf.infer() arg = inf.arg() arg = arg.reshape(numpy.squeeze(gtImg.shape)) vigra.segShow(rgbImg, arg + 2) vigra.show()
nodeFeatures = rag.accumulateNodeFeatures(imgLab) # do agglomerativeClustering labels = graphs.agglomerativeClustering(graph=rag, edgeWeights=edgeWeights, beta=beta, nodeFeatures=nodeFeatures, nodeNumStop=nodeNumStop) # show result f = pylab.figure() ax1 = f.add_subplot(2, 2, 1) vigra.imshow(gradMag,show=False) ax1.set_title("Input Image") pylab.axis('off') ax2 = f.add_subplot(2, 2, 2) rag.show(img) ax2.set_title("Over-Segmentation") pylab.axis('off') ax3 = f.add_subplot(2, 2, 3) rag.show(img, labels) ax3.set_title("Result-Segmentation") pylab.axis('off') ax4 = f.add_subplot(2, 2, 4) rag.showNested(img, labels) ax4.set_title("Result-Segmentation") pylab.axis('off') vigra.show()
def __init__(self, raw, seg, gt=None, patchSize=[100, 100], **kwargs): #raw input data self.raw = numpy.squeeze(raw) self.seg = numpy.squeeze(seg) self.gt = numpy.squeeze(gt) # shorthands self.shape = self.raw.shape # settings self.patchSize = patchSize # apply paddings ps = self.patchSize padding = ((ps[0], ps[0]), (ps[1], ps[1])) pad = partial(numpy.pad, pad_width=padding) self.paddingMask = pad(numpy.zeros(self.shape), mode='constant', constant_values=1) self.paddedRaw = pad(self.raw, mode='reflect') self.paddedSeg = vigra.analysis.labelImage( pad(self.seg, mode='reflect')).squeeze() self.paddedGt = None if self.gt is not None: self.paddedGt = vigra.analysis.labelImage( pad(self.gt, mode='reflect')).squeeze() #self.paddedGt = pad(self.gt + 1, mode='constant', constant_values=0) # compute cgp self.tGrid = ncgp.TopologicalGrid2D(self.paddedSeg) self.tShape = self.tGrid.topologicalGridShape self.numberOfCells = self.tGrid.numberOfCells self.fGrid = ncgp.FilledTopologicalGrid2D(self.tGrid) self.cellGrids = [ self.fGrid.cellMask([1, 0, 0]), self.fGrid.cellMask([0, 1, 0]), None ] self.cellGrids[0][ self.cellGrids[0] != 0] -= self.fGrid.cellTypeOffset[0] self.cellGrids[1][ self.cellGrids[1] != 0] -= self.fGrid.cellTypeOffset[1] self.cellMasks = [numpy.clip(x, 0, 1) for x in self.cellGrids[0:2]] + [None] rawT = vigra.sampling.resize(self.paddedRaw, self.tShape) #rawT[self.cellMasks[1]!=0] = 0 self.cellsBounds = self.tGrid.extractCellsBounds() self.cellsGeometry = self.tGrid.extractCellsGeometry(fill=True) self.cells1Bounds = self.cellsBounds[1] self.cells1Geometry = self.cellsGeometry[1] # center of mass self.cell1CentersOfMass = self.cells1Geometry.centersOfMass() print(self.cell1CentersOfMass) # compute gt ol = Overlap(self.numberOfCells[2], self.paddedSeg, self.paddedGt) cell1Probs = ol.differentOverlaps(numpy.array(self.cells1Bounds)) with nifty.Timer("makeCellImage"): probImg = ncgp.makeCellImage(rawT, self.cellGrids[1], cell1Probs * 255.0) vigra.imshow(probImg.astype('uint8'), cmap='gist_heat') vigra.show() if False: vigra.imshow(self.paddingMask) vigra.show() vigra.imshow(self.paddedRaw) vigra.show() vigra.segShow(self.paddedRaw, self.paddedSeg) vigra.show() vigra.segShow(self.paddedRaw, self.paddedGt) vigra.show()
def runLiftedMc(dataDict, settings): rawData = dataDict['raw'] outDir = dataDict['outDir'] localRfPredictDir = os.path.join(outDir, 'localClfProbs') ragsAndSuperpixels = getRagsAndSuperpixels(dataDict, settings) liftedFeaturesDir = os.path.join(outDir, 'liftedFeatures') liftedProbsDir = os.path.join(outDir, 'liftedProbs') ragsAndSuperpixels = getRagsAndSuperpixels(dataDict, settings) for sliceIndex in range(rawData.shape[0]): # local probs predictionFile = os.path.join( localRfPredictDir, 'rag_pred_clf%s_%d.h5' % ('0', sliceIndex)) localProbs = h5Read(predictionFile)[:, 1] # lifted probs liftedProbsFile = os.path.join(liftedProbsDir, 'lifted_probs_%d.h5' % (sliceIndex)) liftedProbs = h5Read(liftedProbsFile) # set up the lifted objective rag, sp = ragsAndSuperpixels[sliceIndex] obj = nifty.graph.lifted_multicut.liftedMulticutObjective(rag) liftedGraph = obj.liftedGraph distance = obj.insertLiftedEdgesBfs( 5, returnDistance=True).astype('float32') liftedUvIds = obj.liftedUvIds() # numeric factor to not get to small weights # might not be necessary C = 100.0 # local weights eps = 0.0001 clipped = numpy.clip(localProbs, eps, 1.0 - eps) beta = settings['betaLocal'] wLocal = numpy.log((1.0 - clipped) / (clipped)) + numpy.log( (1.0 - beta) / (beta)) # normalize by number of local edges length wLocal *= C / len(wLocal) wLocal *= settings['gamma'] # non local weights eps = 0.0001 clipped = numpy.clip(liftedProbs, eps, 1.0 - eps) beta = settings['betaLifted'] wLifted = numpy.log((1.0 - clipped) / (clipped)) + numpy.log( (1.0 - beta) / (beta)) # normalize by the distance (give close one more weight) distanceWeight = 1.0 / (distance - 1.0) #wLifted *= C/distanceWeight.sum() wLifted *= C / len(wLifted) print numpy.abs(wLocal).sum(), numpy.abs(wLifted).sum() # write the weighs into the objective obj.setLiftedEdgesCosts(wLifted, overwrite=True) obj.setGraphEdgesCosts(wLocal, overwrite=True) # warm start with normal multicut mcObj = nifty.graph.multicut.multicutObjective(rag, wLocal) solverFactory = mcObj.multicutIlpCplexFactory() solver = solverFactory.create(mcObj) visitor = mcObj.multicutVerboseVisitor() argMc = solver.optimize(visitor) emc = obj.evalNodeLabels(argMc) # finaly optimize it solverFactory = obj.liftedMulticutAndresGreedyAdditiveFactory() solver = solverFactory.create(obj) visitor = obj.verboseVisitor() arg = solver.optimize(visitor) eg = obj.evalNodeLabels(arg) solverFactory = obj.liftedMulticutAndresKernighanLinFactory() solver = solverFactory.create(obj) visitor = obj.verboseVisitor() arg = solver.optimize(visitor, arg.copy()) ekl = obj.evalNodeLabels(arg) print "e", emc, eg, ekl #projectToPixels pixelData = nifty.graph.rag.projectScalarNodeDataToPixels( rag, arg.astype('uint32')) vigra.segShow(rawData[sliceIndex, :, :], pixelData) vigra.show()
def runLiftedMc(dataDict, settings): rawData = dataDict['raw'] outDir = dataDict['outDir'] localRfPredictDir = os.path.join(outDir,'localClfProbs') ragsAndSuperpixels = getRagsAndSuperpixels(dataDict, settings) liftedFeaturesDir = os.path.join(outDir,'liftedFeatures') liftedProbsDir = os.path.join(outDir,'liftedProbs') ragsAndSuperpixels = getRagsAndSuperpixels(dataDict, settings) for sliceIndex in range(rawData.shape[0]): # local probs predictionFile = os.path.join(localRfPredictDir,'rag_pred_clf%s_%d.h5'%('0', sliceIndex)) localProbs = h5Read(predictionFile)[:,1] # lifted probs liftedProbsFile = os.path.join(liftedProbsDir,'lifted_probs_%d.h5'%(sliceIndex)) liftedProbs = h5Read(liftedProbsFile) # set up the lifted objective rag, sp = ragsAndSuperpixels[sliceIndex] obj = nifty.graph.lifted_multicut.liftedMulticutObjective(rag) liftedGraph = obj.liftedGraph distance = obj.insertLiftedEdgesBfs(5, returnDistance=True).astype('float32') liftedUvIds = obj.liftedUvIds() # numeric factor to not get to small weights # might not be necessary C = 100.0 # local weights eps = 0.0001 clipped = numpy.clip(localProbs, eps, 1.0-eps) beta = settings['betaLocal'] wLocal = numpy.log((1.0-clipped)/(clipped)) + numpy.log((1.0-beta)/(beta)) # normalize by number of local edges length wLocal *= C/len(wLocal) wLocal *= settings['gamma'] # non local weights eps = 0.0001 clipped = numpy.clip(liftedProbs, eps, 1.0-eps) beta = settings['betaLifted'] wLifted = numpy.log((1.0-clipped)/(clipped)) + numpy.log((1.0-beta)/(beta)) # normalize by the distance (give close one more weight) distanceWeight = 1.0 / (distance-1.0) #wLifted *= C/distanceWeight.sum() wLifted *= C/len(wLifted) print numpy.abs(wLocal).sum(), numpy.abs(wLifted).sum() # write the weighs into the objective obj.setLiftedEdgesCosts(wLifted, overwrite=True) obj.setGraphEdgesCosts(wLocal, overwrite=True) # warm start with normal multicut mcObj = nifty.graph.multicut.multicutObjective(rag, wLocal) solverFactory = mcObj.multicutIlpCplexFactory() solver = solverFactory.create(mcObj) visitor = mcObj.multicutVerboseVisitor() argMc = solver.optimize(visitor) emc = obj.evalNodeLabels(argMc) # finaly optimize it solverFactory = obj.liftedMulticutAndresGreedyAdditiveFactory() solver = solverFactory.create(obj) visitor = obj.verboseVisitor() arg = solver.optimize(visitor) eg = obj.evalNodeLabels(arg) solverFactory = obj.liftedMulticutAndresKernighanLinFactory() solver = solverFactory.create(obj) visitor = obj.verboseVisitor() arg = solver.optimize(visitor, arg.copy()) ekl = obj.evalNodeLabels(arg) print "e",emc,eg,ekl #projectToPixels pixelData = nifty.graph.rag.projectScalarNodeDataToPixels(rag, arg.astype('uint32')) vigra.segShow(rawData[sliceIndex,:,:], pixelData) vigra.show()
def test_mcgala(): # get the dataset imgs, gts = make_dataset(10, noise=4.5, shape=(200, 200)) Obj = G.MulticutObjective CG = G.EdgeContractionGraph CGObj = CG.MulticutObjective greedyFactory = Obj.greedyAdditiveFactory() ilpFactory = Obj.multicutIlpFactory( ilpSolver="cplex", addThreeCyclesConstraints=False, addOnlyViolatedThreeCyclesConstraints=False # memLimit= 0.01 ) fmFactoryA = CGObj.fusionMoveBasedFactory( # fusionMove=CGObj.fusionMoveSettings(mcFactory=greedyFactory), fusionMove=CGObj.fusionMoveSettings(mcFactory=ilpFactory), # proposalGen=nifty.greedyAdditiveProposals(sigma=30,nodeNumStopCond=-1,weightStopCond=0.0), proposalGen=CGObj.watershedProposals(sigma=1, seedFraction=0.1), numberOfIterations=10, numberOfParallelProposals=40, # no effect if nThreads equals 0 or 1 numberOfThreads=40, stopIfNoImprovement=2, fuseN=2, ) ragTrain = makeRag(imgs[0], showSeg=True) fOpTrain, minVal, maxVal = makeFeatureOp(ragTrain, imgs[0]) edgeGt = makeEdgeGt(ragTrain, gts[0]) cOrderSettigns = G.galaContractionOrderSettings(mcMapFactory=fmFactoryA, runMcMapEachNthTime=10) # gala class settings = G.galaSettings( threshold0=0.1, threshold1=0.9, thresholdU=0.1, numberOfEpochs=1, numberOfTrees=200, contractionOrderSettings=cOrderSettigns # mapFactory=fmFactoryA, # perturbAndMapFactory=fmFactoryB ) gala = G.gala(settings) trainingInstance = ngala.galaTrainingInstance(ragTrain, fOpTrain, edgeGt) gala.addTrainingInstance(trainingInstance) gala.train() for x in range(3, 10): ragTest = makeRag(imgs[x], showSeg=False) fOpTest, minVal, maxVal = makeFeatureOp(ragTest, imgs[x], minVal, maxVal) instance = ngala.galaInstance(ragTest, fOpTest) edgeGt = makeEdgeGt(ragTest, gts[x]) nodeRes = gala.predict(instance) pixelNodeRes = nrag.projectScalarNodeDataToPixels(ragTest, nodeRes, -1) vigra.segShow(imgs[x], pixelNodeRes) vigra.show()
def makeFeat(rag, raw, labels, show=True): featureExtractor = graphs.gridRagFeatureExtractor(rag, labels) featureExtractor.labels = labels featureExtractor.graph = rag geoFeat = featureExtractor.geometricFeatures() topoFeat = featureExtractor.topologicalFeatures() features = [geoFeat, topoFeat] # ward facs wardness = numpy.array([0.0, 0.1, 0.15, 0.25, 0.5, 1.0], dtype="float32") accFeat = featureExtractor.accumulatedFeatures(raw) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.filters.gaussianGradientMagnitude(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.filters.laplacianOfGaussian(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.gaussianSmoothing(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) # st ev for s in [2.0, 3.0, 4.0]: res = stEv(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) mean = accFeat[:, 0] ucm = featureExtractor.ucmTransformFeatures(mean[:, None], wardness) features.extend([accFeat, ucm]) if False: for x in range(ucm.shape[1]): rag.showEdgeFeature(raw, ucm[:, x]) vigra.show() # hessian ev for s in [1.0, 3.0, 4.0, 6.0, 8.0]: img = hessianEv2(raw, s, 2.0) # vigra.imshow(img) # vigra.show() accFeat = featureExtractor.accumulatedFeatures(img) mean = accFeat[:, 0] ucm = featureExtractor.ucmTransformFeatures(mean[:, None], wardness) features.extend([accFeat, ucm]) if False: for x in range(ucm.shape[1]): print "x", x rag.showEdgeFeature(raw, ucm[:, x]) vigra.show() # break # features = numpy.concatenate(features, axis=1) return features
def test_mcgala(): # get the dataset imgs, gts = make_dataset(10, noise=4.5, shape=(200, 200)) Obj = G.MulticutObjective CG = G.EdgeContractionGraph CGObj = CG.MulticutObjective greedyFactory = Obj.greedyAdditiveFactory() ilpFactory = Obj.multicutIlpFactory( ilpSolver='cplex', addThreeCyclesConstraints=False, addOnlyViolatedThreeCyclesConstraints=False #memLimit= 0.01 ) fmFactoryA = CGObj.fusionMoveBasedFactory( #fusionMove=CGObj.fusionMoveSettings(mcFactory=greedyFactory), fusionMove=CGObj.fusionMoveSettings(mcFactory=ilpFactory), #proposalGen=nifty.greedyAdditiveProposals(sigma=30,nodeNumStopCond=-1,weightStopCond=0.0), proposalGen=CGObj.watershedProposals(sigma=1, seedFraction=0.1), numberOfIterations=10, numberOfParallelProposals=40, # no effect if nThreads equals 0 or 1 numberOfThreads=40, stopIfNoImprovement=2, fuseN=2, ) ragTrain = makeRag(imgs[0], showSeg=True) fOpTrain, minVal, maxVal = makeFeatureOp(ragTrain, imgs[0]) edgeGt = makeEdgeGt(ragTrain, gts[0]) cOrderSettigns = G.galaContractionOrderSettings(mcMapFactory=fmFactoryA, runMcMapEachNthTime=10) # gala class settings = G.galaSettings( threshold0=0.1, threshold1=0.9, thresholdU=0.1, numberOfEpochs=1, numberOfTrees=200, contractionOrderSettings=cOrderSettigns #mapFactory=fmFactoryA, #perturbAndMapFactory=fmFactoryB ) gala = G.gala(settings) trainingInstance = ngala.galaTrainingInstance(ragTrain, fOpTrain, edgeGt) gala.addTrainingInstance(trainingInstance) gala.train() for x in range(3, 10): ragTest = makeRag(imgs[x], showSeg=False) fOpTest, minVal, maxVal = makeFeatureOp(ragTest, imgs[x], minVal, maxVal) instance = ngala.galaInstance(ragTest, fOpTest) edgeGt = makeEdgeGt(ragTest, gts[x]) nodeRes = gala.predict(instance) pixelNodeRes = nrag.projectScalarNodeDataToPixels(ragTest, nodeRes, -1) vigra.segShow(imgs[x], pixelNodeRes) vigra.show()
def makeFeat(rag, raw, labels, show=True): featureExtractor = graphs.gridRagFeatureExtractor(rag, labels) featureExtractor.labels = labels featureExtractor.graph = rag geoFeat = featureExtractor.geometricFeatures() topoFeat = featureExtractor.topologicalFeatures() features = [geoFeat, topoFeat] # ward facs wardness = numpy.array([0.0, 0.1, 0.15, 0.25, 0.5, 1.0], dtype='float32') accFeat = featureExtractor.accumulatedFeatures(raw) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.filters.gaussianGradientMagnitude(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.filters.laplacianOfGaussian(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.gaussianSmoothing(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) # st ev for s in [2.0, 3.0, 4.0]: res = stEv(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) mean = accFeat[:, 0] ucm = featureExtractor.ucmTransformFeatures(mean[:, None], wardness) features.extend([accFeat, ucm]) if False: for x in range(ucm.shape[1]): rag.showEdgeFeature(raw, ucm[:, x]) vigra.show() # hessian ev for s in [1.0, 3.0, 4.0, 6.0, 8.0]: img = hessianEv2(raw, s, 2.0) #vigra.imshow(img) #vigra.show() accFeat = featureExtractor.accumulatedFeatures(img) mean = accFeat[:, 0] ucm = featureExtractor.ucmTransformFeatures(mean[:, None], wardness) features.extend([accFeat, ucm]) if False: for x in range(ucm.shape[1]): print "x", x rag.showEdgeFeature(raw, ucm[:, x]) vigra.show() #break # features = numpy.concatenate(features, axis=1) return features