Beispiel #1
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)
Beispiel #2
0
 def rand(n, yp):
     G = nx.grid_2d_graph(INPUT_SIZE[0], INPUT_SIZE[1])
     nlabels_dict = dict()
     for u, v, d in G.edges(data=True):
         if u[0] == v[0]:
             channel = 0
         else:
             channel = 1
         d['weight'] = yp[0, u[0], u[1], channel]
         nlabels_dict[u] = n[0, u[0], u[1], 0]
         nlabels_dict[v] = n[0, v[0], v[1], 0]
     lowT, lowE = ev.FindMinEnergyThreshold(G)
     RE = ev.FindRandErrorAtThreshold(G, nlabels_dict, lowT)
     return RE
def rand_error(YP, Y):              
    G = nx.grid_2d_graph(SIZE[0], SIZE[1])
    nlabels_dict = dict()
    upto = 0
    for u, v, d in G.edges(data = True):
        if u[0] == v[0]:
            channel = 0
        else:
            channel = 1
        d['weight'] =  YP[u[0], u[1], channel]
        nlabels_dict[u] = Y[u[0], u[1], 0]
        nlabels_dict[v] = Y[v[0], v[1], 0]
    [bestT, bestE] = ev.FindMinEnergyThreshold(G)
    error = ev.FindRandErrorAtThreshold(G, nlabels_dict, bestT)
    return error
def rand_image(YP):
    G = nx.grid_2d_graph(SIZE[0], SIZE[1])
    for u, v, d in G.edges(data = True):
        if u[0] == v[0]:
            channel = 0
        else:
            channel = 1
        d['weight'] =  YP[u[0], u[1], channel]
    [bestT, bestE] = ev.FindMinEnergyThreshold(G)
    L = ev.GetLabelsAtThreshold(G, bestT)
    img = np.zeros((SIZE[0], SIZE[1]), np.single)
    for i in range(img.shape[0]):        
        for j in range(img.shape[1]):
            img[i,j] = L[(i,j)]
    return img
Beispiel #5
0
def test_sample(images, nlabels, elabels):
    G = nx.grid_2d_graph(elabels.shape[0], elabels.shape[1])
    for (u, v, d) in G.edges(data=True):
        if u[0] == v[0]:
            channel = 0
        else:
            channel = 1

        d['weight'] = elabels[u[0], u[1], channel]

    lowT, lowE = ev.FindMinEnergyThreshold(G)
    lg = G.copy()
    lg.remove_edges_from([(u, v) for (u, v, d) in G.edges(data=True)
                          if d['weight'] <= lowT])
    L = {
        node: color
        for color, comp in enumerate(nx.connected_components(lg))
        for node in comp
    }

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

    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(figsize=(8, 4))

    fig.add_subplot(1, 4, 1)
    plt.imshow(np.squeeze(images))
    fig.add_subplot(1, 4, 2)
    plt.imshow(np.squeeze(nlabels), cmap='nipy_spectral')
    fig.add_subplot(1, 4, 3)
    plt.imshow(result_image, cmap='nipy_spectral')
    #fig.add_subplot(1, 4, 4)
    #plt.imshow(elabels[:,:,1], cmap='nipy_spectral')

    plt.show()
Beispiel #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)
Beispiel #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)