예제 #1
0
def segment_lmc(ws, affs, n_labels, offsets, return_merged_nodes=False):
    rag = nrag.gridRag(ws, numberOfLabels=n_labels, numberOfThreads=1)
    lifted_uvs, local_features, lifted_features = nrag.computeFeaturesAndNhFromAffinities(
        rag, affs, offsets, numberOfThreads=1)
    uv_ids = rag.uvIds()

    lmc = cseg.LiftedMulticut('kernighan-lin', weight_edges=False)
    local_costs = lmc.probabilities_to_costs(local_features[:, 0])
    local_ignore = (uv_ids == 0).any(axis=1)
    local_costs[local_ignore] = 5 * local_costs.min()

    # we might not have lifted edges -> just solve multicut
    if len(lifted_uvs) == 1 and (lifted_uvs[0] == -1).any():
        mc = cseg.Multicut('kernighan-lin', weight_edges=False)
        graph = nifty.graph.undirectedGraph(n_labels)
        graph.insertEdges(uv_ids)
        node_labels = mc(graph, local_costs)

    else:
        lifted_costs = lmc.probabilities_to_costs(lifted_features[:, 0])
        lifted_ignore = (lifted_uvs == 0).any(axis=1)
        lifted_costs[lifted_ignore] = 5 * lifted_costs.min()
        node_labels = lmc(uv_ids, lifted_uvs, local_costs, lifted_costs)

    if return_merged_nodes:
        return get_merged_nodes(uv_ids, node_labels)
    else:
        return nrag.projectScalarNodeDataToPixels(rag, node_labels)
def compute_features(seg, affs):
    import nifty.graph.rag as nrag
    offsets = [[-1, 0, 0], [0, -1, 0], [0, 0, -1], [-2, 0, 0], [0, -3, 0],
               [0, 0, -3], [-3, 0, 0], [0, -9, 0], [0, 0, -9], [-4, 0, 0],
               [0, -27, 0], [0, 0, -27]]
    rag = nrag.gridRag(seg, numberOfLabels=int(seg.max()) + 1)
    lr_uvs, local_features, lr_features = nrag.computeFeaturesAndNhFromAffinities(
        rag, affs, offsets)
    return rag, lr_uvs, local_features[:, 0], lr_features[:, 0]
예제 #3
0
def compute_lmc(ws, affs, offsets, n_labels, weight_mulitcut_edges,
                weighting_exponent):
    # compute the region adjacency graph
    rag = nrag.gridRag(ws, numberOfLabels=n_labels, numberOfThreads=1)
    uv_ids = rag.uvIds()

    # compute the lifted uv-ids, and features for local and lifted edges
    # from the affinities
    lifted_uvs, local_feats, lifted_feats = nrag.computeFeaturesAndNhFromAffinities(
        rag, affs, offsets, numberOfThreads=1)

    local_probs = local_feats[:, 0]
    local_sizes = local_feats[:, -1].astype('uint64')

    lifted_probs = local_feats[:, 0]
    lifted_sizes = lifted_feats[:, -1].astype('uint64')

    # build the original graph and lifted objective
    # with lifted uv-ids
    graph = nifty.graph.undirectedGraph(n_labels)
    graph.insertEdges(uv_ids)

    # we may not get any lifted edges, in this case, fall back to normal multicut
    if lifted_uvs.size == 0:
        merge_indicator = run_mc(
            graph,
            local_probs,
            uv_ids,
            weighting_exponent=weighting_exponent,
            edge_sizes=local_sizes if weight_mulitcut_edges else None,
            with_ignore_edges=True)
        return uv_ids, merge_indicator, local_sizes

    # we don't weight, because we might just have few lifted edges
    # and this would downvote the local edges significantly
    # weight the costs
    # n_local, n_lifted = len(uv_ids), len(lifted_uv_ids)
    # total = float(n_lifted) + n_local
    # local_costs *= (n_lifted / total)
    # lifted_costs *= (n_local / total)

    _, merge_indicator = run_lmc(
        graph,
        uv_ids,
        lifted_uvs,
        local_probs,
        lifted_probs,
        local_sizes=local_sizes if weight_mulitcut_edges else None,
        lifted_sizes=lifted_sizes if weight_mulitcut_edges else None,
        weighting_exponent=weighting_exponent,
        with_ignore_edges=True)
    return uv_ids, merge_indicator, local_sizes
예제 #4
0
def extract_features_and_labels(sample):
    offsets = [[-1, 0, 0], [0, -1, 0], [0, 0, -1], [-2, 0, 0], [0, -3, 0],
               [0, 0, -3], [-3, 0, 0], [0, -9, 0], [0, 0, -9], [-4, 0, 0],
               [0, -27, 0], [0, 0, -27]]
    path = '/home/papec/mnt/papec/Work/neurodata_hdd/cremi_warped/sample%s_train.n5' % sample
    f = z5py.File(path)
    ws = f['segmentations/watershed'][:]
    rag = nrag.gridRag(ws, numberOfLabels=int(ws.max()) + 1)

    affs = 1. - f['predictions/full_affs'][:]
    lifted_uvs, local_features, lifted_features = nrag.computeFeaturesAndNhFromAffinities(
        rag, affs, offsets)

    gt = f['segmentations/groundtruth'][:]
    node_labels = nrag.gridRagAccumulateLabels(rag, gt)

    uv_ids = rag.uvIds()

    local_valid_edges = (node_labels[uv_ids] != 0).all(axis=1)
    local_labels = (node_labels[uv_ids[:, 0]] !=
                    node_labels[uv_ids[:, 1]]).astype('uint8')
    assert len(local_features) == len(
        local_labels), "%i, %i" % (len(local_features), len(local_labels))

    lifted_valid_edges = (node_labels[lifted_uvs] != 0).all(axis=1)
    lifted_labels = (node_labels[lifted_uvs[:, 0]] !=
                     node_labels[lifted_uvs[:, 1]]).astype('uint8')
    assert len(lifted_features) == len(
        lifted_labels), "%i, %i" % (len(lifted_features), len(lifted_labels))

    print("Number of valid local edges", np.sum(local_valid_edges),
          local_valid_edges.size)
    print("Number of valid lifted edges", np.sum(lifted_valid_edges),
          lifted_valid_edges.size)

    local_labels = local_labels[local_valid_edges]
    local_features = local_features[local_valid_edges]
    assert len(local_features) == len(
        local_labels), "%i, %i" % (len(local_features), len(local_labels))

    lifted_labels = lifted_labels[lifted_valid_edges]
    lifted_features = lifted_features[lifted_valid_edges]
    assert len(lifted_features) == len(
        lifted_labels), "%i, %i" % (len(lifted_features), len(lifted_labels))

    return local_labels, local_features, lifted_labels, lifted_features
예제 #5
0
def full_features(rag, affs, offsets):
    lifted_uvs, local_features, lifted_features = nrag.computeFeaturesAndNhFromAffinities(rag,
                                                                                          affs,
                                                                                          offsets)
    return lifted_uvs, np.nan_to_num(local_features), np.nan_to_num(lifted_features)
예제 #6
0
def get_lifted_problem(affs, seg, offsets):
    rag = nrag.gridRag(seg, numberOfLabels=int(seg.max()) + 1)
    lifted_uvs, local_features, lifted_features = nrag.computeFeaturesAndNhFromAffinities(rag,
                                                                                          affs,
                                                                                          offsets)
    return rag, lifted_uvs, local_features, lifted_features