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()
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)
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_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
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 m_weight(y_p, n): G = nx.grid_2d_graph(INPUT_SIZE[0], INPUT_SIZE[1]) for u, v, d in G.edges(data=True): if u[0] == v[0]: channel = 0 else: channel = 1 d['weight'] = y_p[0, u[0], u[1], channel] 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) mstEdges = ev.mstEdges(G) MST = nx.Graph() MST.add_edges_from(mstEdges) u = (randint(0, IMAGE_SIZE[0] - 1), randint(0, IMAGE_SIZE[1] - 1)) v = (randint(0, IMAGE_SIZE[0] - 1), randint(0, IMAGE_SIZE[1] - 1)) while u == v: u = (randint(0, IMAGE_SIZE[0] - 1), randint(0, IMAGE_SIZE[1] - 1)) v = (randint(0, IMAGE_SIZE[0] - 1), randint(0, IMAGE_SIZE[1] - 1)) path = nx.shortest_path(MST, source=u, target=v) (u, v) = min([edge for edge in nx.utils.pairwise(path)], key=lambda e: G.edges[e]['weight']) if u[0] == v[0]: channel = 0 else: channel = 1 WY[0, u[0], u[1], channel] = 1.0 if n[0, u[0], u[1], 0] == n[0, v[0], v[1], 0]: SY[0, u[0], u[1], channel] = 1.0 return WY, SY
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()
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] [ bestT, lowE, posCounts, negCounts, mstEdges, mstEdgeWeights, totalPos, totalNeg ] = ev.FindMinEnergyAndRandCounts(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) TY = bestT * np.ones((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, TY
def rand_error(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] error = ev.FindRandErrorAtThreshold(G, nlabels_dict, 0.0) return error
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): d['weight'] = YP[upto, 0] upto = upto + 1 nlabels_dict[u] = Y[u[0], u[1], 0] nlabels_dict[v] = Y[v[0], v[1], 0] error = ev.FindRandErrorAtThreshold(G, nlabels_dict, 0.0) return error
def rand_image(YP): G = nx.grid_2d_graph(SIZE[0], SIZE[1]) upto = 0 for u, v, d in G.edges(data=True): d['weight'] = YP[upto, 0] upto = upto + 1 L = ev.GetLabelsAtThreshold(G, 0.0) 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
def TrainConv(img, seg): (patchImg, patchSeg, nlabels, elabels) = ev.SamplePatch(img, seg) #ShowPatch(patchImg, patchSeg) X_train = tf.reshape( patchImg, [1, patchImg.shape[0], patchImg.shape[1], patchImg.shape[2]], name='image') Y_train = np.ones((121, 1), np.float32) #X_train, Y_train = SynTestData(numFeatures, numSamples) #Y_train = Y_train.reshape(Y_train.shape[0], 1) #print(Y_train.shape) #pca = PCA(n_components=numFeatures, whiten=True) #X_train = pca.fit_transform(X_train) weights_shape = (kernelSize, kernelSize, numChannels, numOutputs) bias_shape = (1, numOutputs) 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") #X = tf.placeholder(tf.float32, [batchSize, numFeatures], name="X") #Y = tf.placeholder(tf.float32, [batchSize, numOutputs], name="Y") YP = tf.subtract(tf.squeeze(tf.nn.conv2d(X, W, [1, 1, 1, 1], "VALID")), b) loss_function = test_loss(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(100): result = sess.run(learner, {X: X_train, Y: Y_train}) if i % 10 == 0: mistakes = 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(mistakes)) #y_pred = sess.run(YP, {X: xmap}) W_final, b_final = sess.run([W, b]) print(W_final) print(b_final)
def rand_error(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] error = ev.FindRandErrorAtThreshold(G, nlabels_dict, 0.0) return error
def rand_error(model, X, YN): YP = model(X) 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] = YN[0, u[0], u[1], 0] nlabels_dict[v] = YN[0, v[0], v[1], 0] error = ev.FindRandErrorAtThreshold(G, nlabels_dict, 0.0) return error
def rand_image(YP): 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 L = ev.GetLabelsAtThreshold(G) img = np.zeros((INPUT_SIZE[0], INPUT_SIZE[1]), np.float32) for i in range(img.shape[0]): for j in range(img.shape[1]): img[i, j] = L[(i, j)] return img
def rand_image(model, X): YP = model(X) G = nx.grid_2d_graph(SIZE[0], SIZE[1]) 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 L = ev.GetLabelsAtThreshold(G) 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
def Train(img, seg): imgn = (img / img.max()) * 2.0 - 1.0 (patchImg, patchSeg, nlabels, elabels) = ev.SamplePatch(imgn, seg) #ShowPatch(patchImg, patchSeg) (X_train, Y_train) = UnrollData(patchImg, patchSeg, nlabels, elabels) #print(X_train.shape) #print(Y_train.shape) #pca = PCA(n_components=D, whiten=True) #X_train = pca.fit_transform(X_train) #print(X_train.shape) 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") #X = tf.placeholder(tf.float32, [batchSize, numFeatures], name="X") #Y = tf.placeholder(tf.float32, [batchSize, numOutputs], name="Y") YP = tf.subtract(tf.matmul(X, W), b) loss_function = test_loss(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(100): result = sess.run(learner, {X: X_train, Y: Y_train}) if i % 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)) y_pred = sess.run(YP, {X: xmap}) W_final, b_final = sess.run([W, b])
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] WY = np.zeros((1, SIZE[0], SIZE[1], 2), np.single) SY = np.zeros((1, SIZE[0], SIZE[1], 2), np.single) #make a random pair of vertices u = (randint(0, SIZE[0]-1), randint(0, SIZE[1]-1)) v = (randint(0, SIZE[0]-1), randint(0, SIZE[1]-1)) while u == v: u = (randint(0, SIZE[0]-1), randint(0, SIZE[1]-1)) v = (randint(0, SIZE[0]-1), randint(0, SIZE[1]-1)) #find the path between u and v #create the MST MST = nx.Graph() mstEdges = ev.mstEdges(G) MST.add_edges_from(mstEdges) path = nx.shortest_path(MST, source=u, target=v) (mu,mv) = min(([edge for edge in nx.utils.pairwise(path)]), key=lambda e: G.edges[e]['weight']) if mu[0] > mv[0] or mu[1] > mv[1]: mu, mv = mv, mu if mu[0] == mv[0]: m_channel = 0 else: m_channel = 1 WY[0, mu[0], mu[1], m_channel] = 1.0 #compare label of u and v if Y[0, u[0], u[1], 0] == Y[0, v[0], v[1], 0]: SY[0, mu[0], mu[1], m_channel] = 1.0 else: SY[0, mu[0], mu[1], m_channel] = -1.0 #print(u,v, mu, mv) return [WY, SY]
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): if u[0] == v[0]: channel = 0 else: channel = 1 d['weight'] = YP[0, u[0], u[1], channel] 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], 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) SY[0, u[0], u[1], channel] = 1.0 if WS < 0.0: WY[0, u[0], u[1], channel] = abs(WS) 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 TestEnergyRand(): mySeed = 37 width = 3 np.random.seed(mySeed) RG = syn.InitRandom(width) #RG = syn.InitSimple(width) viz.DrawGraph(RG, title='original') CCLabels, CCE, param = dsnNode.Minimize(RG, nodeType='CC') viz.DrawGraph(RG, labels=CCLabels, title='CC labels') print(param) (thresh, bestE, posCounts, negCounts, mstEdges, mstEdgeWeights) = ev.FindMinEnergyAndRandCounts(RG, CCLabels) print("Energy: " + str(bestE) + " at threshold " + str(thresh)) plt.show()
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]
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]
def get_maximin_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] WY = np.zeros((1, SIZE[0], SIZE[1], 1), np.single) SY = np.zeros((1, SIZE[0], SIZE[1], 1), np.single) #build an MST mstEdges = ev.mstEdges(G) MST = nx.Graph() MST.add_edges_from(mstEdges) #get a random pair u,v u = (randint(0, SIZE[0] - 1), randint(0, SIZE[1] - 1)) v = (randint(0, SIZE[0] - 1), randint(0, SIZE[1] - 1)) while u == v: u = (randint(0, SIZE[0] - 1), randint(0, SIZE[1] - 1)) v = (randint(0, SIZE[0] - 1), randint(0, SIZE[1] - 1)) #find the maximin path between u and v on the MST path = nx.shortest_path(MST, source=u, target=v) #the maximin edge (us, vs) = min([edge for edge in nx.utils.pairwise(path)], key=lambda e: G.edges[e]['weight']) WY[0, us[0], us[1], 0] += 0.5 WY[0, vs[0], vs[1], 0] += 0.5 if Y[0, u[0], u[1], 0] == Y[0, v[0], v[1], 0]: SY[0, us[0], us[1], 0] += 0.5 SY[0, vs[0], vs[1], 0] += 0.5 else: SY[0, us[0], us[1], 0] += -0.5 SY[0, vs[0], vs[1], 0] += -0.5 return [WY, SY]
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()
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]
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] 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) #create the MST mstEdges = ev.mstEdges(G) MST = nx.Graph() MST.add_edges_from(mstEdges) #make a random pair of vertices u = (randint(0, SIZE[0] - 1), randint(0, SIZE[1] - 1)) v = (randint(0, SIZE[0] - 1), randint(0, SIZE[1] - 1)) while u == v: u = (randint(0, SIZE[0] - 1), randint(0, SIZE[1] - 1)) v = (randint(0, SIZE[0] - 1), randint(0, SIZE[1] - 1)) #find the path between u and v path = nx.shortest_path(MST, source=u, target=v) (mu, mv) = min([edge for edge in nx.utils.pairwise(path)], key=lambda e: G.edges[e]['weight']) if mu[0] > mv[0] or mu[1] > mv[1]: mu, mv = mv, mu e = edge_idx[(mu, mv)] WY[0, e, 0] = 1.0 if Y[0, u[0], u[1], 0] == Y[0, v[0], v[1], 0]: SY[0, e, 0] = 1.0 elif Y[0, u[0], u[1], 0] != Y[0, v[0], v[1], 0]: SY[0, e, 0] = -1.0 return [WY, SY]
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)
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)
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)
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