Beispiel #1
0
def TestRand():

    mySeed = 37
    width = 3

    np.random.seed(mySeed)
    RG = syn.InitRandom(width)
    #RG = syn.InitSimple(width)
    viz.DrawGraph(RG, title='original')

    myLabels = dict()
    i = 1
    for n in RG:
        myLabels[n] = i
        i = i + 1

    viz.DrawGraph(RG, labels=myLabels, title='Ind labels')

    #CCLabels, CCE, param = dsnNode.Minimize(RG, nodeType='CC')
    #viz.DrawGraph(RG, labels=CCLabels, title='CC labels')

    (posCounts, negCounts, mstEdges) = ev.FindRandCounts(RG, myLabels)
    #print(posCounts)
    #print(negCounts)
    plt.show()
Beispiel #2
0
def TestMSTSwap():
    print("Loading")
    # This gets 100 by 100
    (imgOrig, segOrig) = bsds.LoadTrain(0)    
    (img1, seg1, img, seg) = bsds.ScaleAndCropData(imgOrig, segOrig)    
    
    bsds.VizTrainTest(img1, seg1, img, seg)
    
    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)
    
    ShowPatch(patchImg, patchSeg)    
    
    (X_train, Y_train) = UnrollData(patchImg, patchSeg, G, nlabels, elabels)
    
    pca = PCA(n_components=D, whiten=True)    
    X_train = pca.fit_transform(X_train)
    
    W = np.ones((D,1), np.float32)
    print(X_train.shape)
    Y_pred = np.matmul(X_train, W)
    print(Y_pred.shape)
    
    upto = 0
    for u, v, d in G.edges(data = True):
        d['weight'] = Y_pred[upto]
        upto = upto + 1

    [posCounts, negCounts, mstEdges, edgeInd, totalPos, totalNeg] = ev.FindRandCounts(G, nlabels)
    mstW = np.zeros( (len(posCounts), 1))

    posError = totalPos
    negError = 0.0
    swapToPos = 0.0
    swapToNeg = 0.0
    for i in range(len(posCounts)):

        posError = posError - posCounts[i]
        negError = negError + posCounts[i]                                        
        mstW[i] = posError - negError
        
        if mstW[i] > 0.0:
            if Y_train[ edgeInd[i] ] < 0.0:
                swapToPos = swapToPos + 1.0
        else:
            if Y_train[ edgeInd[i] ] > 0.0:
                swapToNeg = swapToNeg + 1.0

    print("MST has " + str(len(posCounts)) + " edges")
    print("MST had " + str(swapToPos) + " swap to pos")
    print("MST had " + str(swapToNeg) + " swap to neg")
    plt.show()
Beispiel #3
0
    def GetRandWeights(n, y):
        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'] = y[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]

        [posCounts, negCounts, mstEdges, totalPos,
         totalNeg] = ev.FindRandCounts(G, nlabels_dict)

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

        WY = np.zeros((1, INPUT_SIZE[0], INPUT_SIZE[1], 2), np.float32)
        SY = np.zeros((1, INPUT_SIZE[0], INPUT_SIZE[1], 2), np.float32)
        for i in range(len(posCounts)):
            posError = posError - posCounts[i]
            negError = negError + negCounts[i]

            WS = posError - negError

            (u, v) = mstEdges[i]

            if u[0] == v[0]:  #vertical, dy, channel 0
                channel = 0
            else:  #horizontal, dx, channel 1
                channel = 1

            if WS > 0.0:
                WY[0, u[0], u[1], channel] = abs(WS) + posWeight
                #if nlabels_dict[u] != nlabels_dict[v]:
                #SY[0, u[0], u[1], channel] = -1.0
                SY[0, u[0], u[1], channel] = 1.0
            if WS < 0.0:
                WY[0, u[0], u[1], channel] = abs(WS) + negWeight
                #if nlabels_dict[u] == nlabels_dict[v]:
                #SY[0, u[0], u[1], channel] = -1.0
                SY[0, u[0], u[1], channel] = -1.0

        # Std normalization
        totalW = np.sum(WY)
        if totalW != 0.0:
            WY = np.divide(WY, totalW)

        return WY, SY
def get_rand_weight(YP, Y):
    G = nx.grid_2d_graph(SIZE[0], 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] = Y[0, u[0], u[1], 0]
        nlabels_dict[v] = Y[0, v[0], v[1], 0]
    [posCounts, negCounts, mstEdges, totalPos,
     totalNeg] = ev.FindRandCounts(G, nlabels_dict)
    posError = totalPos
    negError = 0.0
    WY = np.zeros((1, SIZE[0], SIZE[1], 2), np.single)
    SY = np.zeros((1, SIZE[0], SIZE[1], 2), np.single)
    for i in range(len(posCounts)):
        (u, v) = mstEdges[i]
        if u[0] == v[0]:  #vertical, dy, channel 0
            channel = 0
        else:  #horizontal, dx, channel 1
            channel = 1

        #''diff'' loss
        '''
        if YP[0, u[0], u[1], channel] < 0.0:
            WY[0, u[0], u[1], channel] = posCounts[i]/(posCounts[i]+negCounts[i])
            SY[0, u[0], u[1], channel] = 1.0
        elif YP[0, u[0], u[1], channel] > 0.0:
            WY[0, u[0], u[1], channel] = negCounts[i]/(posCounts[i]+negCounts[i])
            SY[0, u[0], u[1], channel] = -1.0
        '''

        #''hit'' loss
        WS = posCounts[i] - negCounts[i]
        if WS > 0.0:
            SY[0, u[0], u[1], channel] = 1.0
            WY[0, u[0], u[1], channel] = WS / (posCounts[i] + negCounts[i])
        elif WS < 0.0:
            SY[0, u[0], u[1], channel] = -1.0
            WY[0, u[0], u[1], channel] = -WS / (posCounts[i] + negCounts[i])

    return [WY, SY]
Beispiel #5
0
def get_rand_weight(YP, Y):
    G = nx.grid_2d_graph(SIZE[0], SIZE[1])
    nlabels_dict = dict()
    for u, v, d in G.edges(data=True):
        if u[0] == v[0]:  #vertical, dy, channel 0
            channel = 0
        if u[1] == v[1]:  #horizontal, dy, channel 1
            channel = 1
        d['weight'] = (YP[0, u[0], u[1], 0] + YP[0, v[0], v[1], 0]) / 2.0
        nlabels_dict[u] = Y[0, u[0], u[1], 0]
        nlabels_dict[v] = Y[0, v[0], v[1], 0]

    [posCounts, negCounts, mstEdges, totalPos,
     totalNeg] = ev.FindRandCounts(G, nlabels_dict)
    posError = totalPos
    negError = 0.0
    WY = np.zeros((1, SIZE[0], SIZE[1], 1), np.single)
    SY = np.zeros((1, SIZE[0], SIZE[1], 1), np.single)
    for i in range(len(posCounts)):
        posError = posError - posCounts[i]
        negError = negError + negCounts[i]
        WS = posError - negError
        (u, v) = mstEdges[i]
        if u[0] == v[0]:  #vertical, dy, channel 0
            channel = 0
        if u[1] == v[1]:  #horizontal, dy, channel 1
            channel = 1

        WY[0, u[0], u[1], 0] += abs(WS) / 2.0
        WY[0, v[0], v[1], 0] += abs(WS) / 2.0
        if WS > 0.0:
            SY[0, u[0], u[1], 0] += 0.5
            SY[0, v[0], v[1], 0] += 0.5
        if WS < 0.0:
            SY[0, u[0], u[1], 0] += -0.5
            SY[0, v[0], v[1], 0] += -0.5
    # Std normalization
    totalW = np.sum(WY)
    if totalW != 0.0:
        WY = np.divide(WY, totalW)
    #SY = np.divide(SY, np.max(SY))
    return [WY, SY]
Beispiel #6
0
    def get_rand_weight(YP, YN):
        G = nx.grid_2d_graph(INPUT_SIZE[0], INPUT_SIZE[1])
        nlabels_dict = dict()
        for u, v, d in G.edges(data=True):

            d['weight'] = (YP[0, u[0], u[1], 0] + YP[0, v[0], v[1], 0]) / 2.0
            nlabels_dict[u] = YN[0, u[0], u[1], 0]
            nlabels_dict[v] = YN[0, v[0], v[1], 0]

        [posCounts, negCounts, mstEdges, totalPos,
         totalNeg] = ev.FindRandCounts(G, nlabels_dict)

        # start off with every point in own cluster
        posError = totalPos
        negError = 0.0

        WY = np.zeros((1, INPUT_SIZE[0], INPUT_SIZE[1], 1), np.float32)
        SY = np.zeros((1, INPUT_SIZE[0], INPUT_SIZE[1], 1), np.float32)

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

            WS = posError - negError

            (u, v) = mstEdges[i]

            WY[0, u[0], u[1], 0] = abs(WS) / 2.0
            WY[0, v[0], v[1], 0] = abs(WS) / 2.0
            if WS > 0.0:
                SY[0, u[0], u[1], 0] = 1.0
                SY[0, v[0], v[1], 0] = 1.0
            if WS < 0.0:
                SY[0, u[0], u[1], 0] = -1.0
                SY[0, v[0], v[1], 0] = -1.0

        # Std normalization
        totalW = np.sum(WY)
        if totalW != 0.0:
            WY = np.divide(WY, totalW)

        return [WY, SY]
def get_rand_weight(YP, Y):
    G = nx.grid_2d_graph(SIZE[0], SIZE[1])
    nlabels_dict = dict()
    edge_idx = dict()
    upto = 0
    for u, v, d in G.edges(data=True):
        d['weight'] = YP[0, upto, 0]
        edge_idx[(u, v)] = upto
        upto = upto + 1
        nlabels_dict[u] = Y[0, u[0], u[1], 0]
        nlabels_dict[v] = Y[0, v[0], v[1], 0]
    [posCounts, negCounts, mstEdges, totalPos,
     totalNeg] = ev.FindRandCounts(G, nlabels_dict)
    posError = totalPos
    negError = 0.0
    WY = np.zeros((1, SIZE[0] * (SIZE[0] - 1) * 2, 1), np.single)
    SY = np.zeros((1, SIZE[0] * (SIZE[0] - 1) * 2, 1), np.single)
    for i in range(len(posCounts)):
        e = edge_idx[mstEdges[i]]
        #w = (len(posCounts)-i)/len(posCounts)
        '''
        if YP[0, e, 0] < 0.0:
            WY[0, e, 0] = posCounts[i]/(posCounts[i]+negCounts[i])
            SY[0, e, 0] = 1.0
        elif YP[0, e, 0] > 0.0:
            WY[0, e, 0] = negCounts[i]/(posCounts[i]+negCounts[i])
            SY[0, e, 0] = -1.0
        '''
        WS = posCounts[i] - negCounts[i]
        if WS > 0.0:
            SY[0, e, 0] = 1.0
            WY[0, e, 0] = WS / (posCounts[i] + negCounts[i])
        elif WS < 0.0:
            SY[0, e, 0] = -1.0
            WY[0, e, 0] = -WS / (posCounts[i] + negCounts[i])

    return [WY, SY]
Beispiel #8
0
        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)
Beispiel #9
0
    def get_rand_weight(YP, YN):
        G = nx.grid_2d_graph(INPUT_SIZE[0], INPUT_SIZE[1])
        nlabels_dict = dict()
        for u, v, d in G.edges(data=True):

            d['weight'] = (YP[0, u[0], u[1], 0] + YP[0, v[0], v[1], 0]) / 2.0
            nlabels_dict[u] = YN[0, u[0], u[1], 0]
            nlabels_dict[v] = YN[0, v[0], v[1], 0]

        [posCounts, negCounts, mstEdges, totalPos,
         totalNeg] = ev.FindRandCounts(G, nlabels_dict)

        # start off with every point in own cluster
        posError = totalPos
        negError = 0.0

        PW = np.zeros((1, INPUT_SIZE[0], INPUT_SIZE[1], 1), np.float32)
        NW = np.zeros((1, INPUT_SIZE[0], INPUT_SIZE[1], 1), np.float32)

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

            (u, v) = mstEdges[i]

            PW[0, u[0], u[1], 0] = PW[0, u[0], u[1], 0] + (posError / 2.0)
            PW[0, v[0], v[1], 0] = PW[0, v[0], v[1], 0] + (posError / 2.0)
            NW[0, u[0], u[1], 0] = NW[0, u[0], u[1], 0] + (negError / 2.0)
            NW[0, v[0], v[1], 0] = NW[0, v[0], v[1], 0] + (negError / 2.0)

        WY = np.zeros((1, INPUT_SIZE[0], INPUT_SIZE[1], 1), np.float32)
        SY = np.zeros((1, INPUT_SIZE[0], INPUT_SIZE[1], 1), np.float32)

        numPP = 0
        numPN = 0
        numWP = 0
        numWN = 0
        USE_DIFF = False

        for i in range(INPUT_SIZE[0]):
            for j in range(INPUT_SIZE[1]):
                if USE_DIFF:
                    diff = PW[0, i, j, 0] - NW[0, i, j, 0]
                    if diff > 0.0:
                        SY[0, i, j, 0] = 1.0
                        WY[0, i, j, 0] = diff
                        numWN = numWN + NW[0, i, j, 0]
                    elif diff < 0.0:
                        SY[0, i, j, 0] = -1.0
                        WY[0, i, j, 0] = -diff
                        numWP = numWP + PW[0, i, j, 0]
                    else:
                        numWN = numWN + NW[0, i, j, 0]
                        numWP = numWP + PW[0, i, j, 0]
                else:
                    if YP[0, i, j, 0] > 0.0:
                        WY[0, i, j, 0] = NW[0, i, j, 0]
                        SY[0, i, j, 0] = -1.0
                        numPP = numPP + 1.0
                        if NW[0, i, j, 0] > 0.1:
                            numWN = numWN + 1
                    elif YP[0, i, j, 0] < 0.0:
                        WY[0, i, j, 0] = PW[0, i, j, 0]
                        SY[0, i, j, 0] = 1.0
                        numPN = numPN + 1.0
                        if PW[0, i, j, 0] > 0.1:
                            numWP = numWP + 1

        # Std normalization
        totalW = np.sum(WY)
        if totalW != 0.0:
            WY = np.divide(WY, totalW)

        if USE_DIFF:
            print("W: " + str(totalW) + " Ignore: " + str(numWN + numWP) +
                  " from Pos: " + str(numWP) + " and " + str(numWN))
        else:
            print("W: " + str(totalW) + " Pos: " + str(numWN) + " of " +
                  str(numPP) + " Neg: " + str(numWP) + " of " + str(numPN) +
                  " from " + str(numPP + numPN) + " out of " +
                  str(INPUT_SIZE[0] * INPUT_SIZE[1]))

        return [WY, SY]