Example #1
0
def make_seg_map(prob_map_path, dense_gt_path):
    prob_map = read_h5(prob_map_path)
    dense_gt = read_h5(dense_gt_path)
    nodes_in_dgt = len(np.unique(dense_gt))
    print "nodes in dgt", nodes_in_dgt
    seed_map = wsDtSegmentation(prob_map, pmin=0.5, minMembraneSize=10, minSegmentSize=0, sigmaMinima=6, sigmaWeights=1, cleanCloseSeeds=True,
                                returnSeedsOnly=True)
    seg_map, sup_map, wsDt_data, agglCl_data = get_segmentation(prob_map, pmin=0.5, minMemb=10, minSeg=10, sigMin=6, sigWeights=1, sigSmooth=0.1,
                                                                     cleanCloseSeeds=True, returnSeedsOnly=False, edgeLengths=None, nodeFeatures=None,
                                                                     nodeSizes=None, nodeLabels=None, nodeNumStop=nodes_in_dgt, beta=0, metric='l1',
                                                                     wardness=0.2, out=None)
    print "seeds", len(np.unique(seed_map))
    print "seg", len(np.unique(seg_map))

    #make folder for seeds
    seed_folder = prob_map_path.split("prob")[0] + "seeds/"
    if not os.path.exists(seed_folder):
        os.mkdir(seed_folder)
    seed_map_outpath = seed_folder + prob_map_path.split("/")[-1]

    #make folder for super pixels
    sup_folder = prob_map_path.split("prob")[0] + "sup_maps/"
    if not os.path.exists(sup_folder):
        os.mkdir(sup_folder)
    sup_map_outpath = sup_folder + prob_map_path.split("/")[-1]

    #make folder for segmentations
    seg_folder = prob_map_path.split("prob")[0] + "seg_maps/"
    if not os.path.exists(seg_folder):
        os.mkdir(seg_folder)
    seg_map_outpath =  seg_folder + prob_map_path.split("/")[-1]

    #make folder for segmentation data
    data_folder = prob_map_path.split("prob")[0] + "seg_data/"
    if not os.path.exists(data_folder):
        os.mkdir(data_folder)
    data_outpath = data_folder + prob_map_path.split("/")[-1]

    #get segmentation data
    ri, voi = rand_index_variation_of_information(seg_map, dense_gt)
    print "ri, voi", ri , voi

    ri_data = np.array((ri))
    voi_data = np.array((voi))
    ri_voi_data = np.array((ri, voi))

    #save seeds, superpixels and segmentations
    # save_h5(ri_voi_data, data_outpath, key)
    # save_h5(ri_data, data_outpath, "rand index", None)
    # save_h5(voi_data, data_outpath, "variation of information", None)
    # save_h5(seed_map, seed_map_outpath, "data", None)
    save_h5(sup_map,sup_map_outpath, "data", None)
    save_h5(seg_map,seg_map_outpath, "data", None)
Example #2
0
def get_segmentation(predict, pmin=0.5, minMemb=10, minSeg=10, sigMin=6, sigWeights=1, sigSmooth=0.1, cleanCloseSeeds=True,
                     returnSeedsOnly=False, edgeLengths=None,nodeFeatures=None, nodeSizes=None, nodeLabels=None,
                     nodeNumStop=None, beta=0, metric='l1', wardness=0.2, out=None):
    """ Get segmentation through watershed and agglomerative clustering
    :param predict: prediction map
    :return: segmentation map
    """
    #use watershed and save superpixels map
    super_pixels = wsDtSegmentation(predict, pmin, minMemb, minSeg, sigMin, sigWeights, cleanCloseSeeds, returnSeedsOnly)
    # seeds = wsDtSegmentation(predict, pmin, minMemb, minSeg, sigMin, sigWeights, cleanCloseSeeds, True)
    # save_h5(seeds, "/home/stamylew/delme/seeds.h5", "data")
    print
    print "#Nodes in superpixels", len(np.unique(super_pixels))
    # save_h5(super_pixels, "/home/stamylew/delme/super_pixels.h5", "data")

    #smooth prediction map
    probs = vf.gaussianSmoothing(predict, sigSmooth)
     # save_h5(probs, "/home/stamylew/delme/probs.h5", "data")

    #make grid graph
    grid_graph = vg.gridGraph(super_pixels.shape, False)

    grid_graph_edge_indicator = vg.edgeFeaturesFromImage(grid_graph, probs)
    #make region adjacency graph
    rag = vg.regionAdjacencyGraph(grid_graph, super_pixels)

    #accumulate edge features from grid graph node map
    edge_weights = rag.accumulateEdgeFeatures(grid_graph_edge_indicator)
    edge_weights_tag = "mean of the probabilities"

    #do agglomerative clustering

    labels = vg.agglomerativeClustering(rag, edge_weights, edgeLengths, nodeFeatures, nodeSizes,
            nodeLabels, nodeNumStop, beta, metric, wardness, out)

    #segmentation data
    wsDt_data = np.zeros((8,1))
    wsDt_data[:,0] = (pmin, minMemb, minSeg, sigMin, sigWeights, sigSmooth, cleanCloseSeeds, returnSeedsOnly)
    agglCl_data = edge_weights_tag, str(edgeLengths), str(nodeFeatures), str(nodeSizes), str(nodeLabels), str(nodeNumStop), \
                  str(beta), metric, str(wardness), str(out)

    #project labels back to data
    segmentation = rag.projectLabelsToBaseGraph(labels)
    print "#nodes in segmentation", len(np.unique(segmentation))
    # save_h5(segmentation, "/home/stamylew/delme/segmap.h5", "data", None)
    print "seg", np.unique(segmentation)
    return segmentation, super_pixels, wsDt_data, agglCl_data
def get_segmentation(predict, pmin=0.5, minMemb=10, minSeg=10, sigMin=6, sigWeights=1, sigSmooth=0.1, cleanCloseSeeds=True,
                     returnSeedsOnly=False, edgeLengths=None,nodeFeatures=None, nodeSizes=None, nodeLabels=None, nodeNumStop=None,
                     beta=0, metric='l1', wardness=0.2, out=None):
    """ Get segmentation through watershed and agglomerative clustering
    :param predict: prediction map
    :return: segmentation map
    """
    #use watershed and save superpixels map
    super_pixels = wsDtSegmentation(predict, pmin, minMemb, minSeg, sigMin, sigWeights, cleanCloseSeeds, returnSeedsOnly)
    # seeds = wsDtSegmentation(predict, pmin, minMemb, minSeg, sigMin, sigWeights, cleanCloseSeeds, True)
    # save_h5(seeds, "/home/stamylew/delme/seeds.h5", "data")
    print
    print "#Nodes in superpixels", len(np.unique(super_pixels))
    # save_h5(super_pixels, "/home/stamylew/delme/super_pixels.h5", "data")

    #smooth prediction map
    probs = vf.gaussianSmoothing(predict, sigSmooth)
     # save_h5(probs, "/home/stamylew/delme/probs.h5", "data")

    #make grid graph
    grid_graph = vg.gridGraph(super_pixels.shape, False)

    grid_graph_edge_indicator = vg.edgeFeaturesFromImage(grid_graph, probs)
    #make region adjacency graph
    rag = vg.regionAdjacencyGraph(grid_graph, super_pixels)

    #accumulate edge features from grid graph node map
    edge_weights = rag.accumulateEdgeFeatures(grid_graph_edge_indicator)
    edge_weights_tag = "mean of the probabilities"

    #do agglomerative clustering

    def agglomerativeClustering_beststop_th05(graph, edgeWeights=None, edgeStoppers=None,
                                          edgeLengths=None, nodeFeatures=None, nodeSizes=None,
                                          nodeLabels=None, beta=0, metric=None,
                                          wardness=1.0, sameLabelMultiplier=1.0,  out=None):


        vg.ac(graph, edgeWeights=edgeWeights, edgeStoppers=edgeStoppers,
                                       edgeLengths=edgeLengths, nodeFeatures=nodeFeatures,
                                       nodeSizes=nodeSizes, nodeLabels=nodeLabels, nodeNumStop=0,
                                       beta=beta, wardness=wardness, sameLabelMultiplier=sameLabelMultiplier, out=out)

        f = open('/tmp/ac_bestNodenumstop.txt', 'r')
        nodeNumStop = f.readline()
        f.close()
        print "best nodeNumStop:", nodeNumStop

        # in experiments if n of the groundtruth is n the file contains n-1. probably in the hc.hxx the
        #  number is written to file too late. does this make any sense??

        nodeNumStop = int(nodeNumStop)
        nodeNumStop += 1

        ac_res, nodelabel_out_1 = vg.ac(graph, edgeWeights=edgeWeights, edgeStoppers=edgeStoppers,
                                            edgeLengths=edgeLengths, nodeFeatures=nodeFeatures,
                                            nodeSizes=nodeSizes, nodeLabels=nodeLabels, nodeNumStop=nodeNumStop,
                                            beta=beta, wardness=wardness, sameLabelMultiplier=sameLabelMultiplier,
                                            out=out)

        return ac_res, nodeNumStop, nodelabel_out_1

    labels, _, _ = agglomerativeClustering_beststop_th05(rag, edgeWeights=edge_weights, edgeStoppers=edge_weights,
                                          edgeLengths=edgeLengths, nodeFeatures=nodeFeatures, nodeSizes=nodeSizes,
                                          nodeLabels=nodeLabels, beta=beta, metric=metric,
                                          wardness=0.1, sameLabelMultiplier=1.0,  out=out)


    # labels = vg.agglomerativeClustering(rag, edge_weights, edgeLengths, nodeFeatures, nodeSizes,
    #         nodeLabels, nodeNumStop, beta, metric, wardness, out)

    #segmentation data
    wsDt_data = np.zeros((8,1))
    wsDt_data[:,0] = (pmin, minMemb, minSeg, sigMin, sigWeights, sigSmooth, cleanCloseSeeds, returnSeedsOnly)
    agglCl_data = edge_weights_tag, str(edgeLengths), str(nodeFeatures), str(nodeSizes), str(nodeLabels), str(nodeNumStop), str(beta), metric, str(wardness), str(out)

    #project labels back to data
    segmentation = rag.projectLabelsToBaseGraph(labels)
    print "#nodes in segmentation", len(np.unique(segmentation))
    # save_h5(segmentation, "/home/stamylew/delme/segmap.h5", "data", None)

    return segmentation, super_pixels, wsDt_data, agglCl_data