예제 #1
0
파일: PGM.py 프로젝트: kolesman/pygm
    def loadFromH5(file_name):

        gm = opengm.loadGm(file_name)

        cardinalities = [gm.numberOfLabels(i) for i in range(gm.numberOfVariables)]

        factors = []
        for factor in list(gm.factors()):
            members = tuple(map(int, np.array(factor.variableIndices)))
            values = factor.copyValues().reshape(tuple([cardinalities[member] for member in members])).T
            factors.append(Factor(members, values))

        return GraphicalModel(factors, make_tree_decomposition=True)
import opengm
import numpy
#---------------------------------------------------------------
# MinSum  with SelfFusion
#---------------------------------------------------------------
numpy.random.seed(42)


gm=opengm.loadGm("/home/tbeier/models/mrf-inpainting/house-gm.h5","gm")
#---------------------------------------------------------------
# Minimize
#---------------------------------------------------------------
#get an instance of the optimizer / inference-algorithm

inf = opengm.inference.CheapInitialization(gm)
inf.infer()
arg = inf.arg()
print gm.evaluate(arg)






infParam = opengm.InfParam(
    numIt=2000,
    generator='upDown'
)
inf=opengm.inference.FusionBased(gm, parameter=infParam)
inf.setStartingPoint(arg)
# start inference (in this case verbose infernce)
예제 #3
0
def runBenchmark(fNames,solvers,outFolder,dataSetName,plot=False):

	nFiles  = len(fNames)
	nSolver = len(solvers)

	result = dict()

	for fNr,fName in enumerate(fNames):

		#if fNr!=1:
		#	continue
		print fNr+1,"/",nFiles,":",fName
		print "load gm"
		if isinstance(fName,str):
			print "from string"
			gm = opengm.loadGm(fName)
		else :
			print "from gm"
			gm = fName
		print gm

		if plot:
			pr=[]
		names=[]
		#fig, ax = plt.subplots()


		fileResult=dict()
		#fileResult[fn]=fName

		for sNr,solver in enumerate(solvers) :

			


			(sName,sClass,sParam)=solver
			print sName
			inf=sClass(gm=gm,parameter=sParam)
			tv=inf.timingVisitor(verbose=True,multiline=False,visitNth=1)
			inf.infer(tv)

			# store results
			solverResult=dict()

			solverResult['values'] 		= tv.getValues()
			solverResult['times'] 		= tv.getTimes()
			solverResult['bounds'] 		= tv.getBounds()
			solverResult['iterations'] 	= tv.getIterations()
			solverResult['name']		= sName
			solverResult['arg']			= inf.arg()
			solverResult['gmName']		= fName


			# write to file
			storeSingleResult(result=tv,outFolder=outFolder,dataSetName=dataSetName,solverName=sName,gmName=fName)


			# write into result dict
			fileResult[sName] 			= solverResult

			if plot:
				pr.append(plotInfRes(tv))
			print sName
			names.append(sName)

		result[fName]=fileResult

		print names
		if plot:
			plt.legend( names,loc= 5)
			#plt.legend(pr,names)
			plt.show()
예제 #4
0
import opengm
import numpy
#---------------------------------------------------------------
# MinSum  with SelfFusion
#---------------------------------------------------------------
numpy.random.seed(42)

gm = opengm.loadGm("/home/tbeier/models/mrf-inpainting/house-gm.h5", "gm")
#---------------------------------------------------------------
# Minimize
#---------------------------------------------------------------
#get an instance of the optimizer / inference-algorithm

inf = opengm.inference.CheapInitialization(gm)
inf.infer()
arg = inf.arg()
print gm.evaluate(arg)

infParam = opengm.InfParam(numIt=2000, generator='upDown')
inf = opengm.inference.FusionBased(gm, parameter=infParam)
inf.setStartingPoint(arg)
# start inference (in this case verbose infernce)
visitor = inf.verboseVisitor(printNth=1, multiline=True)
inf.infer(visitor)
arg = inf.arg()
예제 #5
0
import opengm
import vigra
import nifty
import numpy


#f = "/home/tbeier/Desktop/mc_models/knott-3d-150/gm_knott_3d_039.h5"
#f = "/home/tbeier/Desktop/mc_models/knot-3d-550/gm_knott_3d_119.h5"
f = "/home/tbeier/Desktop/mc_models/knott-3d-450/gm_knott_3d_103.h5"
#f = "/home/tbeier/Downloads/gm_large_3.gm"
#f = "/home/tbeier/Downloads/gm_small_1.gm"
f = "/home/tbeier/Desktop/mc_models/knott-3d-300/gm_knott_3d_072.h5"
gm = opengm.loadGm(f)





def opengmToNumpy(gm):
    nNodes = gm.numberOfVariables
    nEdges = gm.numberOfFactors


    factorSubset=opengm.FactorSubset(gm)
    weights = factorSubset.evaluate([0,1])
    weightsB = factorSubset.evaluate([0,0])
    print "wb", weightsB.sum()
    vis =  factorSubset.variableIndices()
    assert len(numpy.unique(vis.reshape(-1))) == nNodes
    #print vis.shape,weights.shape
    assert vis.max()+1 == nNodes
import opengm
import numpy
np = numpy
#---------------------------------------------------------------
# MinSum  with SelfFusion
#---------------------------------------------------------------
numpy.random.seed(42)

#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/3096.bmp.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/175032.bmp.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/291000.bmp.h5","gm")
gm = opengm.loadGm("/home/tbeier/datasets/image-seg/148026.bmp.h5", "gm")
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-450/gm_knott_3d_102.h5","gm")#(ROTTEN)
gm = opengm.loadGm("/home/tbeier/datasets/knott-3d-300/gm_knott_3d_078.h5",
                   "gm")
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-150/gm_knott_3d_038.h5","gm")

#---------------------------------------------------------------
# Minimize
#---------------------------------------------------------------
#get an instance of the optimizer / inference-algorithm

print gm

N = np.arange(0.0, 10, 0.5)
R = np.arange(0.1, 0.99, 0.1)

print N
print R

for n in N:
import opengm
import numpy
#---------------------------------------------------------------
# MinSum  with SelfFusion
#---------------------------------------------------------------
numpy.random.seed(42)

#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/3096.bmp.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/175032.bmp.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/291000.bmp.h5","gm")
gm=opengm.loadGm("/home/tbeier/datasets/image-seg/148026.bmp.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-450/gm_knott_3d_102.h5","gm")#(ROTTEN)
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-450/gm_knott_3d_096.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-300/gm_knott_3d_078.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-150/gm_knott_3d_038.h5","gm")
gm=opengm.loadGm("/home/tbeier/datasets/modularity-clustering/polbooks.h5","gm")
#---------------------------------------------------------------
# Minimize
#---------------------------------------------------------------
#get an instance of the optimizer / inference-algorithm



print gm





verbose = True
useQpbo = True
예제 #8
0
import opengm
import numpy
np = numpy
#---------------------------------------------------------------
# MinSum  with SelfFusion
#---------------------------------------------------------------
numpy.random.seed(42)

#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/3096.bmp.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/175032.bmp.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/291000.bmp.h5","gm")
gm=opengm.loadGm("/home/tbeier/datasets/image-seg/148026.bmp.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-450/gm_knott_3d_102.h5","gm")#(ROTTEN)
gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-300/gm_knott_3d_078.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-150/gm_knott_3d_038.h5","gm")

#---------------------------------------------------------------
# Minimize
#---------------------------------------------------------------
#get an instance of the optimizer / inference-algorithm



print gm





N = np.arange(0.0, 10, 0.5)
R = np.arange(0.1, 0.99, 0.1)
def runBenchmark(fNames, solvers, outFolder, dataSetName, plot=False):

    nFiles = len(fNames)
    nSolver = len(solvers)

    result = dict()

    for fNr, fName in enumerate(fNames):

        #if fNr!=1:
        #	continue
        print fNr + 1, "/", nFiles, ":", fName
        print "load gm"
        if isinstance(fName, str):
            print "from string"
            gm = opengm.loadGm(fName)
        else:
            print "from gm"
            gm = fName
        print gm

        if plot:
            pr = []
        names = []
        #fig, ax = plt.subplots()

        fileResult = dict()
        #fileResult[fn]=fName

        for sNr, solver in enumerate(solvers):

            (sName, sClass, sParam) = solver
            print sName
            inf = sClass(gm=gm, parameter=sParam)
            tv = inf.timingVisitor(verbose=True, multiline=False, visitNth=1)
            inf.infer(tv)

            # store results
            solverResult = dict()

            solverResult['values'] = tv.getValues()
            solverResult['times'] = tv.getTimes()
            solverResult['bounds'] = tv.getBounds()
            solverResult['iterations'] = tv.getIterations()
            solverResult['name'] = sName
            solverResult['arg'] = inf.arg()
            solverResult['gmName'] = fName

            # write to file
            storeSingleResult(result=tv,
                              outFolder=outFolder,
                              dataSetName=dataSetName,
                              solverName=sName,
                              gmName=fName)

            # write into result dict
            fileResult[sName] = solverResult

            if plot:
                pr.append(plotInfRes(tv))
            print sName
            names.append(sName)

        result[fName] = fileResult

        print names
        if plot:
            plt.legend(names, loc=5)
            #plt.legend(pr,names)
            plt.show()
예제 #10
0
import opengm
import numpy
#---------------------------------------------------------------
# MinSum  with SelfFusion
#---------------------------------------------------------------
numpy.random.seed(42)

#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/3096.bmp.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/175032.bmp.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/image-seg/291000.bmp.h5","gm")
gm = opengm.loadGm("/home/tbeier/datasets/image-seg/148026.bmp.h5", "gm")
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-450/gm_knott_3d_102.h5","gm")#(ROTTEN)
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-450/gm_knott_3d_096.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-300/gm_knott_3d_078.h5","gm")
#gm=opengm.loadGm("/home/tbeier/datasets/knott-3d-150/gm_knott_3d_038.h5","gm")
gm = opengm.loadGm("/home/tbeier/datasets/modularity-clustering/polbooks.h5",
                   "gm")
#---------------------------------------------------------------
# Minimize
#---------------------------------------------------------------
#get an instance of the optimizer / inference-algorithm

print gm

verbose = True
useQpbo = True
useCgc = True
useWs = True

with opengm.Timer("with new method"):
    segA = segA.swapaxes(0,1)
    f.close()
    ctable = numpy.random.random((segA.max()+1, 3)).astype(numpy.float32)
    segA_img = ctable[segA]
    
    ret = 1.0*imgLab #+ 0.6*segA_img 
    return ret

if __name__ == "__main__":
    num = 69015
    
    r = initImage(num)
    
    gmFname = "/media/tkroeger/pinky/image-seg/image-seg/0.3/%d.bmp.h5" % num
    
    gm = opengm.loadGm(gmFname, "gm")
    r = cgp.GeometryReader("/tmp/geom.h5")
    
    #MC-I
    param_mci = opengm.InfParam(workFlow="CCIFD")
    mci = opengm.inference.Multicut(gm, parameter=param_mci)
    mci.infer()
    arg_mci = mci.arg().astype(numpy.uint32)
    statesOpt = node_to_edge_labeling(arg_mci, r)
    resultFor(69015, r, arg=arg_mci, fname = "%d_MCI.png" % num, statesOpt=statesOpt)
    
    #a-exp-ecc
    f=h5py.File("69015_sol_aexpcc.h5"); arg_aexpcc = f["states"].value.astype(numpy.uint32); f.close()
    resultFor(69015, r, arg=arg_aexpcc,  fname = "%d_aexpcc.png"  % num, statesOpt=statesOpt)
    sys.exit()
   
예제 #12
0
    f = h5py.File("/tmp/seg.h5", 'w')
    f.create_dataset("seg", data=(segA).astype(numpy.uint32))
    f.close()

    segmentation.oversegmentation.cgpx("/tmp/seg.h5", "/tmp/tg.h5")
    segmentation.oversegmentation.cgpr("/tmp/tg.h5", "/tmp/geom.h5")
    segmentation.oversegmentation.tgslices("/tmp/tg.h5", "/tmp/tgslices.h5")
    
    r = cgp.GeometryReader("/tmp/geom.h5")
    return r

if __name__ == "__main__":
    num = 69015
    r = initImage(num)
    
    statesOpt = optimalStates(num, r)
    
    gm = opengm.loadGm(" ./gm.h5", "gm")
    r = cgp.GeometryReader("/tmp/geom.h5")
    param = opengm.InfParam(bookkeeping=True, planar=True)
    cgc = opengm.inference.Cgc(gm, parameter=param)
    cgc.infer()
    labels = cgc.arg().astype(numpy.uint32)
    resultFor(69015, r, arg=labels, fname = "%d_CGC.png" % num)
    
    resultFor(num, r, "MC_CCFDB", statesOpt=statesOpt)
    resultFor(num, r, "CCP", statesOpt=statesOpt)
    resultFor(num, r, "KL", statesOpt=statesOpt)
    resultFor(num, r, "planarcc_40", statesOpt=statesOpt)
    resultFor(num, r, "MC_CCIFD", statesOpt=statesOpt)
예제 #13
0
import opengm
import vigra
import nifty
import numpy

f = "/home/tbeier/Desktop/mc_models/knott-3d-150/gm_knott_3d_039.h5"
#f = "/home/tbeier/Desktop/mc_models/knot-3d-550/gm_knott_3d_119.h5"
#f = "/home/tbeier/Desktop/mc_models/knott-3d-450/gm_knott_3d_103.h5"
#f = "/home/tbeier/Downloads/gm_large_3.gm"
#f = "/home/tbeier/Downloads/gm_small_1.gm"
#f = "/home/tbeier/Desktop/mc_models/knott-3d-300/gm_knott_3d_072.h5"
gm = opengm.loadGm(f)


def opengmToNumpy(gm):
    nNodes = gm.numberOfVariables
    nEdges = gm.numberOfFactors

    factorSubset = opengm.FactorSubset(gm)
    weights = factorSubset.evaluate([0, 1])
    weightsB = factorSubset.evaluate([0, 0])
    print "wb", weightsB.sum()
    vis = factorSubset.variableIndices()
    assert len(numpy.unique(vis.reshape(-1))) == nNodes
    #print vis.shape,weights.shape
    assert vis.max() + 1 == nNodes
    return nNodes, weights, vis


nNodes, weights, uvs = opengmToNumpy(gm)
nFac = weights.shape[0]