예제 #1
0
def TestBansal():
    G = syn.InitRandom(10, makeInt=True)

    WG = G.copy()
    WG.remove_edges_from([(u, v) for (u, v, d) in WG.edges(data=True)
                          if d['weight'] < 0.0])

    viz.DrawGraph(WG, title='original')

    print("Trying Bansal")
    ban = bansal.BansalSolver(WG)
    components = ban.run()
    labels = dict()
    ci = 1
    for component in components:
        for v in component:
            labels[v] = ci
        ci = ci + 1

    E = seg.GetLabelEnergy(G, labels)
    print("Bansal energy is " + str(E))

    L = seg.GetLabelsAtThreshold(G, theta=0)
    E = seg.GetLabelEnergy(G, L)
    print("CC at zero is " + str(E))

    thresh, minE = seg.FindMinEnergyThreshold(G)
    print("CC min is " + str(minE))

    plt.show()
    print("Done")
예제 #2
0
def FindBestThreshold(NG, RG, WCLabels):

    edgeWeights = [(u, v, w) for (u, v, w) in NG.edges(data='weight')]
    sortedEdges = sorted(edgeWeights, reverse=True, key=lambda edge: edge[2])

    bestL = WCLabels.copy()
    bestE = seg.GetLabelEnergy(RG, bestL)
    bestT = sortedEdges[0][2] + DELTA_TOLERANCE
    print("E = " + str(bestE) + " from T = " + str(bestT))

    for e in sortedEdges:
        t = e[2] - DELTA_TOLERANCE
        L = seg.GetLabelsAtThreshold(NG, theta=t)
        lowerL = WCLabels.copy()
        for n in lowerL:
            lowerL[n] = L[lowerL[n]]

        lowerE = seg.GetLabelEnergy(RG, lowerL)
        if lowerE < bestE:
            bestE = lowerE
            bestL = lowerL.copy()
            bestT = t
            print("E = " + str(bestE) + " from T = " + str(bestT))

    return bestL, bestE, bestT
예제 #3
0
def TestKruskal():
    #np.random.seed(1)
    WG = syn.InitRandom(10, 1, False)
    viz.DrawGraph(WG, title='original')

    # Get the labeling at threshold 0 and calculate energy
    L = seg.GetLabelsAtThreshold(WG, theta=0)
    E = seg.GetLabelEnergy(WG, L)
    print("Energy at zero is " + str(E))
    viz.DrawGraph(WG, labels=LG, title='original')

    # Find the best threshold and check it
    thresh, minE = seg.FindMinEnergyThreshold(WG)
    print("Minimum energy at " + str(thresh) + " is " + str(minE))
    # check to see if that's correct
    L = seg.GetLabelsAtThreshold(WG, theta=thresh)
    E2 = seg.GetLabelEnergy(WG, L)
    print("Energy check: " + str(E2))
    viz.DrawGraph(WG, labels=LG, title='bestThresh')

    plt.show()
예제 #4
0
def Apply(img, seg, W, b):
    
    USE_CC_INFERENCE = True

    imgn = (img / img.max()) * 2.0 - 1.0
    
    mySeed = 37
    
    np.random.seed(mySeed)

    (patchImg, patchSeg, G, nlabels, elabels) = ev.SamplePatch(imgn, seg, PATCH_SIZE, KERNEL_SIZE)

    (X_test, Y_test) = UnrollData(patchImg, patchSeg, G, nlabels, elabels)
    
    pca = PCA(n_components=D, whiten=True)    
    X_test = pca.fit_transform(X_test)

    X = tf.placeholder(tf.float32,name="X")
    Y = tf.placeholder(tf.float32, name="Y")
        
    YP = DefineNetwork(X, W, b) 

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        y_pred = sess.run(YP, {X: X_test})
                        
    upto = 0
    for u, v, d in G.edges(data = True):
        d['weight'] = float(y_pred[upto])
        upto = upto + 1
    
    if USE_CC_INFERENCE:
        [bestT, bestE] = ev.FindMinEnergyThreshold(G)
    else:
        bestT = 0.0

    predMin = y_pred.min()
    predMax = y_pred.max()
    print("Best T " + str(bestT) + " from range " + str(predMin) + " -> " + str(predMax))

    labels = seglib.GetLabelsAtThreshold(G, bestT)
    #viz.DrawGraph(G, labels=labels, title='Pred labels')         
    #plt.show()

    patchPred = np.zeros( (patchSeg.shape[0], patchSeg.shape[1]), np.float32)
    for j in range(patchSeg.shape[1]):
        for i in range(patchSeg.shape[0]):
            patchPred[i,j] = labels[(i,j)]

    ShowResult(patchImg, patchSeg, patchPred)
예제 #5
0
def Minimize(WG, nodeType=None):

    if nodeType is None:
        nodeType = 'CC'

    #print("DsnNode: Minimizing...")
    param = dict()
    param['nodeType'] = nodeType

    if nodeType == 'CC':
        thresh, minE = seg.FindMinEnergyThreshold(WG)
        #print("WS Min  Energy: " + str(minE) + "           @ t=" + str(thresh))
        param['threshold'] = thresh
        L = seg.GetLabelsAtThreshold(WG, theta=thresh)
        #E2 = seg.GetLabelEnergy(WG, L)
        #print("Energy check: " + str(minE) + " verse " + str(E2))

    elif nodeType == 'WC':
        WC = seg.GetWatershedGraph(WG)

        thresh, minE = seg.FindMinEnergyThreshold(WC, eval=WG)
        #print("WS Min  Energy: " + str(minE) + "           @ t=" + str(thresh))
        param['threshold'] = thresh
        L = seg.GetLabelsAtThreshold(WC, theta=thresh)
        #E2 = seg.GetLabelEnergy(WG, L)
        #print("Energy check: " + str(E2))

    elif nodeType == 'WS':  # fixed threshold WC
        WC = seg.GetWatershedGraph(WG)

        param['threshold'] = 0.0
        L = seg.GetLabelsAtThreshold(WC, theta=0.0)
        minE = seg.GetLabelEnergy(WG, L)
        #print("Energy check: " + str(E2))

    #print("DsnNode: Done")
    return L, minE, param
예제 #6
0
def Train(img, seg, USE_CC_INFERENCE=False, NUM_ITERATIONS=1000):

    imgn = (img / img.max()) * 2.0 - 1.0

    mySeed = 37

    np.random.seed(mySeed)

    (patchImg, patchSeg, G, nlabels,
     elabels) = ev.SamplePatch(imgn, seg, PATCH_SIZE, KERNEL_SIZE)

    (X_train, Y_train) = UnrollData(patchImg, patchSeg, G, nlabels, elabels)

    #return
    pca = PCA(n_components=D, whiten=True)
    X_train = pca.fit_transform(X_train)
    print(X_train.shape)
    print(Y_train.shape)

    #viz.DrawGraph(G, labels=nlabels, title='GT labels')
    #ShowPatch(patchImg, patchSeg)

    def rand_loss_function(YT, YP):
        def GetRandWeights(Y, A):
            edgeInd = dict()
            upto = 0
            for u, v, d in G.edges(data=True):
                d['weight'] = A[upto]
                edgeInd[(u, v)] = upto
                upto = upto + 1

            myT = np.zeros((1, 1), np.float32)

            if USE_CC_INFERENCE:
                [
                    bestT, lowE, posCounts, negCounts, mstEdges, totalPos,
                    totalNeg
                ] = ev.FindMinEnergyAndRandCounts(G, nlabels)
                myT[0] = bestT
            else:
                [posCounts, negCounts, mstEdges, totalPos,
                 totalNeg] = ev.FindRandCounts(G, nlabels)

            #print("Num pos: " + str(totalPos) + " neg: " + str(totalNeg))
            WY = np.zeros((N, 1), np.float32)
            SY = np.ones((N, 1), np.float32)
            posIndex = np.zeros((N, 1), np.bool)
            negIndex = np.zeros((N, 1), np.bool)

            # for class imbalance
            posWeight = 0.0
            negWeight = 0.0
            # start off with every point in own cluster
            posError = totalPos
            negError = 0.0

            for i in range(len(posCounts)):
                ind = edgeInd[mstEdges[i]]
                posError = posError - posCounts[i]
                negError = negError + negCounts[i]

                WS = posError - negError

                WY[ind] = abs(WS)
                if WS > 0.0:
                    posWeight = posWeight + WY[ind]
                    posIndex[ind] = True
                    if Y[ind] < 0.0:
                        SY[ind] = -1.0

                if WS < 0.0:
                    negWeight = negWeight + WY[ind]
                    negIndex[ind] = True
                    if Y[ind] > 0.0:
                        SY[ind] = -1.0

            # Std normalization
            totalW = np.sum(WY)
            if totalW > 0.0:
                WY = WY / totalW
            #print("Num pos: " + str(sum(posIndex)) + " neg: " + str(sum(negIndex)))
            # Equal class weight
            #if posWeight > 0.0:
            #    WY[posIndex]  = WY[posIndex] / posWeight
            #if negWeight > 0.0:
            #    WY[negIndex]  = WY[negIndex] / negWeight

            return (SY, WY, myT)

        (SY, WY, bestT) = tf.py_func(GetRandWeights, [YT, YP],
                                     [tf.float32, tf.float32, tf.float32])

        newY = tf.multiply(SY, YT)

        if USE_CC_INFERENCE:
            edgeLoss = tf.maximum(
                0.0, tf.subtract(1.0, tf.multiply(tf.subtract(YP, bestT),
                                                  newY)))
        else:
            edgeLoss = tf.maximum(0.0, tf.subtract(1.0, tf.multiply(YP, newY)))

        weightedLoss = tf.multiply(WY, edgeLoss)

        return tf.reduce_sum(weightedLoss)

    def test_loss(YT, YP):
        sloss = tf.maximum(0.0, tf.subtract(1.0, tf.multiply(YP, YT)))
        #sloss = tf.maximum(0.0, tf.multiply(-1.0, tf.multiply(YP, YT)))
        #sloss = tf.square(tf.subtract(YT, YP))
        #print(sloss)
        #return tf.reduce_sum(sloss)
        return tf.reduce_mean(sloss)

    weights_shape = (D, NUM_OUTPUTS)
    bias_shape = (1, NUM_OUTPUTS)

    W = tf.Variable(
        dtype=tf.float32,
        initial_value=tf.random_normal(weights_shape))  # Weights of the model
    b = tf.Variable(dtype=tf.float32,
                    initial_value=tf.random_normal(bias_shape))
    #W = tf.Variable(dtype=tf.float32, initial_value=tf.zeros(weights_shape))  # Weights of the model
    #b = tf.Variable(dtype=tf.float32, initial_value=tf.ones(bias_shape))

    X = tf.placeholder(tf.float32, name="X")
    Y = tf.placeholder(tf.float32, name="Y")

    YP = tf.subtract(tf.matmul(X, W), b)

    #loss_function = test_loss(Y, YP)
    loss_function = rand_loss_function(Y, YP)

    learner = tf.train.GradientDescentOptimizer(0.1).minimize(loss_function)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(NUM_ITERATIONS):
            result = sess.run(learner, {X: X_train, Y: Y_train})
            if i % (NUM_ITERATIONS / 10) == 0:
                loss = sess.run(loss_function, {X: X_train, Y: Y_train})
                #print("Iteration {}:\tLoss={:.6f}".format(i, sess.run(error_function, {X: X_train, Y: Y_train})))
                print("Iteration " + str(i) + ": " + str(loss))

        W_final, b_final = sess.run([W, b])
        y_pred = sess.run(YP, {X: X_train})

        loss = sess.run(loss_function, {X: X_train, Y: Y_train})
        print("Final loss: " + str(loss))

    upto = 0
    for u, v, d in G.edges(data=True):
        d['weight'] = float(y_pred[upto])
        upto = upto + 1

    predMin = y_pred.min()
    predMax = y_pred.max()
    print("Prediction has range " + str(predMin) + " -> " + str(predMax))

    if USE_CC_INFERENCE:
        [bestT, bestE] = ev.FindMinEnergyThreshold(G)
    else:
        bestT = 0.0

    finalRand = ev.FindRandErrorAtThreshold(G, nlabels, bestT)
    print("At threshold " + str(bestT) + " Final Error: " + str(finalRand))

    labels = seglib.GetLabelsAtThreshold(G, bestT)
    #viz.DrawGraph(G, labels=labels, title='Pred labels')
    #plt.show()

    patchPred = np.zeros((patchSeg.shape[0], patchSeg.shape[1]), np.float32)
    for j in range(patchSeg.shape[1]):
        for i in range(patchSeg.shape[0]):
            patchPred[i, j] = labels[(i, j)]

    ShowResult(patchImg, patchSeg, patchPred)
    #print(W_final)
    #print(b_final)
    return (W_final, b_final)
예제 #7
0
def Apply(img, seg, W_train, B_train, USE_CC_INFERENCE=False):

    imgn = (img / img.max()) * 2.0 - 1.0

    mySeed = 600

    np.random.seed(mySeed)

    (patchImg, patchSeg, G, nlabels,
     elabels) = ev.SamplePatch(imgn, seg, PATCH_SIZE, KERNEL_SIZE)

    (X_train, Y_train) = UnrollData(patchImg, patchSeg, G, nlabels, elabels)

    #return
    pca = PCA(n_components=D, whiten=True)
    X_train = pca.fit_transform(X_train)
    #print(X_train.shape)
    #print(Y_train.shape)

    #viz.DrawGraph(G, labels=nlabels, title='GT labels')
    #ShowPatch(patchImg, patchSeg)

    W = tf.Variable(dtype=tf.float32, initial_value=W_train)
    b = tf.Variable(dtype=tf.float32, initial_value=B_train)

    X = tf.placeholder(tf.float32, name="X")
    Y = tf.placeholder(tf.float32, name="Y")

    YP = tf.subtract(tf.matmul(X, W), b)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        y_pred = sess.run(YP, {X: X_train})

    upto = 0
    for u, v, d in G.edges(data=True):
        d['weight'] = float(y_pred[upto])
        upto = upto + 1

    predMin = y_pred.min()
    predMax = y_pred.max()
    print("Prediction has range " + str(predMin) + " -> " + str(predMax))

    if USE_CC_INFERENCE:
        [bestT, bestE] = ev.FindMinEnergyThreshold(G)
    else:
        bestT = 0.0

    finalRand = ev.FindRandErrorAtThreshold(G, nlabels, bestT)
    print("At threshold " + str(bestT) + " Final Error: " + str(finalRand))

    labels = seglib.GetLabelsAtThreshold(G, bestT)
    #viz.DrawGraph(G, labels=labels, title='Pred labels')
    #plt.show()

    patchPred = np.zeros((patchSeg.shape[0], patchSeg.shape[1]), np.float32)
    for j in range(patchSeg.shape[1]):
        for i in range(patchSeg.shape[0]):
            patchPred[i, j] = labels[(i, j)]

    ShowResult(patchImg, patchSeg, patchPred)
예제 #8
0
def test(mode=None, pretrained_weights=None):
    my_model = model.unet(mode, pretrained_weights)

    print('Getting data')
    f = open('../synimage/test.p', 'rb')
    data = pickle.load(f)
    f.close()
    print('Done getting data')

    x = np.array(data[0])
    y = np.array(data[1])
    z = np.array(data[2])

    pred = my_model.predict([x, y], batch_size=1)

    result = []

    for idx in range(x.shape[0]):
        images, nlabels, elabels = x[idx], y[idx], z[idx]
        G = nx.grid_2d_graph(elabels.shape[0], elabels.shape[1])
        gtLabels = dict()
        for (u, v, d) in G.edges(data=True):
            if u[0] == v[0]:
                channel = 0
            else:
                channel = 1
            gtLabels[u] = nlabels[u[0], u[1], 0]
            gtLabels[v] = nlabels[v[0], v[1], 0]
            d['weight'] = pred[idx, u[0], u[1], channel]

        a = pred[idx, :, :, 0]
        b = pred[idx, :, :, 1]

        lowT, lowE, posCountsRand, negCountsRand, mstEdges, mstEdgeWeights, totalPosRand, totalNegRand = ev.FindMinEnergyAndRandCounts(
            G, gtLabels)
        randE = ev.FindRandErrorAtThreshold(G, gtLabels, lowT)
        print('------------------')
        print(np.min(a), np.percentile(a, 25), np.median(a),
              np.percentile(a, 75), np.max(a), np.mean(a))
        print(np.min(b), np.percentile(b, 25), np.median(b),
              np.percentile(b, 75), np.max(b), np.mean(b), lowT)

        L = seglib.GetLabelsAtThreshold(G, lowT)

        result_image = np.zeros((elabels.shape[0], elabels.shape[1]),
                                np.float32)

        for j in range(result_image.shape[1]):
            for i in range(result_image.shape[0]):
                result_image[i, j] = L[(i, j)]

        fig = plt.figure()
        fig.add_subplot(1, 5, 1)
        plt.imshow(np.squeeze(images))
        fig.add_subplot(1, 5, 2)
        plt.imshow(np.squeeze(nlabels))
        fig.add_subplot(1, 5, 3)
        plt.imshow(pred[idx, :, :, 0])
        fig.add_subplot(1, 5, 4)
        plt.imshow(pred[idx, :, :, 1])
        fig.add_subplot(1, 5, 5)
        plt.imshow(result_image)
        plt.title('lowT: ' + str(lowT) + 'lowE: ' + str(lowE))

        result.append([lowT, lowE, randE])

        fname = 'test/' + str(mode) + '-' + str(idx) + '.png'
        fig.savefig(fname)
    return result