Exemplo n.º 1
0
    def predict(self, graphData, rfPath, stopProbs=[0.5], damping=0.05):
        print "load random forest"
        rf = vigra.learning.RandomForest(rfPath,'rf')
        mg = graphs.mergeGraph(graphData.rag)
        df = graphs.NeuroDynamicFeatures(graphData.rag, mg)

        # assign features
        df.assignEdgeCues(graphData.eX)
        df.assignNodeCues(graphData.nX)
        df.assignEdgeSizes(graphData.eSize)
        df.assignNodeSizes(graphData.nSize)

        #NOT assign labels
        #df.assignLabels(eY)

        # register callbacks
        df.registerCallbacks()


        stopProbsArray = numpy.array(stopProbs,dtype=numpy.float32)

        labelsArray = df.predict(rf=rf, stopProbs=stopProbsArray, damping=float(damping))

        rLabels = []
        for i,sp in enumerate(stopProbs):
            print "sp",sp
            rLabels.append(labelsArray[i,:])
        return rLabels
Exemplo n.º 2
0
    def predict(self, graphData, rfPath, stopProbs=[0.5], damping=0.05):
        print "load random forest"
        rf = vigra.learning.RandomForest(rfPath, 'rf')
        mg = graphs.mergeGraph(graphData.rag)
        df = graphs.NeuroDynamicFeatures(graphData.rag, mg)

        # assign features
        df.assignEdgeCues(graphData.eX)
        df.assignNodeCues(graphData.nX)
        df.assignEdgeSizes(graphData.eSize)
        df.assignNodeSizes(graphData.nSize)

        #NOT assign labels
        #df.assignLabels(eY)

        # register callbacks
        df.registerCallbacks()

        stopProbsArray = numpy.array(stopProbs, dtype=numpy.float32)

        labelsArray = df.predict(rf=rf,
                                 stopProbs=stopProbsArray,
                                 damping=float(damping))

        rLabels = []
        for i, sp in enumerate(stopProbs):
            print "sp", sp
            rLabels.append(labelsArray[i, :])
        return rLabels
Exemplo n.º 3
0
    def getNewRf(self, graphData, eY, rfPath, rfPathNew):

        print "load random forest"
        rf = vigra.learning.RandomForest(rfPath,'rf')

        X = vigra.impex.readHDF5(rfPath, 'X')
        Y = vigra.impex.readHDF5(rfPath, 'Y')

        mg = graphs.mergeGraph(graphData.rag)
        df = graphs.NeuroDynamicFeatures(graphData.rag, mg)

        # assign features
        df.assignEdgeCues(graphData.eX)
        df.assignNodeCues(graphData.nX)
        df.assignEdgeSizes(graphData.eSize)
        df.assignNodeSizes(graphData.nSize)

        # assign labels
        df.assignLabels(eY)

        # register callbacks
        df.registerCallbacks()



        ret = df.getNewFeatureByClustering(rf=rf,noiseMagnitude=self.noise)



        if len(ret) == 1 :
            return "done"

        nN, nX, nY = ret
        print "collected",nN,"new training instances"
        nX = nX[0:nN,:]
        nY = nY[0:nN,:]

        X = numpy.concatenate([X,nX], axis=0)
        Y = numpy.concatenate([Y,nY], axis=0)

        print "train random forest"
        rf = self.getFreshRf(treeCount=1000)

        oob = rf.learnRF(X, Y)

        print oob

        print "save random forest"
        rf.writeHDF5(rfPathNew, 'rf')

        print "save out of bag"
        oobA = numpy.array([oob],dtype=numpy.float32)
        vigra.impex.writeHDF5(oobA, rfPathNew, 'oob')

        print "save features and labels "
        vigra.impex.writeHDF5(X, rfPathNew, 'X')
        vigra.impex.writeHDF5(Y, rfPathNew, 'Y')

        #sys.exit(0)
        return "not_done"
Exemplo n.º 4
0
    def cluster(wardness):

        edgeLengthsNew = numpy.concatenate(
            [eLen, numpy.zeros(nAdditionalEdges)]).astype('float32')
        edgeIndicatorNew = numpy.concatenate(
            [edgeIndicator, numpy.zeros(nAdditionalEdges)]).astype('float32')

        nodeSizes = nodeSizes_.copy()
        nodeLabels = vgraph.graphMap(originalGraph, 'node', dtype='uint32')

        nodeFeatures = vgraph.graphMap(liftedGraph, 'node', addChannelDim=True)
        nodeFeatures[:] = 0

        outWeight = vgraph.graphMap(liftedGraph,
                                    item='edge',
                                    dtype=numpy.float32)

        mg = vgraph.mergeGraph(liftedGraph)
        clusterOp = vgraph.minEdgeWeightNodeDist(mg,
                                                 edgeWeights=edgeIndicatorNew,
                                                 edgeLengths=edgeLengthsNew,
                                                 nodeFeatures=nodeFeatures,
                                                 nodeSizes=nodeSizes,
                                                 nodeLabels=nodeLabels,
                                                 beta=0.5,
                                                 metric='l1',
                                                 wardness=wardness,
                                                 outWeight=outWeight)

        clusterOp.setLiftedEdges(whereLifted)

        hc = vgraph.hierarchicalClustering(clusterOp,
                                           nodeNumStopCond=1,
                                           buildMergeTreeEncoding=False)
        hc.cluster()

        assert mg.edgeNum == 0, str(mg.edgeNum)

        # FIXME I am disabling these checks for now, but will need to investigate this further
        # They can fail because with defects and seg mask we can get unconnected pieces in the graph

        # if we have completely defected slcies, we get a non-connected merge graph
        # TODO I don't know if this is a problem, if it is, we could first remove them
        # and then add dummy values later
        #if not with_defects:
        #    assert mg.nodeNum == 1, str(mg.nodeNum)
        #else:
        #    # TODO test hypothesis
        #    assert mg.nodeNum == len(ds.defect_slice_list) + 1, "%i, %i" % (mg.nodeNum, len(ds.defect_slice_list) + 1)

        tweight = edgeIndicatorNew.copy()
        hc.ucmTransform(tweight)

        whereInLifted = liftedGraph.findEdges(extraUV)
        assert whereInLifted.min() >= 0
        feat = tweight[whereInLifted]
        assert feat.shape[0] == extraUV.shape[0]
        return feat[:, None]
Exemplo n.º 5
0
    def getNewRf(self, graphData, eY, rfPath, rfPathNew):

        print "load random forest"
        rf = vigra.learning.RandomForest(rfPath, 'rf')

        X = vigra.impex.readHDF5(rfPath, 'X')
        Y = vigra.impex.readHDF5(rfPath, 'Y')

        mg = graphs.mergeGraph(graphData.rag)
        df = graphs.NeuroDynamicFeatures(graphData.rag, mg)

        # assign features
        df.assignEdgeCues(graphData.eX)
        df.assignNodeCues(graphData.nX)
        df.assignEdgeSizes(graphData.eSize)
        df.assignNodeSizes(graphData.nSize)

        # assign labels
        df.assignLabels(eY)

        # register callbacks
        df.registerCallbacks()

        ret = df.getNewFeatureByClustering(rf=rf, noiseMagnitude=self.noise)

        if len(ret) == 1:
            return "done"

        nN, nX, nY = ret
        print "collected", nN, "new training instances"
        nX = nX[0:nN, :]
        nY = nY[0:nN, :]

        X = numpy.concatenate([X, nX], axis=0)
        Y = numpy.concatenate([Y, nY], axis=0)

        print "train random forest"
        rf = self.getFreshRf(treeCount=1000)

        oob = rf.learnRF(X, Y)

        print oob

        print "save random forest"
        rf.writeHDF5(rfPathNew, 'rf')

        print "save out of bag"
        oobA = numpy.array([oob], dtype=numpy.float32)
        vigra.impex.writeHDF5(oobA, rfPathNew, 'oob')

        print "save features and labels "
        vigra.impex.writeHDF5(X, rfPathNew, 'X')
        vigra.impex.writeHDF5(Y, rfPathNew, 'Y')

        #sys.exit(0)
        return "not_done"
Exemplo n.º 6
0
    def initialTraining(self, graphData, eY, rfPath):
        # do the inital training
        mg = graphs.mergeGraph(graphData.rag)
        df = graphs.NeuroDynamicFeatures(graphData.rag, mg)

        # assign features
        df.assignEdgeCues(graphData.eX)
        df.assignNodeCues(graphData.nX)
        df.assignEdgeSizes(graphData.eSize)
        df.assignNodeSizes(graphData.nSize)

        # assign labels
        df.assignLabels(eY)

        # register callbacks
        df.registerCallbacks()


        # the training set from level 0
        print "compute inital training set"
        features, labels  = df.computeInitalTrainingSet()

        print "features/labels.shape", features.shape, labels.shape

        print "train random forest"
        rf = self.getFreshRf(treeCount=100)
        oob = rf.learnRF(features, labels)

        print "OOB", oob

        print "save random forest"
        rf.writeHDF5(rfPath, 'rf')

        print "save out of bag"
        oobA = numpy.array([oob],dtype=numpy.float32)
        vigra.impex.writeHDF5(oobA, rfPath, 'oob')

        print "save features and labels "
        vigra.impex.writeHDF5(features, rfPath, 'X')
        vigra.impex.writeHDF5(labels, rfPath, 'Y')
Exemplo n.º 7
0
    def initialTraining(self, graphData, eY, rfPath):
        # do the inital training
        mg = graphs.mergeGraph(graphData.rag)
        df = graphs.NeuroDynamicFeatures(graphData.rag, mg)

        # assign features
        df.assignEdgeCues(graphData.eX)
        df.assignNodeCues(graphData.nX)
        df.assignEdgeSizes(graphData.eSize)
        df.assignNodeSizes(graphData.nSize)

        # assign labels
        df.assignLabels(eY)

        # register callbacks
        df.registerCallbacks()

        # the training set from level 0
        print "compute inital training set"
        features, labels = df.computeInitalTrainingSet()

        print "features/labels.shape", features.shape, labels.shape

        print "train random forest"
        rf = self.getFreshRf(treeCount=100)
        oob = rf.learnRF(features, labels)

        print "OOB", oob

        print "save random forest"
        rf.writeHDF5(rfPath, 'rf')

        print "save out of bag"
        oobA = numpy.array([oob], dtype=numpy.float32)
        vigra.impex.writeHDF5(oobA, rfPath, 'oob')

        print "save features and labels "
        vigra.impex.writeHDF5(features, rfPath, 'X')
        vigra.impex.writeHDF5(labels, rfPath, 'Y')
Exemplo n.º 8
0
print "load sizes"
edgeSize = vigra.impex.readHDF5(dopt['ragL1EdgeSize'], 'data')
nodeSize = vigra.impex.readHDF5(dopt['ragL1NodeSize'], 'data')

print "load label file"
edgeLabels = vigra.impex.readHDF5(dopt['ragL1EdgeGt'], 'data')

print "load rag"

rag = graphs.loadGridRagHDF5(dopt['ragL1'], 'data')
gridGraph = rag.baseGraph

print "get merge graph"

mg = graphs.mergeGraph(rag)

df = graphs.NeuroDynamicFeatures(rag, mg)

# assign features
df.assignEdgeCues(edgeCues)
df.assignNodeCues(nodeCues)
df.assignEdgeSizes(edgeSize)
df.assignNodeSizes(nodeSize)

# assign labels
df.assignLabels(edgeLabels)

# register callbacks
df.registerCallbacks()
Exemplo n.º 9
0

# load image and convert to LAB
img = vigra.impex.readImage(filepath)

# get super-pixels with slic on LAB image
imgLab = vigra.colors.transform_RGB2Lab(img)
labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight, superpixelDiameter)
labels = vigra.analysis.labelImage(labels)


gridGraph = graphs.gridGraph(img.shape[0:2])
rag = graphs.regionAdjacencyGraph(gridGraph, labels)

# get the merge graph
mg = graphs.mergeGraph(rag)

# do n runs where we erase k edges in each run
n = 3
k = 200
for r in range(n):

    erased = 0

    while erased < k:

        # get a random edge
        randEdgeId = numpy.random.randint(rag.edgeNum)

        print("random edge:", randEdgeId)
        # edge could be gone